Executive Summary
Model Context Protocol (MCP) gives AI coding agents a standardized, type-safe interface to call local scripts, databases, and internal APIs. This guide walks through building a StdioServerTransport MCP server with Node.js 20+, TypeScript, @modelcontextprotocol/sdk (1.x), and zod, then wiring it into an MCP-compatible client.
Background and Problem Statement
Integrating AI coding agents with custom developer workflows used to require bespoke plugins for every IDE or CLI tool. Maintaining separate wrappers across multiple clients creates an N x M integration burden and fragments input validation.
MCP solves this by standardizing tool discovery and invocation over JSON-RPC 2.0. You implement the tool contract once on the server side, and any MCP-compatible agent can discover and call it through a single configuration entry.

Step-by-Step Implementation and Transport Comparison
1. Initialize the TypeScript Project
Create an ES module project on Node.js 20 or newer and install the official MCP SDK alongside zod for runtime argument validation.
# Create project directory and install dependencies
mkdir mcp-server-demo && cd mcp-server-demo
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript tsx @types/nodeConfigure package.json with "type": "module" and build scripts.
{
"name": "mcp-server-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"build": "tsc",
"start": "tsx src/index.ts"
}
}Because @modelcontextprotocol/sdk uses subpath imports with .js extensions, configure tsconfig.json with NodeNext module resolution.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}2. Implement the Server and Register a Tool
Create src/index.ts, instantiate McpServer, and register a tool with strict zod input descriptions so the model knows exactly what arguments to supply.
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// Initialize MCP server instance
const server = new McpServer({
name: "dev-utility-mcp",
version: "1.0.0",
});
// Register tool with Zod schema validation
server.tool(
"calculate-budget",
"Calculate combined monthly cloud cost from compute and storage estimates.",
{
compute: z.number().nonnegative().describe("Monthly compute cost in USD"),
storage: z.number().nonnegative().describe("Monthly storage cost in USD"),
},
async ({ compute, storage }) => {
const total = compute + storage;
return {
content: [
{
type: "text",
text: `Estimated monthly total: $${total.toFixed(2)}`,
},
],
};
}
);
async function main() {
// Bind server to standard I/O transport
const transport = new StdioServerTransport();
await server.connect(transport);
// Log exclusively to stderr to keep stdout clean for JSON-RPC
console.error("dev-utility-mcp server running on stdio");
}
main().catch((error) => {
console.error("Fatal error starting MCP server:", error);
process.exit(1);
});3. Build and Connect to an MCP Client
Compile the TypeScript source and register the absolute path to dist/index.js in your MCP client configuration (such as claude_desktop_config.json or your IDE’s MCP server settings).
npm run build{
"mcpServers": {
"dev-utility": {
"command": "node",
"args": ["/absolute/path/to/mcp-server-demo/dist/index.js"]
}
}
}4. Comparing Transport Options
Choose the transport layer based on whether the tool runs locally for a single developer or centrally for an entire team.
| Aspect | StdioServerTransport | Streamable HTTP (Remote) |
|---|---|---|
| Execution Host | Local machine (spawned child process) | Shared cloud or internal server |
| Auth Complexity | Low (inherits local user permissions) | High (requires OAuth or token auth) |
| Best Fit | Local file tasks, CLI wrappers, personal tools | Shared internal APIs, team knowledge bases |
| Latency | Minimal (local IPC) | Dependent on network round-trip |
Caveats and Troubleshooting
- Never write to
stdout:StdioServerTransportreservesstdoutexclusively for JSON-RPC frames. Any strayconsole.log()breaks the protocol parser; route all diagnostic output toconsole.error(). - Avoid relative paths: MCP clients launch servers from their own working directory, not your repository root. Always pass absolute paths in client config
argsand resolve file paths explicitly inside the server. - Write precise schema descriptions: AI agents rely on the tool description and
.describe()metadata to decide when and how to invoke a tool. Vague descriptions lead to malformed calls. - Enforce least privilege: If a tool writes files or mutates remote resources, validate allowed directories and enforce explicit safety checks inside the server handler rather than trusting prompt instructions alone.
Key Takeaways
- Combining
@modelcontextprotocol/sdkwithzodandNodeNextTypeScript settings delivers a reliable, type-safe local MCP server in under 50 lines of code. - Keep
stdoutstrictly clean for JSON-RPC messages by logging only tostderr, and register absolute paths to the compileddist/index.jsentry point. - Start with
StdioServerTransportfor local developer automation and migrate to Streamable HTTP only when you need centralized authentication and multi-user access.


