Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

Claude Code MCP: build your own tiny MCP server in Python in 15 minutes

By Elena Novak, NBT Most 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 oth

By Elena Novak, NBT

Most 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.

This 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.

What we're building

A runbook server that exposes the Markdown files in a runbooks/ folder:

  • a tool list_runbooks() that returns the available runbook names
  • a tool read_runbook(name) that returns one runbook's text, and refuses paths outside the folder

It's read-only by design, which makes it easy to allow without prompts.

Step 1: set up the project

The SDK needs Python 3.11 or later. With uv:

uv init runbook-mcp
cd runbook-mcp
uv add "mcp[cli]"
mkdir runbooks
printf "# Restart the API\n\n1. Check the health endpoint.\n2. Run the deploy rollback.\n" > runbooks/restart-api.md

(pip install "mcp[cli]" works too.) The cli extra adds the mcp command, which we'll use to test.

Step 2: write the server

Create server.py:

import logging
from pathlib import Path

from mcp.server import MCPServer

logger = logging.getLogger(__name__)  # logs go to stderr

RUNBOOK_DIR = (Path(__file__).parent / "runbooks").resolve()

mcp = MCPServer("runbooks")


@mcp.tool()
def list_runbooks() -> list[str]:
    """List the names of all available runbooks."""
    return sorted(p.stem for p in RUNBOOK_DIR.glob("*.md"))


@mcp.tool()
def read_runbook(name: str) -> str:
    """Return the full text of one runbook, by name (without .md)."""
    path = (RUNBOOK_DIR / f"{name}.md").resolve()
    if RUNBOOK_DIR not in path.parents:
        raise ValueError("Runbook name must not contain a path.")
    if not path.exists():
        raise ValueError(f"No runbook named {name!r}. Call list_runbooks first.")
    logger.info("read_runbook %s", name)
    return path.read_text(encoding="utf-8")


if __name__ == "__main__":
    mcp.run(transport="stdio")

Some things worth noticing:

  • The type hints are the schema. name: str and -> list[str] become the tool's input and output definitions. You don't write any JSON Schema.
  • The docstring is the tool description. Claude reads it to decide when to call the tool, so write it for the model: what it does and when to use it.
  • Error messages give the next step. "Call list_runbooks first" tells Claude how to recover.
  • Never print() in a stdio server. The official guide is explicit: in a server that talks over stdio, anything written to stdout corrupts the protocol messages. Use logging, which writes to stderr.

Step 3: test it before Claude ever sees it

uv run mcp dev server.py

This 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.

Step 4: connect it to Claude Code

From your application's repository (not the server's folder), add the server. Everything after -- is the command that starts it:

claude mcp add runbooks -- uv --directory /ABSOLUTE/PATH/TO/runbook-mcp run server.py

Check that it connected:

claude mcp get runbooks
claude mcp list

claude mcp list shows a status next to each server, such as βœ” Connected or ✘ Failed to connect. Inside a session, /mcp shows the same.

Share it with the team

To commit the server configuration for everyone, use project scope:

claude mcp add --scope project runbooks -- uv --directory /ABSOLUTE/PATH/TO/runbook-mcp run server.py

This 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.

Step 5: allow it without prompts (it's read-only)

MCP tools are named mcp__<server>__<tool>. Because both tools only read, allow the whole server in .claude/settings.json:

{
  "permissions": {
    "allow": ["mcp__runbooks__*"]
  }
}

For a server with write tools, allow the read tools by full name (mcp__myserver__list_items) and leave the write tools to prompt.

Step 6: tell Claude when to use it

Add one line to CLAUDE.md or AGENTS.md:

- For incidents and operational tasks, check the `runbooks` MCP server (list_runbooks, read_runbook) before improvising steps.

Then try it:

The API is returning 502s. What does our runbook say?

Claude should call list_runbooks, then read_runbook("restart-api"), and answer from your text rather than from general knowledge.

Taking it further

Next step How
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)
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>
Add a write tool safely Keep it separate, validate every input, and leave it out of your allow list so it always prompts

Common mistakes

  • print() debugging in a stdio server. It breaks the connection in confusing ways. Use logging.
  • Relative paths in the launch command. Claude Code may start the server from a different directory. Use --directory with an absolute path, or an environment variable in .mcp.json as above.
  • Vague docstrings. "Gets data" gives Claude nothing to go on. Say what the tool returns and when to call it.
  • One giant tool. Several small, clearly named tools are easier for the model to use correctly than one tool with a mode parameter.

For background on adding and scoping existing servers (the other half of the story), see our guide to Claude Code MCP.

Disclosure: 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.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.