[v2] Stateless servers rebuild every tool and resource definition per request: support defining them once
維護者通常 1 天內回覆
@Amogh887 已經在處理了。
開始於 2026年12月3日。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 45/100
- Issue 類型
- 功能
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- typescript
研究方向
Start by reading the stateless Streamable HTTP server examples, McpServer registration APIs, and the related issues #2838, #2839, and #2090. Compare reusable tool, resource, and prompt definitions with per-instance schema conversion, then identify which API design preserves callback type inference. Done means definitions can be reused across request-scoped servers, schema conversion is memoized as intended, and the stateless examples show the pattern.
由索引模型根據 Issue 內容生成。
描述
What happened?
With stateless Streamable HTTP, an McpServer instance can only serve one transport (connect() throws AlreadyConnected: "Use a separate Server instance per connection"), so the SDK's stateless examples and createMcpHandler take a factory, and a fresh instance is created for every request. The natural way to write that factory registers each tool inline:
function createServer() {
const server = new McpServer({ name: 'example', version: '1.0.0' }, { instructions: dedent`...` });
server.registerTool('get_item', { description: '...', inputSchema: z.object({ id: z.string().describe('...') }) }, async (args, ctx) => ...);
// ...
return server;
}
That rebuilds every zod schema, description and ResourceTemplate on every request, including ping and initialize. In our server (@modelcontextprotocol/server 2.4.0, zod 4.6.7, 17 small tools and 2 resource templates), createServer() took ~325 µs per call. Under load it was ~21% of the process's CPU, plus a share of GC (~15% of CPU).
Hoisting the definitions out of the factory brought createServer() to ~42 µs, cut the server's CPU per request by ~25–35%, and raised throughput by ~30–70% for light tools. But the SDK has no API for it. There's no tool or config type to define a tool with outside a server (we derived ours from Parameters<McpServer['registerTool']>), and no way to hand a server a set of definitions. So every app has to come up with its own pattern, and the examples steer them toward the slow one.
Even with hoisted schemas, every tools/list on a fresh instance converts each schema to JSON Schema again. Since #2889, conversion happens on demand per instance, and #2839 (memoizing the conversion per schema instance) was closed without being merged. For our 17 small schemas that's ~170 µs per tools/list, and #2838 measured ~18 ms for 53 tools of 25 fields each.
What did you expect?
Defining tools, resources and prompts once per process, and registering them cheaply on each per-request instance. For example:
- Memoize the Standard Schema → JSON Schema conversion per schema instance, as #2839 did, so a hoisted schema is converted once per process rather than once per
tools/list. - Reusable definitions: a way to define a tool, resource or prompt outside a server and register it on many. For example:
defineTool(name, config, cb)returning a definition thatserver.registerTool(definition)ornew McpServer(info, { tools: [...] })accepts, keeping the callback's argument inference from the schema;- or a registry object that a factory reuses, which
createMcpHandlercould take directly in place of a factory.
- Export the config types (
registerTool's config,registerResource's config) so apps can build their own definitions without deriving them from method parameters. - Show it in the docs: the stateless examples, and
createMcpHandler's docs, could show the definitions built once outside the factory.
Our workaround
export type Feature = (server: McpServer) => void;
type ToolConfig = Omit<Parameters<McpServer['registerTool']>[1], 'inputSchema' | 'outputSchema'>;
export function defineTool<InputArgs extends StandardSchemaWithJSON | undefined = undefined>(
name: string,
config: ToolConfig & { inputSchema?: InputArgs },
cb: ToolCallback<InputArgs>,
): Feature {
return (server) => {
server.registerTool(name, config, cb);
};
}
export function defineResourceTemplate(...args: Parameters<McpServer['registerResource']>): Feature {
return (server) => {
server.registerResource(...args);
};
}
// Defined once, at module level
const FEATURES: Feature[] = [defineTool('get_item', { inputSchema: z.object({ id: z.string() }) }, async (args, ctx) => ...)];
function createServer() {
const server = new McpServer(INFO, { instructions: INSTRUCTIONS });
for (const register of FEATURES) register(server);
return server;
}
SDK version
@modelcontextprotocol/server 2.4.0, zod 4.6.7
Area
Server
Related: #2838 (conversion at registration, fixed by #2889), #2839 (memoization, closed), #2090 (per-request allocation in stateless mode).
This issue was drafted with AI assistance (Claude Code).
- 主要語言
- TypeScript
- 星號
- 13.8k
- 分支
- 2.5k
- 平均合併
- 6 天 23 小時
- 30 天內合併 PR
- 2
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/typescript-sdk 的其他 Issue
-
v2
難度 1/5 1 小時以內 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 則留言 ·
維護者通常 1 天內回覆
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`可能已有人在做 @mgoldsborough 於 6 天前認領。 未關閉v2
難度 2/5 1-3 小時 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3249 ·
維護者通常 1 天內回覆
-
v2
難度 2/5 1-3 小時 新手友好度 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 則留言 ·
維護者通常 1 天內回覆
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` reply可能已有人在做 @Tidehyc 於 8 天前認領。 未關閉v2
難度 2/5 1-3 小時 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 則留言 ·
維護者通常 1 天內回覆
-
UriTemplate.match() throws a TypeError on an empty expression it accepts可能已有人在做 @yunusemrejs 於 12 天前認領。 未關閉v1 v2
難度 2/5 1-3 小時 新手友好度 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 則留言 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/typescript-sdk 的全部 Issue
相似的 Issue
-
triage
難度 2/5 1-3 小時 新手友好度 83/100
維護者通常 1 天內回覆
-
enhancement from-app needs-triage
難度 2/5 1-3 小時 新手友好度 68/100
nightly-labs/openbot#23943 ·
維護者通常 1 天內回覆
-
comp/desktop desktop/chat P3 type/bug
難度 2/5 1-3 小時 新手友好度 77/100
NousResearch/hermes-agent#164451 ·
維護者通常 1 天內回覆
-
shortkeys-2188:ready
難度 2/5 1-3 小時 新手友好度 82/100
crittermike/shortkeys#1121 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 67/100
Doist/todoist-cli#742 ·