[v2] Every inbound message is re-classified with up to four full zod parses after the transport already parsed it

Aperta
#3,189 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
56/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start by reading packages/server/src/server/streamableHttp.ts:838-839, then trace message handling through packages/core-internal/src/shared/protocol.ts:838-845 and protocol.ts:1887, plus the guards in types/guards.ts. Compare the two proposed classification approaches and check existing protocol tests. Done means inbound messages avoid redundant schema parses while validation remains strict.

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

Descrizione

v1 v2
What happened?

On a Streamable HTTP server, each inbound JSON-RPC request goes through five zod schema walks before its handler runs, though its kind is settled by the first one. Line references are at 919bb8e (main); 2.4.0 behaves the same.

  1. The transport parses the message with JSONRPCMessageSchema.parse (packages/server/src/server/streamableHttp.ts:838-839). That union's first member is JSONRPCRequestSchema, so this parse has already established that the message is a request.
  2. Protocol's onmessage (packages/core-internal/src/shared/protocol.ts:838-845) discards that result and classifies the message again with the guards in types/guards.ts. Each guard is a full safeParse. For a request this runs isJSONRPCResultResponse (fails), then isJSONRPCErrorResponse (fails), then isJSONRPCRequest (succeeds): three more walks. A notification gets four.
  3. Dispatch validates the request again through the era codec (protocol.ts:1887, validateRequest).

The three parses in step 2 do no work that step 1 hasn't already done. On our server (@modelcontextprotocol/server 2.4.0, about 1000 concurrent sessions), zod accounts for 6.8% of server CPU in a profile, and this classification chain is a large share of it.

What did you expect?

Each message is classified once. Two options:

  • Have the transport pass on the kind it already parsed, so onmessage doesn't classify again.
  • Or classify by key presence (method with id, method without id, result, error) and run only the one schema those keys select.

Either option keeps validation strict and drops two or three full parses from every inbound message.

Related: #3178 asks for pluggable protocol validation, mainly to reduce bundle size, and lists skipping inbound validation as one option. This issue is narrower. It asks for no new API, only for each message to be parsed once rather than three to five times.

Code to reproduce

Every inbound message takes this path, with no conditions. POST any request (for example tools/call) to a McpServer on StreamableHTTPServerTransport and step through streamableHttp.ts:838 → protocol.ts:838-845 → protocol.ts:1887. The guard chain in onmessage runs on every message the transport delivers, after the transport has already parsed it with JSONRPCMessageSchema.

SDK version

@modelcontextprotocol/server 2.4.0 (also present on main at 919bb8e)

Area

Server

Lingua principale
TypeScript
Stelle
13.8k
Fork
2.5k
Merge medio
6g 23h
PR unite (30g)
2

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/typescript-sdk

Tutte le issue di modelcontextprotocol/typescript-sdk

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.