go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

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

Open piyushbag opened this issue 5 months ago • 1 comments

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

  • [x] go test ./internal/jsonrpc2/... -v
  • [x] go test ./mcp/... -run TestIOConnRead_EmptyMethod -v
  • [x] go test ./... -count=1
  • [x] go test -race ./internal/jsonrpc2/... -count=1
  • [x] Server conformance (npx @modelcontextprotocol/conformance server --url http://127.0.0.1:3000/mcp) — 40 passed, 0 failed
  • [x] 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 avatar Jun 10 '26 02:06 piyushbag

Re-ran the local gate on fix-976-method-key-present (9d3764c) today:

go test ./... -count=1
ok  github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2
ok  github.com/modelcontextprotocol/go-sdk/mcp
ok  github.com/modelcontextprotocol/go-sdk/oauthex

go test -race ./internal/jsonrpc2/... ./mcp/... \
  -run 'TestIOConnRead_EmptyMethod|TestWire|TestDecode' -count=1
ok  github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2
ok  github.com/modelcontextprotocol/go-sdk/mcp

Happy to rebase or adjust scope if you prefer coordinating with #980 / #996 on #976. Thanks for reviewing when CI is approved for fork PRs.

piyushbag avatar Jun 11 '26 02:06 piyushbag