Skip to content

fix(shared): strip envelope fields when forwarding JSONRPCRequest#2573

Open
charles-adedotun wants to merge 1 commit into
modelcontextprotocol:mainfrom
charles-adedotun:fix/forward-jsonrpc-envelope-collision
Open

fix(shared): strip envelope fields when forwarding JSONRPCRequest#2573
charles-adedotun wants to merge 1 commit into
modelcontextprotocol:mainfrom
charles-adedotun:fix/forward-jsonrpc-envelope-collision

Conversation

@charles-adedotun
Copy link
Copy Markdown

Summary

Fixes #2548.

BaseSession.send_request builds the outgoing wire object as:

JSONRPCRequest(jsonrpc="2.0", id=request_id, **request_data)

When request_data originates from model_dump() on an existing JSONRPCRequest (a typical forwarding/proxy scenario — e.g. a ServerSession receiving an incoming request and re-emitting it via a ClientSession), the dump preserves the envelope fields jsonrpc and id, which collide with the explicit kwargs above:

TypeError: got multiple values for keyword argument 'jsonrpc'

Approach

Conservative: pop jsonrpc and id from request_data before unpacking. This keeps the wire-envelope construction authoritative at this call site and doesn't change any model-layer dump behavior (which would have wider blast radius).

As mentioned in the issue thread, I'm happy to take the model-layer approach instead (e.g. excluding envelope fields from model_dump on forwarded requests) if the maintainers prefer that — let me know.

Test

Added test_send_request_strips_envelope_fields_when_forwarding in tests/shared/test_session.py that reproduces the bug (test fails without the fix, passes with it) and verifies clean forwarding end-to-end with a mock server.

Checklist

  • Tests added that fail without the fix
  • Full tests/shared/test_session.py suite passes (9/9)
  • Full tests/ suite passes (1173 passed, 98 skipped, 1 xfailed)
  • ruff format / ruff check clean
  • pyright clean (0 errors, 0 warnings)
  • No changes to public API

BaseSession.send_request constructs a JSONRPCRequest as:
    JSONRPCRequest(jsonrpc="2.0", id=request_id, **request_data)

When request_data came from model_dump() on an existing JSONRPCRequest
(forwarding/proxy scenarios — e.g. a ServerSession receiving a request
and re-emitting it through a ClientSession), the dump preserves the
envelope fields jsonrpc and id, which then collide with the explicit
kwargs above and raise:
    TypeError: got multiple values for keyword argument 'jsonrpc'

Fix: pop jsonrpc and id from request_data before the unpack. Added a
regression test that exercises the forwarding pattern via
create_client_server_memory_streams.

Fixes modelcontextprotocol#2548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseSession.send_request fails when forwarding a request received via ServerSession (envelope-fields collision)

1 participant