[v2] Stateless servers rebuild every tool and resource definition per request: support defining them once
Les mainteneurs répondent en général sous 1 jour
@Amogh887 y travaille déjà.
Depuis le 3/12/2026.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 45/100
- Type d'issue
- Fonctionnalité
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- typescript
- Domaine
- backend, developer-experience, performance
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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).
- 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
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modelcontextprotocol/typescript-sdk
-
v2
Difficulté 1/5 Moins d'une heure Accessibilité débutants 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`Peut-être pris @mgoldsborough l’a pris il y a 6 jours. Ouvertev2
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
modelcontextprotocol/typescript-sdk#3249 ·
Les mainteneurs répondent en général sous 1 jour
-
v2
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` replyPeut-être pris @Tidehyc l’a pris il y a 8 jours. Ouvertev2
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
UriTemplate.match() throws a TypeError on an empty expression it acceptsPeut-être pris @yunusemrejs l’a pris il y a 12 jours. Ouvertev1 v2
Difficulté 2/5 1-3 heures Accessibilité débutants 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de modelcontextprotocol/typescript-sdk
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Vessel9817/onion-site-template#512 ·
Les mainteneurs répondent en général sous 1 jour
-
nightly-tests
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
BlackBeltTechnology/pi-agent-dashboard#1256 ·
Les mainteneurs répondent en général sous 1 jour
-
approved
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
fritzprix/libr-agent#3266 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
better-auth/better-auth#13101 · 1 commentaire · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour