MCP (Model Context Protocol)
MCP (Model Context Protocol)
MCP (Model Context Protocol)
The mcp module defines configuration and execution classes for tool use via MCP (Model Context Protocol).
MCPProvider configures remote MCP servers via SSE or Streamable HTTP transport. LocalStdioMCPProvider configures local MCP servers as subprocesses via stdio transport. ToolConfig defines which tools are available for LLM columns and how they are constrained.
For user-facing guides, see:
Holds MCP provider configurations. Can be empty (MCP is optional). Created first during resource initialization.
The central registry for tool configurations:
ToolConfig instances by tool_aliasMCPFacade instances via get_mcp(tool_alias)A lightweight facade scoped to a specific ToolConfig. Key methods:
Properties: tool_alias, providers, max_tool_call_turns, allow_tools, timeout_sec
The io.py module provides low-level MCP communication with performance optimizations:
Single event loop architecture: All MCP operations funnel through a dedicated background daemon thread running an asyncio event loop. This allows:
Session pooling: MCP sessions are created lazily and kept alive for the program’s duration:
atexit handlerRequest coalescing:
The list_tools operation uses request coalescing to prevent thundering herd:
Parallel tool execution:
The call_tools_parallel() function executes multiple tool calls concurrently via asyncio.gather(). This is used by MCPFacade when the model returns parallel tool calls in a single response.
The ModelFacade.generate() method accepts an optional tool_alias parameter:
When tool_alias is provided:
ModelFacade looks up the MCPFacade from MCPRegistryMCPFacade processes tool calls