fix(mcp): add --tools CLI flag for CODEGRAPH_MCP_TOOLS fallback (#1192)#1232
Open
umi008 wants to merge 1 commit into
Open
fix(mcp): add --tools CLI flag for CODEGRAPH_MCP_TOOLS fallback (#1192)#1232umi008 wants to merge 1 commit into
umi008 wants to merge 1 commit into
Conversation
…ymchenry#1192) Some MCP clients (OpenCode, Antigravity IDE) don't forward the `env` block from the MCP server config to the spawned process, so users pinning the tool surface via `env.CODEGRAPH_MCP_TOOLS` see only the default `codegraph_explore` regardless of what they set. The new `codegraph serve --mcp --tools=<list>` flag mirrors the env var and is set BEFORE MCPServer construction, so it propagates through every runtime path: the proxy's static ListTools (runLocalHandshakeProxy -> getStaticTools), the detached daemon via the spawn's `env: { ...process.env }`, and the direct-mode ToolHandler. The CLI flag wins over a pre-set env var so the operator's intent in the MCP config is always honored, and an empty value is treated as unset to match the existing whitespace-only env-var behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
codegraph serve --mcp --tools=<list>as a CLI fallback for theCODEGRAPH_MCP_TOOLSenv var. Some MCP clients (OpenCode, AntigravityIDE) don't forward the
envblock from the MCP server config to thespawned process, so users pinning the tool surface via
env.CODEGRAPH_MCP_TOOLSsee only the defaultcodegraph_exploreregardless of what they set.
Root cause
The filtering logic in
getStaticTools()(proxy) andToolHandler.getTools()(direct / daemon) is correct — both readprocess.env.CODEGRAPH_MCP_TOOLSat call time. The bug is that someMCP clients don't inject the
envblock from the MCP config into thespawned process, so
process.env.CODEGRAPH_MCP_TOOLSis empty whenthe server reads it.
Fix
Add a
--toolsflag to theserve --mcpcommand. The flag is parsedby commander and sets
process.env.CODEGRAPH_MCP_TOOLSbeforeMCPServerconstruction, so it propagates through every runtime path:runLocalHandshakeProxy→getStaticTools()reads theenv var at call time.
env: { ...process.env }(line 156 insrc/mcp/index.ts), so the daemon inherits the env var from thelauncher.
ToolHandler.getTools()→toolAllowlist()reads the env var.
The CLI flag wins over a pre-set env var so the operator's explicit
intent in the MCP config is always honored. An empty value is treated as
unset to match the existing whitespace-only env-var behavior in
toolAllowlist().Validation
__tests__/cli-mcp-tools-flag.test.tsexercisesend-to-end against the built binary:
codegraph_explore) when--toolsis omitted--tools=explore,search,node--tools=callers)--toolswins when both flag and env var are set--tools(env var set in thelauncher process before the proxy intercepts
tools/list)mcp-tool-allowlist.test.ts(7 tests) andmcp-initialize.test.ts(3 tests) still pass — no regression.Reproduction
Closes #1192