🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
title: 'Connect to Existing MCP Tools in CAMEL ChatAgent' description: 'How to plug any MCP protocol tool (like file systems, web APIs) into your CAMEL ChatAgent for seamless tool-using agents.' icon: 'network'
doc_code_map: - "camel/toolkits/mcp_toolkit.py" - "camel/utils/mcp_client.py"
Overview
You can connect any Model Context Protocol (MCP) tool—like the official filesystem server—directly to your CAMEL ChatAgent. This gives your agents natural language access to external filesystems, databases, or any MCP-compatible service.
Use Case: Let your agent list files or read documents by wiring up the official MCP Filesystem server as a tool—no code changes to the agent required! You can use any MCP-compatible tool. For this example, we'll use the official filesystem server from the Model Context Protocol community.Install globally using npm:
```bash Terminal npm install -g @modelcontextprotocol/server-filesystem ``` Start the server in the directory you want to expose (e.g., your project root):
```bash Terminal npx -y @modelcontextprotocol/server-filesystem . ``` This limits all file operations to the current directory. Now, connect the MCP tool server to CAMEL using
MCPClient or MCPToolkit.
Direct Client Example (Async): ```python mcp_filesystem_client.py lines icon="python" import asyncio from camel.utils.mcp_client import MCPClient
async def mcp_client_example():
config = {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "."],
}
async with MCPClient(config) as client:
mcp_tools = await client.list_mcp_tools()
print("Available MCP tools:", [tool.name for tool in mcp_tools.tools])
call_tool_result = await client.call_tool(
"list_directory", {"path": "."}
)
print("Directory Contents:")
for i, item in enumerate(call_tool_result.content[0].text.split('\n')):
if item.strip():
print(f" {item}")
if i >= 4:
break
asyncio.run(mcp_client_example())
```
Add the tools exposed by the MCP server to your ChatAgent.
Async Agent Integration: ```python mcp_agent_integration.py lines icon="python" import asyncio from pathlib import Path from camel.agents import ChatAgent from camel.models import ModelFactory from camel.toolkits import MCPToolkit from camel.types import ModelPlatformType, ModelType
async def mcp_toolkit_example():
config_path = Path(__file__).parent / "mcp_servers_config.json"
async with MCPToolkit(config_path=str(config_path)) as mcp_toolkit:
sys_msg = "You are a helpful assistant"
model = ModelFactory.create(
model_platform=ModelPlatformType.DEFAULT,
model_type=ModelType.DEFAULT,
)
camel_agent = ChatAgent(
system_message=sys_msg,
model=model,
tools=[*mcp_toolkit.get_tools()],
)
user_msg = "List 5 files in the project, using relative paths"
response = await camel_agent.astep(user_msg)
print(response.msgs[0].content)
print(response.info['tool_calls'])
asyncio.run(mcp_toolkit_example())
```
<br />
<strong>Config Example (<code>mcp_servers_config.json</code>):</strong>
```json mcp_servers_config.json lines icon="settings"
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/[email protected]",
"."
]
}
},
"mcpWebServers": {}
}
```
Use step() for synchronous execution:
```python mcp_agent_sync.py lines icon="python" from pathlib import Path from camel.agents import ChatAgent from camel.models import ModelFactory from camel.toolkits import MCPToolkit from camel.types import ModelPlatformType, ModelType
def mcp_toolkit_example_sync():
config_path = Path(__file__).parent / "mcp_servers_config.json"
with MCPToolkit(config_path=str(config_path)) as mcp_toolkit:
sys_msg = "You are a helpful assistant"
model = ModelFactory.create(
model_platform=ModelPlatformType.DEFAULT,
model_type=ModelType.DEFAULT,
)
camel_agent = ChatAgent(
system_message=sys_msg,
model=model,
tools=[*mcp_toolkit.get_tools()],
)
user_msg = "List 5 files in the project, using relative paths"
response = camel_agent.step(user_msg)
print(response.msgs[0].content)
print(response.info['tool_calls'])
mcp_toolkit_example_sync()
```
That's it!
Your CAMEL agent can now leverage any external tool (filesystem, APIs, custom scripts) that supports MCP. Plug and play!