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

オープン 初心者向け
#3,876 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #3909 @EnesKartal による — マージされずにクローズ
  • #3924 @EnesKartal による — マージされずにクローズ

評価

難易度
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 の本文から書いたものです。

説明

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.

主要言語
Python
スター
25k
フォーク
4.2k
平均マージ
10分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/python-sdk のほかの issue

modelcontextprotocol/python-sdk の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。