convert_cell_type
Change a Jupyter notebook cell's type between code, markdown, or raw formats to modify content behavior and presentation.
Instructions
Convert a cell from one type to another
Input Schema
TableJSON 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_type | Yes | New cell type |
Implementation Reference
- src/jupyter-handler.js:269-311 (handler)Main handler function that performs the cell type conversion by modifying the notebook JSON, handling type-specific properties like execution_count and outputs, and persisting the changes.async convertCellType(notebookPath, cellIndex, newType) { const notebook = await this.readNotebook(notebookPath); this.validateCellIndex(notebook.cells, cellIndex); this.validateCellType(newType); const cell = notebook.cells[cellIndex]; const oldType = cell.cell_type; if (oldType === newType) { return { content: [ { type: "text", text: `Cell ${cellIndex} is already of type '${newType}'` } ] }; } // Convert cell type cell.cell_type = newType; // Handle type-specific properties if (newType === 'code') { cell.execution_count = null; cell.outputs = []; } else { // Remove code-specific properties for non-code cells delete cell.execution_count; delete cell.outputs; } await this.writeNotebook(notebookPath, notebook); return { content: [ { type: "text", text: `Successfully converted cell ${cellIndex} from '${oldType}' to '${newType}'` } ] }; }
- src/index.js:166-184 (schema)Input schema defining the parameters for the convert_cell_type tool.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_type: { type: "string", enum: ["code", "markdown", "raw"], description: "New cell type" } }, required: ["notebook_path", "cell_index", "new_type"] }
- src/index.js:163-185 (registration)Tool registration in the ListTools response, providing name, description, and schema.{ name: "convert_cell_type", description: "Convert a cell from one type to another", 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_type: { type: "string", enum: ["code", "markdown", "raw"], description: "New cell type" } }, required: ["notebook_path", "cell_index", "new_type"] } },
- src/index.js:362-367 (registration)Dispatch/routing in CallToolRequest handler to invoke the convertCellType method.case "convert_cell_type": return await this.jupyterHandler.convertCellType( args.notebook_path, args.cell_index, args.new_type );
- src/jupyter-handler.js:89-93 (helper)Helper method to validate the new cell type against supported types.validateCellType(cellType) { if (!this.supportedCellTypes.includes(cellType)) { throw new Error(`Invalid cell type '${cellType}'. Supported types: ${this.supportedCellTypes.join(', ')}`); } }