edit_cell
Modify Jupyter notebook cell source code by specifying cell ID or index to update content while preserving metadata.
Instructions
Edit the source code of a specific cell by ID or index
Input Schema
TableJSON 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 |
Implementation Reference
- src/jupyter-handler.js:600-618 (handler)The main handler function for the 'edit_cell' tool. Resolves cell by ID or index and delegates to editCellSource to update the notebook.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)Input schema definition for the 'edit_cell' tool, used in the 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)Dispatch registration in the CallToolRequestSchema handler that routes 'edit_cell' calls to JupyterHandler.editCell.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 of cell source by index, converting string to Jupyter source array format and writing the notebook.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}` } ] }; }