The official TypeScript SDK is the reference implementation for Node.js MCP servers. This guide creates a small stdio server with one typed tool and a repeatable build. It deliberately avoids framework-specific shortcuts so the structure can grow into a real project.
1. Create the package
mkdir repo-audit-mcp && cd repo-audit-mcp
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript tsx @types/node
Set "type": "module" in package.json and add scripts:
{
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "node dist/server.js",
"dev": "tsx src/server.ts"
}
}
A minimal tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"outDir": "dist",
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"]
}
Pin the SDK version in the lockfile. The MCP specification and SDK surface change between releases; a working example is tied to a version, not to “latest” forever.
2. Create the server
src/server.ts:
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: "repo-audit-mcp",
version: "0.1.0",
});
server.registerTool(
"count_lines",
{
title: "Count lines in a text snippet",
description:
"Counts the number of lines in a text snippet. Use this for quick references; it does not read files.",
inputSchema: {
text: z.string().min(1).describe("Text to count. Newlines separate lines."),
},
annotations: {
readOnlyHint: true,
openWorldHint: false,
},
},
async ({ text }) => {
const lines = text.split(/\r?\n/).length;
return {
content: [{ type: "text", text: `${lines} line(s)` }],
};
},
);
const transport = new StdioServerTransport();
await server.connect(transport);
The inputSchema is a raw shape of Zod validators. The SDK converts it to JSON Schema for tool discovery, and the tool callback receives parsed arguments. Field descriptions matter: they are part of the contract the model reads.
3. Build and run it
npm run build
node dist/server.js
A stdio server waits for input; it will look like it hangs. That is expected. Do not add startup banners or console logs on stdout. For diagnostics, write to stderr:
console.error("repo-audit-mcp started");
Test the server with MCP Inspector rather than guessing at JSON:
npx @modelcontextprotocol/inspector node dist/server.js
Inspector shows the tool schema, lets you invoke it, and displays protocol errors.
4. Connect a client
For Cursor, create .cursor/mcp.json:
{
"mcpServers": {
"repo-audit": {
"type": "stdio",
"command": "node",
"args": ["/absolute/path/to/repo-audit-mcp/dist/server.js"]
}
}
}
For Codex:
[mcp_servers.repo-audit]
command = "node"
args = ["/absolute/path/to/repo-audit-mcp/dist/server.js"]
Use absolute paths in client configuration. The client may launch the server from a different working directory than your terminal. If the client cannot find node, use the absolute path reported by which node or where node.
5. Verify, then extend
The release gate for this setup is simple:
- The client lists the server.
count_linesappears with a required string field.- A call with two lines returns
2 line(s). - An empty string is rejected by the schema.
- The process stays alive between calls.
Next, move from a toy tool to a defensible one: TypeScript tool schemas covers enums, pagination, structured errors, and annotations. If the server needs to run remotely, see Streamable HTTP and the production checklist.
Sources
Sources were checked on Oct 23, 2026. Versions and client behavior can change; confirm the current documentation before relying on a configuration.