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

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

Open cclabadmin opened this issue 6 months ago • 7 comments

Describe the bug

Some malformed JSON-RPC requests sent over stdio receive no JSON-RPC error response even though the request carries an id and the server continues processing later requests.

The clearest case is an empty method name. After this request, the server gives no response for id=5, but later ping requests still succeed on the same stdio session. This looks like a recoverable request-dispatch path.

I am filing this as a conformance and robustness issue, not as a security issue. Malformed requests should be rejected, but requests with ids should receive a correlated JSON-RPC error response when the session can continue.

Environment tested:

  • Stable release: v1.6.1 (d454bbaf06a342aee5336df3370321d9cdec2478)
  • main snapshot from 2026-05-26: c044cdb734d8d321d596f5b6077072798dce4ad6
  • Server used: conformance/everything-server
  • Transport: stdio

To Reproduce

Steps to reproduce the behavior:

  1. Start the Go SDK stdio server.
  2. Complete a normal init handshake.
  3. Send this malformed request:
{"jsonrpc":"2.0","id":5,"method":"","params":{}}
  1. Observe that no response is returned for that request.
  2. Send later ping request and observe that the server still responds.

Expected behavior

For the primary case, I would expect a JSON-RPC error response for id=5, most likely -32601 Method not found or -32600 Invalid Request depending on how strictly the SDK wants to validate method names.

Reasoning:

  • JSON-RPC 2.0 requests include an id; notifications omit id and do not require a response.
  • method is a string in both JSON-RPC and MCP. If an empty method name is invalid at the MCP layer, this still looks like a recoverable request-dispatch error rather than a transport read failure, because the session continues.

Observed behavior

The primary repro case produced no JSON-RPC response for id=5, and a later ping health check succeeded on the same session.

Additional context

The same isolated test also found other malformed request cases. I am including them for completeness because they show the boundary between recoverable no-response behavior and connection-fatal behavior.

Case Request Observed behavior
id as null {"jsonrpc":"2.0","id":null,"method":"ping","params":{}} No JSON-RPC response for id=null; later ping requests still succeeded.
Wrong JSON-RPC version {"jsonrpc":"1.0","id":3,"method":"ping","params":{}} No JSON-RPC response; process exited with Server failed: invalid message version tag "1.0"; expected "2.0".
Missing jsonrpc field {"id":4,"method":"ping","params":{}} No JSON-RPC response; process exited with Server failed: invalid message version tag ""; expected "2.0".
method as number {"jsonrpc":"2.0","id":8,"method":12345,"params":{}} No JSON-RPC response; process exited with Server failed: unmarshaling jsonrpc message: json: cannot unmarshal ... into Go struct field ... method of type string.
  • The id:null case may be another recoverable MCP-level rejection case. JSON-RPC 2.0 treats id:null as distinct from an omitted id, although null ids are discouraged. MCP narrows request ids to string or number.

  • The other three cases are structurally invalid JSON-RPC envelopes: jsonrpc must be "2.0" and method must be a string. If the Go SDK intentionally treats these decoder/envelope failures as fatal stdio read errors, documenting that policy would help clients distinguish expected connection teardown from a lost response.

Related prior issue: #179 was closed in the v0.3.0 milestone and appears to cover a narrower malformed trailing-garbage case. The isolated cases above still reproduce on v1.6.1 and current main.

cclabadmin avatar May 26 '26 19:05 cclabadmin

I'd like to work on this issue.

Root cause (my reading): jsonrpc2.DecodeMessage treats messages with an empty method string as responses (because method is empty), so the stdio server silently drops them when there is no matching outgoing call (see the TODO in readIncoming).

Proposed fix: Decode messages with an empty method but a valid id and/or params as requests so they flow through normal dispatch and return -32601 Method not found.

Tests: decode regression in internal/jsonrpc2, ioConn.Read check, and ServerSession.handle for empty method.

I'll open a PR shortly from @piyushbag if this approach looks reasonable.

piyushbag avatar Jun 10 '26 01:06 piyushbag

