TypeScript Server

Building a Production MCP Server in TypeScript

Let's build a more complete MCP server — a note-taking server that Claude can use to create, read, update, and delete notes. This covers all the patterns you'll use in real projects.
typescript
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

interface Note { id: string; title: string; content: string; createdAt: string; }
const notes = new Map();

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

server.tool("create_note", "Create a new note", {
  title: z.string().describe("Note title"),
  content: z.string().describe("Note content"),
}, async ({ title, content }) => {
  const id = crypto.randomUUID();
  const note: Note = { id, title, content, createdAt: new Date().toISOString() };
  notes.set(id, note);
  return { content: [{ type: "text", text: `Created note "${title}" (ID: ${id})` }] };
});

server.tool("list_notes", "List all notes", {}, async () => {
  const all = Array.from(notes.values());
  if (all.length === 0) return { content: [{ type: "text", text: "No notes yet." }] };
  const list = all.map(n => `- ${n.title} (ID: ${n.id})`).join("\n");
  return { content: [{ type: "text", text: list }] };
});

server.tool("read_note", "Read a specific note", {
  id: z.string().describe("Note ID"),
}, async ({ id }) => {
  const note = notes.get(id);
  if (!note) return { isError: true, content: [{ type: "text", text: "Note not found" }] };
  return { content: [{ type: "text", text: `# ${note.title}\n\n${note.content}` }] };
});

server.tool("delete_note", "Delete a note", {
  id: z.string().describe("Note ID"),
}, async ({ id }) => {
  if (!notes.delete(id)) return { isError: true, content: [{ type: "text", text: "Note not found" }] };
  return { content: [{ type: "text", text: "Note deleted." }] };
});

const transport = new StdioServerTransport();
await server.connect(transport);

Tip:This pattern — a Map for storage + CRUD tools — is the foundation for most MCP servers. Replace the Map with a real database for production use.

PreviewTypeScriptRead Only
Copy this code and run it locally with npx tsx server.ts

💬 Got questions? Ask me anything!