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