对接小智MCP协议控制

This commit is contained in:
myifeng
2025-05-29 13:42:31 +08:00
parent 8ea48f9875
commit 4b212753fb
4 changed files with 262 additions and 98 deletions
+15
View File
@@ -10,6 +10,7 @@ import threading
import traceback
import subprocess
import websockets
from core.handle.mcpHandle import call_mcp_tool
from core.utils.util import (
extract_json_from_string,
initialize_modules,
@@ -560,6 +561,12 @@ class ConnectionHandler:
functions = None
if self.intent_type == "function_call" and hasattr(self, "func_handler"):
functions = self.func_handler.get_functions()
if self.mcp_client is not None:
mcp_tools = self.mcp_client.get_available_tools()
if mcp_tools is not None and len(mcp_tools) > 0:
if functions is None:
functions = []
functions.extend(mcp_tools)
response_message = []
try:
@@ -681,6 +688,14 @@ class ConnectionHandler:
# 处理MCP工具调用
if self.mcp_manager.is_mcp_tool(function_name):
result = self._handle_mcp_tool_call(function_call_data)
elif self.mcp_client is not None and self.mcp_client.has_tool(function_name):
# 如果是MCP工具调用
self.logger.bind(tag=TAG).debug(
f"调用MCP工具: {function_name}, 参数: {function_arguments}"
)
result = asyncio.run_coroutine_threadsafe(call_mcp_tool(self, self.mcp_client, function_name, function_arguments), self.loop).result()
self.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}")
result = ActionResponse(action=Action.REQLLM, result=result, response="")
else:
# 处理系统函数
result = self.func_handler.handle_llm_function_call(