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 <noreply@anthropic.com>
This commit is contained in:
ooaaooaa123
2026-05-06 20:40:56 +08:00
parent b2429ec30c
commit caaf006a49
2 changed files with 11 additions and 2 deletions

View File

@@ -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),

View File

@@ -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)