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

Aperta Adatta ai principianti
#3,876 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #3909 di @EnesKartal — chiusa senza merge
  • #3924 di @EnesKartal — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Inizia in src/mcp/server/mcpserver/context.py, da Context.request_id e dai suoi utilizzi in elicit(), elicit_url() e log(). Controlla i tipi di ID della richiesta correlati e i test esistenti per questi metodi, quindi aggiungi test per gli ID interi e mancanti. Il lavoro è completato quando il tipo originale dell’ID viene preservato e gli ID mancanti non vengono convertiti in stringhe.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
25k
Fork
4.2k
Merge medio
10m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/python-sdk

Tutte le issue di modelcontextprotocol/python-sdk

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.