Knowledge

Writing your first MCP server in TypeScript: two tools over stdio

Updated 2026-11-25 · Present

  • Build a small MCP server in TypeScript with two tools, add_note and list_notes, running over stdio.
  • Use the @modelcontextprotocol/server and zod packages to declare parameters; test with the MCP Inspector on the command line.
  • With a stdio server, never log with console.log, because stdout is reserved for JSON-RPC messages.

Goal

After this article you have a working MCP server with two tools: one saves a note and one lists the titles of saved notes. You also know how to test the server without an AI application. The article assumes you have read the introduction to what MCP is (host, client, server, tools, resources, prompts) and know a little TypeScript.

The notes live in the process memory, so they are lost when the server stops. The purpose of this article is to understand the MCP skeleton, not to build a storage system.

Preparation

node --version
npm --version

This article was tested with @modelcontextprotocol/server 2.4.0, zod 4.6.7, typescript 7.0.2 and @types/node 26.6.10. The SDK API is still changing, so if you install other versions and hit compile errors, compare with the "Build an MCP server" guide in the References.

Steps

Steps

Step 1: create the project

mkdir notes-mcp && cd notes-mcp
npm init -y
npm install @modelcontextprotocol/server zod
npm install -D typescript @types/node
mkdir src

Open package.json and add "type": "module" so Node treats the source as ES modules.

Create tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "types": ["node"],
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*"]
}

Step 2: write the server

Create src/index.ts:

import { McpServer } from "@modelcontextprotocol/server";
import { serveStdio } from "@modelcontextprotocol/server/stdio";
import { z } from "zod";

const notes = new Map<string, string>();

function createServer() {
  const server = new McpServer({ name: "notes", version: "1.0.0" });

  server.registerTool(
    "add_note",
    {
      description: "Save a short note under a title",
      inputSchema: z.object({
        title: z.string().min(1).max(80).describe("Note title"),
        text: z.string().max(2000).describe("Note text"),
      }),
    },
    async ({ title, text }) => {
      notes.set(title, text);
      return { content: [{ type: "text", text: `Saved note "${title}"` }] };
    },
  );

  server.registerTool(
    "list_notes",
    {
      description: "List the titles of all saved notes",
      inputSchema: z.object({}),
    },
    async () => ({
      content: [{ type: "text", text: [...notes.keys()].join("\n") || "No notes yet" }],
    }),
  );

  return server;
}

serveStdio(createServer, {
  onerror: (error) => console.error("notes server error:", error),
});
console.error("notes MCP server running on stdio");

A few points to understand:

Step 3: build

npx tsc

The command creates a build/ directory containing index.js.

Verification

The MCP Inspector has a command-line mode that lets you call the server without an AI application. The official documentation recommends testing in the modern "protocol era":

npx @modelcontextprotocol/inspector --cli node build/index.js --protocol-era modern --method tools/list

The output must list add_note and list_notes together with the JSON Schema of their parameters. The Inspector defaults to the legacy era, which only tries the old-style handshake, so run it once more with --protocol-era legacy: serveStdio is designed to serve both.

Call a tool:

npx @modelcontextprotocol/inspector --cli node build/index.js --protocol-era modern \
  --method tools/call --tool-name add_note --tool-arg title=demo --tool-arg text=hello

The result has a content containing the line Saved note "demo". Note that every Inspector command starts a new server process, so list_notes in the next command will report that there are no notes: data in memory does not survive between runs. For something more durable, write to a file or a database.

To use it in an AI application, you declare the start command (for example node with the absolute path to build/index.js) in that application's MCP configuration; the way to configure it differs between applications, so see its documentation.

Common errors

Summary

You built an MCP server with McpServer, registerTool, zod and serveStdio, then tested it with the Inspector. The real difficulty comes next: once a tool does real work, who may call it, with which parameters and whether a person must confirm. The next article answers that: Permissions and safety when letting AI call real tools through MCP.

References