-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmcp_manager.py
77 lines (64 loc) · 2.54 KB
/
mcp_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
"""MCP Server Example
This example demonstrates how to use the MCP (Managed Code Processing) server
with CAMEL agents for file operations.
Setup:
1. Install Node.js and npm
2. Install MCP filesystem server globally:
```bash
npm install -g @modelcontextprotocol/server-filesystem
```
Usage:
1. Run this script to start an MCP filesystem server
2. The server will only operate within the specified directory
3. All paths in responses will be relative to maintain privacy
"""
import asyncio
from pathlib import Path
from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.toolkits import MCPToolkitManager
from camel.types import ModelPlatformType, ModelType
async def main():
config_path = Path(__file__).parent / "mcp_servers_config.json"
manager = MCPToolkitManager.from_config(str(config_path))
async with manager.connection():
tools = manager.get_all_tools()
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=tools,
)
user_msg = "List all README files in the project, using relative paths"
response = await camel_agent.astep(user_msg)
print(response.msgs[0].content)
print(response.info['tool_calls'])
if __name__ == "__main__":
asyncio.run(main())
'''
===============================================================================
Here are all the README files in the project, listed with their relative paths:
- `./README.md`
- `./apps/agents/README.md`
- `./apps/data_explorer/README.md`
- `./docs/README.md`
- `./.pytest_cache/README.md`
===============================================================================
'''