The session proxy layer is responsible for wiring together the local WebSocket server, the FastMCP proxy, and the middleware chain so that an AI agent’s tool calls are transparently forwarded to a live Google Colab browser session. Three classes share this responsibility:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/googlecolab/colab-mcp/llms.txt
Use this file to discover all available pages before exploring further.
ColabSessionProxy acts as the top-level orchestrator, ColabProxyClient manages the lifecycle of the FastMCP Client that connects to Colab, and ColabTransport provides the ClientTransport implementation that routes MCP messages over ColabWebSocketServer’s memory streams.
ColabSessionProxy
Module:colab_mcp.session
ColabSessionProxy is the entry point for enabling the proxy subsystem. It creates and wires together a ColabWebSocketServer, a ColabProxyClient, a FastMCPProxy, and the two middleware layers. All resources are tracked through an AsyncExitStack so that a single cleanup() call tears everything down cleanly.
Startup Pattern
The following pattern, taken directly from__init__.py, shows how ColabSessionProxy is used to extend the main FastMCP server:
Methods
start_proxy_server()
- Enters
ColabWebSocketServeras an async context manager and assigns it toself.wss. - Enters
ColabProxyClient(self.wss)as an async context manager (which kicks off the background task that waits for the WebSocket connection). - Constructs a
FastMCPProxywithclient_factory=proxy_client.client_factoryandinstructions="Connects to a user's Google Colab session in a browser and allows for interactions with their Google Colab notebook", and assigns it toself.proxy_server. - Appends
ColabProxyMiddleware(proxy_client)thenToolInjectionMiddleware(tools=[check_session_proxy_tool])toself.middleware.
self.proxy_server and self.middleware are fully initialized and ready to be mounted on the main MCP server.
cleanup()
AsyncExitStack. This shuts down the ColabWebSocketServer (closing the WebSocket listener and both memory streams) and the ColabProxyClient (cancelling any pending background tasks and closing the proxy Client).
Attributes
The
FastMCPProxy instance to mount on the main MCP server with mcp.mount(). None before start_proxy_server() is called. The proxy uses proxy_client.client_factory to obtain either the live Colab client or a stubbed empty client on each request.Ordered list of middleware to register with
mcp.add_middleware(). After start_proxy_server() this list contains exactly two entries:ColabProxyMiddleware— sets connection state on every message and handles theopen_colab_browser_connectionwait logic.ToolInjectionMiddleware— injects theopen_colab_browser_connectiontool into the tool list.
The
ColabWebSocketServer instance. None before start_proxy_server() is called.ColabProxyClient
Module:colab_mcp.session
ColabProxyClient manages the FastMCP Client that communicates with the Colab browser session via ColabTransport. It uses an async background task to establish the Client connection (which blocks until the WebSocket is connected), so the rest of the application can continue initializing without blocking.
Usage
ColabProxyClient is used as an async context manager:
Constructor
The
ColabWebSocketServer whose connection_live event and memory streams will be used to detect and serve the active connection.Methods
is_connected()
True only when both conditions are met:
wss.connection_liveis set (the WebSocket client is actively connected), andproxy_mcp_clientis notNone(theClientcontext has been fully entered).
connection_live is set as soon as the WebSocket handshake completes, but proxy_mcp_client is only assigned once the Client async context manager’s __aenter__ finishes.
client_factory()
Client for the current state:
- If
is_connected()isTrue: returnsproxy_mcp_client— the live client connected to the Colab session. - Otherwise: returns
stubbed_mcp_client— aClientconnected to a local emptyFastMCP()instance that returns empty responses for all requests.
FastMCPProxy, which calls it on every proxied request.
await_proxy_connection()
UI_CONNECTION_TIMEOUT (60.0 seconds) for both wss.connection_live to be set and the background _start_proxy_client task to complete. Uses asyncio.wait_for with asyncio.gather internally; a TimeoutError is suppressed so this method always returns without raising.
ColabTransport
Module:colab_mcp.session
ColabTransport implements the ClientTransport interface expected by FastMCP’s Client, routing all MCP session I/O through ColabWebSocketServer’s in-process memory streams rather than a real network connection.
Constructor
The
ColabWebSocketServer instance whose read_stream and write_stream will be used as the transport’s I/O channels.Methods
connect_session(**session_kwargs)
ClientSession backed by wss.read_stream and wss.write_stream. Any keyword arguments are forwarded directly to the ClientSession constructor. This is the method called by the FastMCP Client internals when it needs to establish a session.