Following up on the PR for this (#1000). I see #980 addresses the same decode path and already has green CI — happy to close #1000 if that is the preferred approach, or rebase and compare diffs if a second opinion is useful.

Fork workflows have not been approved yet, so CI has not run on my branch. Local validation includes both conformance suites (server 40/40, client 220/220) plus lint, test, and race.

piyushbag avatar Jun 10 '26 23:06 piyushbag

I found a related but distinct JSON-RPC error-handling case.

In addition to the no-response cases described above, recoverable request-level errors can also return a JSON-RPC error object with numeric error code 0, rather than a standard JSON-RPC 2.0 code.

Tested with v1.6.1 (d454bbaf06a342aee5336df3370321d9cdec2478) using conformance/everything-server.

Requests:

{"jsonrpc":"2.0","id":7,"method":"nonexistent/method","params":{}}
{"jsonrpc":"2.0","id":9,"method":"ping","params":["a","b"]}

Observed over stdio:

{"jsonrpc":"2.0","id":7,"error":{"code":0,"message":"JSON RPC not handled: \"nonexistent/method\" unsupported"}}
{"jsonrpc":"2.0","id":9,"error":{"code":0,"message":"handling 'ping': unmarshaling params: json: cannot unmarshal array into Go value of type mcp.PingParams"}}

Expected codes would likely be:

  • -32601 Method not found for the nonexistent method
  • -32602 Invalid params for the invalid params shape

I also observed the invalid-params case returning code 0 over Streamable HTTP via SSE. For the nonexistent-method case over Streamable HTTP, the server returned plain-text 400 Bad Request with no JSON-RPC error object.

This is related but not identical to the no-response behavior in this issue. In these cases the server often does return a response, but it is either not a JSON-RPC error object or uses a non-standard numeric code. I am mentioning it here because it may share the same error-handling path; I can split it into a separate issue if that would be easier to track.

Unknown methods can occur during normal operation due to version skew, optional extensions, experimental methods, or methods renamed/removed across protocol or SDK versions, so a recoverable JSON-RPC error response is useful for client-side correlation and recovery.

cclabadmin avatar Jun 12 '26 10:06 cclabadmin

@cclabadmin thank you for flagging these issues. PR #1000 fixes the case for method ""

guglielmo-san avatar Jun 16 '26 08:06 guglielmo-san

Regarding your second message, this behavior will be fixed with the new release of the new protocol version

guglielmo-san avatar Jun 16 '26 08:06 guglielmo-san

@guglielmo-san Thank you for the earlier follow-up. I tested a separate registered-method code: 0 case against main at 65ece5964304528e7568507e2b17351824ef73fc (2026-06-28), using the 2026-07-28 protocol revision over stdio.

This seems distinct from the unknown-method case in my earlier comment: subscriptions/listen is a registered SDK method, and dispatch reaches the typed params unmarshal path before failing.

For example, subscriptions/listen with invalid params over stdio returns code: 0:

// request
{"jsonrpc":"2.0","id":1001,"method":"subscriptions/listen","params":{"notifications":"value","_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientInfo":{"name":"repro","version":"0.1.0"},"io.modelcontextprotocol/clientCapabilities":{}}}}

// response
{"jsonrpc":"2.0","id":1001,"error":{"code":0,"message":"handling 'subscriptions/listen': unmarshaling ... into a *mcp.SubscriptionsListenParams: ..."}}

I would expect this to be -32602 Invalid params, and the relevant path appears to wrap the params unmarshal error with fmt.Errorf rather than returning/wrapping a JSON-RPC *WireError, so toWireError falls through to the zero-value Code: 0.

I wanted to flag this in case it is useful for the 2026-07-28 follow-up work. Happy to open a separate issue if that would be easier to track.

cclabadmin avatar Jun 29 '26 04:06 cclabadmin

Thank you for the great contribution at finding these issues @cclabadmin The empty error code in case of error in unmarshalParameters is fixed in PR #1087

guglielmo-san avatar Jul 14 '26 14:07 guglielmo-san