fix(mcp): don't block initialize handshake on heavy init (#172)#177
Merged
Conversation
The MCP `initialize` handler was awaiting `tryInitializeDefault` — which opens the SQLite DB and runs `await initGrammars()` (tree-sitter WASM bootstrap) — before sending the JSON-RPC response. On slow filesystems (Docker Desktop VirtioFS on macOS, WSL2) this could exceed Claude Code's ~30s handshake timeout, leaving the codegraph child process alive and unresponsive with no tools visible in the client. Send the response first; defer the open to a tracked background promise. The lazy retry path used by `tools/list` and `tools/call` now awaits that promise instead of racing it with `openSync`, so we never double-open the SQLite file. Adds a subprocess-based regression test that asserts the JSON-RPC response arrives on stdout before `startWatching()` logs to stderr. This ordering check catches the regression on any filesystem, not just slow ones where the timing matters in practice. Reported by @sashanclrp; isolated by @sgrimm's wire capture. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Fixes #172 — codegraph MCP tools silently failing to appear in Claude Code on slow filesystems.
Root cause
handleInitializeinsrc/mcp/index.tswas doingawait this.tryInitializeDefault(projectPath)before callingsendResult. That await chain runsCodeGraph.open()→await initGrammars()→Parser.init()(tree-sitter WASM runtime bootstrap). On Docker Desktop VirtioFS (macOS) and WSL2, that bootstrap can take longer than Claude Code's ~30 s handshake timeout. Symptom: codegraph process is alive (psshows it), the initialize message arrived on its stdin, but no JSON-RPC response was sent before the client timed out — so tools never appeared and there was no UI error.Fix
tryInitializeDefaultas a tracked background promise (this.initPromise).retryInitIfNeededbecomes async and awaits the in-flight promise so we never open the SQLite file twice concurrently.await this.retryInitIfNeeded().Why the diagnosis is solid
Two independent reports converged on the same wire evidence:
Test plan
npm test— all 577 tests pass, including the new__tests__/mcp-initialize.test.ts.dist/bin/codegraph.js serve --mcpsubprocess, asserts the JSON-RPC response on stdout arrives beforestartWatching's "File watcher active" log on stderr. Verified the test fails when the fix is reverted (expected 1 to be less than 0).@modelcontextprotocol/sdkclient (the SDK Claude Code embeds): handshake in 48 ms, 9 tools listed,codegraph_searchcallTool succeeds.Credit
🤖 Generated with Claude Code