edit_cell_source
Modify Jupyter notebook cell source code by specifying the notebook file path, cell index, and new code content to update programming logic or documentation.
Instructions
Edit the source code of a specific cell by index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| notebook_path | Yes | Absolute path to the Jupyter notebook file | |
| cell_index | Yes | Zero-based index of the cell | |
| new_source | Yes | New source code for the cell |
Input Schema (JSON Schema)
{
"properties": {
"cell_index": {
"description": "Zero-based index of the cell",
"type": "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_index",
"new_source"
],
"type": "object"
}
Implementation Reference
- src/jupyter-handler.js:139-172 (handler)The main handler function that reads the notebook, validates the cell index, updates the cell source by converting the new source to the proper array format, writes back the notebook, and returns a success message.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}` } ] }; }
- src/index.js:71-92 (schema)The input schema definition for the 'edit_cell_source' tool, including parameters notebook_path (string), cell_index (integer), and new_source (string).{ name: "edit_cell_source", description: "Edit the source code of a specific cell by index", inputSchema: { type: "object", properties: { notebook_path: { type: "string", description: "Absolute path to the Jupyter notebook file" }, cell_index: { type: "integer", description: "Zero-based index of the cell" }, new_source: { type: "string", description: "New source code for the cell" } }, required: ["notebook_path", "cell_index", "new_source"] } },
- src/index.js:337-342 (registration)The dispatch logic in the CallToolRequestSchema handler that maps 'edit_cell_source' tool calls to the jupyterHandler.editCellSource method.case "edit_cell_source": return await this.jupyterHandler.editCellSource( args.notebook_path, args.cell_index, args.new_source );