BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSllms.txtGitHubX
Posts/ai
AI-generated illustration. Three small pieces snapped together behind one standard plug.
Beginner4 min read

How to Build an MCP Server in 30 Lines of TypeScript

An MCP server is just a small program that lists tools and runs them when an AI asks. Build one in TypeScript in three steps and plug it into your editor.

Mohsen Fallahnejad

Mohsen Fallahnejad

Dec 2, 2026Discuss

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):

bash
npm init -y
npm install @modelcontextprotocol/sdk zod

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:

ts
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());

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.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:

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

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#

StepWhat you write
Create the servernew McpServer({ name, version })
Add a toolserver.registerTool(name, { description, inputSchema }, handler)
Connect locallyawait server.connect(new StdioServerTransport())
Log safelyUse console.error(), never console.log()
Wire to editorAdd command + args under mcpServers

Advanced · optional

Going deeper#

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:

bash
npx @modelcontextprotocol/inspector npx tsx server.ts

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#

  • Build an MCP Server (official guide)
  • MCP TypeScript SDK on GitHub
Filed under#ai#mcp#typescript#nodejs#tutorial

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderWhat Is MCP? The USB-C Port for AINewer →Is AI Really Faster and Cheaper for Building Products?

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

What Is MCP? The USB-C Port for AI

MCP (Model Context Protocol) is a standard plug between AI apps and your tools. Learn how clients, servers, and tools work—and how to explain it in an interview.

Dec 2, 20265 min read

Is AI Really Faster and Cheaper for Building Products?

AI makes the first 80% of a product wildly faster and cheaper—and can quietly make the last 20% slower if nobody owns the design. Here is the honest trade-off.

Dec 2, 20263 min read

AsyncLocalStorage: A Name Badge for Every Request

AsyncLocalStorage in Node.js gives each request its own data that follows it through every await. Learn how it works and how Next.js cookies() uses it.

Dec 1, 20264 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

4 min left
Discuss👍 React
👍