Merge pull request #1396 from myifeng/dev

监听MCP类型消息,等待后续适配
This commit is contained in:
CGD
2025-05-30 09:11:57 +08:00
committed by GitHub
5 changed files with 318 additions and 5 deletions
+17
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,
check_vad_update,
@@ -145,6 +146,8 @@ class ConnectionHandler:
) # 在原来第一道关闭的基础上加60秒,进行二道关闭
self.audio_format = "opus"
# {"mcp":true} 表示启用MCP功能
self.features = None
async def handle_connection(self, ws):
try:
@@ -578,6 +581,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:
@@ -698,6 +707,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(