Skip to content

Commit 3c85242

Browse files
authored
Merge pull request #1421 from xinnan-tech/test-mcp
Test mcp
2 parents 49eab95 + 3c288e1 commit 3c85242

File tree

7 files changed

+423
-3
lines changed

7 files changed

+423
-3
lines changed

main/xiaozhi-server/core/connection.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import traceback
1111
import subprocess
1212
import websockets
13+
from core.handle.mcpHandle import call_mcp_tool
1314
from core.utils.util import (
1415
extract_json_from_string,
1516
check_vad_update,
@@ -146,6 +147,8 @@ def __init__(
146147
) # 在原来第一道关闭的基础上加60秒,进行二道关闭
147148

148149
self.audio_format = "opus"
150+
# {"mcp":true} 表示启用MCP功能
151+
self.features = None
149152

150153
async def handle_connection(self, ws):
151154
try:
@@ -579,6 +582,12 @@ def chat(self, query, tool_call=False):
579582
functions = None
580583
if self.intent_type == "function_call" and hasattr(self, "func_handler"):
581584
functions = self.func_handler.get_functions()
585+
if hasattr(self, "mcp_client"):
586+
mcp_tools = self.mcp_client.get_available_tools()
587+
if mcp_tools is not None and len(mcp_tools) > 0:
588+
if functions is None:
589+
functions = []
590+
functions.extend(mcp_tools)
582591
response_message = []
583592

584593
try:
@@ -696,9 +705,23 @@ def chat(self, query, tool_call=False):
696705
"arguments": function_arguments,
697706
}
698707

699-
# 处理MCP工具调用
708+
# 处理Server端MCP工具调用
700709
if self.mcp_manager.is_mcp_tool(function_name):
701710
result = self._handle_mcp_tool_call(function_call_data)
711+
elif hasattr(self, "mcp_client") and self.mcp_client.has_tool(function_name):
712+
# 如果是小智端MCP工具调用
713+
self.logger.bind(tag=TAG).debug(
714+
f"调用小智端MCP工具: {function_name}, 参数: {function_arguments}"
715+
)
716+
try:
717+
result = asyncio.run_coroutine_threadsafe(call_mcp_tool(self, self.mcp_client, function_name, function_arguments), self.loop).result()
718+
self.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}")
719+
result = ActionResponse(action=Action.REQLLM, result=result, response="")
720+
except Exception as e:
721+
self.logger.bind(tag=TAG).error(f"MCP工具调用失败: {e}")
722+
result = ActionResponse(
723+
action=Action.REQLLM, result="MCP工具调用失败", response=""
724+
)
702725
else:
703726
# 处理系统函数
704727
result = self.func_handler.handle_llm_function_call(

main/xiaozhi-server/core/handle/functionHandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def register_nessary_functions(self):
6161
self.function_registry.register_function("plugin_loader")
6262
self.function_registry.register_function("get_time")
6363
self.function_registry.register_function("get_lunar")
64-
self.function_registry.register_function("handle_speaker_volume_or_screen_brightness")
64+
# self.function_registry.register_function("handle_speaker_volume_or_screen_brightness")
6565

6666
def register_config_functions(self):
6767
"""注册配置中的函数,可以不同客户端使用不同的配置"""

main/xiaozhi-server/core/handle/helloHandle.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import random
55
import shutil
66
import asyncio
7+
from core.handle.mcpHandle import MCPClient, send_mcp_initialize_message, send_mcp_tools_list_request
78
from core.handle.sendAudioHandle import send_stt_message
89
from core.utils.util import remove_punctuation_and_length
910
from core.providers.tts.dto.dto import ContentType, InterfaceType
@@ -31,6 +32,17 @@ async def handleHelloMessage(conn, msg_json):
3132
if conn.asr is not None:
3233
conn.asr.set_audio_format(format)
3334
conn.welcome_msg["audio_params"] = audio_params
35+
features = msg_json.get("features")
36+
if features:
37+
conn.logger.bind(tag=TAG).info(f"客户端特性: {features}")
38+
conn.features = features
39+
if features.get("mcp"):
40+
conn.logger.bind(tag=TAG).info("客户端支持MCP")
41+
conn.mcp_client = MCPClient()
42+
# 发送初始化
43+
asyncio.create_task(send_mcp_initialize_message(conn))
44+
# 发送mcp消息,获取tools列表
45+
asyncio.create_task(send_mcp_tools_list_request(conn))
3446

3547
await conn.websocket.send(json.dumps(conn.welcome_msg))
3648

0 commit comments

Comments
 (0)