Repository navigation
Open
Description
Release Line
2.x (main branch)
Problem Description
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:
@property
def request_id(self) -> str:
"""Get the unique ID for this request."""
return str(self.request_context.request_id)And in ctx.elicit(), ctx.elicit_url(), and ctx.log() (lines 226, 261, 285):
related_request_id=self.request_idThis causes two bugs:
- Integer request ID corruption: When an incoming JSON-RPC request uses an integer ID (e.g.,
42),ctx.request_idcoerces it to"42". Downstream notifications and elicitation messages carryrelated_request_id: "42"instead of42, breaking correlation on clients that distinguish numeric and string IDs. Nonecoerced to string"None": When a handler runs in a context without a request ID (e.g., notification handlers or server-initiated tasks whererequest_context.request_idisNone),str(None)evaluates to the string"None". As a result, the server sendsrelated_request_id: "None"over the wire rather thanNoneor omitting the field.
Minimal Reproduction Code
from mcp.server.context import ServerRequestContext
from mcp.server.mcpserver.context import Context
# 1. Integer Request ID
req_ctx_int = ServerRequestContext(
session=None,
lifespan_context={},
method="tools/call",
request_id=42,
protocol_version="2026-07-28",
)
ctx_int = Context(request_context=req_ctx_int)
print(f"Integer ID result: {ctx_int.request_id!r} (type: {type(ctx_int.request_id).__name__})")
assert ctx_int.request_id == 42, f"Expected 42 (int), got {ctx_int.request_id!r}"
# 2. None Request ID (e.g. notifications)
req_ctx_none = ServerRequestContext(
session=None,
lifespan_context={},
method="notifications/message",
request_id=None,
protocol_version="2026-07-28",
)
ctx_none = Context(request_context=req_ctx_none)
print(f"None ID result: {ctx_none.request_id!r} (type: {type(ctx_none.request_id).__name__})")
assert ctx_none.request_id is None, f"Expected None, got {ctx_none.request_id!r}"Actual Output
Integer ID result: '42' (type: str)
AssertionError: Expected 42 (int), got '42'
None ID result: 'None' (type: str)
AssertionError: Expected None, got 'None'
Expected Output
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:
@property
def request_id(self) -> RequestId | None:
"""Get the unique ID for this request."""
if self._request_context is None:
return None
return self._request_context.request_idand 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.
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.