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

オープン
#3,189 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
56/100
issue の種類
リファクタリング
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
TypeScript
スター
13.8k
フォーク
2.5k
平均マージ
6日 23時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/typescript-sdk のほかの issue

modelcontextprotocol/typescript-sdk の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。