From 68dbc91d54a734efab90c6f668b5f19fbd4d2867 Mon Sep 17 00:00:00 2001 From: CGD <3030332422@qq.com> Date: Wed, 11 Jun 2025 22:02:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:MCP=20=E5=B7=A5=E5=85=B7=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=83=BD=E5=8A=9B=EF=BC=8C=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=84=8F=E5=9B=BE=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 4 +- .../core/handle/intentHandler.py | 40 ++++++++++++++++--- .../providers/intent/intent_llm/intent_llm.py | 7 ++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 9102b76d..c4aae725 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -612,7 +612,7 @@ class ConnectionHandler: uuid_str = str(uuid.uuid4()).replace("-", "") self.sentence_id = uuid_str - if functions is not None: + if self.intent_type == "function_call" and functions is not None: # 使用支持functions的streaming接口 llm_responses = self.llm.response_with_functions( self.session_id, @@ -639,7 +639,7 @@ class ConnectionHandler: for response in llm_responses: if self.client_abort: break - if functions is not None: + if self.intent_type == "function_call" and functions is not None: content, tools_call = response if "content" in response: content = response["content"] diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index 35660bdf..027a03a6 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -1,11 +1,13 @@ import json +import asyncio import uuid from core.handle.sendAudioHandle import send_stt_message from core.handle.helloHandle import checkWakeupWords from core.utils.util import remove_punctuation_and_length from core.providers.tts.dto.dto import ContentType from core.utils.dialogue import Message -from plugins_func.register import Action +from core.handle.mcpHandle import call_mcp_tool +from plugins_func.register import Action, ActionResponse from loguru import logger TAG = __name__ @@ -101,10 +103,38 @@ async def process_intent_result(conn, intent_result, original_text): # 使用executor执行函数调用和结果处理 def process_function_call(): conn.dialogue.put(Message(role="user", content=original_text)) - result = conn.func_handler.handle_llm_function_call( - conn, function_call_data - ) - logger.bind(tag=TAG).debug(f"检测到Action : {result.action}") + + # 处理Server端MCP工具调用 + if conn.mcp_manager.is_mcp_tool(function_name): + result = conn._handle_mcp_tool_call(function_call_data) + elif hasattr(conn, "mcp_client") and conn.mcp_client.has_tool( + function_name + ): + # 如果是小智端MCP工具调用 + conn.logger.bind(tag=TAG).debug( + f"调用小智端MCP工具: {function_name}, 参数: {function_args}" + ) + try: + result = asyncio.run_coroutine_threadsafe( + call_mcp_tool( + conn, conn.mcp_client, function_name, function_args + ), + conn.loop, + ).result() + conn.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}") + result = ActionResponse( + action=Action.REQLLM, result=result, response="" + ) + except Exception as e: + conn.logger.bind(tag=TAG).error(f"MCP工具调用失败: {e}") + result = ActionResponse( + action=Action.REQLLM, result="MCP工具调用失败", response="" + ) + else: + # 处理系统函数 + result = conn.func_handler.handle_llm_function_call( + conn, function_call_data + ) if result: if result.action == Action.RESPONSE: # 直接回复前端 diff --git a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py index 29605393..3eafced8 100644 --- a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py +++ b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py @@ -151,6 +151,13 @@ class IntentProvider(IntentProviderBase): if self.promot == "": functions = conn.func_handler.get_functions() + if hasattr(conn, "mcp_client"): + mcp_tools = conn.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) + self.promot = self.get_intent_system_prompt(functions) music_config = initialize_music_handler(conn) From 6139c51b6082148850f7e3380df62914c7f3bec7 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 12 Jun 2025 09:54:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=20logger=E4=B8=ADvad=E5=92=8Casr?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config/logger.py | 1 + main/xiaozhi-server/core/connection.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py index a99013aa..7cff241c 100644 --- a/main/xiaozhi-server/config/logger.py +++ b/main/xiaozhi-server/config/logger.py @@ -114,6 +114,7 @@ def update_module_string(selected_module_str): current_module = logger._core.extra["selected_module"] if current_module == selected_module_str: + logger.debug(f"组件未更改无需更新") return try: diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index c4aae725..cf9a157d 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -424,6 +424,14 @@ class ConnectionHandler: init_vad = check_vad_update(self.common_config, private_config) init_asr = check_asr_update(self.common_config, private_config) + if init_vad: + self.config["selected_module"]["VAD"] = private_config["selected_module"][ + "VAD" + ] + if init_asr: + self.config["selected_module"]["ASR"] = private_config["selected_module"][ + "ASR" + ] if private_config.get("TTS", None) is not None: init_tts = True self.config["TTS"] = private_config["TTS"]