Skip to content

Accept null for the unused JSON-RPC response member - #7326

Merged
ChrisJBurns merged 1 commit into
mainfrom
ChrisJBurns/upstream-validation-null-members
Nov 26, 2026
Merged

ChrisJBurns merged 1 commit into
mainfrom
ChrisJBurns/upstream-validation-null-members

Conversation

@ChrisJBurns

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #7317, addressing the two non-blocking notes from @rdimitrov's review.

  • Backends that serialize the unused member as null stopped working. Validate upstream JSON-RPC responses in transparent proxy #7317 answered 502 to {"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, so mcp.ValidateResponse now treats an explicit null for the unused member as absent:
    • A null error is always dropped, since it is never a valid error object.
    • A null result is dropped only beside an error, since "result":null on its own is a valid result.
    • The forwarded bytes are unchanged.
  • A rejected initialize leaves the backend's session open. The proxy drops the backend's Mcp-Session-Id without sending a DELETE, 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 the initialize behaviour.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test). Ran the pkg/mcp/... and pkg/transport/proxy/transparent/... suites with -race.
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

New TestValidateResponse cases:

  • Accepted: result with "error":null, error with "result":null, and both null.
  • Still rejected: "error":null with no result, a result with "error":null but no id, and "result":null beside 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 reason invalid_response) when a backend's response carried "error":null beside a result or "result":null beside an error. After: these responses are forwarded unchanged, as they were before #7317.

Special notes for reviewers

  • Deliberate deviation. This is more lenient than both strict JSON-RPC and the request-side 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.
  • A response with both members null is accepted as a null result. That is how a serializer that emits every field would encode a successful null result.

🤖 Generated with Claude Code

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]>
@github-actions github-actions Bot added the size/XS Extra small PR: < 100 lines changed label Nov 26, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
    • result with "error":null
    • an error with "result":null
    • both members null
    • "error" : null with extra whitespace
  • Still 502:
    • "error":null alone
    • result plus "error":null with no id
    • "result":null beside 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

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.23%. Comparing base (c038d02) to head (51f9912).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisJBurns
ChrisJBurns merged commit da96c40 into main Nov 26, 2026
47 checks passed
@ChrisJBurns
ChrisJBurns deleted the ChrisJBurns/upstream-validation-null-members branch November 26, 2026 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Extra small PR: < 100 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants