modelcontextprotocol / modelcontextprotocol/python-sdk

Make JSON-RPC ID type coercion configurable

Aperta
#1,795 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug P3 v2
Lingua principale
Python
Stelle
24.3k
Fork
4k
Merge medio
1g 1h
PR unite (30g)
31

Descrizione

Background

PR #1720 added automatic type coercion for JSON-RPC response IDs, allowing string IDs like "0" to match integer request IDs like 0. This makes the client more tolerant of servers that echo back IDs in a different but semantically equivalent format.

Spec Language

Per the JSON-RPC 2.0 and MCP specifications, response IDs should be exact matches:

JSON-RPC 2.0: Response ID "MUST be the same as the value of the id member in the Request Object"

MCP Spec:

  • Result responses "MUST include the same ID as the request they correspond to"
  • Error responses "MUST include the same ID as the request they correspond to"

The language "same as the value" / "same ID" implies exact matching including type. The spec does not describe IDs as opaque and does not mandate type conversion or normalization. Servers should echo back the exact ID they received.

Current Behavior

PR #1720 makes the client more lenient by accepting type-coerced IDs (e.g., "0" matching 0). This is a workaround for non-compliant servers.

Proposal

Make this ID type coercion behavior configurable:

  • Strict mode: Require exact type matching (spec-compliant behavior)
  • Lenient mode: Allow type coercion between numeric strings and integers (current behavior after #1720)

This could be exposed as a session or client configuration option, defaulting to lenient for backwards compatibility.

Ideally strict mode is default, with an error message being clear as to why something fails. If we receive a response with an ID that's a string, even though the original request ID was an int we could first check if there are any IDs which would match after type coercion and then include that in the error message. "Request ID with same value of different type found, but ignored due to strict ID matching configuration".

Reasoning

By spec language, request ID 1 and request ID "1" are different IDs, but with this type coercion they are treated as the same. Given that many clients/servers are non-compliant with this anyway and do their own type conversions we should allow server/client users of the SDK to configure the behaviour for themselves.

Related

  • #1720 - Fix JSON-RPC error response ID matching

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia esaminando PR #1720 e l’implementazione attuale della corrispondenza degli ID di risposta JSON-RPC; l’issue non indica file o test specifici. Determina dove deve risiedere la configurazione della sessione o del client, quindi verifica che sia possibile selezionare la corrispondenza rigorosa e la coercizione numerica permissiva, con una gestione chiara degli ID di risposta non corrispondenti e mantenendo la compatibilità all’indietro.

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

Valutazione

Stack tecnologico
python
Ambito
api, backend-api-design
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.