{"slug": "claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes", "title": "Claude Code MCP: build your own tiny MCP server in Python in 15 minutes", "summary": "A developer built a small read-only MCP server in Python using the official MCP Python SDK that exposes a folder of Markdown runbooks to Claude Code through two tools, list_runbooks and read_runbook, with path-traversal checks blocking access outside the directory. The server is tested in the MCP Inspector, connected to Claude Code via the claude mcp add command, and shared with teammates through a project-scoped .mcp.json that uses environment-variable expansion for per-user paths. Because both tools are read-only, the whole server can be allowlisted in .claude/settings.json without prompts.", "body_md": "*By Elena Novak, NBT*\n\nMost Claude Code MCP guides are about connecting other people's servers. Building your own is often more useful: a 30-line server can give Claude safe, structured access to the internal things it otherwise guesses about, such as your feature flags, your on-call runbook or a read-only view of a staging database.\n\nThis post builds a small but real server with the official MCP Python SDK, tests it, connects it to Claude Code and tightens its permissions. The SDK details come from the SDK's README and the \"Build an MCP server\" guide at modelcontextprotocol.io; the Claude Code details from code.claude.com. All were checked on 6 December 2026.\n\nA **runbook server** that exposes the Markdown files in a `runbooks/` folder:\n\n`list_runbooks()` that returns the available runbook names`read_runbook(name)` that returns one runbook's text, and refuses paths outside the folder\nIt's read-only by design, which makes it easy to allow without prompts.\n\nThe SDK needs Python 3.11 or later. With `uv`:\n\n```\nuv init runbook-mcp\ncd runbook-mcp\nuv add \"mcp[cli]\"\nmkdir runbooks\nprintf \"# Restart the API\\n\\n1. Check the health endpoint.\\n2. Run the deploy rollback.\\n\" > runbooks/restart-api.md\n```\n\n(`pip install \"mcp[cli]\"` works too.) The `cli` extra adds the `mcp` command, which we'll use to test.\n\nCreate `server.py`:\n\n``` python\nimport logging\nfrom pathlib import Path\n\nfrom mcp.server import MCPServer\n\nlogger = logging.getLogger(__name__)  # logs go to stderr\n\nRUNBOOK_DIR = (Path(__file__).parent / \"runbooks\").resolve()\n\nmcp = MCPServer(\"runbooks\")\n\n@mcp.tool()\ndef list_runbooks() -> list[str]:\n    \"\"\"List the names of all available runbooks.\"\"\"\n    return sorted(p.stem for p in RUNBOOK_DIR.glob(\"*.md\"))\n\n@mcp.tool()\ndef read_runbook(name: str) -> str:\n    \"\"\"Return the full text of one runbook, by name (without .md).\"\"\"\n    path = (RUNBOOK_DIR / f\"{name}.md\").resolve()\n    if RUNBOOK_DIR not in path.parents:\n        raise ValueError(\"Runbook name must not contain a path.\")\n    if not path.exists():\n        raise ValueError(f\"No runbook named {name!r}. Call list_runbooks first.\")\n    logger.info(\"read_runbook %s\", name)\n    return path.read_text(encoding=\"utf-8\")\n\nif __name__ == \"__main__\":\n    mcp.run(transport=\"stdio\")\n```\n\nSome things worth noticing:\n\n`name: str` and `-> list[str]` become the tool's input and output definitions. You don't write any JSON Schema.`print()` in a stdio server.`logging`, which writes to stderr.\n\n```\nuv run mcp dev server.py\n```\n\nThis opens the MCP Inspector, where you can call `list_runbooks` and `read_runbook` with test inputs. Try `read_runbook(\"../server\")` and confirm it's refused. It's much faster to fix a server here than by asking Claude why it's confused.\n\nFrom your application's repository (not the server's folder), add the server. Everything after `--` is the command that starts it:\n\n```\nclaude mcp add runbooks -- uv --directory /ABSOLUTE/PATH/TO/runbook-mcp run server.py\n```\n\nCheck that it connected:\n\n```\nclaude mcp get runbooks\nclaude mcp list\n```\n\n`claude mcp list` shows a status next to each server, such as `✔ Connected` or `✘ Failed to connect`. Inside a session, `/mcp` shows the same.\n\nTo commit the server configuration for everyone, use project scope:\n\n```\nclaude mcp add --scope project runbooks -- uv --directory /ABSOLUTE/PATH/TO/runbook-mcp run server.py\n```\n\nThis writes a `.mcp.json` in the repository. Since each teammate's path differs, edit the path in `.mcp.json` to `${RUNBOOK_MCP_DIR}`: Claude Code expands environment variables in `.mcp.json`, so each person sets their own (and `${VAR:-default}` supplies a fallback). Teammates are asked to approve project servers before they connect, which is the right default. Keep secrets out of `.mcp.json`. If your server needs a token, read it from an environment variable each person sets.\n\nMCP tools are named `mcp__<server>__<tool>`. Because both tools only read, allow the whole server in `.claude/settings.json`:\n\n```\n{\n  \"permissions\": {\n    \"allow\": [\"mcp__runbooks__*\"]\n  }\n}\n```\n\nFor a server with write tools, allow the read tools by full name (`mcp__myserver__list_items`) and leave the write tools to prompt.\n\nAdd one line to `CLAUDE.md` or `AGENTS.md`:\n\n```\n- For incidents and operational tasks, check the `runbooks` MCP server (list_runbooks, read_runbook) before improvising steps.\n```\n\nThen try it:\n\nThe API is returning 502s. What does our runbook say?\n\nClaude should call `list_runbooks`, then `read_runbook(\"restart-api\")`, and answer from your text rather than from general knowledge.\n\n| Next step | How | \n|---|---|\n| Expose data Claude can attach as context | `@mcp.resource(\"runbook://{name}\")` with a function that returns the text (the SDK's README shows the templated-resource pattern) | \n| Run it as a shared network service | Serve over Streamable HTTP: `uv run mcp run server.py --transport streamable-http` , then`claude mcp add --transport http runbooks <url>` | \n| Add a write tool safely | Keep it separate, validate every input, and leave it out of your allow list so it always prompts | \n\n`--directory` with an absolute path, or an environment variable in `.mcp.json` as above.`mode` parameter.\nFor background on adding and scoping existing servers (the other half of the story), see our guide to [Claude Code MCP](https://www.ninjabuilder.app/blog/claude-code-mcp).\n\nDisclosure: we make Ninja Builder, a desktop app that runs Claude Code. It can use MCP servers you build like this one, connected under Settings › MCPs. The steps above work in plain Claude Code too.", "url": "https://wpnews.pro/news/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes", "canonical_source": "https://dev.to/attilaalbert/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes-f2a", "published_at": "2026-12-06 03:06:29+00:00", "updated_at": "2026-12-06 03:14:32.997563+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "developer-tools", "ai-tools"], "entities": ["Claude Code", "Anthropic", "Model Context Protocol", "MCP Python SDK", "MCP Inspector", "uv", "Elena Novak"], "also_reported_by": [], "takeaway": "A ~30-line MCP server using the SDK's @mcp.tool decorator derives tool input and output schemas from Python type hints, and resolving paths with Path.resolve() plus a parents check blocks traversal like read_runbook(\"../server\").", "recommendation": {"advice": "Build read-only MCP tools with typed signatures so schemas are generated automatically, resolve and validate every path against a fixed directory, and log to stderr since print() breaks stdio transport.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes", "markdown": "https://wpnews.pro/news/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes.md", "text": "https://wpnews.pro/news/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes.txt", "jsonld": "https://wpnews.pro/news/claude-code-mcp-build-your-own-tiny-mcp-server-in-python-in-15-minutes.jsonld"}}