diff --git a/src/pipecat/services/mcp_service.py b/src/pipecat/services/mcp_service.py index a644d8f1b..48b0f9f1d 100644 --- a/src/pipecat/services/mcp_service.py +++ b/src/pipecat/services/mcp_service.py @@ -1,3 +1,11 @@ +# +# Copyright (c) 2024–2025, Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# + +"""MCP (Model Context Protocol) client for integrating external tools with LLMs.""" + import json from typing import Any, Dict, List, Optional, Union @@ -19,6 +27,20 @@ except ModuleNotFoundError as e: class MCPClient(BaseObject): + """Client for Model Context Protocol (MCP) servers. + + Enables integration with MCP servers to provide external tools and resources + to LLMs. Supports both stdio and SSE server connections with automatic tool + registration and schema conversion. + + Args: + server_params: Server connection parameters (stdio or SSE). + **kwargs: Additional arguments passed to the parent BaseObject. + + Raises: + TypeError: If server_params is not a supported parameter type. + """ + def __init__( self, server_params: Union[StdioServerParameters, SseServerParameters], @@ -39,6 +61,17 @@ class MCPClient(BaseObject): ) async def register_tools(self, llm) -> ToolsSchema: + """Register all available MCP tools with an LLM service. + + Connects to the MCP server, discovers available tools, converts their + schemas to Pipecat format, and registers them with the LLM service. + + Args: + llm: The Pipecat LLM service to register tools with. + + Returns: + A ToolsSchema containing all successfully registered tools. + """ tools_schema = await self._register_tools(llm) return tools_schema @@ -46,13 +79,13 @@ class MCPClient(BaseObject): self, tool_name: str, tool_schema: Dict[str, Any] ) -> FunctionSchema: """Convert an mcp tool schema to Pipecat's FunctionSchema format. + Args: tool_name: The name of the tool tool_schema: The mcp tool schema Returns: A FunctionSchema instance """ - logger.debug(f"Converting schema for tool '{tool_name}'") logger.trace(f"Original schema: {json.dumps(tool_schema, indent=2)}") @@ -72,6 +105,7 @@ class MCPClient(BaseObject): async def _sse_register_tools(self, llm) -> ToolsSchema: """Register all available mcp.run tools with the LLM service. + Args: llm: The Pipecat LLM service to register tools with Returns: @@ -120,6 +154,7 @@ class MCPClient(BaseObject): async def _stdio_register_tools(self, llm) -> ToolsSchema: """Register all available mcp.run tools with the LLM service. + Args: llm: The Pipecat LLM service to register tools with Returns: