|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | +"""MCP Server Example |
| 15 | +
|
| 16 | +This example demonstrates how to use the MCP (Managed Code Processing) server |
| 17 | +with CAMEL agents for file operations. |
| 18 | +
|
| 19 | +Setup: |
| 20 | +1. Install Node.js and npm |
| 21 | +
|
| 22 | +2. Install MCP filesystem server globally: |
| 23 | + ```bash |
| 24 | + npm install -g @modelcontextprotocol/server-filesystem |
| 25 | + ``` |
| 26 | +
|
| 27 | +Usage: |
| 28 | +1. Run this script to start an MCP filesystem server |
| 29 | +2. The server will only operate within the specified directory |
| 30 | +3. All paths in responses will be relative to maintain privacy |
| 31 | +""" |
| 32 | + |
| 33 | +import asyncio |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | +from camel.agents import ChatAgent |
| 37 | +from camel.models import ModelFactory |
| 38 | +from camel.toolkits import MCPToolkit |
| 39 | +from camel.types import ModelPlatformType, ModelType |
| 40 | +from camel.utils.mcp_client import MCPClient |
| 41 | + |
| 42 | + |
| 43 | +async def mcp_client_example(): |
| 44 | + config = { |
| 45 | + "command": "uvx", |
| 46 | + "args": ["terminal-toolkit-mcp"], |
| 47 | + } |
| 48 | + async with MCPClient(config) as client: |
| 49 | + mcp_tools = await client.list_mcp_tools() |
| 50 | + print("Available MCP tools:", [tool.name for tool in mcp_tools.tools]) |
| 51 | + # Use shell_exec to list directory contents instead |
| 52 | + call_tool_result = await client.call_tool( |
| 53 | + "shell_exec", {"id": "main", "command": "ls -la"} |
| 54 | + ) |
| 55 | + print("Directory Contents:") |
| 56 | + print(call_tool_result.content[0].text) |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +async def mcp_toolkit_example(): |
| 63 | + # Use either config path or config dict to initialize the MCP toolkit. |
| 64 | + # 1. Use config path: |
| 65 | + config = { |
| 66 | + "mcpServers": { |
| 67 | + "terminal-toolkit-mcp": { |
| 68 | + "command": "uvx", |
| 69 | + "args": ["terminal-toolkit-mcp"], |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + # Connect to all MCP servers. |
| 75 | + async with MCPToolkit(config=config) as mcp_toolkit: |
| 76 | + sys_msg = "You are a helpful assistant" |
| 77 | + model = ModelFactory.create( |
| 78 | + model_platform=ModelPlatformType.DEFAULT, |
| 79 | + model_type=ModelType.DEFAULT, |
| 80 | + ) |
| 81 | + camel_agent = ChatAgent( |
| 82 | + system_message=sys_msg, |
| 83 | + model=model, |
| 84 | + tools=[*mcp_toolkit.get_tools()], |
| 85 | + ) |
| 86 | + user_msg = "Use terminal-toolkit-mcp to list 5 files in the project, using relative paths" |
| 87 | + response = await camel_agent.astep(user_msg) |
| 88 | + print(response.msgs[0].content) |
| 89 | + print(response.info['tool_calls']) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + asyncio.run(mcp_client_example()) |
| 94 | + # asyncio.run(mcp_toolkit_example()) |
| 95 | + |
0 commit comments