
Turn Any Python Function Into a Tool Claude Code Can Call: Your First MCP Server in 15 Lines
Chris Harper
3 min read
Aug 27, 2026 · 04:01 UTC
TL;DR: The MCP Python SDK turns any typed Python function into a Claude-callable tool in 15 lines — decorators handle the JSON schema, the CLI handles the inspector, no protocol code needed.
What you'll be able to do after this: expose your own tools, APIs, or data sources to Claude Code (and any MCP host) so they're available as first-class tools in every session — without touching protocol or serialization code.
@mcp.tool()is all you write. Type hints become JSON schema automatically; the SDK handles request parsing, validation, and transport.mcp dev server.pylaunches a browser inspector so you can invoke your tools and inspect the protocol messages before connecting anything to Claude.- Use MCP when multiple agents or editors need the same capability. For a single session's private helper, a shell tool is simpler. MCP pays off when the tool is shared.
Minimal server (Python, 15 lines):
from mcp.server import MCPServer
mcp = MCPServer("my-tools")
@mcp.tool()
def search_docs(query: str, max_results: int = 10) -> list[dict]:
"""Search the internal docs API and return matching pages."""
return fetch_docs(query, limit=max_results)
if __name__ == "__main__":
mcp.run()
Steps:
- Install:
pip install "mcp[cli]"(Python 3.10+; oruv add "mcp[cli]") - Decorate each capability with
@mcp.tool(),@mcp.resource(), or@mcp.prompt() - Test:
mcp dev server.py— opens MCP Server Inspector in your browser; invoke tools and inspect JSON without connecting Claude - Wire to Claude Code — add to
~/.claude/settings.json:
{
"mcpServers": {
"my-tools": { "command": "python", "args": ["path/to/server.py"] }
}
}
Then run claude mcp login my-tools if the server requires authentication, and restart Claude Code.
Three things that don't work automatically:
- Authentication: MCP has no built-in auth for tool calls. Wire a shared secret or OAuth yourself, or use the
claude mcp logininteractive flow. - Error propagation: unhandled exceptions become opaque errors. Return a structured dict with an
errorkey for failures the model can read and act on. - Response size: the SDK serializes anything you return, but Claude Code enforces a per-tool result-size limit. Keep responses under ~50K characters or paginate.
The SDK is at v2, aligning with the 2026-07-28 MCP specification, which changes transports (Streamable HTTP is the new default; SSE still works). If you have a v1 server, check the migration guide — the API surface changed.
Start here: the official MCP quickstart at modelcontextprotocol.io builds a two-tool weather server end-to-end and connects it to Claude Desktop. Anthropic's free course Introduction to Model Context Protocol goes deeper — all three primitives (tools, resources, prompts) and a document management system you build and test with the MCP Server Inspector.
Sources: Build an MCP server — modelcontextprotocol.io · MCP Python SDK — GitHub · Introduction to MCP — Anthropic Skilljar · Step-by-step guide — Composio