Skip to main content
Glama

add_cell

Insert a new code, markdown, or raw cell into a Jupyter notebook at a specified position with optional initial content.

Instructions

Add a new cell to the notebook

Input Schema

NameRequiredDescriptionDefault
notebook_pathYesAbsolute path to the Jupyter notebook file
sourceNoInitial source code/content for the cell
cell_typeNoType of cell to createcode
positionNoPosition to insert the cell (defaults to end if not specified)

Input Schema (JSON Schema)

{ "properties": { "cell_type": { "default": "code", "description": "Type of cell to create", "enum": [ "code", "markdown", "raw" ], "type": "string" }, "notebook_path": { "description": "Absolute path to the Jupyter notebook file", "type": "string" }, "position": { "description": "Position to insert the cell (defaults to end if not specified)", "type": "integer" }, "source": { "default": "", "description": "Initial source code/content for the cell", "type": "string" } }, "required": [ "notebook_path" ], "type": "object" }

Implementation Reference

  • The handler function that implements the core logic for the 'add_cell' tool by computing the insertion position and delegating to insertCell.
    async addCell(notebookPath, source = '', cellType = 'code', position = null) { const notebook = await this.readNotebook(notebookPath); // If position not specified, add at the end const insertPosition = position !== null ? position : notebook.cells.length; return await this.insertCell(notebookPath, insertPosition, cellType, source); }
  • The input schema definition for the 'add_cell' tool provided in the ListTools response.
    { name: "add_cell", description: "Add a new cell to the notebook", inputSchema: { type: "object", properties: { notebook_path: { type: "string", description: "Absolute path to the Jupyter notebook file" }, source: { type: "string", default: "", description: "Initial source code/content for the cell" }, cell_type: { type: "string", enum: ["code", "markdown", "raw"], default: "code", description: "Type of cell to create" }, position: { type: "integer", description: "Position to insert the cell (defaults to end if not specified)" } }, required: ["notebook_path"] }
  • src/index.js:381-387 (registration)
    The dispatch registration in the CallToolRequestSchema handler that maps 'add_cell' calls to the JupyterHandler.addCell method.
    case "add_cell": return await this.jupyterHandler.addCell( args.notebook_path, args.source, args.cell_type, args.position );
  • The insertCell helper method called by addCell to perform the actual cell insertion into the notebook.
    async insertCell(notebookPath, position, cellType = 'code', source = '') { const notebook = await this.readNotebook(notebookPath); this.validateCellType(cellType); if (position < 0 || position > notebook.cells.length) { throw new Error(`Invalid position ${position}. Must be between 0 and ${notebook.cells.length}`); } // Convert string to array format - each line should end with \n except the last let sourceArray; if (!source) { sourceArray = ['']; } else { const lines = source.split('\n'); sourceArray = lines.map((line, index) => { if (index === lines.length - 1) { return line === '' ? '' : line; } else { return line + '\n'; } }); // Remove empty last element if original ended with \n if (sourceArray.length > 1 && sourceArray[sourceArray.length - 1] === '') { sourceArray.pop(); } } const newCell = { cell_type: cellType, metadata: {}, source: sourceArray }; if (cellType === 'code') { newCell.execution_count = null; newCell.outputs = []; } notebook.cells.splice(position, 0, newCell); await this.writeNotebook(notebookPath, notebook); return { content: [ { type: "text", text: `Successfully inserted ${cellType} cell at position ${position}` } ] }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/azharlabs/mcp-jupyter-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server