alpha

Instructs a coding agent how to build MCP servers with the TypeScript SDK 2.x: setup, imports, tools, transports and tests, and when not to apply it.
description: 'Instructions to build Model Context Protocol (MCP) servers in TypeScript with the MCP TypeScript SDK 2.x'
applyTo: '**/.ts, **/.js, **/package.json, **/tsconfig.json'
These instructions apply to the [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk) 2.x. SDK 2.4.x implements the MCP specification revision [2026-07-28](https://modelcontextprotocol.io/specification/2026-07-28). A client enables it with versionNegotiation. By default, a client still uses the 2025 initialize handshake.
Scope: Apply these instructions only to projects that use the MCP TypeScript SDK 2.x directly. Do not apply them to code that uses the v1 package
@modelcontextprotocol/sdk. Update that code first with the codemod. These instructions do not cover the Hono and Fastify adapters.
"type": "module". Use a tsconfig.json with module: nodenext and types: ["node"].npm install @modelcontextprotocol/server zod@^4.2. The SDK does not support Zod 3.@modelcontextprotocol/sdk. Run npx @modelcontextprotocol/codemod@latest v1-to-v2 . when you update a server. Then search for @mcp-codemod-error markers. Read the [upgrade guide](https://ts.sdk.modelcontextprotocol.io/v2/migration/upgrade-to-v2.html) for the rest.McpServer, ResourceTemplate, completable, getDisplayName, createMcpHandler, inputRequired, and acceptedContent come from @modelcontextprotocol/server.serveStdio from @modelcontextprotocol/server/stdio. Import toNodeHandler from @modelcontextprotocol/node. Import createMcpExpressApp from @modelcontextprotocol/express.McpServer class for most servers. Use the Server class only for low-level control. Register handlers with server.setRequestHandler('tools/call', async (request, ctx) => { ... }).import * as z from 'zod/v4'. Give inputSchema, outputSchema, and argsSchema as z.object({ ... }) values. The raw shape form { param: z.string() } is deprecated.title for tools, resources, and prompts for better UI display.registerTool(), registerResource(), and registerPrompt(). The variadic tool(), resource(), and prompt() methods do not exist.serveStdio(createServer) or to createMcpHandler(createServer).serveStdio(createServer).toNodeHandler(createMcpHandler(createServer)). For Express, attach it to the app that createMcpExpressApp() returns.createMcpHandler(factory) calls the factory for each request. Keep the factory cheap. Create database pools, API clients, and caches once at module scope. Use them in the factory. There are no sessions, no Mcp-Session-Id header, and no initialize handshake on 2026-07-28.sessionIdGenerator or NodeStreamableHTTPServerTransport in a new server. The transport is only for 2025-era clients.createMcpHandler does not check the Host and Origin headers. createMcpExpressApp protects the default 127.0.0.1 bind. When you bind to all interfaces, pass allowedHosts and allowedOrigins. Do not use enableDnsRebindingProtection. It is deprecated.MCP-Protocol-Version and Mcp-Method headers. A tools/call, resources/read, or prompts/get request also needs Mcp-Name. createMcpHandler answers HTTP 400 with -32020 when a header is missing or does not match the body.content (for display) and structuredContent (for structured data) from a tool that has an outputSchema.ResourceTemplate for dynamic resources with URI parameters: new ResourceTemplate('resource://{param}', { list: undefined }).registerResource() always takes a metadata object. Use {} when you have no metadata.completable(schema, callback).optional(). Put .optional() on the outside.ctx.mcpReq.elicitInput(). It throws on a 2026-07-28 request. Return inputRequired({ inputRequests: { id: inputRequired.elicit({ message, requestedSchema }) } }). On the retry, read the answer with acceptedContent(ctx.mcpReq.inputResponses, 'id', schema). Use the same zod schema that you gave to inputRequired.elicit.inputRequired.elicitUrl(...). There is no elicitationId. Use requestState to link the retries. requestState comes back from the client. Treat it as untrusted input. Create one codec at module scope, outside the factory, with createRequestStateCodec({ key }) from @modelcontextprotocol/server. Load a key of at least 32 bytes from the configuration. Each server process must use the same key. Pass a bind function that returns the user and ctx.mcpReq.method. Every tool call has the method tools/call. Thus, also put the tool name and the important arguments in the state. On the retry, reject the state if they do not match the call. Return requestState: await codec.mint(state, ctx) in inputRequired(...). Set requestState: { verify: codec.verify } in the McpServer options. Read the state with ctx.mcpReq.requestState<T>(). The codec signs the state but does not encrypt it. Do not put secrets in the state.server.server.createMessage()). The revision 2026-07-28 deprecates it. Call the LLM provider API directly. If existing code must keep sampling, use inputRequired.createMessage on 2026-07-28 connections.ctx.mcpReq.log). The revision 2026-07-28 deprecates it. Log to stderr or use OpenTelemetry.new McpServer(info, { debouncedNotificationMethods: ['notifications/tools/list_changed'] })..enable(), .disable(), .update(), and .remove() on registered items send list change notifications. On HTTP, change the data that the factory reads, then call handler.notify.toolsChanged(), promptsChanged(), resourcesChanged(), or resourceUpdated(uri) on the result of createMcpHandler.getDisplayName() for UI display names.Client from @modelcontextprotocol/client and StdioClientTransport from @modelcontextprotocol/client/stdio.npx @modelcontextprotocol/inspector..describe(...) to each property in inputSchema.isError: true in tool results for error conditions that the model must read. Do not throw for a tool failure.ctx.mcpReq.log) is deprecated (SEP-2577). Use stderr or OpenTelemetry.ttlMs and cacheScope. The SDK uses ttlMs: 0 and cacheScope: 'private' by default. Set cacheHints in the server options or cacheHint per resource. Do this only when the data is safe to cache.outputSchema and inputSchema can use any JSON Schema 2020-12 keyword. structuredContent can be any JSON value.The snippets use import * as z from 'zod/v4'. Most of them show only the imports that they need.
import { McpServer } from '@modelcontextprotocol/server';
import { serveStdio } from '@modelcontextprotocol/server/stdio';
import * as z from 'zod/v4';
function createServer(): McpServer {
const server = new McpServer({ name: 'my-server', version: '0.1.0' });
server.registerTool(
'add',
{
title: 'Add',
description: 'Add two numbers.',
inputSchema: z.object({
a: z.number().describe('First number'),
b: z.number().describe('Second number')
}),
outputSchema: z.object({ sum: z.number() })
},
async ({ a, b }) => {
const sum = a + b;
return {
content: [{ type: 'text', text: JSON.stringify({ sum }) }],
structuredContent: { sum }
};
}
);
return server;
}
void serveStdio(createServer);
console.error('my-server is running on stdio');
server.registerTool(
'word_count',
{
title: 'Word Count',
description: 'Count the words in a text.',
inputSchema: z.object({ text: z.string().describe('The text to count') }),
outputSchema: z.object({ words: z.number().int() }),
annotations: { readOnlyHint: true }
},
async ({ text }) => {
const words = text.trim() === '' ? 0 : text.trim().split(/\s+/).length;
return {
content: [{ type: 'text', text: JSON.stringify({ words }) }],
structuredContent: { words }
};
}
);
server.registerTool(
'divide',
{
title: 'Divide',
description: 'Divide a by b.',
inputSchema: z.object({ a: z.number(), b: z.number() })
},
async ({ a, b }) => {
if (b === 0) {
return { content: [{ type: 'text', text: 'b must not be zero.' }], isError: true };
}
return { content: [{ type: 'text', text: String(a / b) }] };
}
);
A tool handler produces only tool errors. The SDK converts an exception from a tool handler into a result with isError: true. Resource, prompt, and completion callbacks have no isError channel. They throw ProtocolError (from @modelcontextprotocol/server). The v2 error classes are ProtocolError, SdkError, and SdkHttpError (with .status).
A missing resource throws ResourceNotFoundError. The SDK answers with the code -32602. Custom error codes use a value outside -32768 to -32000. Do not use the legacy codes -32000 to -32019 in a new server. The specification reserves -32020 to -32099. UnsupportedProtocolVersionError (-32022) and MissingRequiredClientCapabilityError (-32021) are protocol errors that the SDK sends. They are not tool errors.
import { ResourceNotFoundError, ResourceTemplate } from '@modelcontextprotocol/server';
server.registerResource(
'config',
'config://app',
{ title: 'App Config', description: 'Application configuration', mimeType: 'application/json' },
async uri => ({
contents: [{ uri: uri.href, text: '{"debug":false}' }]
})
);
const users = new Map([['1', 'Ada']]);
server.registerResource(
'user',
new ResourceTemplate('users://{id}', { list: undefined }),
{ title: 'User', description: 'A user by id' },
async (uri, { id }) => {
const user = users.get(String(id));
if (user === undefined) throw new ResourceNotFoundError(uri.href);
return { contents: [{ uri: uri.href, text: `User ${user}` }] };
}
);
import { completable } from '@modelcontextprotocol/server';
const languages = ['typescript', 'javascript', 'python', 'rust', 'go'];
server.registerPrompt(
'code-review',
{
title: 'Code Review',
description: 'Review code in a language.',
argsSchema: z.object({
language: completable(z.string().describe('Programming language'), value =>
languages.filter(language => language.startsWith(value))
).optional()
})
},
({ language }) => ({
messages: [
{
role: 'user' as const,
content: { type: 'text' as const, text: `Review this ${language ?? 'source'} code.` }
}
]
})
);
server.registerTool(
'lookup',
{
title: 'Lookup',
description: 'Look up a query in a slow backend.',
inputSchema: z.object({ query: z.string() })
},
async ({ query }, ctx) => {
const progressToken = ctx.mcpReq._meta?.progressToken;
if (progressToken !== undefined) {
await ctx.mcpReq.notify({
method: 'notifications/progress',
params: { progressToken, progress: 1, total: 2, message: 'Halfway' }
});
}
const text = await slowLookup(query);
return { content: [{ type: 'text', text }] };
}
);
Pass ctx.mcpReq.signal to the backend call to stop the work when the client cancels the request.
import { createMcpExpressApp } from '@modelcontextprotocol/express';
import { toNodeHandler } from '@modelcontextprotocol/node';
import { createMcpHandler } from '@modelcontextprotocol/server';
const app = createMcpExpressApp();
const node = toNodeHandler(createMcpHandler(createServer));
app.all('/mcp', (req, res) => void node(req, res, req.body));
const port = Number(process.env.PORT ?? 3000);
app.listen(port, '127.0.0.1');
createMcpExpressApp parses the JSON body. Pass req.body as the third argument of the handler. createServer is the factory from the stdio example.
import { Client } from '@modelcontextprotocol/client';
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
import { afterAll, beforeAll, expect, test } from 'vitest';
let client: Client;
beforeAll(async () => {
client = new Client({ name: 'test-client', version: '1.0.0' }, { versionNegotiation: { mode: { pin: '2026-07-28' } } });
await client.connect(new StdioClientTransport({ command: process.execPath, args: ['dist/index.js'] }));
});
afterAll(async () => {
await client.close();
});
test('add', async () => {
const result = await client.callTool({ name: 'add', arguments: { a: 2, b: 3 } });
expect(result.structuredContent).toEqual({ sum: 5 });
});
© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com


Pink haired woman lounging on stadium steps
Creates a dreamy multi-shot music-video sequence of one pink-haired character in playgrounds and a stadium, with her look locked to a reference image.
video-prompt, music-video, multi-shot, character-consistency, reference-image, cinematic, photorealistic, camera-movement, fashion, natural-light, muted-colors, pastel, soft-lighting, dreamy, medium-shot, wide-shot, image-to-video, seedance, timed-beats, sports, long-form, structured-output, negative-prompt, vertical-format, widescreen

Every Friday, compiles a short digest from emails, Teams and calendar covering decisions made, commitments owed to the user, open items and Monday's first priorities.
scheduled-prompt, summarizer, action-items, briefing, productivity, time-management, email-triage, microsoft-teams, outlook, microsoft-365, for-managers, for-executives, guardrails, concise, structured-output, office-administration, quick, planner

Vintage Camera LCD Screen Shot
Generates a close-up of a glowing compact camera LCD showing a candid early-2010s flash photo of a laughing woman by a comic book shelf, with camera overlays.
image-prompt, gpt-image, photorealistic, smartphone-photo, candid, vintage, y2k, film-grain, close-up, night-scene, woman, portrait, text-rendering, books, bokeh, for-photographers, for-content-creators
Loading reviews…
Loading comments…