-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtool.py
303 lines (209 loc) · 8.86 KB
/
tool.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import asyncio
import pathlib
import time
from typing import List, Any, Dict
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain_mcp import MCPToolkit
from langchain_core.tools import BaseTool
from typing import Any, Dict, List
from langchain_core.tools import BaseTool
from pydantic import Field, PrivateAttr
class MCPToolWrapper(BaseTool):
"""A wrapper for an individual tool managed by the SyncInvocationManager."""
_manager: Any = PrivateAttr()
_tool: Any = PrivateAttr()
def __init__(self, tool: BaseTool, manager: "SyncInvocationManager"):
super().__init__(name=tool.name, description=tool.description)
self.name = tool.name
self.description = tool.description
self._manager = manager
self._tool = tool
def _run(self, **kwargs: Any) -> Any:
"""Run the tool synchronously using the SyncInvocationManager."""
try:
print(f"Running tool: {self.name} with args: {kwargs}")
result = self._manager.invoke_tool_sync(self._tool, kwargs)
if result is None:
print(f"Tool {self.name} returned no result.")
else:
print(f"Tool {self.name} result: {result}")
return result
except Exception as e:
print(f"Error while running tool {self.name}: {e}")
return None
async def _arun(self, **kwargs: Any) -> Any:
"""Asynchronous run (if needed), wraps the synchronous call."""
return self._run(**kwargs)
class MCPToolManager:
"""Manages tools provided by the SyncInvocationManager and converts them into LangChain tools."""
def __init__(self, manager: "SyncInvocationManager"):
self.manager = manager
self.tools: List[BaseTool] = []
def load_tools(self) -> List[BaseTool]:
"""Load tools from SyncInvocationManager and wrap them in LangChain-compatible structure."""
raw_tools = self.manager.get_tools_sync()
self.tools = [MCPToolWrapper(tool, self.manager) for tool in raw_tools]
return self.tools
class SyncInvocationManager:
def __init__(self, command: str, args: list[str], env: dict[str, str] | None = None):
self.loop = asyncio.new_event_loop()
self.server_params = StdioServerParameters(
command=command,
args=args,
env=env,
)
self.client_ctx = None
self.client = None
self.session_ctx = None
self.session = None
self.toolkit = None
self._task = None # Add this line
async def _start_async(self):
# Manually enter the stdio_client context
self.client_ctx = stdio_client(self.server_params)
self.client = await self.client_ctx.__aenter__()
read, write = self.client
# Manually enter the ClientSession context
self.session_ctx = ClientSession(read, write)
self.session = await self.session_ctx.__aenter__()
self.toolkit = MCPToolkit(session=self.session)
await self.toolkit.initialize()
def get_tools_sync(self) -> List[BaseTool]:
# Now that session is open, just return tools directly
return self.toolkit.get_tools()
def invoke_tool_sync(self, tool: BaseTool, input_data: Dict[str, Any]) -> Any:
try:
return self.loop.run_until_complete(tool.ainvoke(input_data))
except Exception as e:
print(f"Error invoking tool {tool.name}: {e}")
return None
def start(self):
asyncio.set_event_loop(self.loop)
self._task = self.loop.create_task(self._start_async())
self.loop.run_until_complete(self._task)
def stop(self):
if self._task and not self._task.done():
cleanup_task = self.loop.create_task(self._stop_async())
self.loop.run_until_complete(cleanup_task)
self.loop.close()
async def _stop_async(self):
# Exit contexts in the same task and loop they were entered
if self.session_ctx:
await self.session_ctx.__aexit__(None, None, None)
if self.client_ctx:
await self.client_ctx.__aexit__(None, None, None)
def file_system_tool():
print("""
This is file_system_tool
""")
manager = SyncInvocationManager(command="npx", args=["-y", "@modelcontextprotocol/server-filesystem", "/"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
def memory_tool():
print("""
This is memory_tool
""")
manager = SyncInvocationManager(command="npx", args=["-y", "@modelcontextprotocol/server-memory"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
def playwright():
print("""
This is playwright
""")
manager = SyncInvocationManager(command="npx", args=["-y", "@executeautomation/playwright-mcp-server"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
def youtube_transcript():
print("""
This is youtube_transcript
""")
manager = SyncInvocationManager(command="npx", args=["-y", "@kimtaeyoon83/mcp-server-youtube-transcript"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
def fetch():
print("""
This is fetch
""")
manager = SyncInvocationManager(command="uvx", args=["mcp-server-fetch"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
def websearch():
print("""
This is websearch
""")
manager = SyncInvocationManager(command="npx", args=["-y", "@mzxrai/mcp-webresearch"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
print(tools)
return tools
custom_mcp_severs_ = []
previous_mcp_servers = []
loaded_mcp_servers = []
def custom_mcp_servers():
print("Custom MCP Servers")
global custom_mcp_severs_
global previous_mcp_servers
global loaded_mcp_servers
if custom_mcp_severs_ == previous_mcp_servers:
print("Returning loaded mcp servers")
return loaded_mcp_servers
else:
# The custom_mcp_servers_ list is like [{name: "file_system_tool", command:"npx", args:["-y", "@mzxrai/mcp-webresearch"]}, {name: "memory_tool", command:"npx", args:["-y", "@mzxrai/mcp-webresearch"]}]
# We shouldnt load same mcp server twice. For that we need to intersect the custom_mcp_servers_ and previous_mcp_servers
# and load only the difference
# This is to avoid loading the same mcp server twice
# Get the names of the mcp servers that are already loaded
previous_mcp_server_names = [mcp_server["name"] for mcp_server in loaded_mcp_servers]
# Get the names of the mcp servers that are in the custom_mcp_servers_ list
custom_mcp_server_names = [mcp_server["name"] for mcp_server in custom_mcp_severs_]
# Get the names of the mcp servers that are not loaded
mcp_server_names_to_load = list(set(custom_mcp_server_names) - set(previous_mcp_server_names))
# Load the mcp servers that are not loaded
for mcp_server in custom_mcp_severs_:
if mcp_server["name"] in mcp_server_names_to_load:
manager = SyncInvocationManager(command=mcp_server["command"], args=mcp_server["args"])
manager.start()
tool_manager = MCPToolManager(manager)
tools = tool_manager.load_tools()
loaded_mcp_servers = loaded_mcp_servers + tools
previous_mcp_servers = custom_mcp_severs_
print("Returning loaded mcp servers", loaded_mcp_servers)
return loaded_mcp_servers
def add_custom_mcp_server(name: str, command: str, args: List[str]):
global custom_mcp_severs_
print("****************\nAdding custom mcp server")
print(name, command, args)
custom_mcp_severs_.append({"name": name, "command": command, "args": args})
def remove_custom_mcp_server(name: str):
global custom_mcp_severs_
custom_mcp_severs_ = [mcp_server for mcp_server in custom_mcp_severs_ if mcp_server["name"] != name]
def get_custom_mcp_server(name: str):
global custom_mcp_severs_
for mcp_server in custom_mcp_severs_:
if mcp_server["name"] == name:
return mcp_server
return None
the_tools_ = None
def mcp_tools():
global the_tools_
if the_tools_ is None:
#the_tools_ = file_system_tool()
the_tools_ = []
return the_tools_ + custom_mcp_servers()