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
| Name | Required | Description | Default |
|---|---|---|---|
| notebook_path | Yes | Absolute path to the Jupyter notebook file | |
| source | No | Initial source code/content for the cell | |
| cell_type | No | Type of cell to create | code |
| position | No | Position 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
- src/jupyter-handler.js:591-598 (handler)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); }
- src/index.js:256-283 (schema)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 );
- src/jupyter-handler.js:174-224 (helper)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}` } ] }; }