Repository navigation
Accept null for the unused JSON-RPC response member - #7326
Conversation
Upstream response validation rejected responses that carry an explicit
null for the member they do not use, such as {"result":{},"error":null}
or "result":null beside an error. Serializers that do not omit nulls
emit these, lenient clients accept them, and before #7317 the proxy
forwarded them, so a backend that worked would start getting 502s.
They are unambiguous, so treat the null member as absent.
Also document that a rejected initialize response leaves the backend
session it carried open until the backend's idle timeout, since the
proxy does not send it a DELETE.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
rdimitrov
left a comment
There was a problem hiding this comment.
Approving. This resolves both notes from #7317. The leniency is narrow, applies only to upstream responses, and the forwarded bytes are unchanged.
How I tested it: I built thv from this branch and ran thv proxy in front of a mock backend.
- Now forwarded as 200:
resultwith"error":null- an error with
"result":null - both members
null "error" : nullwith extra whitespace
- Still 502:
"error":nullaloneresultplus"error":nullwith noid"result":nullbeside a malformed error- a case-aliased
"Error":null "error":{}- the #5247 repro
The official Go SDK client still connects and calls a tool returning 5 MB through the proxy, with both JSON-response and SSE servers. go test -race passes for pkg/mcp and pkg/transport/proxy/transparent. ValidateResponse has no callers besides the proxy, so nothing else picks up the leniency.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7326 +/- ##
==========================================
- Coverage 82.27% 82.23% -0.04%
==========================================
Files 935 935
Lines 95159 95169 +10
==========================================
- Hits 78288 78265 -23
- Misses 16866 16899 +33
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Follow-up to #7317, addressing the two non-blocking notes from @rdimitrov's review.
nullstopped working. Validate upstream JSON-RPC responses in transparent proxy #7317 answered502to{"jsonrpc":"2.0","id":1,"result":{},"error":null}and to an error response that also carries"result":null. Serializers that don't omit nulls produce exactly these, lenient clients accept them, and the proxy forwarded them before Validate upstream JSON-RPC responses in transparent proxy #7317. They are unambiguous, somcp.ValidateResponsenow treats an explicitnullfor the unused member as absent:nullerror is always dropped, since it is never a valid error object.nullresult is dropped only beside an error, since"result":nullon its own is a valid result.initializeleaves the backend's session open. The proxy drops the backend'sMcp-Session-Idwithout sending aDELETE, so that session lingers until the backend's own idle timeout. This is low impact, since clients never receive the id. It is now stated in the architecture doc next to the rest of theinitializebehaviour.Type of change
Test plan
task test). Ran thepkg/mcp/...andpkg/transport/proxy/transparent/...suites with-race.task test-e2e)task lint-fix)New
TestValidateResponsecases:"error":null, error with"result":null, and bothnull."error":nullwith no result, a result with"error":nullbut noid, and"result":nullbeside a malformed error.Does this introduce a user-facing change?
Yes. Before: the transparent proxy answered
502 Bad Gateway("Invalid upstream JSON-RPC response", WARN reasoninvalid_response) when a backend's response carried"error":nullbeside a result or"result":nullbeside an error. After: these responses are forwarded unchanged, as they were before #7317.Special notes for reviewers
mcp.DecodeMessage, which require the unused member to be absent. It applies only to upstream responses, where the goal is to catch broken output without breaking backends that work today.nullis accepted as anullresult. That is how a serializer that emits every field would encode a successfulnullresult.🤖 Generated with Claude Code