Build an MCP server in TypeScript
Build the same one-tool MCP server in TypeScript with the official MCP TypeScript SDK and Zod, then build it, test it with MCP Inspector and connect it to Claude Code. 9 min
Read
Set up the project
The MCP quickstart asks for Node.js 20 or later. It installs @modelcontextprotocol/server, plus zod to describe the tool's inputs, and compiles with TypeScript (checked 18 November 2026). Anthropic's own connector quickstart on claude.com uses the older @modelcontextprotocol/sdk package and an HTTP server instead. Both reach Claude, but follow one guide and do not mix imports from the two packages.
Set "type": "module" in package.json, add a build script, and add a tsconfig.json that compiles src into build.
mkdir opening-hours cd opening-hours npm init -y npm install @modelcontextprotocol/server zod npm install -D @types/node typescript mkdir src
{
"type": "module",
"scripts": {
"build": "tsc"
}
}{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"types": ["node"],
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}Lesson notes: Build an MCP server in TypeScript
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You have a working TypeScript MCP server with one tool that Claude can call.
Step 1 of 6 · Read
Set up the project
The MCP quickstart asks for Node.js 20 or later. It installs
@modelcontextprotocol/server, pluszodto describe the tool's inputs, and compiles with TypeScript (checked 18 November 2026). Anthropic's own connector quickstart on claude.com uses the older@modelcontextprotocol/sdkpackage and an HTTP server instead. Both reach Claude, but follow one guide and do not mix imports from the two packages.Set
"type": "module"inpackage.json, add a build script, and add atsconfig.jsonthat compilessrcintobuild.Install mkdir opening-hours cd opening-hours npm init -y npm install @modelcontextprotocol/server zod npm install -D @types/node typescript mkdir src
package.json (add these fields) { "type": "module", "scripts": { "build": "tsc" } }tsconfig.json { "compilerOptions": { "target": "ES2022", "module": "Node16", "moduleResolution": "Node16", "types": ["node"], "outDir": "./build", "rootDir": "./src", "strict": true, "esModuleInterop": true, "skipLibCheck": true }, "include": ["src/**/*"] }Step 2 of 6 · Read
The server
Save this as
src/index.ts.registerTooltakes the tool name, a description with a Zod input schema, and the function that runs. The.describe()text on each field tells Claude what to pass.The server is built inside
createServer()becauseserveStdiomay ask for a fresh instance more than once while it works out which protocol version the client speaks. Older tutorials connect withserver.connect(new StdioServerTransport()), which the quickstart says only serves clients still on the 2025 protocol. As in Python, neverconsole.log()in a stdio server: useconsole.error(), and returnisError: truewith a helpful message when something goes wrong.src/index.ts import { McpServer } from "@modelcontextprotocol/server"; import { serveStdio } from "@modelcontextprotocol/server/stdio"; import { z } from "zod"; const HOURS: Record<string, string> = { leeds: "Monday to Friday 9am to 5.30pm, Saturday 10am to 4pm", york: "Monday to Saturday 9am to 5pm, closed Sunday", }; function createServer() { const server = new McpServer({ name: "opening-hours", version: "1.0.0" }); server.registerTool( "get_opening_hours", { description: "Get the opening hours for one of our branches. Use this when someone asks when a branch is open. Returns regular weekly hours only, not bank holiday changes.", inputSchema: z.object({ branch: z.string().describe("Branch name, for example Leeds or York"), }), }, async ({ branch }) => { const hours = HOURS[branch.trim().toLowerCase()]; if (!hours) { const known = Object.keys(HOURS).sort().join(", "); return { content: [{ type: "text", text: `Unknown branch '${branch}'. Known branches: ${known}.` }], isError: true, }; } return { content: [{ type: "text", text: hours }] }; }, ); return server; } serveStdio(createServer, { onerror: (error) => console.error("opening-hours server error:", error), }); console.error("opening-hours MCP server running on stdio");Step 3 of 6 · Try it in the terminal
Build it
Compile the TypeScript into the build folder using the build script you added to package.json.
Hint: npm run, then the script name.
Show the command
$ npm run build > opening-hours@1.0.0 build > tsc
Step 4 of 6 · Try it in the terminal
List its tools with MCP Inspector
Use MCP Inspector in CLI mode to list the tools of the built server, which starts with: node build/index.js
Hint: npx @modelcontextprotocol/inspector --cli, then node build/index.js, then --method tools/list.
Show the command
$ npx @modelcontextprotocol/inspector --cli node build/index.js --method tools/list { "tools": [ { "name": "get_opening_hours", "description": "Get the opening hours for one of our branches. ...", "inputSchema": { "type": "object", "properties": { "branch": { "type": "string" } }, "required": ["branch"] } } ] }Step 5 of 6 · Read
Check both protocol versions, then connect
The Inspector defaults to the legacy protocol era, which only tests the way 2025 clients connect. The quickstart suggests listing the tools once with
--protocol-era modern(the stateless 2026-07-28 protocol) and once with--protocol-era legacy, so you know newer and older clients can both use your server.Then add it to Claude Code with an absolute path and run
/mcpto confirm it is connected. For the Claude desktop app, putnodeand the path tobuild/index.jsundermcpServersinclaude_desktop_config.json, then restart the app. Runnpm run buildagain after every change, or Claude keeps running the old code.Claude Code claude mcp add --transport stdio opening-hours \ -- node /absolute/path/to/opening-hours/build/index.js
Step 6 of 6 · Quiz
Check your understanding
Someone asks for the Harrogate branch, which is not in your list. What should the tool handler return?
- An empty string, so nothing breaks
- A thrown error with a stack trace
- isError: true with text naming the branches that do exist
Show the answer
isError: true with text naming the branches that do exist Claude learns the call failed and has the information to recover, for example by asking which branch the person meant.