- Understand what the protocol solves and what it does not
- Write a real MCP server in TypeScript
- Design a tool schema as a contract
- Limit permissions with annotations and path validation
- Test a tool without depending on the model
What the protocol solves
Before MCP, every agent host invented its own way to expose tools. Integrating the same system into three clients meant writing three adapters and maintaining three behaviours.
MCP standardises transport and contract: a server exposes tools, resources, and prompts; any compatible host discovers and calls them the same way. The server does not know which model uses it, and that is the point.
- Tools: actions with validated input.
- Resources: data the host may read.
- Prompts: reusable templates.
- Transport: stdio for local processes, HTTP for services.
The minimal server
A repository reader is enough to see the whole pattern: two tools, one schema per tool, and the stdio transport.
The current SDK is imported from @modelcontextprotocol/server; the 1.x line used @modelcontextprotocol/sdk. The code is the same with a different package.
import { McpServer } from "@modelcontextprotocol/server";
import { serveStdio } from "@modelcontextprotocol/server/stdio";
import { readFile } from "node:fs/promises";
import path from "node:path";
import * as z from "zod/v4";
const ROOT = process.cwd();
function resolveInsideRoot(relative: string) {
const absolute = path.resolve(ROOT, relative);
if (!absolute.startsWith(ROOT + path.sep)) {
throw new Error("Path outside the workspace");
}
return absolute;
}
const server = new McpServer({ name: "repo-reader", version: "1.0.0" });
server.registerTool(
"read_file",
{
title: "Read a file",
description:
"Read a UTF-8 text file from the workspace. Paths are relative to the workspace root.",
inputSchema: z.object({
path: z.string().describe("Repository-relative path, e.g. src/app/page.tsx"),
maxBytes: z.number().int().min(1).max(200_000).optional(),
}),
annotations: { readOnlyHint: true, openWorldHint: false },
},
async ({ path: relative, maxBytes = 20_000 }) => {
try {
const text = await readFile(resolveInsideRoot(relative), "utf8");
return {
content: [{ type: "text", text: text.slice(0, maxBytes) }],
};
} catch (error) {
return {
isError: true,
content: [
{
type: "text",
text: "Could not read " + relative + ": " + (error as Error).message,
},
],
};
}
},
);
serveStdio(() => server);The schema is the contract
You write one schema with Zod. The SDK derives the JSON Schema the model sees, validates arguments before your handler runs, and infers the TypeScript types.
If the model invents a field or sends an out-of-range number, the call never reaches your code. That is what separates a tool from an open endpoint.
- Every ambiguous field carries describe(): the model reads it.
- Limits are part of the design, not an optimisation.
- If a tool needs no arguments, omit inputSchema entirely.
Permissions: what the agent cannot do
The first decision is not which tool to add, it is what it will not be able to touch. A path resolver that fails outside the workspace turns a prompt bug into a controlled error.
Annotations do not block anything by themselves: they tell the host what to expect. readOnlyHint allows silent confirmations; destructiveHint forces a prompt. Real safety lives in the code and in the process scope.
function resolveInsideRoot(relative: string) {
const absolute = path.resolve(ROOT, relative);
if (!absolute.startsWith(ROOT + path.sep)) {
throw new Error("Path outside the workspace");
}
return absolute;
}
// A tool error is not a crash: the model can read it and correct course.
return {
isError: true,
content: [{ type: "text", text: "File not found: " + relative }],
};Consuming and testing it
Any compatible host is configured with the command and its arguments. To debug, the MCP Inspector lists the discovered tools and lets you invoke them by hand.
Most importantly: the handler is a function. It is tested without a model, without the protocol, and without network.
{
"mcpServers": {
"repo-reader": {
"command": "node",
"args": ["/absolute/path/to/dist/index.js"]
}
}
}Layout-level composition. Owns the document structure and orchestrates which features render.
A tool that cannot break anything
Write a read-only MCP tool for your project (list files, read a file, or search text). Add the path guard, the annotations, and a test that verifies a path containing ../ is rejected.
Show a hint
If your tool can write, it is not read-only: do not add readOnlyHint to make it look better.