Skip to content

The agent that grows with you

feat(cli): filter MCP server spawning by -t/--toolsets flag + fix orphan subprocess leak

Michel Alexandere73257ccc100
diff --git a/tools/mcp_tool.py b/tools/mcp_tool.py
index ee33d3a1cbf..52e7926d2d5 100644
--- a/tools/mcp_tool.py
+++ b/tools/mcp_tool.py
@@ -8201,7 +8201,7 @@ def register_mcp_servers(servers: Dict[str, dict]) -> List[str]:
return _existing_tool_names()
-def discover_mcp_tools() -> List[str]:
+def discover_mcp_tools(allowed_mcp_names: Optional[List[str]] = None) -> List[str]:
"""Entry point: load config, connect to MCP servers, register tools.
Called from ``model_tools`` after ``discover_builtin_tools()``. Safe to call even when
@@ -8210,6 +8210,19 @@ def discover_mcp_tools() -> List[str]:
Idempotent for already-connected servers. If some servers failed on a
previous call, only the missing ones are retried.
+ Args:
+ allowed_mcp_names: If provided, only spawn MCP servers whose names
+ appear in this list. Built-in toolset names (e.g. "web", "memory")
+ in the list are ignored — only matching MCP-server names trigger
+ spawning. Pass ``None`` (default) to spawn all configured servers
+ for backwards compatibility.
+
+ This is used by ``hermes -z -t <toolsets>`` to skip cold-starting
+ MCP subprocesses that the caller doesn't need — saving 10-60s of
+ startup wait per non-needed server. The full set of MCP names is
+ still discoverable via the ``-t`` validation path; this filter
+ only affects which servers are actually started.
+
Returns:
List of all registered MCP tool names.
"""
@@ -8224,6 +8237,25 @@ def discover_mcp_tools() -> List[str]:
logger.debug("MCP SDK not available -- skipping MCP tool discovery")
return []
+ if allowed_mcp_names is not None:
+ # Filter by MCP-server-name match. Built-in toolset names that aren't
+ # MCP servers will simply not match — that's fine; they don't need
+ # MCP spawning anyway.
+ allowed_set = {str(n) for n in allowed_mcp_names}
+ filtered = {name: cfg for name, cfg in servers.items() if name in allowed_set}
+ skipped_count = len(servers) - len(filtered)
+ if skipped_count:
+ logger.debug(
+ "MCP discovery filter: spawning %d/%d configured server(s) per --toolsets filter "
+ "(skipped: %s)",
+ len(filtered), len(servers),
+ ",".join(sorted(set(servers) - set(filtered))),
+ )
+ servers = filtered
+ if not servers:
+ logger.debug("No MCP servers in --toolsets filter; skipping MCP load entirely")
+ return []
+
# Cross-process discovery guard (#62771). A lock loser waits for
# the holder, then performs its own process-local discovery. If locking is
# unavailable or the bounded wait expires, preserve the previous