Issue 00 · Autumn 2026日本語 · RSS
AgentKoji

MCP Servers 101 and Five Design Rules

A minimal, tested MCP server, plus how to defend your tools against agent inputs that look nothing like human typos.

TL;DR

  • MCP is an open protocol connecting AI clients to tools and data; our minimal notes server rejected a ../../etc/passwd path with isError: true.
  • Five rules: one implementation behind CLI and MCP, accept JSON, expect agent-shaped mistakes, let callers shrink responses, and say when to use the tool in its description.
  • Always-loaded definitions cost context every turn: a local-model task took 24.6 s at 2,834 tokens per turn versus 4 min 35 s at 30,301 tokens.

MCP is an open protocol that connects AI clients to tools and data. Write one server and any MCP client can call it. Here is a minimal server that reads local notes, followed by five rules we learned running our own agent tools.

1. A minimal working server

npm i @modelcontextprotocol/server zod
import { McpServer } from '@modelcontextprotocol/server';
import { serveStdio } from '@modelcontextprotocol/server/stdio';
import { z } from 'zod';
import path from 'node:path';
import fs from 'node:fs/promises';

const ROOT = path.resolve(process.env.NOTES_DIR ?? './notes');

async function readNoteImpl({ name }) {
  if (!/^[\w.-]{1,64}$/.test(name) || name.includes('..')) throw new Error(`invalid name: ${JSON.stringify(name)}`);
  const p = path.resolve(ROOT, name + '.md');
  if (!p.startsWith(ROOT + path.sep)) throw new Error('path escapes root');
  return fs.readFile(p, 'utf8');
}

function createServer() {
  const server = new McpServer({ name: 'notes', version: '0.1.0' });
  server.registerTool('read_note', {
    description: 'Read one markdown note by name (without .md). Use when the user refers to a saved note.',
    inputSchema: z.object({ name: z.string().describe('note name, e.g. "deploy-checklist"') }),
  }, async (args) => ({ content: [{ type: 'text', text: await readNoteImpl(args) }] }));

  return server;
}

serveStdio(() => createServer());

This uses TypeScript SDK v2, the stable line, which implements the 2026-07-28 MCP spec. It needs Node 20+ and zod 4.2+. We start the server with serveStdio, which also serves clients on the 2026-07-28 spec.

In our test, read_note({name:"hello"}) returned the note and read_note({name:"../../etc/passwd"}) came back with isError: true. We re-checked this on v2 on 2026-11-02.

If you are on v1 (the single @modelcontextprotocol/sdk package), v1 and v2 can coexist, and the official codemod migrates your code: npx @modelcontextprotocol/codemod@latest v1-to-v2 .

Source: https://ts.sdk.modelcontextprotocol.io/v2/migration/upgrade-to-v2 (checked 2026-11-02)

2. Five rules

  1. Keep one implementation behind both your CLI and your MCP server. Generate schemas from your types and write tests once, against the implementation. Design for the agent first; the human CLI is a by-product.
  2. Accept JSON rather than piles of flags. Models write JSON well, and meaning gets lost when it is spread across many flags.
  3. Expect agent-shaped mistakes: path traversal (../../.ssh), queries embedded in IDs, double URL encoding, invisible control characters. Validate against an allowlist, and check that the resolved path is still inside the root.
  4. Let callers shrink responses with field masks or NDJSON. Every byte you return costs context.
  5. Say when to use the tool in its description. Put invariants in your skill docs. (See How to Create Claude Code Skills (SKILL.md).)

To limit what a tool can write, use a read-only wrapper: permission design.

3. Always-loaded MCP costs context on every turn

Demoting 15 skills nobody had called in 30 days cut always-loaded descriptions from 14.5k to 7.3k characters. With a local model, the same task took 24.6 s at 2,834 tokens per turn and 4 min 35 s at 30,301 tokens, mostly tool definitions.

Measurements from trimming always-loaded text: Trim Your CLAUDE.md.

4. When a deleted server comes back

“Resurrected” servers are usually leftover config: user-scope registration, per-project local settings, an enable-all-project-servers flag, or stale allow-list entries. And GUI-launched clients may not inherit your shell env, so ${API_KEY} may not expand to what you expect. In Claude Code, an unset ${VAR} without a default is passed as the literal ${VAR} text with a warning in claude mcp list, while credential names such as ANTHROPIC_API_KEY or NPM_TOKEN in a remote server’s url and headers read as empty with no warning (https://code.claude.com/docs/en/mcp, checked 2026-11-14). Log whether the key is present (never its value) at startup.

To build a server with the Python SDK and register it in Claude Code, see How to Build Your Own MCP Server. When choosing and installing an existing MCP server, see How to Choose MCP Servers for criteria and how to narrow its permissions.

FAQ

Which package do I install for an MCP server with TypeScript SDK v2?
Run npm i @modelcontextprotocol/server zod. v2 is the stable line, implements the 2026-07-28 MCP spec, and needs Node 20+ and zod 4.2+.
Does my v1 MCP server code keep working, and how do I migrate it?
v1 is the single @modelcontextprotocol/sdk package, and v1 and v2 can coexist. The official codemod migrates your code: npx @modelcontextprotocol/codemod@latest v1-to-v2 .
Why validate names inside the MCP tool?
Agents make mistakes that look nothing like human typos, such as path traversal (../../.ssh). Validate against an allowlist and check that the resolved path is still inside the root. Our server rejected ../../etc/passwd with isError: true.
Why should a tool description say when to use the tool?
That is rule 5: say when to use the tool in its description, as in our example, "Use when the user refers to a saved note." Put invariants in your skill docs.