Skip to main content
Glama

insert_cell

Add a new code, markdown, or raw cell at a specified position in a Jupyter notebook to organize content and structure.

Instructions

Insert a new cell at a specific position

Input Schema

NameRequiredDescriptionDefault
notebook_pathYesAbsolute path to the Jupyter notebook file
positionYesPosition to insert the cell (0-based)
cell_typeNoType of cell to createcode
sourceNoInitial source code/content for the cell

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 (0-based)", "type": "integer" }, "source": { "default": "", "description": "Initial source code/content for the cell", "type": "string" } }, "required": [ "notebook_path", "position" ], "type": "object" }

Implementation Reference

  • The main handler function that reads the notebook JSON, validates inputs, creates a new cell object, inserts it at the specified position using Array.splice, writes the updated notebook back to file, and returns a success message.
    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}` } ] }; }
  • Defines the MCP tool schema for 'insert_cell', including name, description, input properties (notebook_path, position, cell_type, source), and required fields.
    { name: "insert_cell", description: "Insert a new cell at a specific position", inputSchema: { type: "object", properties: { notebook_path: { type: "string", description: "Absolute path to the Jupyter notebook file" }, position: { type: "integer", description: "Position to insert the cell (0-based)" }, cell_type: { type: "string", enum: ["code", "markdown", "raw"], default: "code", description: "Type of cell to create" }, source: { type: "string", default: "", description: "Initial source code/content for the cell" } }, required: ["notebook_path", "position"] } },
  • src/index.js:344-350 (registration)
    Registers the tool handler by dispatching 'insert_cell' calls in the CallToolRequestHandler switch statement to the JupyterHandler.insertCell method, mapping arguments appropriately.
    case "insert_cell": return await this.jupyterHandler.insertCell( args.notebook_path, args.position, args.cell_type || "code", args.source || "" );

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