Skip to content

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

Open
piyushbag wants to merge 1 commit into
modelcontextprotocol:mainfrom
piyushbag:fix-976-method-key-present
Open

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

Conversation

@piyushbag

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.

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.

Fixes modelcontextprotocol#976
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