Node.js MCP Server: Build Your First One Step by Step
Build a working Node.js MCP server in ten minutes. Real code, tested steps, and how to connect it to Claude Code. No computer science degree needed.
A Node.js MCP server is a small program that gives an AI app a tool it can call. You write it in TypeScript, run it with Node, and connect it to Claude Code or another MCP client. The smallest useful one is about 35 lines. This guide builds it step by step.
We built this server and tested it with the official Inspector before writing it up, using SDK version 2.4.0.
New to the idea? Read what is an MCP server first.
What you'll build
A server with one tool, work_out_quote. You give it hours and an hourly rate. It gives back a total. It's tiny on purpose. Once you can build one tool, you can build ten.
Before you start
You need Node.js 20 or newer, and a terminal. That's all. The SDK's package says it needs Node 20 or higher.
Step 1: Make the project
mkdir quote-helper && cd quote-helper
npm init -y
npm pkg set type=module
npm install @modelcontextprotocol/server@2.4.0 zod
npm install -D typescript @types/node
The TypeScript SDK now publishes split packages. @modelcontextprotocol/server is the one for building servers. Its repo says v2 is the stable release line, and that the older @modelcontextprotocol/sdk package (v1.x) stays on a branch that still gets bug fixes and security updates (MCP TypeScript SDK). If you see tutorials using the old package, they still work. New projects should start on v2.
Step 2: Add a TypeScript config
Create a file called tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"types": ["node"]
},
"include": ["src/**/*"]
}
Step 3: Write the server
Make a folder called src and a file inside it called index.ts:
import { McpServer } from "@modelcontextprotocol/server";
import { serveStdio } from "@modelcontextprotocol/server/stdio";
import { z } from "zod";
function createServer() {
const server = new McpServer({
name: "quote-helper",
version: "1.0.0",
});
server.registerTool(
"work_out_quote",
{
description: "Work out a quote total from hours and an hourly rate",
inputSchema: z.object({
hours: z.number().positive().describe("Hours of work"),
hourlyRate: z.number().positive().describe("Your hourly rate in dollars"),
}),
},
async ({ hours, hourlyRate }) => {
const total = Math.round(hours * hourlyRate * 100) / 100;
return {
content: [
{
type: "text",
text: `${hours} hours at ${hourlyRate.toFixed(2)} an hour is ${total.toFixed(2)}.`,
},
],
};
},
);
return server;
}
serveStdio(createServer, {
onerror: (error) => console.error("quote-helper error:", error),
});
console.error("quote-helper running on stdio");
Here's what each part does:
McpServeris the server. You give it a name and a version.registerTooladds one tool. You give it a name, a plain-English description, the inputs it takes, and a function that runs when the AI calls it.zodchecks the inputs. If the AI sends text where a number should be, it gets an error back.serveStdioruns the server over standard input and output. That's how a local MCP client talks to it.
One rule that trips everyone up
Never use console.log() in a stdio server. It writes to standard output, which is the channel the AI uses to talk to your server. A stray log line corrupts the messages and the connection breaks. The official MCP quickstart says the same. Use console.error() for logs, as the code above does (MCP build-a-server guide).
Step 4: Build it
npx tsc
This makes a build folder with index.js inside.
Step 5: Test it before you connect it
Run the server and talk to it with the official Inspector, a tool made for this job (MCP Inspector).
From your project folder, list the tools:
npx @modelcontextprotocol/inspector --cli node build/index.js --method tools/list
You should see work_out_quote listed, with its two inputs. Now call it:
npx @modelcontextprotocol/inspector --cli node build/index.js --method tools/call --tool-name work_out_quote --tool-arg hours=12 --tool-arg hourlyRate=150
You get back: "12 hours at $150.00 an hour is $1800.00." If you do, your server works.
Step 6: Connect it to Claude Code
Claude Code adds a local server with this pattern: claude mcp add --transport stdio <name> -- <command>. Everything after the double dash is the command that runs your server (Claude Code docs).
claude mcp add --transport stdio quote-helper -- node /full/path/to/quote-helper/build/index.js
Use the full path to index.js. Then start Claude Code and ask: "Use the quote helper to work out a quote for 12 hours at 150 an hour." Claude should call your tool.
Where to go from here
You've built a server that does one thing. Real ones usually do one of three jobs:
- Read data from a file, a database or an API.
- Do an action, like add a row or send a message.
- Wrap a rule only your business knows, like how you price a job.
Start with number 3. It's safe, it's useful and nothing can break. For the full picture, including HTTP servers, sign-in and deployment, read our guide to building an MCP server. New to Claude Code? Our Claude Code tutorial gets you set up. And MCP vs API explains when you need one. Before you build, check whether a ready-made one exists in the best MCP servers for founders.
Want a server built for you instead? Devwiz builds them as part of AI apps.
FAQ
What do I need to build an MCP server in Node.js?
Node.js 20 or newer, the @modelcontextprotocol/server package and zod for checking inputs. You write the server in TypeScript and run it with Node.
What is a stdio MCP server?
It's a server that talks to an AI app through standard input and output. It runs on your own computer. That makes it simple to build and test. For a server on the internet you'd use HTTP instead.
Which MCP SDK version should I use?
For a new project, v2 (@modelcontextprotocol/server). The v1 package, @modelcontextprotocol/sdk, still gets fixes. Our older guide uses it.
Your next step
Build the project above in a fresh folder today. Change the tool so it works out a price your way.