mcp_service
MCP (Model Context Protocol) client for integrating external tools with LLMs.
- class pipecat.services.mcp_service.MCPClient(server_params: StdioServerParameters | SseServerParameters | StreamableHttpParameters, tools_filter: list[str] | None = None, tools_output_filters: dict[str, Callable[[Any], Any]] | None = None, tools_arguments: dict[str, dict[str, Any]] | None = None, **kwargs)[source]
Bases:
BaseObjectClient for Model Context Protocol (MCP) servers.
Enables integration with MCP servers to provide external tools and resources to LLMs. Supports stdio, SSE, and streamable HTTP server connections with automatic tool registration and schema conversion.
The client maintains a persistent connection to the MCP server, opened on the first call to
tools()(or ahead of time viastart()). The tool schemas returned bytools()carry their call handlers, so the LLM service registers them automatically when they are advertised through anLLMContext, and closes the connection when the pipeline is torn down:mcp = MCPClient(server_params=...) context = LLMContext(messages=[...], tools=await mcp.tools())
A tool call that finds its connection gone drops that session and returns the error to the model, and the call after it connects again.
Call
close()only to release the connection earlier than pipeline teardown (e.g. from anon_client_disconnectedhandler). A closed client stays closed untilstart()ortools()opens it again, and a tool call on one raises.start()andclose()may be called from different tasks — the session is owned by a dedicated internal task. Scoping the client withasync with MCPClient(...)is also supported.- Raises:
TypeError – If server_params is not a supported parameter type.
- __init__(server_params: StdioServerParameters | SseServerParameters | StreamableHttpParameters, tools_filter: list[str] | None = None, tools_output_filters: dict[str, Callable[[Any], Any]] | None = None, tools_arguments: dict[str, dict[str, Any]] | None = None, **kwargs)[source]
Initialize the MCP client with server parameters.
- Parameters:
server_params – Server connection parameters (stdio, SSE, or streamable HTTP).
tools_filter – Optional list of tool names to register. If None, all tools are registered.
tools_output_filters – Optional dict mapping tool names to filter functions that process tool outputs. Each filter function receives the raw tool output (any type) and returns the processed output (any type).
tools_arguments – Optional dict mapping tool names to fixed arguments that are merged into every call of that tool (overriding any model-supplied values). The fixed parameter names are removed from the advertised tool schema so the LLM never sees them.
**kwargs – Additional arguments passed to the parent BaseObject.
- async start() None[source]
Start a persistent connection to the MCP server.
Opens the transport and initializes the MCP session. The session is reused for all subsequent tool calls and schema requests until close() is called. Idempotent, and called automatically by
tools().Can also be used via async context manager:
async with MCPClient(server_params=...) as mcp: ...
- async close() None[source]
Close the persistent MCP connection.
Called automatically at pipeline teardown once the client’s tools have been registered with an LLM service; call it directly only to release the connection earlier (e.g. on client disconnect). Safe to call multiple times, without having called start(), and from a different task than the one that called start().
The client stays closed until
start()ortools()opens it again: a tool call on a closed client raises rather than connect.
- async tools() ToolsSchema[source]
Get the available MCP tools, ready for LLM auto-registration.
Starts the server connection if needed, then returns a ToolsSchema whose function schemas carry their call handlers, so the LLM service registers them automatically when the context advertises its tools:
context = LLMContext(messages=[...], tools=await mcp.tools())
The connection is closed automatically at pipeline teardown; call
close()only to release it earlier.- Returns:
A ToolsSchema containing the available tools with handlers attached.
- async register_tools(llm: LLMService | LLMSwitcher) ToolsSchema[source]
Register all available MCP tools with an LLM service.
Deprecated since version 1.8.0: Use
tools()instead — pass its result toLLMContext(tools=...)and the handlers register automatically. Will be removed in 2.0.0.Discovers available tools from the active session, converts their schemas to Pipecat format, and registers them with the LLM service.
- Parameters:
llm – The Pipecat LLM service to register tools with.
- Returns:
A ToolsSchema containing all successfully registered tools.
- async get_tools_schema() ToolsSchema[source]
Get the schema of all available MCP tools without registering them.
Deprecated since version 1.8.0: Use
tools()instead. Will be removed in 2.0.0.Requires the client to be started via start() or async with.
- Returns:
A ToolsSchema containing all available tools.
- async register_tools_schema(tools_schema: ToolsSchema, llm: LLMService | LLMSwitcher) None[source]
Register previously obtained MCP tools with the LLM service.
Deprecated since version 1.8.0: Use
tools()instead — its schemas carry handlers that register automatically. Will be removed in 2.0.0.- Parameters:
tools_schema – The ToolsSchema to register with the LLM service.
llm – The Pipecat LLM service to register tools with.