Camel.toolkits.mcp toolkit
MCPClient
Internal class that provides an abstraction layer to interact with external tools using the Model Context Protocol (MCP). It supports three modes of connection:
-
stdio mode: Connects via standard input/output streams for local command-line interactions.
-
SSE mode (HTTP Server-Sent Events): Connects via HTTP for persistent, event-based interactions.
-
streamable-http mode: Connects via HTTP for persistent, streamable interactions.
Connection Lifecycle: There are three ways to manage the connection lifecycle:
- Using the async context manager:
- Using the factory method:
- Using explicit connect/disconnect:
Attributes:
command_or_url (str): URL for SSE mode or command executable for stdio
mode. (default: :obj:None
)
args (List[str]): List of command-line arguments if stdio mode is used.
(default: :obj:None
)
env (Dict[str, str]): Environment variables for the stdio mode command.
(default: :obj:None
)
timeout (Optional[float]): Connection timeout.
(default: :obj:None
)
headers (Dict[str, str]): Headers for the HTTP request.
(default: :obj:None
)
mode (Optional[str]): Connection mode. Can be “sse” for Server-Sent
Events, “streamable-http” for streaming HTTP,
or None for stdio mode.
(default: :obj:None
)
strict (Optional[bool]): Whether to enforce strict mode for the
function call. (default: :obj:False
)
init
connect_sync
Synchronously connect to the MCP server.
disconnect_sync
Synchronously disconnect from the MCP server.
connection_sync
Synchronously connect to the MCP server.
list_mcp_tools_sync
Synchronously list the available tools from the connected MCP server.
generate_function_from_mcp_tool
Dynamically generates a Python callable function corresponding to a given MCP tool.
Parameters:
- mcp_tool (Tool): The MCP tool definition received from the MCP server.
Returns:
Callable: A dynamically created async Python function that wraps the MCP tool.
generate_function_from_mcp_tool_sync
Synchronously generate a function from an MCP tool.
_build_tool_schema
get_tools
Returns:
List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit.
get_text_tools
Returns:
str: A string containing the descriptions of the tools in the toolkit.
call_tool_sync
Synchronously call a tool.
session
create_sync
Synchronously create and connect to the MCP server.
enter
Synchronously enter the async context manager.
exit
Synchronously exit the async context manager.
Parameters:
- exc_type (Optional[Type[Exception]]): The type of exception that occurred during the execution of the with statement.
- exc_val (Optional[Exception]): The exception that occurred during the execution of the with statement.
- exc_tb (Optional[TracebackType]): The traceback of the exception that occurred during the execution of the with statement.
Returns:
None
MCPToolkit
MCPToolkit provides a unified interface for managing multiple MCP server connections and their tools.
This class handles the lifecycle of multiple MCP server connections and offers a centralized configuration mechanism for both local and remote MCP services.
Connection Lifecycle: There are three ways to manage the connection lifecycle:
- Using the async context manager:
- Using the factory method:
- Using explicit connect/disconnect:
Parameters:
- servers (Optional[List[MCPClient]]): List of MCPClient instances to manage. (default: :obj:
None
) - config_path (Optional[str]): Path to a JSON configuration file defining MCP servers. (default: :obj:
None
) - config_dict (Optional[Dict[str, Any]]): Dictionary containing MCP server configurations in the same format as the config file. (default: :obj:
None
) - strict (Optional[bool]): Whether to enforce strict mode for the function call. (default: :obj:
False
) - Note: Either
servers
,config_path
, orconfig_dict
must be provided. If multiple are provided, servers from all sources will be combined. For web servers in the config, you can specify authorization headers using the “headers” field to connect to protected MCP server endpoints. Example configuration: .. code-block:: json{ "mcpServers": { "protected-server": { "url": "https://example.com/mcp", "timeout": 30, "headers": { "Authorization": "Bearer YOUR_TOKEN", "X-API-Key": "YOUR_API_KEY" } } } }
- Attributes:
- servers (List[MCPClient]): List of MCPClient instances being managed.
init
_load_servers_from_config
Loads MCP server configurations from a JSON file.
Parameters:
- config_path (str): Path to the JSON configuration file.
- strict (bool): Whether to enforce strict mode for the function call. (default: :obj:
False
)
Returns:
List[MCPClient]: List of configured MCPClient instances.
_load_servers_from_dict
Loads MCP server configurations from a dictionary.
Parameters:
- config (Dict[str, Any]): Dictionary containing server configurations.
- strict (bool): Whether to enforce strict mode for the function call. (default: :obj:
False
)
Returns:
List[MCPClient]: List of configured MCPClient instances.
connect_sync
Synchronously connect to all MCP servers.
disconnect_sync
Synchronously disconnect from all MCP servers.
connection_sync
Synchronously connect to all MCP servers.
is_connected
Returns:
bool: True if connected, False otherwise.
get_tools
Returns:
List[FunctionTool]: Combined list of all available function tools.
get_text_tools
Returns:
str: A string containing the descriptions of the tools in the toolkit.