Most “hello world” MCP demos stop at a single echo tool. Production-shaped work starts earlier: clear capability boundaries, stable schemas, honest error messages, and a host connection you can debug without guessing. This page is a working MCP server tutorial for that path.
01
What MCP is (and is not)
The Model Context Protocol (MCP) is a standard way for an AI host (an IDE agent, a desktop app, or your own runtime) to discover and call external capabilities. Your server advertises tools, resources, and optionally prompts; the host decides when to invoke them.
- Tools are actions with typed inputs and outputs (search, create issue, run a query).
- Resources are readable context (files, docs, snapshots) the host may fetch by URI.
- Prompts are reusable message templates the host can offer to users.
When the minimal server works, graduate to the MCP structured content · MCP server best practices checklist (naming, least privilege, idempotency, ops)—this tutorial stays on first connection. For a broader coding agent workflow—state, eval, recovery, and delivery—see the agent engineering series.
02
Minimal server steps
The smallest useful server exposes one tool with a strict schema and fails loudly on bad input. Below is a TypeScript-shaped sketch using the common SDK pattern; adapt package names to the SDK version you pin.
- Create a package, pin an MCP SDK, and choose a transport (stdio for local hosts, Streamable HTTP for remote).
- Register at least one tool with name, description, and JSON Schema for arguments.
- Implement the handler: validate → authorize → execute → return structured content.
- Wire the transport and start the process so a host can spawn or connect to it.
- Smoke-test with a host or inspector before writing more tools.
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "notes-search",
version: "0.1.0",
});
server.tool(
"search_notes",
{
query: z.string().min(1).max(200),
limit: z.number().int().min(1).max(20).default(5),
},
async ({ query, limit }) => {
const hits = await searchNotes(query, limit); // your code
return {
content: [{ type: "text", text: JSON.stringify(hits, null, 2) }],
};
},
);
const transport = new StdioServerTransport();
await server.connect(transport);Prefer returning compact, structured text or typed content the host can reason over. Dumping megabytes of logs into the model context is a common early mistake.
03
Tools, resources, and prompts
Tools
Write tool descriptions as contracts for a model, not as marketing copy. State side effects, required permissions, and what “success” means. Keep argument names stable; renaming a field breaks every prompt that learned the old shape.
Resources
Use resources when the host should pull context on demand instead of stuffing everything into every tool response. Give URIs a clear scheme (notes://id/…) and document list vs read behavior.
Prompts
Prompt templates help hosts expose repeatable workflows (“summarize this ticket with acceptance criteria”). Keep them short and parameterize the variable parts; do not hide business rules only inside a prompt string.
04
Connect to a host
Local hosts usually spawn your server over stdio and pass environment variables for secrets. Remote or multi-user setups typically use an HTTP transport with authentication in front of the process.
{
"mcpServers": {
"notes-search": {
"command": "node",
"args": ["./dist/server.js"],
"env": {
"NOTES_API_TOKEN": "…from a secret store, not the repo…"
}
}
}
}After connecting, verify three things before writing more features: the host lists your tools, a happy-path call returns expected content, and an invalid argument returns a clear error instead of a hang.
05
Common pitfalls
- Unbounded tools: missing max lengths, page sizes, or timeouts lets one call dominate context and cost.
- Secrets in argv or logs: pass tokens via env or a secret manager; never print them in tool output.
- Over-trusting the model: validate every argument server-side; treat tool calls as untrusted input.
- Chatty responses: return the fields the next step needs, not raw upstream payloads.
- No idempotency: create/update tools need stable keys so retries do not duplicate work.
- stdio + extra logging: writing to stdout breaks the protocol; log to stderr or a file.
- Version drift: pin SDK and protocol versions; document breaking schema changes.
06
Ship checklist
- One tool with a strict schema and a real backend call.
- Authz check that rejects cross-tenant IDs.
- Timeouts, size limits, and structured errors.
- Host config checked into a private ops doc, not a public gist with secrets.
- A short fixture set you re-run after SDK upgrades.
Related reading: MCP server best practices · MCP tool annotations · MCP sampling vs elicitation · mcp.json example · MCP vs function calling · When to use MCP · MCP stdio vs Streamable HTTP · MCP resources vs tools · Playwright MCP vs Chrome DevTools MCP · How to write a good agent prompt · Cursor agent tips · Cursor rules examples · How to write CLAUDE.md · Agent engineering series
07
FAQ
What is an MCP server?
An MCP server is a process that advertises tools, resources, and optionally prompts to an AI host over the Model Context Protocol. The host discovers those capabilities and calls them; your server validates, authorizes, and executes within hard limits.
Do I need HTTP for a first MCP server tutorial project?
No. Most local hosts spawn a server over stdio. Use an HTTP transport when you need remote or multi-user access, with auth in front of the process.
How is MCP different from a normal API?
A product API is for your application. MCP is a controlled adapter so a model can discover and invoke a small set of capabilities with typed schemas. It does not replace auth, job queues, or your public API surface.
What should I read after this MCP server tutorial?
Harden design and ops with MCP server best practices, decide MCP resources vs tools when to expose context versus actions, compare browser MCPs with Playwright MCP vs Chrome DevTools MCP, write clearer tool-calling briefs with how to write a good agent prompt, apply editor habits from cursor agent tips, encode durable contracts with cursor rules examples or how to write CLAUDE.md, and map delivery work with the agent engineering series.