|
@@ -26,6 +26,7 @@ class Tools:
|
|
|
self.mcp_tools = MCPTools()
|
|
|
|
|
|
def get_tool_list(self):
|
|
|
+ # 基础工具
|
|
|
tools = [
|
|
|
{
|
|
|
"type": "function",
|
|
@@ -37,8 +38,23 @@ class Tools:
|
|
|
}
|
|
|
]
|
|
|
|
|
|
- # 添加MCP工具
|
|
|
- tools.extend(self.mcp_tools.get_mcp_tool_list())
|
|
|
+ # 获取所有MCP服务的实际工具
|
|
|
+ mcp_tools = self.mcp_tools.get_all_mcp_tools_sync()
|
|
|
+
|
|
|
+ # 为每个MCP工具创建独立的工具定义
|
|
|
+ for tool in mcp_tools:
|
|
|
+ tools.append({
|
|
|
+ "type": "function",
|
|
|
+ "function": {
|
|
|
+ "name": tool.get("name", "unknown_tool"),
|
|
|
+ "description": tool.get("description", "未知工具"),
|
|
|
+ "parameters": tool.get("inputSchema", {
|
|
|
+ "type": "object",
|
|
|
+ "properties": {},
|
|
|
+ "required": []
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
return tools
|
|
|
|
|
@@ -61,21 +77,36 @@ class Tools:
|
|
|
return f"读取网页错误: {str(e)}"
|
|
|
|
|
|
# 处理MCP工具
|
|
|
- elif tool_name.endswith("_mcp"):
|
|
|
- result = self.mcp_tools.call_mcp_tool(tool_name, parameters)
|
|
|
- # 解析结果并检查是否有错误
|
|
|
- try:
|
|
|
- result_dict = json.loads(result)
|
|
|
- if "error" in result_dict:
|
|
|
- error_msg = result_dict['error']
|
|
|
- # 提供更详细的错误信息和建议
|
|
|
- if "unhandled errors in a TaskGroup" in error_msg:
|
|
|
- server_name = tool_name[5:-4] # 提取服务名
|
|
|
- return f"MCP调用错误: {server_name}服务内部出现未处理的异常。这通常表示服务端存在问题。错误详情: {error_msg}"
|
|
|
- return f"MCP调用错误: {error_msg}"
|
|
|
- except json.JSONDecodeError:
|
|
|
- pass # 如果不是JSON格式,直接返回原始结果
|
|
|
- return result
|
|
|
+ else:
|
|
|
+ # 根据工具名称确定所属的MCP服务
|
|
|
+ server_name = self.mcp_tools.get_server_for_tool(tool_name)
|
|
|
+ if server_name:
|
|
|
+ # 构造正确的MCP调用参数
|
|
|
+ mcp_params = {
|
|
|
+ "method": "tools/call",
|
|
|
+ "params": {
|
|
|
+ "name": tool_name,
|
|
|
+ "arguments": parameters
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mcp_tool_name = f"call_{server_name}_mcp"
|
|
|
+ result = self.mcp_tools.call_mcp_tool(mcp_tool_name, mcp_params)
|
|
|
+
|
|
|
+ # 解析结果并检查是否有错误
|
|
|
+ try:
|
|
|
+ result_dict = json.loads(result)
|
|
|
+ if "error" in result_dict:
|
|
|
+ error_msg = result_dict['error']
|
|
|
+ # 提供更详细的错误信息和建议
|
|
|
+ if "unhandled errors in a TaskGroup" in error_msg:
|
|
|
+ return f"MCP调用错误: {server_name}服务内部出现未处理的异常。这通常表示服务端存在问题。错误详情: {error_msg}"
|
|
|
+ return f"MCP调用错误: {error_msg}"
|
|
|
+ except json.JSONDecodeError:
|
|
|
+ pass # 如果不是JSON格式,直接返回原始结果
|
|
|
+ return result
|
|
|
+ else:
|
|
|
+ return f"未知工具: {tool_name}"
|
|
|
|
|
|
return f"工具 {tool_name} 执行完成"
|
|
|
|