fix(mcp): read the initialize handshake before the transport consumes the body - #10191
Merged
Conversation
… the body The #10175 handshake telemetry called `c.req.raw.clone()` after createMcpHandler had already read the request body. The Fetch spec forbids cloning a request whose body is disturbed, so it threw `TypeError: unusable`; handleMcpRequest's catch rethrew, and a correct 2xx MCP response was discarded in favour of an unhandled 500 -- on every `initialize`, the first call of every MCP session. The telemetry key gate did not contain it: the handshake read is an argument to recordMcpInitialize, evaluated before that function's own no-op check, so the throw happened whether or not POSTHOG_API_KEY was set. Parse the JSON-RPC envelope once, before the handler runs, and derive both the usage metadata and the clientInfo handshake from it. No clone survives past the handler. Closes #10190
Contributor
|
Important 🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨 ⏳ LoopOver is waiting…LoopOver has seen this pull request and is waiting on CI checks to finish before reviewing it. This comment will update once the review runs. 🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed · 🟨 Waiting |
Contributor
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10191 +/- ##
===========================================
+ Coverage 80.44% 91.34% +10.89%
===========================================
Files 282 934 +652
Lines 58764 114145 +55381
Branches 6965 27585 +20620
===========================================
+ Hits 47274 104267 +56993
+ Misses 11199 8771 -2428
- Partials 291 1107 +816
Flags with carried forward coverage won't be shown. Click here to find out more.
|
3 tasks
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
Every MCP
initializerequest has been returning 500 since #10175 landed. The handshake telemetry it added calledc.req.raw.clone()aftercreateMcpHandlerhad already consumed the request body:readInitializeHandshakethrewTypeError: unusable.handleMcpRequest's catch block rethrows, so a correct 2xx MCP response was discarded and replaced by anunhandled_route_error→ 500.recordMcpInitializeno-ops withoutPOSTHOG_API_KEY, but the handshake read is an argument to it and is evaluated first — the throw happened regardless of configuration.initializeis the first call of every MCP session, so no client could complete a handshake.tools/listwas unaffected (recordMcpToolsListreads the server's own registered tool names and never touches the body).The fix parses the JSON-RPC envelope once, before the handler runs, and derives both the usage metadata and the
clientInfohandshake from that single parse.describeMcpUsageRequestandreadInitializeHandshakebecome pure functions over that envelope, so no clone can survive past the handler again.Closes #10190
Scope
type(scope): short summaryConventional Commit format, for examplefix(api): restore profile access checks.CONTRIBUTING.mdand does not reintroduce GitHub Pages, VitePress,site/, orCNAME.Closes #123) — a linked open issue is required for every contributor PR.Validation
git diff --checknpm run actionlintnpm run typechecknpm run test:coveragelocally;codecov/patchrequires ≥99% coverage of the lines AND branches you changed (aim for 100% on your diff so CI variance does not fail near the threshold). Global coverage is a non-blocking trend with a loose 90% backstop, not the gate.npm run test:workersnpm run build:mcpnpm run test:mcp-packnpm run ui:openapi:checknpm run ui:lintnpm run ui:typechecknpm run ui:buildnpm audit --audit-level=moderateIf any required check was skipped, explain why:
test:coveragerun: 100% of the changed lines and branches insrc/mcp/server.tsare covered (verified against the lcov report fortest/unit/mcp-server-telemetry.test.ts+test/integration/api.test.ts). The unchecked commands are untouched surfaces — this diff changes no workflow, worker binding, OpenAPI schema, MCP package manifest, or UI file — and are left to CI.TypeError: unusable) and pass with it, so they pin the regression rather than merely passing.Safety
UI Evidencesection below with JPG/JPEG or PNG screenshots arranged as organized, captioned, clickable thumbnails. SVG screenshots are not used as review evidence. Review-only screenshots or recordings are not committed to the repository.UI Evidence
Not applicable — no visible UI, frontend, docs, or extension change. This is a server-side request-handling fix.
Notes
The existing integration test
test/integration/api.test.ts > api routes > serves private MCP tool listing and tool callswas already failing onmainat itsexpect(telemetryDownInitialize.status).toBe(200)assertion; it passes again with this change. The two added unit tests cover the handshake path directly, including aninitializewith noparamsand one whoseclientInfofields are not strings — both must still succeed, since those fields are optional by contract and a client must never be failed over LoopOver's own instrumentation.