DEV Community

Cover image for How to Build an MCP Server in 30 Lines of TypeScript
Mohsen Fallahnejad
Mohsen Fallahnejad

Posted on Originally published at bitlyst.blog

How to Build an MCP Server in 30 Lines of TypeScript

If you read what MCP is, you know it's the USB-C plug between AI apps and your tools. So how hard is it to build your own MCP server?

Honestly, it's much smaller than it sounds. You don't need a web framework or a big setup. A working MCP server fits in about 30 lines of TypeScript.

The whole idea in one picture

Think of an MCP server like a tiny coffee shop counter. You only need three things to open the window:

  1. A shop name (create the server).
  2. A menu (register your tools: what each one is called, what inputs it takes, and what it does).
  3. A counter window (connect a transport so the AI app can pass orders in and get answers back).

Building an MCP server is just three steps: create the server, register your tools with their input shapes, and connect a transport.

Once those three pieces are in place, Cursor, Claude Desktop, or VS Code can plug right in and call your code.

Step 1: Install the SDK

Start a folder and install the official TypeScript SDK plus zod (for describing tool inputs):

npm init -y
npm install @modelcontextprotocol/sdk zod
Enter fullscreen mode Exit fullscreen mode

Step 2: Write the server

Here is a complete local MCP server with one tool, word_count, that counts words in any text the AI sends it:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// 1. Open the shop
const server = new McpServer({ name: "my-tools", version: "1.0.0" });

// 2. Put a tool on the menu
server.registerTool(
  "word_count",
  {
    description: "Count the words in a piece of text",
    inputSchema: { text: z.string() },
  },
  async ({ text }) => {
    const count = text.trim().split(/\s+/).filter(Boolean).length;
    return { content: [{ type: "text", text: `${count} words` }] };
  }
);

// 3. Open the counter window (local stdio)
await server.connect(new StdioServerTransport());
Enter fullscreen mode Exit fullscreen mode

Look how plain that is. The SDK handles all the JSON-RPC wiring and discovery for you. When the AI app connects, it asks for the menu, sees word_count and its text input, and knows when to call it.

AI-generated illustration. Define the tool, describe its inputs, and connect the transport—the SDK handles the rest.

Step 3: Plug it into your AI editor

Point Cursor, Claude Desktop, or VS Code at your script in its MCP config:

{
  "mcpServers": {
    "my-tools": {
      "command": "npx",
      "args": ["tsx", "/absolute/path/to/server.ts"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Restart or reload your MCP servers, then ask your AI: "How many words are in this paragraph?" It will pick your word_count tool and run it.

In practice: the one bug everyone hits first

If you build a local (stdio) MCP server, remember one golden rule: never call console.log().

Why? Because stdio uses standard output (stdout) as the wire between your server and the AI app. One stray console.log("debug") breaks the JSON stream and crashes the connection. Log to console.error() instead—that goes to stderr, which stays out of the protocol's way.

Also, write clear description fields. The AI model reads those descriptions to decide when to call your tool. A vague description means the model either ignores your tool or calls it at the wrong time.

Say it in an interview

"Building an MCP server takes three steps with the official SDK: instantiate an McpServer, register your tools with a description, a schema for the inputs, and a handler function, then connect a transport—usually StdioServerTransport for local tools or Streamable HTTP for remote ones. The main gotcha with local servers is never writing logs to stdout, because stdout carries the JSON-RPC messages."

Cheat sheet

Step What you write
Create the server new McpServer({ name, version })
Add a tool server.registerTool(name, { description, inputSchema }, handler)
Connect locally await server.connect(new StdioServerTransport())
Log safely Use console.error(), never console.log()
Wire to editor Add command + args under mcpServers

Going deeper

Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

Testing without restarting your editor

Anthropic maintains an official MCP Inspector CLI that gives you a local web UI to list your tools and call them by hand:

npx @modelcontextprotocol/inspector npx tsx server.ts
Enter fullscreen mode Exit fullscreen mode

It's the fastest way to check your schemas and responses before plugging the server into Cursor or Claude.

When to switch from stdio to HTTP

stdio is great when the tool runs on your own laptop (reading local files, querying a local database, wrapping a CLI). When you want a whole team to use the same server without installing anything locally—like an internal docs search or a company API—host it behind Streamable HTTP with OAuth or a bearer token instead.

Further reading


Originally published on Bitlyst.

Top comments (0)