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

Offen Anfängerfreundlich
#3,876 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

  • #3909 von @EnesKartal — ohne Merge geschlossen
  • #3924 von @EnesKartal — ohne Merge geschlossen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Beginne in src/mcp/server/mcpserver/context.py bei Context.request_id und dessen Verwendungen in elicit(), elicit_url() und log(). Prüfe die zugehörigen Request-ID-Typen und die vorhandenen Tests für diese Methoden und ergänze dann Tests für ganzzahlige und fehlende IDs. Fertig, wenn der ursprüngliche ID-Typ erhalten bleibt und fehlende IDs nicht in Zeichenketten umgewandelt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
25k
Forks
4.2k
Ø Merge
10 Min.
Gemergte PRs (30 T.)
1

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/python-sdk

Alle Issues in modelcontextprotocol/python-sdk

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.