Skip to content

fix(mcp): auto-detect project via roots/list when no rootUri (#196)#214

Merged
colbymchenry merged 1 commit into
mainfrom
fix/mcp-roots-project-detection
May 20, 2026
Merged

fix(mcp): auto-detect project via roots/list when no rootUri (#196)#214
colbymchenry merged 1 commit into
mainfrom
fix/mcp-roots-project-detection

Conversation

@colbymchenry
Copy link
Copy Markdown
Owner

Problem

MCP tools failed with CodeGraph not initialized even when the index existed, reported in #196 by @zhangyu1197.

When a client launches the MCP server from a directory other than the project and doesn't pass a rootUri/workspaceFolders in initialize (some IDE/JetBrains-family integrations), the server fell back to its own process.cwd(), missed the project's .codegraph/, and returned a misleading "Run 'codegraph init' first" on every call. The only workaround was passing projectPath to each tool by hand.

Root cause: rootUri/workspaceFolders are LSP-style fields (non-standard for MCP) — the spec's actual mechanism for a client to expose its workspace root, roots/list, was never implemented.

Fix

  • transport.ts — the stdio transport can now send server→client requests and match their responses by id. Previously a roots/list reply would have been dropped as "Invalid Request" (responses carry no method). Includes a timeout so a non-answering client degrades gracefully, and pending requests are rejected on shutdown.
  • index.ts — when initialize carries no explicit path, the server no longer commits to cwd. On the first tool call it asks the client via roots/list (gated on the roots capability), uses the first root, and only falls back to cwd if that yields nothing. One-shot guarded so it never re-issues roots/list per call. The fast-initialize contract from MCP server fails to connect with Claude Code: Content-Length framing vs newline-delimited JSON transport mismatch #172 is preserved — resolution stays lazy and off the handshake path.
  • tools.ts — the "not initialized" error is now actionable: it names the directory it searched and tells you to pass projectPath or add --path /abs/project, instead of pointing at a re-init you don't need.

Tests

New __tests__/mcp-roots.test.ts (3 tests, real spawned server over stdio):

  • resolves the project from roots/list when no rootUri is sent
  • returns the actionable error when there's no signal and no roots capability
  • still honors an explicit rootUri without asking for roots

Full suite green (639 passed), including the #172 handshake-timing regression test.

Credit to @zhangyu1197 for discovering and diagnosing the issue, and for the projectPath workaround.

Closes #196

🤖 Generated with Claude Code

MCP tools failed with "CodeGraph not initialized" when a client launched
the server outside the project and sent no rootUri/workspaceFolders — the
server fell back to its own cwd, missed the project's .codegraph/, and
returned a misleading "run codegraph init" error on every call. The only
workaround was passing projectPath by hand to each tool.

When no explicit path is given, the server now asks the client for its
workspace root via the standard MCP roots/list request (gated on the
client advertising the roots capability) before falling back to cwd. This
required teaching the stdio transport to send server->client requests and
match their responses by id (previously responses were dropped as invalid).

When a project still can't be resolved, the error now names the directory
it searched and tells the user to pass projectPath or add --path to the
MCP config, instead of pointing at a re-init they don't need.

Reported-by: @zhangyu1197
Closes #196

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@colbymchenry colbymchenry merged commit 1cd162a into main May 20, 2026
@colbymchenry colbymchenry deleted the fix/mcp-roots-project-detection branch May 20, 2026 16:35
jorgerobles pushed a commit to jorgerobles/codegraph that referenced this pull request Jun 1, 2026
…henry#196) (colbymchenry#214)

MCP tools failed with "CodeGraph not initialized" when a client launched
the server outside the project and sent no rootUri/workspaceFolders — the
server fell back to its own cwd, missed the project's .codegraph/, and
returned a misleading "run codegraph init" error on every call. The only
workaround was passing projectPath by hand to each tool.

When no explicit path is given, the server now asks the client for its
workspace root via the standard MCP roots/list request (gated on the
client advertising the roots capability) before falling back to cwd. This
required teaching the stdio transport to send server->client requests and
match their responses by id (previously responses were dropped as invalid).

When a project still can't be resolved, the error now names the directory
it searched and tells the user to pass projectPath or add --path to the
MCP config, instead of pointing at a re-init they don't need.

Reported-by: @zhangyu1197
Closes colbymchenry#196

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeGraph MCP tools fail with "CodeGraph not initialized" when projectPath is omitted, even though index exists

1 participant