Context.request_id stringifies integer request IDs and None, corrupting related_request_id correlation

Đang mở Phù hợp với người mới
#3,876 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #3909 của @EnesKartal — đã đóng, không merge
  • #3924 của @EnesKartal — đã đóng, không merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Bắt đầu tại src/mcp/server/mcpserver/context.py, ở Context.request_id và các chỗ sử dụng thuộc tính này trong elicit(), elicit_url() và log(). Kiểm tra các kiểu ID yêu cầu liên quan và các bài kiểm thử hiện có cho những phương thức này, sau đó bổ sung kiểm thử cho ID kiểu số nguyên và ID bị thiếu. Hoàn tất khi kiểu ID ban đầu được giữ nguyên và ID bị thiếu không bị chuyển thành chuỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug v1 v2
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:

  1. 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.
  2. 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.
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.

Ngôn ngữ chính
Python
Star
25k
Fork
4.2k
Merge trung bình
10 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/python-sdk

Tất cả issue của modelcontextprotocol/python-sdk

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.