Unified MCP Client
This module provides a unified interface for connecting to MCP servers using different transport protocols (stdio, sse, streamable-http, websocket). The client can automatically detect the transport type based on configuration.
Supported transport types.
Validate that either command or url is provided.
Automatically detect transport type based on configuration.
Unified MCP client that automatically detects and connects to servers using the appropriate transport protocol.
This client provides a unified interface for connecting to Model Context Protocol (MCP) servers using different transport protocols including STDIO, HTTP/HTTPS, WebSocket, and Server-Sent Events (SSE). The client automatically detects the appropriate transport type based on the configuration provided.
The client should be used as an async context manager for automatic connectionmanagement.
Parameters:
ServerConfig
object or a dictionary that will be converted to a :obj:ServerConfig
. The configuration determines the transport type and connection parameters.None
)10.0
)Get the detected transport type.
Convert complex MCP connection errors to simple, understandable messages.
Get the current session if connected.
Check if the client is currently connected.
Returns:
ListToolsResult: Result containing available MCP tools.
Dynamically generates a Python callable function corresponding to a given MCP tool.
Parameters:
Returns:
Callable: A dynamically created Python function that wraps the MCP tool and works in both sync and async contexts.
Build tool schema for OpenAI function calling format.
Returns:
List[FunctionTool]: A list of :obj:FunctionTool
objects
representing the available tools from the MCP server. Returns
an empty list if the client is not connected.
Note:
This method requires an active connection to the MCP server. If the client is not connected, an empty list will be returned.
Returns:
str: Text description of tools
Synchronously call a tool by name with the provided arguments.
Parameters:
Returns:
Any: The result returned by the tool execution.
Create an MCP client from configuration.
Factory function that creates an :obj:MCPClient
instance from various
configuration formats. This is the recommended way to create MCP clients
as it handles configuration validation and type conversion automatically.
Parameters:
ServerConfig
object. If a dictionary is provided, it will be automatically converted toServerConfig
. **kwargs: Additional keyword arguments passed to the :obj:MCPClient
constructor, such as :obj:client_info
, :obj:timeout
.Returns:
MCPClient: A configured :obj:MCPClient
instance ready for use as
an async context manager.
Create an MCP client from a configuration file.
Parameters:
Returns:
MCPClient: MCPClient instance as an async context manager.
Example config file:
\{ "mcpServers": \{ "filesystem": \{ "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/path" ] \}, "remote-server": \{ "url": "https://api.example.com/mcp", "headers": \{"Authorization": "Bearer token"\} \} \} \}
Usage: .. code-block:: python
async with create_mcp_client_from_config_file( “config.json”, “filesystem” ) as client: tools = client.get_tools()