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

Ouverte
#3,189 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
56/100
Type d'issue
Refactorisation
Clarté
Clairement spécifiée
Activité
Active
Stack technique
typescript
Domaine
backend, performance

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
TypeScript
Étoiles
13.8k
Forks
2.5k
Merge moyen
6 j 23 h
PR mergées (30 j)
2

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modelcontextprotocol/typescript-sdk

Toutes les issues de modelcontextprotocol/typescript-sdk

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.