Build a Custom MCP Server in TypeScript
A minimal stdio MCP server using @modelcontextprotocol/sdk with a zod-validated tool.
Setup time20-30 minutes (estimate)
Estimated costFree (open-source SDK).
VerificationLast verified 2026-10-14
Install
npm init -y && npm i @modelcontextprotocol/sdk zod
npm i -D typescript @types/node && npx tsc --initSet "type": "module" in package.json.
src/index.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: "unit-converter", version: "1.0.0" });
server.registerTool(
"celsius_to_fahrenheit",
{
description: "Convert a temperature from Celsius to Fahrenheit",
inputSchema: { celsius: z.number() },
},
async ({ celsius }) => ({
content: [{ type: "text", text: String(celsius * 9 / 5 + 32) }],
})
);
await server.connect(new StdioServerTransport());Notes
- Never write logs to stdout in a stdio server; it corrupts the protocol stream. Use
console.error. - Test with
npx @modelcontextprotocol/inspector node build/index.js. server.tool(...)is deprecated in current SDK 1.x releases; useregisterTool(shown above).
Failure modes
- console.log to stdout corrupts the stdio stream; use console.error.
- Missing "type": "module" breaks the ESM imports.
- Deprecated server.tool() calls may break in a future major; use registerTool.
Markdown version: /blueprints/mcp-typescript-server.md
Want a deeper build of this?
Vote for an extended, fully tested version (more tools, a full workflow file). Votes decide what we build next.
Get one fully built blueprint a week
A weekly email: one blueprint with config, cost, setup time and failure modes, plus a vote on what we build next.
Related blueprints
MCP & Tool Use
Build a Custom MCP Server in Python (MCPServer, formerly FastMCP)
Expose your own functions as MCP tools and resources using the official Python SDK in about 20 lines.
MCP & Tool Use
GitHub MCP Server with a Scoped Token
Run GitHub's official MCP server in Docker with a fine-grained, least-privilege personal access token.
MCP & Tool Use
MCP Starter Config for Claude Desktop
A claude_desktop_config.json wiring up filesystem, fetch, and git MCP servers with scoped access.