Home › MCP servers › MCP server (Python) CLAUDE.md

MCP server (Python) CLAUDE.md

CLAUDE.md for building Model Context Protocol servers in Python with the official SDK (FastMCP), tools, and tests.

Claude Code (CLAUDE.md)MCP servers

How to use

Save this as CLAUDE.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

CLAUDE.mdDownload  
# CLAUDE.md: MCP server (Python)

## Commands
- `uv sync` · `uv run pytest -q` · `uv run ruff check .`
- Try it: `uv run mcp dev server.py` (MCP Inspector) or `npx @modelcontextprotocol/inspector uv run server.py`

## Building blocks
- Server: `from mcp.server.fastmcp import FastMCP`; `mcp = FastMCP("name")`; run with `mcp.run()` (stdio) or the streamable HTTP transport for remote servers.
- Tools: `@mcp.tool()` on typed functions. The docstring is the tool description the model reads, so say what it does, when to use it, and what it returns. Use Pydantic models or `Annotated[..., Field(description=...)]` for arguments.
- Resources (`@mcp.resource("notes://{id}")`) for read-only data; prompts (`@mcp.prompt()`) for reusable templates.
- Return concise, structured results. Paginate or truncate large outputs and say so in the result.

## Rules
- Never print to stdout in a stdio server; it corrupts the protocol. Log to stderr or via `ctx.info()`.
- Validate inputs and return clear error messages the model can act on, rather than stack traces.
- Tool names are `snake_case` verbs (`search_issues`). Mark destructive tools clearly in the description and require explicit parameters (no "delete all" defaults).
- Secrets come from env vars passed by the client config; never hard-code them or echo them in results.
- Remote HTTP servers need auth (OAuth per the MCP spec) and must check the `Origin` header.

## Tests
Call tool functions directly in pytest, and add one in-memory client/server session test per tool.

More: all MCP servers rules · all Claude Code (CLAUDE.md) rules

Related rules

MCP server (TypeScript) AGENTS.md

AGENTS.md for building Model Context Protocol servers in TypeScript with clear tools, zod schemas and safe defaults.

AGENTS.mdMCP servers

Claude Agent SDK CLAUDE.md

CLAUDE.md for building agents with the Claude Agent SDK (Python/TypeScript): options, tools, permissions, and hooks.

Claude Code (CLAUDE.md)LLM apps & agents

Hugging Face Transformers CLAUDE.md

CLAUDE.md for Hugging Face Transformers and Datasets projects: model loading, devices, tokenization, inference, and safety.

Claude Code (CLAUDE.md)Hugging Face

Python data & ML CLAUDE.md

CLAUDE.md for data science / ML repos using pandas or polars, notebooks, and reproducible pipelines.

Claude Code (CLAUDE.md)Python Data & ML

PyTorch training CLAUDE.md

CLAUDE.md for PyTorch 2.x model training repos: reproducible configs, data loaders, mixed precision, and checkpoints.

Claude Code (CLAUDE.md)Python Data & ML

RAG pipeline CLAUDE.md

CLAUDE.md for retrieval-augmented generation apps: chunking, embeddings, hybrid search, citations, and RAG evals.

Claude Code (CLAUDE.md)RAG & vector DBs

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.