|
@@ -11,6 +11,8 @@ class MCPTools:
|
|
def __init__(self, config_file="mcp_config.json"):
|
|
def __init__(self, config_file="mcp_config.json"):
|
|
# 从配置文件加载MCP服务配置
|
|
# 从配置文件加载MCP服务配置
|
|
self.mcp_servers = self._load_config(config_file)
|
|
self.mcp_servers = self._load_config(config_file)
|
|
|
|
+ # 缓存每个服务的工具列表
|
|
|
|
+ self._tools_cache = {}
|
|
|
|
|
|
def _load_config(self, config_file):
|
|
def _load_config(self, config_file):
|
|
"""
|
|
"""
|
|
@@ -134,7 +136,7 @@ class MCPTools:
|
|
"properties": {
|
|
"properties": {
|
|
"method": {
|
|
"method": {
|
|
"type": "string",
|
|
"type": "string",
|
|
- "description": "要调用的MCP方法,如tools/list, resources/list等"
|
|
|
|
|
|
+ "description": "要调用的MCP方法,如tools/list, tools/call等"
|
|
},
|
|
},
|
|
"params": {
|
|
"params": {
|
|
"type": "object",
|
|
"type": "object",
|
|
@@ -148,6 +150,63 @@ class MCPTools:
|
|
|
|
|
|
return tools
|
|
return tools
|
|
|
|
|
|
|
|
+ async def get_actual_mcp_tools(self, server_name):
|
|
|
|
+ """
|
|
|
|
+ 获取指定MCP服务的实际工具列表
|
|
|
|
+
|
|
|
|
+ Args:
|
|
|
|
+ server_name (str): 服务器名称
|
|
|
|
+
|
|
|
|
+ Returns:
|
|
|
|
+ list: 实际的工具列表
|
|
|
|
+ """
|
|
|
|
+ if server_name not in self.mcp_servers:
|
|
|
|
+ return []
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ result = await self.call_mcp_server(server_name, "tools/list")
|
|
|
|
+ if isinstance(result, dict) and "tools" in result:
|
|
|
|
+ return result["tools"]
|
|
|
|
+ return []
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"获取 {server_name} 工具列表时出错: {e}")
|
|
|
|
+ return []
|
|
|
|
+
|
|
|
|
+ def get_all_mcp_tools_sync(self):
|
|
|
|
+ """
|
|
|
|
+ 同步获取所有MCP服务的实际工具列表
|
|
|
|
+
|
|
|
|
+ Returns:
|
|
|
|
+ list: 所有MCP服务的工具列表
|
|
|
|
+ """
|
|
|
|
+ all_tools = []
|
|
|
|
+ for server_name in self.mcp_servers.keys():
|
|
|
|
+ try:
|
|
|
|
+ tools = asyncio.run(self.get_actual_mcp_tools(server_name))
|
|
|
|
+ # 缓存工具列表及对应的服务器名称
|
|
|
|
+ self._tools_cache[server_name] = tools
|
|
|
|
+ all_tools.extend(tools)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"获取 {server_name} 工具列表时出错: {e}")
|
|
|
|
+ return all_tools
|
|
|
|
+
|
|
|
|
+ def get_server_for_tool(self, tool_name):
|
|
|
|
+ """
|
|
|
|
+ 根据工具名称获取对应的服务器名称
|
|
|
|
+
|
|
|
|
+ Args:
|
|
|
|
+ tool_name (str): 工具名称
|
|
|
|
+
|
|
|
|
+ Returns:
|
|
|
|
+ str or None: 服务器名称或None
|
|
|
|
+ """
|
|
|
|
+ # 遍历缓存的工具列表查找对应的服务器
|
|
|
|
+ for server_name, tools in self._tools_cache.items():
|
|
|
|
+ for tool in tools:
|
|
|
|
+ if tool.get("name") == tool_name:
|
|
|
|
+ return server_name
|
|
|
|
+ return None
|
|
|
|
+
|
|
def call_mcp_tool(self, tool_name, parameters):
|
|
def call_mcp_tool(self, tool_name, parameters):
|
|
"""
|
|
"""
|
|
执行MCP工具调用
|
|
执行MCP工具调用
|
|
@@ -197,6 +256,10 @@ class MCPTools:
|
|
|
|
|
|
try:
|
|
try:
|
|
result = await self.call_mcp_server(server_name, "tools/list")
|
|
result = await self.call_mcp_server(server_name, "tools/list")
|
|
|
|
+ # 缓存工具列表
|
|
|
|
+ if isinstance(result, dict) and "tools" in result:
|
|
|
|
+ self._tools_cache[server_name] = result["tools"]
|
|
|
|
+
|
|
return {
|
|
return {
|
|
"status": "success",
|
|
"status": "success",
|
|
"server": server_name,
|
|
"server": server_name,
|