> ## Documentation Index
> Fetch the complete documentation index at: https://docs.camel-ai.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Camel.toolkits.mcp toolkit

<a id="camel.toolkits.mcp_toolkit" />

<a id="camel.toolkits.mcp_toolkit.MCPConnectionError" />

## MCPConnectionError

```python theme={"system"}
class MCPConnectionError(Exception):
```

Raised when MCP connection fails.

<a id="camel.toolkits.mcp_toolkit.MCPToolError" />

## MCPToolError

```python theme={"system"}
class MCPToolError(Exception):
```

Raised when MCP tool execution fails.

<a id="camel.toolkits.mcp_toolkit.ensure_strict_json_schema" />

## ensure\_strict\_json\_schema

```python theme={"system"}
def ensure_strict_json_schema(schema: dict[str, Any]):
```

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

<a id="camel.toolkits.mcp_toolkit._ensure_strict_json_schema" />

## \_ensure\_strict\_json\_schema

```python theme={"system"}
def _ensure_strict_json_schema(json_schema: object):
```

<a id="camel.toolkits.mcp_toolkit.resolve_ref" />

## resolve\_ref

```python theme={"system"}
def resolve_ref():
```

<a id="camel.toolkits.mcp_toolkit.is_dict" />

## is\_dict

```python theme={"system"}
def is_dict(obj: object):
```

<a id="camel.toolkits.mcp_toolkit.is_list" />

## is\_list

```python theme={"system"}
def is_list(obj: object):
```

<a id="camel.toolkits.mcp_toolkit.has_more_than_n_keys" />

## has\_more\_than\_n\_keys

```python theme={"system"}
def has_more_than_n_keys(obj: dict[str, object], n: int):
```

<a id="camel.toolkits.mcp_toolkit.MCPToolkit" />

## MCPToolkit

```python theme={"system"}
class MCPToolkit(BaseToolkit):
```

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

2. 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()

3. 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.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.__init__" />

### **init**

```python theme={"system"}
def __init__(
    self,
    clients: Optional[List[MCPClient]] = None,
    config_path: Optional[str] = None,
    config_dict: Optional[Dict[str, Any]] = None,
    timeout: Optional[float] = None
):
```

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.is_connected" />

### is\_connected

```python theme={"system"}
def is_connected(self):
```

**Returns:**

bool: True if the toolkit is connected to all MCP servers,
False otherwise.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.connect_sync" />

### connect\_sync

```python theme={"system"}
def connect_sync(self):
```

Synchronously connect to all MCP servers.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.disconnect_sync" />

### disconnect\_sync

```python theme={"system"}
def disconnect_sync(self):
```

Synchronously disconnect from all MCP servers.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.__enter__" />

### **enter**

```python theme={"system"}
def __enter__(self):
```

Synchronously enter the async context manager.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.__exit__" />

### **exit**

```python theme={"system"}
def __exit__(
    self,
    exc_type,
    exc_val,
    exc_tb
):
```

Synchronously exit the async context manager.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.create_sync" />

### create\_sync

```python theme={"system"}
def create_sync(
    cls,
    clients: Optional[List[MCPClient]] = None,
    config_path: Optional[str] = None,
    config_dict: Optional[Dict[str, Any]] = None,
    timeout: Optional[float] = None
):
```

Synchronously create and connect to all MCP servers.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit._load_clients_from_config" />

### \_load\_clients\_from\_config

```python theme={"system"}
def _load_clients_from_config(self, config_path: str):
```

Load clients from configuration file.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit._load_clients_from_dict" />

### \_load\_clients\_from\_dict

```python theme={"system"}
def _load_clients_from_dict(self, config: Dict[str, Any]):
```

Load clients from configuration dictionary.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit._create_client_from_config" />

### \_create\_client\_from\_config

```python theme={"system"}
def _create_client_from_config(self, name: str, cfg: Dict[str, Any]):
```

Create a single MCP client from configuration.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit._ensure_strict_tool_schema" />

### \_ensure\_strict\_tool\_schema

```python theme={"system"}
def _ensure_strict_tool_schema(self, tool: FunctionTool):
```

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.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.get_tools" />

### get\_tools

```python theme={"system"}
def get_tools(self):
```

**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.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.get_text_tools" />

### get\_text\_tools

```python theme={"system"}
def get_text_tools(self):
```

**Returns:**

str: A string containing the descriptions of all tools.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.call_tool_sync" />

### call\_tool\_sync

```python theme={"system"}
def call_tool_sync(self, tool_name: str, tool_args: Dict[str, Any]):
```

Synchronously call a tool.

<a id="camel.toolkits.mcp_toolkit.MCPToolkit.list_available_tools" />

### list\_available\_tools

```python theme={"system"}
def list_available_tools(self):
```

**Returns:**

Dict\[str, List\[str]]: Dictionary mapping client indices to tool
names.
