From 5c730468dfc75e3a00104ea0eaaf551d14e151e9 Mon Sep 17 00:00:00 2001 From: 3030332422 <3030332422@qq.com> Date: Fri, 24 Oct 2025 14:43:49 +0800 Subject: [PATCH] =?UTF-8?q?update=EF=BC=9Amcp=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E5=85=A5mac=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../providers/tools/server_mcp/mcp_client.py | 21 ++++++++++++++++++- .../providers/tools/server_mcp/mcp_manager.py | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_client.py b/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_client.py index d2cd1ed19..4708b769a 100644 --- a/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_client.py +++ b/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_client.py @@ -4,6 +4,7 @@ from __future__ import annotations from datetime import timedelta import asyncio +import json import os import shutil import concurrent.futures @@ -23,14 +24,16 @@ TAG = __name__ class ServerMCPClient: """服务端MCP客户端,用于连接和管理MCP服务""" - def __init__(self, config: Dict[str, Any]): + def __init__(self, config: Dict[str, Any], conn=None): """初始化服务端MCP客户端 Args: config: MCP服务配置字典 + conn: 连接对象,用于获取设备信息 """ self.logger = setup_logging() self.config = config + self.conn = conn self._worker_task: Optional[asyncio.Task] = None self._ready_evt = asyncio.Event() @@ -113,7 +116,23 @@ class ServerMCPClient: if not self.session: raise RuntimeError("服务端MCP客户端未初始化") + # 加入MAC地址 + if self.conn and hasattr(self.conn, 'device_id') and self.conn.device_id: + tool_data = self.tools_dict.get(name) + if tool_data and hasattr(tool_data, 'inputSchema') and isinstance(tool_data.inputSchema, dict): + properties = tool_data.inputSchema.get('properties', {}) + if 'mac_address' in properties: + args['mac_address'] = self.conn.device_id + self.logger.bind(tag=TAG).info( + f"已将设备MAC地址 {self.conn.device_id} 加入到服务端MCP工具调用参数中" + ) + real_name = self.name_mapping.get(name, name) + + self.logger.bind(tag=TAG).info( + f"调用服务端MCP工具: {real_name},参数: {json.dumps(args, ensure_ascii=False)}" + ) + loop = self._worker_task.get_loop() coro = self.session.call_tool(real_name, args) diff --git a/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_manager.py b/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_manager.py index cf3deb211..44e60f17e 100644 --- a/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_manager.py +++ b/main/xiaozhi-server/core/providers/tools/server_mcp/mcp_manager.py @@ -55,7 +55,7 @@ class ServerMCPManager: try: # 初始化服务端MCP客户端 logger.bind(tag=TAG).info(f"初始化服务端MCP客户端: {name}") - client = ServerMCPClient(srv_config) + client = ServerMCPClient(srv_config, self.conn) await client.initialize() self.clients[name] = client client_tools = client.get_available_tools() @@ -130,7 +130,7 @@ class ServerMCPManager: # 重新初始化客户端 config = self.load_config() if client_name in config: - client = ServerMCPClient(config[client_name]) + client = ServerMCPClient(config[client_name], self.conn) await client.initialize() self.clients[client_name] = client target_client = client