Building MCP Servers with TypeScript SDK
When to use
Use this skill when you need to developing custom, production-ready MCP servers using the official @modelcontextprotocol/sdk TypeScript library.
Prerequisites
- Node.js 18+ or Python 3.10+ runtime.
- Deep familiarity with Model Context Protocol specification and JSON-RPC 2.0 messages.
Step-by-step workflow
- Analyze integration topology: Identify client host application requirements, transport mechanisms, and capability flags.
- Implement protocol contracts: Define standardized schemas, tool definitions, or transport adapters.
- Handle message serialization: Manage JSON-RPC 2.0 request/response parsing, errors, and cancellation signals.
- Enforce security controls: Validate input arguments, sanitize outputs, and apply least-privilege scoping.
- Verify with test client: Test communication flows using MCP Inspector or client configuration files.
Commands / code examples
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
const server = new Server({ name: "math-server", version: "1.0.0" }, { capabilities: { tools: {} } });
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [{
name: "add",
description: "Add two numbers",
inputSchema: {
type: "object",
properties: { a: { type: "number" }, b: { type: "number" } },
required: ["a", "b"]
}
}]
}));
server.setRequestHandler(CallToolRequestSchema, async (req) => {
if (req.params.name === "add") {
const { a, b } = req.params.arguments as { a: number; b: number };
return { content: [{ type: "text", text: String(a + b) }] };
}
throw new Error("Tool not found");
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main();
Common mistakes & pitfalls
- Exposing sensitive environment variables or private filesystem directories through overly permissive tool definitions.
- Forgetting to handle request cancellation tokens, leaving orphaned background tasks running.
- Failing to catch unhandled promise rejections inside tool handlers, crashing the stdio process.
Verification checklist
Related skills
- mcp-primitives-tools-resources-prompts
- mcp-security-prompt-injection-defense
- mcp-security-least-privilege-scoping
- mcp-inspector-testing-and-debugging
1---2name: mcp-server-building-typescript-sdk3description: Use this skill when developing custom, production-ready MCP servers using the official @modelcontextprotocol/sdk TypeScript library.4metadata:5 source: https://skillmd.com/skills/nextboxis/mcp-server-building-typescript-sdk6---78# Building MCP Servers with TypeScript SDK910## When to use11Use this skill when you need to developing custom, production-ready MCP servers using the official @modelcontextprotocol/sdk TypeScript library.1213## Prerequisites14- Node.js 18+ or Python 3.10+ runtime.15- Deep familiarity with Model Context Protocol specification and JSON-RPC 2.0 messages.1617## Step-by-step workflow181. **Analyze integration topology**: Identify client host application requirements, transport mechanisms, and capability flags.192. **Implement protocol contracts**: Define standardized schemas, tool definitions, or transport adapters.203. **Handle message serialization**: Manage JSON-RPC 2.0 request/response parsing, errors, and cancellation signals.214. **Enforce security controls**: Validate input arguments, sanitize outputs, and apply least-privilege scoping.225. **Verify with test client**: Test communication flows using MCP Inspector or client configuration files.2324## Commands / code examples25```ts26import { Server } from "@modelcontextprotocol/sdk/server/index.js";27import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";28import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";2930const server = new Server({ name: "math-server", version: "1.0.0" }, { capabilities: { tools: {} } });3132server.setRequestHandler(ListToolsRequestSchema, async () => ({33 tools: [{34 name: "add",35 description: "Add two numbers",36 inputSchema: {37 type: "object",38 properties: { a: { type: "number" }, b: { type: "number" } },39 required: ["a", "b"]40 }41 }]42}));4344server.setRequestHandler(CallToolRequestSchema, async (req) => {45 if (req.params.name === "add") {46 const { a, b } = req.params.arguments as { a: number; b: number };47 return { content: [{ type: "text", text: String(a + b) }] };48 }49 throw new Error("Tool not found");50});5152async function main() {53 const transport = new StdioServerTransport();54 await server.connect(transport);55}56main();57```5859## Common mistakes & pitfalls60- Exposing sensitive environment variables or private filesystem directories through overly permissive tool definitions.61- Forgetting to handle request cancellation tokens, leaving orphaned background tasks running.62- Failing to catch unhandled promise rejections inside tool handlers, crashing the stdio process.6364## Verification checklist65- [ ] Schema validation rejects malformed requests deterministically.66- [ ] Error responses return standard JSON-RPC 2.0 error codes and descriptions.67- [ ] Input parameters enforce strict bounds and sanitization.68- [ ] Server operates cleanly over target transport without unhandled exceptions.6970## Related skills71- mcp-primitives-tools-resources-prompts72- mcp-security-prompt-injection-defense73- mcp-security-least-privilege-scoping74- mcp-inspector-testing-and-debugging