Lesson 07.03 · Phase 07 · Model Context Protocol
Build an MCP server from scratch
An MCP server is a dispatcher with a contract: tools described by JSON Schema, arguments validated before your code runs, errors split into "tell the model" and "tell the client", and stdout kept clean. Build the small framework once and every server after it is just decorated functions.
Learning objectives
- Build a reusable server class: a @tool decorator, schemas generated from type hints, and handlers for server/discover, tools/list and tools/call.
- Validate arguments against the input schema before a handler runs, and return problems as a tool result the model can fix.
- Split failures correctly: tool errors (isError: true), unexpected crashes (generic message, traceback to stderr), and protocol errors (JSON-RPC error).
- Add tool annotations, structuredContent with an outputSchema, deterministic ordering and cursor pagination.
Lesson 2's server hard-coded two tools inside an if chain. A real server has a dozen tools, each with a schema someone has to keep in sync with the code, each needing its arguments checked, each able to fail in a different way. Write that by hand for every tool and three things go wrong:
Loading the full lesson…