Skip to main content

MCPConnectionError

Raised when MCP connection fails.

MCPToolError

Raised when MCP tool execution fails.

ensure_strict_json_schema

Mutates the given JSON schema to ensure it conforms to the strict standard that the OpenAI API expects.

_ensure_strict_json_schema

resolve_ref

is_dict

is_list

has_more_than_n_keys

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:
  1. 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

  1. 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()
  1. 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)
  • 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.

_ensure_strict_tool_schema

Ensure a tool has a strict schema compatible with OpenAI’s requirements. Strategy:
  • Ensure parameters exist with at least an empty properties object (OpenAI requirement).
  • Try converting parameters to strict using ensure_strict_json_schema.
  • If conversion fails, mark function.strict = False and keep best-effort parameters.

get_tools

Returns: List[FunctionTool]: Combined list of all available function tools from all connected MCP servers with strict schemas. 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.