From 85f5404b3bafcfe84a21a4135f8f5fabc368a881 Mon Sep 17 00:00:00 2001 From: 3030332422 <3030332422@qq.com> Date: Mon, 22 Dec 2025 15:56:40 +0800 Subject: [PATCH] =?UTF-8?q?update=EF=BC=9A=E6=B7=BB=E5=8A=A0=E9=94=81?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E9=81=BF=E5=85=8DMCP=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E7=9A=84=E7=AB=9E=E6=80=81?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/providers/tools/server_mcp/mcp_manager.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 1d0d9cb6..3bca9e48 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 @@ -33,6 +33,7 @@ class ServerMCPManager: ) self.clients: Dict[str, ServerMCPClient] = {} self.tools = [] + self._init_lock = asyncio.Lock() def load_config(self) -> Dict[str, Any]: """加载MCP服务配置""" @@ -58,9 +59,12 @@ class ServerMCPManager: client = ServerMCPClient(srv_config) # 设置超时时间5秒 await asyncio.wait_for(client.initialize(logging_callback=self.logging_callback), timeout=5) - self.clients[name] = client - client_tools = client.get_available_tools() - self.tools.extend(client_tools) + + # 使用锁保护共享状态的修改 + async with self._init_lock: + self.clients[name] = client + client_tools = client.get_available_tools() + self.tools.extend(client_tools) except asyncio.TimeoutError: logger.bind(tag=TAG).error( @@ -89,7 +93,7 @@ class ServerMCPManager: tasks.append(self._init_server(name, srv_config)) if tasks: - await asyncio.gather(*tasks, return_exceptions=True) + await asyncio.gather(*tasks) # 输出当前支持的服务端MCP工具列表 if hasattr(self.conn, "func_handler") and self.conn.func_handler: