Build your first TypeScript MCP server

Register a typed get_note tool, expose it over stdio, and verify discovery and three result paths without a model.

Write the server

Use a Node.js 22 or later TypeScript project in ES module mode, with @modelcontextprotocol/server@2.4.0, @modelcontextprotocol/client@2.4.0 and Zod 4 installed. Compile the two src files in this lesson into dist. Save this first file as src/server.ts.

The SDK checks the argument schema before the handler runs. A correctly shaped but missing ID is a separate case that returns a tool error. The handler only reads from a fixed map.

Code
import { McpServer } from "@modelcontextprotocol/server";
import { serveStdio } from "@modelcontextprotocol/server/stdio";
import * as z from "zod/v4";

const notes = new Map([
  ["weekly-01", { title: "Weekly planning", status: "confirmed" }],
  ["retro-02", { title: "Sprint retrospective", status: "draft" }],
]);

function createServer() {
  const server = new McpServer({ name: "meeting-notes", version: "1.0.0" });
  server.registerTool(
    "get_note",
    {
      description: "Look up a sample meeting note by its ID.",
      inputSchema: z.object({ id: z.string().regex(/^[a-z]+-[0-9]{2}$/) }),
      annotations: { readOnlyHint: true },
    },
    async ({ id }) => {
      const note = notes.get(id);
      if (!note) {
        return { isError: true, content: [{ type: "text", text: "Unknown note ID." }] };
      }
      return { content: [{ type: "text", text: JSON.stringify({ id, ...note }) }] };
    },
  );
  return server;
}

void serveStdio(createServer);

Write a client that tests the contract

Save this as src/smoke.ts. It launches the compiled server in a child process using the same Node executable already running the test. The client checks the tool list and the responses directly. A chat assistant and API key are unnecessary.

Code
import assert from "node:assert/strict";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { fileURLToPath } from "node:url";

const client = new Client({ name: "notes-smoke", version: "1.0.0" });
try {
  await client.connect(new StdioClientTransport({
    command: process.execPath,
    args: [fileURLToPath(new URL("./server.js", import.meta.url))],
  }));

  const { tools } = await client.listTools();
  assert.ok(tools.some(({ name }) => name === "get_note"));
  const invoke = (id: string) => client.callTool({ name: "get_note", arguments: { id } });

  const known = await invoke("weekly-01");
  assert.notEqual(known.isError, true);
  const block = known.content.find((item) => item.type === "text");
  assert.ok(block && block.type === "text");
  assert.deepEqual(JSON.parse(block.text), {
    id: "weekly-01", title: "Weekly planning", status: "confirmed",
  });

  const missing = await invoke("missing-03");
  assert.equal(missing.isError, true);
  assert.ok(missing.content.some((item) => item.type === "text" && item.text === "Unknown note ID."));

  const invalid = await invoke("weekly");
  assert.equal(invalid.isError, true);
  console.log("Discovery, known ID, missing ID, and validation passed.");
} finally {
  await client.close();
}

Build and run the test

From the project directory, run the two commands below in order. The expected terminal line is Discovery, known ID, missing ID, and validation passed. If the test stops earlier, use the failing assertion to tell whether discovery or the call result differed.

Code
npm run build
npm run smoke

Keep connection ownership simple

When running the smoke test, let the client start the server. Starting dist/server.js by itself normally leaves it waiting for protocol input. Leave stdout to MCP and place any server diagnostics on stderr so they cannot be mistaken for protocol messages.

Official stdio server guide

Related reading

What Is MCP Server?

How to Setup MCP?

Adding Real Tools