Skip to content

fix(jsonrpc2): decode requests when method key is present#999

Closed
piyushbag wants to merge 1 commit into
modelcontextprotocol:mainfrom
piyushbag:fix-976-empty-method-jsonrpc-response
Closed

fix(jsonrpc2): decode requests when method key is present#999
piyushbag wants to merge 1 commit into
modelcontextprotocol:mainfrom
piyushbag:fix-976-empty-method-jsonrpc-response

Conversation

@piyushbag

@piyushbag piyushbag commented Jun 10, 2026

Copy link
Copy Markdown

Summary

Fixes #976.

DecodeMessage used msg.Method != "" to classify messages. A wire payload with "method":"" was decoded as a response, so stdio servers dropped it without a correlated JSON-RPC error.

Approach

Decode into a wireDecode struct whose method field is json.RawMessage. Treat the message as a request when the method key is present on the wire (including ""), which matches JSON-RPC semantics.

This revision:

  • Uses a single JSON parse per message (dedicated decode struct, no secondary fields-map unmarshal)
  • Documents why json.RawMessage is used for method
  • Adds regression coverage so normal responses and id-only payloads without a method key stay on the response path

Test plan

  • go test ./internal/jsonrpc2/... -v
  • go test ./mcp/... -run TestIOConnRead_EmptyMethod -v
  • go test ./... -count=1
  • go test -race ./internal/jsonrpc2/... -count=1
  • Server conformance (npx @modelcontextprotocol/conformance server --url http://127.0.0.1:3000/mcp) — 40 passed, 0 failed
  • Client conformance (./scripts/client-conformance.sh) — 217 passed, 0 failed

Scope note

This PR addresses the primary repro in #976 (empty method with valid id). Other cases listed in the issue (id: null, structurally invalid envelopes) are unchanged.

@piyushbag piyushbag force-pushed the fix-976-empty-method-jsonrpc-response branch 4 times, most recently from acbf379 to da4ba1f Compare June 10, 2026 01:59
Use json.RawMessage for the method field so DecodeMessage can
distinguish a missing method key from an explicitly empty method
string. Fixes the silent stdio drop in modelcontextprotocol#976 with a single JSON
parse (no second fields-map unmarshal).

Fixes modelcontextprotocol#976
@piyushbag piyushbag force-pushed the fix-976-empty-method-jsonrpc-response branch from da4ba1f to 1656060 Compare June 10, 2026 02:15
@piyushbag piyushbag changed the title fix(jsonrpc2): respond to requests with empty method names fix(jsonrpc2): decode requests when method key is present Jun 10, 2026
@piyushbag

Copy link
Copy Markdown
Author

Superseded by a clean branch with a single commit.

@piyushbag piyushbag closed this Jun 10, 2026
@piyushbag piyushbag deleted the fix-976-empty-method-jsonrpc-response branch June 10, 2026 02:26
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.

Stdio server leaves some malformed JSON-RPC requests unanswered while the session remains alive

1 participant