fix: correlate invalid JSON-RPC envelope errors with the original request id#2852
Conversation
…uest id When an incoming message is valid JSON but fails JSON-RPC envelope validation, the error response previously could not be correlated with the originating request: the stdio server transport dropped the message with no response at all, and the Streamable HTTP transport replied with a null id. Extract the request id best-effort from the raw parsed payload and preserve it in the error response on both transports, falling back to a null id (per the JSON-RPC 2.0 spec) when no valid id can be extracted. The stdio transport now also answers unparseable lines with a Parse error (-32700, null id), and both transports report envelope-invalid messages as Invalid Request (-32600) instead of Invalid params (-32602), matching the JSON-RPC 2.0 error code semantics.
|
For reviewers — summary to ease review: Bug: when an incoming message is valid JSON but fails JSON-RPC envelope validation (e.g. Fix: a shared Two deliberate calls worth a look:
Both repro cases covered on both transports + null-id fallbacks. Full suite (1785), ruff, pyright all pass. If you'd rather land just the id-correlation core and split the stdio parse-error / error-code changes, I'm glad to narrow it. |
Fixes #2848
When an incoming message is valid JSON but fails JSON-RPC envelope validation (e.g.
"jsonrpc": "1.0", a missingjsonrpcfield, or a non-stringmethod), the error response could not be correlated with the originating request:"server-error"on v1.x), breaking client-side request/response correlation.This PR extracts the request id best-effort from the raw parsed payload (shared helper
extract_raw_request_id; only spec-valid id types are accepted — strings and non-bool integers) and preserves it in the error response on both transports, falling back to a null id per the JSON-RPC 2.0 spec when no valid id can be extracted. The stdio transport now also answers unparseable lines with-32700 Parse error(null id), matching the HTTP transport's existing parse-error behavior. Envelope-invalid messages are now reported as-32600 Invalid Requestinstead of-32602 Invalid params, matching JSON-RPC 2.0 error code semantics (the issue's three repro cases are envelope violations, not parameter errors).Tests cover all three repro cases from the issue on both transports, plus the null-id fallback (unextractable id and unparseable body). Full suite,
ruff, andpyrightpass.