Skip to main content
Glama
azharlabs
by azharlabs

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
NameRequiredDescriptionDefault
notebook_pathYesAbsolute path to the Jupyter notebook file
cell_idYesCell ID or zero-based index of the cell to edit
new_sourceYesNew source code for the cell

Implementation Reference

  • 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);
    }
  • 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
      );
  • 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}`
          }
        ]
      };
    }

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