fix(mcp): auto-detect project via roots/list when no rootUri (#196) (#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 #196

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-05-20 11:35:13 -05:00
committed by GitHub
co-authored by Claude Opus 4.7
parent cf7db7cb98
commit 1cd162a66d
5 changed files with 388 additions and 20 deletions
+17
View File
@@ -57,6 +57,23 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
Thanks to [@essopsp](https://github.com/essopsp) for the repro.
### Fixed
- **MCP**: tools no longer fail with "CodeGraph not initialized" when the index
actually exists. This hit clients that launch the MCP server from a directory
other than your project and don't report a workspace root in `initialize`
(some IDE/JetBrains-family integrations) — the server fell back to its own
working directory, missed the project's `.codegraph/`, and returned the
misleading "Run 'codegraph init' first" on every call. The only workaround
was passing `projectPath` to each tool by hand. Now, when no project path is
supplied, the server asks the client for its workspace root via the standard
MCP `roots/list` request (when the client advertises the `roots` capability)
before falling back to the working directory — so detection just works for
spec-compliant clients. When it still can't resolve a project, the error is
now actionable: it names the directory it searched and tells you to pass
`projectPath` or add `--path /abs/project` to the server's MCP config args,
instead of pointing you at a re-init you don't need. Closes
[#196](https://github.com/colbymchenry/codegraph/issues/196). Thanks to
[@zhangyu1197](https://github.com/zhangyu1197) for the report and the
`projectPath` workaround.
- **MCP**: the server no longer hangs on startup under WSL2 when the project
lives on an NTFS `/mnt/*` mount. Setting up the recursive file watcher
there took tens of seconds — every directory read crosses the Windows/9p