Skip to content

Fix MCP initialize protocol version negotiation - #18

Merged
evanphx merged 1 commit into
mainfrom
fix/mcp-initialize-version-negotiation
Oct 2, 2026
Merged

evanphx merged 1 commit into
mainfrom
fix/mcp-initialize-version-negotiation

Conversation

@evanphx

@evanphx evanphx commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Successfully negotiate initialize requests for differing protocol versions by returning the supported revision, 2025-06-18, rather than -32602.
  • Keep missing, empty, null, and incorrectly typed protocol versions and malformed typed parameters invalid.
  • Add wire-level regression tests for matching, newer, older, and arbitrary version strings, and malformed parameters.

Context

Portal reproduces an initialize failure when requesting 2025-11-25. This fixes the source mismatch; it does not claim newer protocol support or confirm the reported Claude reconnect diagnosis. Portal will still need its pinned dependency updated after this lands.

Validation

  • Targeted initialization tests: PASS
  • go test ./...: PASS
  • go test -race ./...: PASS
  • go vet ./...: clean
  • git diff --check: clean
  • Confirmed the new version-negotiation tests fail against the original handler with -32602.

Select the supported 2025-06-18 revision when clients request another protocol version, while retaining invalid parameter errors.

Add regression coverage for matching and differing versions and malformed parameters.

Co-authored-by: Amp <amp@ampcode.com>

@miren-code-agent miren-code-agent Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍪 biscuit: ✅ approved — human code review not needed

I think this is ready. handleInitialize now follows MCP's version negotiation: if a client asks for a revision the server doesn't support, the server answers with one it does support (2025-06-18) and lets the client decide whether to go on. Before, it sent back -32602, and that's what was breaking clients that requested 2025-11-25. Since only one revision is supported, "echo it back if it matches, otherwise offer ours" comes down to always returning MCPProtocolVersion. That's correct, and the comment where the result is built says so.

I walked each row of TestMCPServerInitializeInvalidParams through the handler to make sure every rejection comes from the check it's meant to test:

  • Missing params, null params, {}, an empty version and a null version all decode to an empty ProtocolVersion. They hit the new nonempty check and get "Invalid params".
  • Array or string params, a numeric version, array capabilities, and string clientInfo all cause a json.Unmarshal type error. That goes down the existing -32602 "Invalid params" path.

So the suite covers both the type-checking rejections and the new empty-string check. The negotiation test sends real wire input. It checks the returned revision and the initialized flag across matching, newer, older and arbitrary version strings, which is the behaviour that matters here.

Changing the old "wrong protocol version" case to "missing protocol version" is the right call. A test asserting the old rejection would now be asserting behaviour that breaks the spec. The new case still covers the remaining -32602 path in handleInitialize. No other test or doc in the repo depended on the old rejection.

Why I'm recommending approval: the change is one small, focused handler edit. It matches the protocol's documented negotiation rules. It only touches initialize, and both the success and rejection paths have wire-level tests that I traced by hand. I don't see anything a human reviewer would need to weigh in on beyond that. As the description says, Portal will still need its pinned dependency bumped after this merges.


🍪 full review note · reviewed at 2c18f3c · comment /biscuit review to run biscuit again.

@evanphx
evanphx marked this pull request as ready for review October 2, 2026 16:11
@evanphx
evanphx requested a review from a team as a code owner October 2, 2026 16:11
@evanphx
evanphx merged commit 8c810c6 into main Oct 2, 2026
2 checks passed
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.

2 participants