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
Transport Method: Changed from
transport="stdio"totransport="streamable-http"Threading: Wrapped server execution in a daemon thread to prevent blocking notebook execution
Environment: Removed
if __name__ == "__main__":guard which is unnecessary in notebooks
Environment Considerations
Python Files: Use
transport="stdio"with standard executionNotebooks (Colab/Jupyter): Use
transport="streamable-http"with threading approachNote:
nest_asyncio.apply()does not resolve this specific issue
Tags
MCP Server
Google Colab
Runtime Error
FastMCP
Transport Configuration
Jupyter Notebooks