edit_cell
Modify Jupyter notebook cell source code by specifying the notebook path, cell identifier, and new content to update programming logic or documentation.
Instructions
Edit the source code of a specific cell by ID or index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| notebook_path | Yes | Absolute path to the Jupyter notebook file | |
| cell_id | Yes | Cell ID or zero-based index of the cell to edit | |
| new_source | Yes | New source code for the cell |
Input Schema (JSON Schema)
{
"properties": {
"cell_id": {
"description": "Cell ID or zero-based index of the cell to edit",
"type": [
"string",
"integer"
]
},
"new_source": {
"description": "New source code for the cell",
"type": "string"
},
"notebook_path": {
"description": "Absolute path to the Jupyter notebook file",
"type": "string"
}
},
"required": [
"notebook_path",
"cell_id",
"new_source"
],
"type": "object"
}
Implementation Reference
- src/jupyter-handler.js:600-618 (handler)The handler function for the 'edit_cell' tool. Locates the target cell by ID (string) or index (integer) and delegates the source editing to the helper function editCellSource.async editCell(notebookPath, cellId, newSource) { const notebook = await this.readNotebook(notebookPath); // Find cell by ID or treat as index let cellIndex = -1; if (typeof cellId === 'string') { // Search by cell ID cellIndex = notebook.cells.findIndex(c => c.id === cellId); if (cellIndex === -1) { throw new Error(`Cell with ID '${cellId}' not found`); } } else { // Treat as index cellIndex = cellId; } return await this.editCellSource(notebookPath, cellIndex, newSource); }
- src/index.js:285-306 (schema)Tool definition including name, description, and input schema for 'edit_cell' in the MCP listTools response.{ name: "edit_cell", description: "Edit the source code of a specific cell by ID or index", inputSchema: { type: "object", properties: { notebook_path: { type: "string", description: "Absolute path to the Jupyter notebook file" }, cell_id: { type: ["string", "integer"], description: "Cell ID or zero-based index of the cell to edit" }, new_source: { type: "string", description: "New source code for the cell" } }, required: ["notebook_path", "cell_id", "new_source"] } },
- src/index.js:389-394 (registration)Routing/registration in the CallToolRequestSchema handler switch, calling the editCell method on jupyterHandler.case "edit_cell": return await this.jupyterHandler.editCell( args.notebook_path, args.cell_id, args.new_source );
- src/jupyter-handler.js:139-172 (helper)Core helper function that performs the actual editing: converts newSource string to Jupyter source array format, updates the notebook cell source, persists the notebook file, and returns success response.async editCellSource(notebookPath, cellIndex, newSource) { const notebook = await this.readNotebook(notebookPath); this.validateCellIndex(notebook.cells, cellIndex); // Convert string to array format - each line should end with \n except the last const lines = newSource.split('\n'); const sourceArray = lines.map((line, index) => { // Add \n to all lines except the last one, unless the original ended with \n if (index === lines.length - 1) { // Last line: only add \n if original text ended with \n (detected by empty last element) return line === '' ? '' : line; } else { // All other lines get \n return line + '\n'; } }); // Remove empty last element if original ended with \n if (sourceArray.length > 1 && sourceArray[sourceArray.length - 1] === '') { sourceArray.pop(); } notebook.cells[cellIndex].source = sourceArray; await this.writeNotebook(notebookPath, notebook); return { content: [ { type: "text", text: `Successfully updated cell ${cellIndex}` } ] }; }