From caaf006a4902af38e32ac313642a1eff11a7ac1c Mon Sep 17 00:00:00 2001 From: ooaaooaa123 Date: Wed, 6 May 2026 20:40:56 +0800 Subject: [PATCH] fix(mcp): wire MCP tools into agent and fix env var inheritance Two bugs found during end-to-end validation with Amap and Chrome DevTools MCP servers: 1. MCP tools were loaded into ToolManager._mcp_tool_instances but never added to the agent's tool list. AgentInitializer._load_tools() only iterated tool_classes (built-in tools). Added a second pass to append all MCP tool instances. 2. When a MCP server config contains an "env" dict, it was passed directly to subprocess.Popen, replacing the entire process environment. This caused npx to fail because PATH and other inherited vars were missing. Fixed by merging config env on top of os.environ. Validated with: - @amap/amap-maps-mcp-server (12 tools, stdio + API key env var) - chrome-devtools-mcp (29 tools, stdio + remote debugging port) Co-Authored-By: Claude Sonnet 4.6 --- agent/tools/mcp/mcp_client.py | 4 +++- bridge/agent_initializer.py | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/agent/tools/mcp/mcp_client.py b/agent/tools/mcp/mcp_client.py index 3148a74b..281d3616 100644 --- a/agent/tools/mcp/mcp_client.py +++ b/agent/tools/mcp/mcp_client.py @@ -6,6 +6,7 @@ MCP SDK dependency. """ import json +import os import subprocess import threading import urllib.request @@ -119,7 +120,8 @@ class McpClient: return False args = self.config.get("args", []) - env = self.config.get("env", None) + extra_env = self.config.get("env", None) + env = {**os.environ, **extra_env} if extra_env else None self._proc = subprocess.Popen( [command] + list(args), diff --git a/bridge/agent_initializer.py b/bridge/agent_initializer.py index 8e62ef7a..d580a495 100644 --- a/bridge/agent_initializer.py +++ b/bridge/agent_initializer.py @@ -382,7 +382,14 @@ class AgentInitializer: tools.append(tool) except Exception as e: logger.warning(f"[AgentInitializer] Failed to load tool {tool_name}: {e}") - + + # Add MCP tools + for mcp_tool in tool_manager._mcp_tool_instances.values(): + tools.append(mcp_tool) + if tool_manager._mcp_tool_instances and session_id is None: + logger.info(f"[AgentInitializer] Added {len(tool_manager._mcp_tool_instances)} MCP tool(s): " + f"{list(tool_manager._mcp_tool_instances.keys())}") + # Add memory tools if memory_tools: tools.extend(memory_tools)