Overview
The TypeScript MCP SDK is the official library for building MCP servers in TypeScript. It supports tools, resources, prompts, transports, and the stdio and streamable-http transport.
Installation
npm install @modelcontextprotocol/sdkMinimal Server Example
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "my-server", version: "1.0.0" });
server.tool(
"add",
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
})
);
const transport = new StdioServerTransport();
await server.connect(transport);Adding a Tool
Tools are the primary way MCP servers expose functionality to clients. Each tool has a name, input schema, and handler function.
server.tool("get_weather", { city: z.string() }, async ({ city }) => {
const data = await fetchWeather(city);
return { content: [{ type: "text", text: JSON.stringify(data) }] };
});Supported Features
tools
resources
prompts
transports
Testing Your Server
- Start the server and verify it prints no startup errors.
- Run
npx @modelcontextprotocol/inspectorand connect over stdio. - Call your tools and verify they return the expected responses.
- Connect from Claude Desktop and confirm tool visibility.