Add MCPClient docstrings

This commit is contained in:
Mark Backman
2025-06-25 17:51:42 -04:00
parent bb3bb8d9c6
commit 04b70ddf13

View File

@@ -1,3 +1,11 @@
#
# Copyright (c) 20242025, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
"""MCP (Model Context Protocol) client for integrating external tools with LLMs."""
import json import json
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
@@ -19,6 +27,20 @@ except ModuleNotFoundError as e:
class MCPClient(BaseObject): 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__( def __init__(
self, self,
server_params: Union[StdioServerParameters, SseServerParameters], server_params: Union[StdioServerParameters, SseServerParameters],
@@ -39,6 +61,17 @@ class MCPClient(BaseObject):
) )
async def register_tools(self, llm) -> ToolsSchema: 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) tools_schema = await self._register_tools(llm)
return tools_schema return tools_schema
@@ -46,13 +79,13 @@ class MCPClient(BaseObject):
self, tool_name: str, tool_schema: Dict[str, Any] self, tool_name: str, tool_schema: Dict[str, Any]
) -> FunctionSchema: ) -> FunctionSchema:
"""Convert an mcp tool schema to Pipecat's FunctionSchema format. """Convert an mcp tool schema to Pipecat's FunctionSchema format.
Args: Args:
tool_name: The name of the tool tool_name: The name of the tool
tool_schema: The mcp tool schema tool_schema: The mcp tool schema
Returns: Returns:
A FunctionSchema instance A FunctionSchema instance
""" """
logger.debug(f"Converting schema for tool '{tool_name}'") logger.debug(f"Converting schema for tool '{tool_name}'")
logger.trace(f"Original schema: {json.dumps(tool_schema, indent=2)}") 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: async def _sse_register_tools(self, llm) -> ToolsSchema:
"""Register all available mcp.run tools with the LLM service. """Register all available mcp.run tools with the LLM service.
Args: Args:
llm: The Pipecat LLM service to register tools with llm: The Pipecat LLM service to register tools with
Returns: Returns:
@@ -120,6 +154,7 @@ class MCPClient(BaseObject):
async def _stdio_register_tools(self, llm) -> ToolsSchema: async def _stdio_register_tools(self, llm) -> ToolsSchema:
"""Register all available mcp.run tools with the LLM service. """Register all available mcp.run tools with the LLM service.
Args: Args:
llm: The Pipecat LLM service to register tools with llm: The Pipecat LLM service to register tools with
Returns: Returns: