MCP Server Runtime in Google Colab / Jupyter Notebook

Last updated: November 12, 2025

Issue Summary

Runtime error when executing Model Context Protocol (MCP) server code from LangChain documentation in Google Colab notebook environment.

Implementation For Google Colab / Jupyter Notebooks

Use transport="streamable-http" with threading to run the server in notebook environments:

# Install MCP library
!pip install mcp -q

from mcp.server.fastmcp import FastMCP

# Create your MCP server
mcp = FastMCP("Math")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

@mcp.tool()
def multiply(a: int, b: int) -> int:
    """Multiply two numbers"""
    return a * b

# Run the server (use streamable-http for Colab/notebooks)
import threading

def run_server():
    mcp.run(transport="streamable-http")

server_thread = threading.Thread(target=run_server, daemon=True)
server_thread.start()

Key Changes

  1. Transport Method: Changed from transport="stdio" to transport="streamable-http"

  2. Threading: Wrapped server execution in a daemon thread to prevent blocking notebook execution

  3. Environment: Removed if __name__ == "__main__": guard which is unnecessary in notebooks

Environment Considerations

  • Python Files: Use transport="stdio" with standard execution

  • Notebooks (Colab/Jupyter): Use transport="streamable-http" with threading approach

  • Notenest_asyncio.apply() does not resolve this specific issue

Tags

  • MCP Server

  • Google Colab

  • Runtime Error

  • FastMCP

  • Transport Configuration

  • Jupyter Notebooks