Skip to main content
Glama
azharlabs
by azharlabs

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
NameRequiredDescriptionDefault
notebook_pathYesAbsolute path to the Jupyter notebook file
cell_indexYesZero-based index of the cell
new_typeYesNew cell type

Implementation Reference

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

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