You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In JSON-RPC 2.0 and the MCP specification, RequestId is defined as str | int.
In src/mcp/server/mcpserver/context.py:299-301, Context.request_id is defined as:
@propertydefrequest_id(self) ->str:
"""Get the unique ID for this request."""returnstr(self.request_context.request_id)
And in ctx.elicit(), ctx.elicit_url(), and ctx.log() (lines 226, 261, 285):
related_request_id=self.request_id
This causes two bugs:
Integer request ID corruption: When an incoming JSON-RPC request uses an integer ID (e.g., 42), ctx.request_id coerces it to "42". Downstream notifications and elicitation messages carry related_request_id: "42" instead of 42, breaking correlation on clients that distinguish numeric and string IDs.
None coerced to string "None": When a handler runs in a context without a request ID (e.g., notification handlers or server-initiated tasks where request_context.request_id is None), str(None) evaluates to the string "None". As a result, the server sends related_request_id: "None" over the wire rather than None or omitting the field.
Context.request_id should return RequestId | None preserving the original type (int, str, or None).
Proposed Fix
Update Context.request_id in src/mcp/server/mcpserver/context.py:
@propertydefrequest_id(self) ->RequestId|None:
"""Get the unique ID for this request."""ifself._request_contextisNone:
returnNonereturnself._request_context.request_id
and ensure related_request_id in elicit(), elicit_url(), and log() passes the optional RequestId.
I would be happy to submit a PR with test coverage if assigned.
Assisted by AI; reviewed and verified with reproduction script.
Root cause confirmed, and the coercion happens in exactly one place: Context.request_id wraps ServerRequestContext.request_id in str(...), so every related_request_id= site (elicit, elicit_url, log, plus the resolve.py resolver path) inherited it. ServerRequestContext.request_id is already RequestId | None, so the wrapper added nothing except the corruption.
Behaviour change, exactly: Context.request_id now returns RequestId | None unchanged.
inbound 42 (int): was "42", now 42
inbound "abc" (str): unchanged, "abc"
no inbound id (notification handler, server initiated task): was "None", now None
One existing assertion encoded the bug: tests/server/mcpserver/test_server.py::TestContextInjection::test_context_logging pinned related_request_id="2" for a client that sends integer ids, so it now expects 2. No production caller needed changing, because the three call sites already pass the property straight through.
GREEN after, same two tests in tests/server/mcpserver/test_elicitation.py: one asserts the elicitation the tool sends carries related_request_id as the integer 42, the other asserts request_id is None for a context with no id. Full suite under branch coverage: 6234 passed, 100.00% coverage, no missing statements or partial branches. The single failure, test_permission_error, also fails on unmodified main when the suite runs as root. ruff check, ruff format in check mode, and pyright are clean.
Activity
Shanmukh-l33t commentedon Nov 19, 2026
Root cause confirmed, and the coercion happens in exactly one place:
Context.request_idwrapsServerRequestContext.request_idinstr(...), so everyrelated_request_id=site (elicit,elicit_url,log, plus theresolve.pyresolver path) inherited it.ServerRequestContext.request_idis alreadyRequestId | None, so the wrapper added nothing except the corruption.Behaviour change, exactly:
Context.request_idnow returnsRequestId | Noneunchanged.42(int): was"42", now42"abc"(str): unchanged,"abc""None", nowNoneOne existing assertion encoded the bug:
tests/server/mcpserver/test_server.py::TestContextInjection::test_context_loggingpinnedrelated_request_id="2"for a client that sends integer ids, so it now expects2. No production caller needed changing, because the three call sites already pass the property straight through.RED before, at upstream main 77001c9:
GREEN after, same two tests in
tests/server/mcpserver/test_elicitation.py: one asserts the elicitation the tool sends carriesrelated_request_idas the integer 42, the other assertsrequest_id is Nonefor a context with no id. Full suite under branch coverage: 6234 passed, 100.00% coverage, no missing statements or partial branches. The single failure,test_permission_error, also fails on unmodified main when the suite runs as root.ruff check,ruff formatin check mode, andpyrightare clean.Branch, no PR: main...Shanmukh-l33t:python-sdk:fix/3876-request-id-preserve
The patch is ready to open as a PR if a maintainer assigns this issue to me.
AI assistance: this analysis and patch were produced with an AI agent, and every test result above was run locally.