If you prefer the Node.js ecosystem, the TypeScript SDK for MCP is excellent. It provides strong typing, which is helpful when defining the schemas for your tools.
Prerequisites
- Node.js 18+
npm install @modelcontextprotocol/sdk zod
The Code (index.ts)
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// Create server instance
const server = new McpServer({
name: "greeting-server",
version: "1.0.0",
});
// Define a tool
server.tool(
"greet_user",
"Greets a user by name",
{
name: z.string().describe("The name of the user"),
},
async ({ name }) => {
return {
content: [
{
type: "text",
text: `Hello, ${name}! Welcome to MCP.`,
},
],
};
},
);
// Connect via Stdio
const transport = new StdioServerTransport();
await server.connect(transport);
Key Concepts
- Zod Schemas: MCP uses JSON Schema to define tool arguments. The SDK uses Zod to make this easy. The description you add to the Zod schema is what the AI reads to understand what the argument expects.
- Transports: We use
StdioServerTransport. This means the server communicates by printing to stdout and reading from stdin. This is the standard for local Desktop apps.
Running It
- Compile:
tsc - Add to Config:
"greeting-server": { "command": "node", "args": ["/path/to/dist/index.js"] }
Why TypeScript?
For complex tools with many parameters, TypeScript’s type safety prevents runtime errors. Plus, if you are integrating with existing JS/TS libraries (like Drizzle ORM or Octokit), it’s the natural choice.