Skip to main content
Glama

Using MCP with Jupyter Notebooks: Agent‑Driven Workflow in Python

Written by on .

Jupyter
Data Science
mcp
Agentic Ai

  1. Setting Up Jupyter MCP Server
    1. Connecting from a Python Frontend or Notebook
      1. Behind the Scenes
        1. My Thoughts
          1. References

            Model Context Protocol (MCP) lets you connect an AI agent directly to Jupyter notebooks using Python frontends. Agents can insert cells, execute code, modify markdown, and query notebook metadata using plain language. With MCP-enabled notebooks, developers can automate experiments and exploration in JupyterLab or Notebook environments using tools like Claude Desktop or Cursor 12.

            Setting Up Jupyter MCP Server

            Image

            First, install the required packages for JupyterLab and the MCP Server extension:

            pip install jupyterlab==4.4.1 jupyter-collaboration==4.0.0 ipykernel pip uninstall -y pycrdt datalayer_pycrdt pip install datalayer_pycrdt==0.12.17 pip install jupyter-mcp-server

            Start JupyterLab with a token for secure access:

            jupyter lab --port 8888 --IdentityProvider.token MY_TOKEN --ip 0.0.0.0

            This starts the MCP server endpoint at your JupyterLab instance and listens for incoming agent connections 23.

            Connecting from a Python Frontend or Notebook

            You can call MCP tools from Python using the official agent or client library.

            from mcp_agent import MCPClient config = { "jupyter": { "transport": "streamable_http", "url": "http://localhost:8888/mcp/v1", "headers": {"Authorization": "Token MY_TOKEN"} } } client = MCPClient(config)

            Image

            Once the client is initialized you can load tools and call them like this:

            await client.load_tools() # Get notebook info info = await client.call("get_notebook_info", {}) print(info) # Insert and execute a new Python cell output = await client.call("insert_execute_code_cell", { "cell_index": 1, "cell_source": "x = 10\nprint('x =', x)" }) print(output)

            You can also use tools like append_markdown_cell, read_cell, list_cells, execute_cell_simple_timeout, and others to interact with notebook contents according to your use case 3.

            Behind the Scenes

            The Jupyter MCP server wraps internal JupyterLab APIs for the kernel, notebook content manager, and collaboration. Each MCP tool maps to a Jupyter action such as inserting code, running it, or editing markdown. The tools include metadata for valid parameters and index ranges. When an agent calls a tool over JSON-RPC, the MCP server translates it into Jupyter API calls. Notebook updates appear in real time via JupyterLab’s collaboration layer 3.

            Authorization uses the token you pass at startup. The server ensures that only configured MCP tool calls are allowed. It logs requests and responses separately to avoid interfering with JSON streams 23.

            My Thoughts

            Integrating MCP into Jupyter notebooks turns a notebook into an AI agent–driven workspace. Agents can insert code, run experiments, and fetch results in real time without manual steps. This workflow is great for data experimentation and demo automation.

            My advice for beginners is to start with simple tools like get_notebook_info and insert_execute_code_cell to understand how inputs work. Always run MCP server in an isolated environment and avoid adding untrusted code. Proper validation of inputs is key to avoiding errors. With a secure configuration, notebook based MCP agents become powerful tools for interactive data science and experiment automation.

            References

            Footnotes

            1. Jupyter MCP Server Overview, LobeHub (June 2025) (link)

            2. Tools and documentation for Datalayer Jupyter MCP Server, official docs (2025) (link) 2 3

            3. Installation and configuration guide for Jupyter MCP server, Playbooks.com (Feb 2025) (link) 2 3 4

            Written by Om-Shree-0709 (@Om-Shree-0709)