forked from mcp-use/mcp-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_server_manager_use.py
More file actions
113 lines (82 loc) · 3.36 KB
/
simple_server_manager_use.py
File metadata and controls
113 lines (82 loc) · 3.36 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import asyncio
import sys
from pathlib import Path
from langchain_core.tools import BaseTool
from langchain_openai import ChatOpenAI
from pydantic import BaseModel
from mcp_use.agents.mcpagent import MCPAgent
# Add the project root to the Python path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from mcp_use.managers.base import BaseServerManager
class DynamicTool(BaseTool):
"""A tool that is created dynamically."""
name: str
description: str
args_schema: type[BaseModel] | None = None
def _run(self) -> str:
return f"Hello from {self.name}!"
async def _arun(self) -> str:
return f"Hello from {self.name}!"
class HelloWorldTool(BaseTool):
"""A simple tool that returns a greeting and adds a new tool."""
name: str = "hello_world"
description: str = "Returns the string 'Hello, World!' and adds a new dynamic tool."
args_schema: type[BaseModel] | None = None
server_manager: "SimpleServerManager"
def _run(self) -> str:
new_tool = DynamicTool(
name=f"dynamic_tool_{len(self.server_manager.tools)}", description="A dynamically created tool."
)
self.server_manager.add_tool(new_tool)
return "Hello, World! I've added a new tool. You can use it now."
async def _arun(self) -> str:
new_tool = DynamicTool(
name=f"dynamic_tool_{len(self.server_manager.tools)}", description="A dynamically created tool."
)
self.server_manager.add_tool(new_tool)
return "Hello, World! I've added a new tool. You can use it now."
class SimpleServerManager(BaseServerManager):
"""A simple server manager that provides a HelloWorldTool."""
def __init__(self):
self._tools: list[BaseTool] = []
self._initialized = False
# Pass a reference to the server manager to the tool
self._tools.append(HelloWorldTool(server_manager=self))
def add_tool(self, tool: BaseTool):
self._tools.append(tool)
async def initialize(self) -> None:
self._initialized = True
@property
def tools(self) -> list[BaseTool]:
return self._tools
def has_tool_changes(self, current_tool_names: set[str]) -> bool:
new_tool_names = {tool.name for tool in self.tools}
return new_tool_names != current_tool_names
async def main():
# Initialize the LLM
llm = ChatOpenAI(model="gpt-5")
# Instantiate the custom server manager
simple_server_manager = SimpleServerManager()
# Create an MCPAgent with the custom server manager
agent = MCPAgent(
llm=llm,
use_server_manager=True,
server_manager=simple_server_manager,
pretty_print=True,
)
# Manually initialize the agent
await agent.initialize()
# Run the agent with a query that uses the custom tool
print("--- First run: calling hello_world ---")
result = await agent.run("Use the hello_world tool", manage_connector=False)
print(result)
# Clear the conversation history to avoid confusion
agent.clear_conversation_history()
# Run the agent again to show that the new tool is available
print("\n--- Second run: calling the new dynamic tool ---")
result = await agent.run("Use the dynamic_tool_1", manage_connector=False)
print(result)
# Manually close the agent
await agent.close()
if __name__ == "__main__":
asyncio.run(main())