Context.request_id stringifies integer request IDs and None, corrupting related_request_id correlation
维护者通常 1 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
从 src/mcp/server/mcpserver/context.py 中的 Context.request_id 及其在 elicit()、elicit_url() 和 log() 中的用法开始。检查相关的请求 ID 类型以及这些方法的现有测试,然后为整数 ID 和缺失的 ID 添加测试。完成标准是保留原始 ID 类型,且缺失的 ID 不会被转成字符串。
由索引模型根据 Issue 内容生成。
描述
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_id
This 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_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.
- 主要语言
- Python
- 星标
- 25k
- 派生
- 4.2k
- 平均合并
- 10 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/python-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 86/100
modelcontextprotocol/python-sdk#3978 · 2 条评论 ·
维护者通常 1 天内回复
-
Client serves cached tools after its context exits with an exception可能已有人在做 @jonxsong 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
modelcontextprotocol/python-sdk#3968 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
modelcontextprotocol/python-sdk#3957 ·
维护者通常 1 天内回复
-
v2
难度 1/5 1 小时以内 新手友好度 91/100
modelcontextprotocol/python-sdk#3934 · 2 条评论 ·
维护者通常 1 天内回复
-
bug v1
难度 2/5 1-3 小时 新手友好度 79/100
modelcontextprotocol/python-sdk#3929 · 2 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
auto-reported bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
backend bug
难度 2/5 1-3 小时 新手友好度 86/100
nolte/kamerplanter#3193 ·
维护者通常 1 天内回复
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise可能已有人在做 @abdulwaarith0 今天认领。 未关闭needs-triage
难度 2/5 1-3 小时 新手友好度 84/100
MakazhanAlpamys/Soup#3690 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复