Building Zero-Dependency MCP Servers in Pure Python
A practical walkthrough for implementing Anthropic's Model Context Protocol from scratch using only the Python standard library, with no SDK or pip dependencies.
This piece walks through implementing Anthropic's Model Context Protocol (MCP) — the open standard connecting AI assistants to external tools and data — entirely from scratch using only Python's standard library, skipping the official SDK altogether. The official mcp package pulls in dependencies like pydantic, httpx, and anyio, ballooning to roughly 47 installed packages, which becomes impractical in sandboxed environments, air-gapped systems, strict security audits, or scenarios demanding single-file distribution. The article builds the JSON-RPC 2.0 stdio transport, NDJSON message framing, the initialize handshake, and the registration mechanics for tools, resources, and prompts, all in raw code.
For engineers, the value lies in understanding the protocol mechanics rather than treating the SDK as a black box, and in shrinking the supply-chain attack surface — the piece cites past CVEs in the official SDK (a path traversal and an SSRF flaw) as concrete motivation for minimizing dependencies. It also flags practical pitfalls, like how stray stdout logging can silently corrupt the protocol stream.
Overall, the guide serves as both an educational deep-dive for Python developers who want to truly understand MCP and a directly usable reference for teams needing to deploy MCP servers in constrained or dependency-restricted environments.