TypeScript MCP Server Development
بناء خوادم MCP بلغة TypeScript.
Instructions to build Model Context Protocol (MCP) servers in TypeScript with the MCP TypeScript SDK 2.x
Works with
copilot (native)
Content
TypeScript MCP Server Development
These instructions apply to the MCP TypeScript SDK 2.x. SDK 2.4.x implements the MCP specification revision 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.
Instructions
- Use Node.js 22.12 or later on an LTS line (22, 24, or 26) with
"type": "module". Use atsconfig.jsonwithmodule: nodenextandtypes: ["node"]. - Install the SDK with
npm install @modelcontextprotocol/server zod@^4.2. The SDK does not support Zod 3. - Do not use the v1 package
@modelcontextprotocol/sdk. Runnpx @modelcontextprotocol/codemod@latest v1-to-v2 .when you update a server. Then search for@mcp-codemod-errormarkers. Read the upgrade guide for the rest. - Import from the package root:
McpServer,ResourceTemplate,completable,getDisplayName,createMcpHandler,inputRequired, andacceptedContentcome from@modelcontextprotocol/server. - Import
serveStdiofrom@modelcontextprotocol/server/stdio. ImporttoNodeHandlerfrom@modelcontextprotocol/node. ImportcreateMcpExpressAppfrom@modelcontextprotocol/express. - Use the
McpServerclass for most servers. Use theServerclass only for low-level control. Register handlers withserver.setRequestHandler('tools/call', async (request, ctx) => { ... }). - Write
import * as z from 'zod/v4'. GiveinputSchema,outputSchema, andargsSchemaasz.object({ ... })values. The raw shape form{ param: z.string() }is deprecated. - Always provide a
titlefor tools, resources, and prompts for better UI display. - Use
registerTool(),registerResource(), andregisterPrompt(). The variadictool(),resource(), andprompt()methods do not exist. - Make a server in a factory function. Pass the factory to
serveStdio(createServer)or tocreateMcpHandler(createServer). - For a local server, use
serveStdio(createServer). - For a remote server, use Streamable HTTP:
toNodeHandler(createMcpHandler(createServer)). For Express, attach it to the app thatcreateMcpExpressApp()returns. - An HTTP server is stateless.
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, noMcp-Session-Idheader, and noinitializehandshake on 2026-07-28. - Do not use
sessionIdGeneratororNodeStreamableHTTPServerTransportin a new server. The transport is only for 2025-era clients. createMcpHandlerdoes not check theHostandOriginheaders.createMcpExpressAppprotects the default127.0.0.1bind. When you bind to all interfaces, passallowedHostsandallowedOrigins. Do not useenableDnsRebindingProtection. It is deprecated.- Each Streamable HTTP POST request needs the
MCP-Protocol-VersionandMcp-Methodheaders. Atools/call,resources/read, orprompts/getrequest also needsMcp-Name.createMcpHandleranswers HTTP 400 with-32020when a header is missing or does not match the body. - Return both
content(for display) andstructuredContent(for structured data) from a tool that has anoutputSchema. - Use
ResourceTemplatefor dynamic resources with URI parameters:new ResourceTemplate('resource://{param}', { list: undefined }). registerResource()always takes a metadata object. Use{}when you have no metadata.- Support completions with
completable(schema, callback).optional(). Put.optional()on the outside. - Do not use
ctx.mcpReq.elicitInput(). It throws on a 2026-07-28 request. ReturninputRequired({ inputRequests: { id: inputRequired.elicit({ message, requestedSchema }) } }). On the retry, read the answer withacceptedContent(ctx.mcpReq.inputResponses, 'id', schema). Use the same zod schema that you gave toinputRequired.elicit. - For a flow in the browser, for example OAuth or a payment, use
inputRequired.elicitUrl(...). There is noelicitationId. UserequestStateto link the retries.requestStatecomes back from the client. Treat it as untrusted input. Create one codec at module scope, outside the factory, withcreateRequestStateCodec({ key })from@modelcontextprotocol/server. Load a key of at least 32 bytes from the configuration. Each server process must use the same key. Pass abindfunction that returns the user andctx.mcpReq.method. Every tool call has the methodtools/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. ReturnrequestState: await codec.mint(state, ctx)ininputRequired(...). SetrequestState: { verify: codec.verify }in theMcpServeroptions. Read the state withctx.mcpReq.requestState<T>(). The codec signs the state but does not encrypt it. Do not put secrets in the state. - Do not use sampling (
server.server.createMessage()). The revision 2026-07-28 deprecates it. Call the LLM provider API directly. If existing code must keep sampling, useinputRequired.createMessageon 2026-07-28 connections. - Do not use MCP logging (
ctx.mcpReq.log). The revision 2026-07-28 deprecates it. Log to stderr or use OpenTelemetry. - Do not use roots. The revision 2026-07-28 deprecates them. Get paths from tool arguments, resource URIs, or the server configuration.
- Enable notification debouncing for bulk updates:
new McpServer(info, { debouncedNotificationMethods: ['notifications/tools/list_changed'] }). - Dynamic updates: Change the tool, prompt, or resource set only when server-wide data changes, not as a side effect of a request. On stdio,
.enable(),.disable(),.update(), and.remove()on registered items send list change notifications. On HTTP, change the data that the factory reads, then callhandler.notify.toolsChanged(),promptsChanged(),resourcesChanged(), orresourceUpdated(uri)on the result ofcreateMcpHandler. - Use
getDisplayName()for UI display names. - Test a server with the SDK
Clientfrom@modelcontextprotocol/clientandStdioClientTransportfrom@modelcontextprotocol/client/stdio. - Test servers manually with the MCP Inspector:
npx @modelcontextprotocol/inspector.
Best Practices
- Keep tool implementations focused on single responsibilities.
- Provide clear, descriptive titles and descriptions that the LLM can understand.
- Add a
.describe(...)to each property ininputSchema. - Use proper TypeScript types for all parameters and return values.
- Return
isError: truein tool results for error conditions that the model must read. Do not throw for a tool failure. - Use async/await for all asynchronous operations.
- Close database connections and release resources properly.
- Validate input parameters before processing.
- On stdio servers, stdout is the protocol channel: log to stderr. MCP logging (
ctx.mcpReq.log) is deprecated (SEP-2577). Use stderr or OpenTelemetry. - Register tools, resources, and prompts inside the factory. Do not register them on a shared instance. The tool list must not vary per connection or as a side effect of other requests. It can vary by the authorization of the request. The SDK lists tools in a deterministic order.
- Cacheable results carry
ttlMsandcacheScope. The SDK usesttlMs: 0andcacheScope: 'private'by default. SetcacheHintsin the server options orcacheHintper resource. Do this only when the data is safe to cache. outputSchemaandinputSchemacan use any JSON Schema 2020-12 keyword.structuredContentcan be any JSON value.- Be careful when a tool gives access to the file system or the network.
- Use environment variables for configuration (for example ports and API keys).
- Document tool capabilities and limitations clearly.
Common Patterns
The snippets use import * as z from 'zod/v4'. Most of them show only the imports that they need.
Basic Server Setup (stdio)
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');
Tool with Structured Output
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 }
};
}
);
Error Handling
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.
Resources and Resource Templates
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}` }] };
}
);
Prompt with Completion
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.` }
}
]
})
);
Async Tool with Progress
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.
Streamable HTTP Server
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.
Integration Test
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 });
});Source and license
- Source
- GitHub Awesome Copilot
- Original
- github/awesome-copilot/instructions/typescript-mcp-server.instructions.md
- License
- MIT