Camel.toolkits.mcp toolkit
MCPConnectionError
Raised when MCP connection fails.
MCPToolError
Raised when MCP tool execution fails.
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. The toolkit manages multiple :obj:MCPClient
instances and
aggregates their tools into a unified interface compatible with the CAMEL
framework.
Connection Lifecycle: There are three ways to manage the connection lifecycle:
- Using the async context manager (recommended):
.. code-block:: python
async with MCPToolkit(config_path=“config.json”) as toolkit:
Toolkit is connected here
tools = toolkit.get_tools()
Toolkit is automatically disconnected here
- Using the factory method:
.. code-block:: python
toolkit = await MCPToolkit.create(config_path=“config.json”)
Toolkit is connected here
tools = toolkit.get_tools()
Don’t forget to disconnect when done!
await toolkit.disconnect()
- Using explicit connect/disconnect:
.. code-block:: python
toolkit = MCPToolkit(config_path=“config.json”) await toolkit.connect()
Toolkit is connected here
tools = toolkit.get_tools()
Don’t forget to disconnect when done!
await toolkit.disconnect()
Parameters:
- clients (Optional[List[MCPClient]], optional): List of :obj:
MCPClient
instances to manage. (default: :obj:None
) - config_path (Optional[str], optional): Path to a JSON configuration file defining MCP servers. The file should contain server configurations in the standard MCP format. (default: :obj:
None
) - config_dict (Optional[Dict[str, Any]], optional): Dictionary containing MCP server configurations in the same format as the config file. This allows for programmatic configuration without file I/O. (default: :obj:
None
) - timeout (Optional[float], optional): Timeout for connection attempts in seconds. This timeout applies to individual client connections. (default: :obj:
None
) - strict (Optional[bool], optional): Flag to indicate strict mode. (default: :obj:
False
) - clients (List[MCPClient]): List of :obj:
MCPClient
instances being managed by this toolkit.
init
is_connected
Returns:
bool: True if the toolkit is connected to all MCP servers, False otherwise.
connect_sync
Synchronously connect to all MCP servers.
disconnect_sync
Synchronously disconnect from all MCP servers.
enter
Synchronously enter the async context manager.
exit
Synchronously exit the async context manager.
create_sync
Synchronously create and connect to all MCP servers.
_load_clients_from_config
Load clients from configuration file.
_load_clients_from_dict
Load clients from configuration dictionary.
_create_client_from_config
Create a single MCP client from configuration.
get_tools
Returns:
List[FunctionTool]: Combined list of all available function tools from all connected MCP servers. Returns an empty list if no clients are connected or if no tools are available.
get_text_tools
Returns:
str: A string containing the descriptions of all tools.
call_tool_sync
Synchronously call a tool.
list_available_tools
Returns:
Dict[str, List[str]]: Dictionary mapping client indices to tool names.