Build an MCP Server in Python — Step by Step
Learn how to build an MCP server in Python using FastMCP. This tutorial covers installation, tool creation, resource handling, testing, and deployment with practical code examples.
Prerequisites
You need Python 3.10 or later and a basic understanding of async/await syntax. The FastMCP library handles all protocol details — JSON-RPC transport, capability negotiation, schema generation — so you can focus entirely on your tool logic. You should also have Claude Desktop or Claude Code installed to test your server.
The mcp package includes the FastMCP high-level framework, the low-level protocol implementation, and all transport handlers. FastMCP is the recommended way to build servers — it provides a decorator-based API similar to Flask or FastAPI that makes server creation straightforward.
Your First MCP Server
Create a file called server.py. The minimal MCP server needs three things: a Server instance, at least one tool function, and a way to run the server. Here is a complete working example that provides a word-counting tool:
FastMCP uses Python type hints and docstrings to automatically generate the JSON Schema that MCP clients need. The text: str parameter becomes a required string in the schema. The include_spaces: bool = True parameter becomes an optional boolean with a default value. The docstring becomes the tool description that the AI reads to understand when and how to use each tool.
Connecting to Claude Desktop
To test your server, add it to the Claude Desktop configuration file. The configuration tells Claude Desktop how to start your server process:
Restart Claude Desktop after saving the configuration. You should see the server name in the MCP tools panel. Now you can ask Claude to count words in any text and it will call your MCP server tool. If something goes wrong, check the Claude Desktop logs — on macOS they are in ~/Library/Logs/Claude/.
Adding Resources
Resources are read-only data sources that the AI can access. Unlike tools (which execute operations), resources provide context. For example, a configuration file, a database schema, or documentation that the AI should reference:
Resources are listed when the client connects and can be read at any time during the conversation. They are ideal for providing context that helps the AI make better tool calls — for instance, showing the database schema so the AI writes correct SQL queries.
Building a Practical Server: File Analyzer
Here is a more realistic example — an MCP server that analyzes files for AI-generated content markers. This demonstrates async operations, error handling, and returning structured data:
Error Handling Best Practices
MCP tools should never raise unhandled exceptions — the protocol does not define how clients should display raw tracebacks. Instead, return error information as structured text that the AI can interpret and communicate to the user. Validate inputs early, catch expected exceptions, and return clear error messages. If a tool depends on an external service, handle connection timeouts and authentication failures gracefully.
For tools that perform potentially dangerous operations (writing files, executing queries, sending messages), include confirmation mechanisms. You can require a confirm: bool parameter, return a preview before executing, or use the MCP prompts capability to guide the user through a multi-step workflow.
Testing Your Server
The MCP SDK includes a testing client you can use to verify your server without Claude Desktop:
Run this with python test_server.py to verify your tools work correctly before connecting them to an AI client. This is especially useful for CI/CD pipelines where you want automated testing of your MCP server.
Deploying to Production
For local use, running the server as a stdio process through Claude Desktop is sufficient. For team or production use, deploy as an SSE or streamable HTTP server. The FastMCP framework supports this natively:
For production deployments, add authentication (API keys or OAuth), rate limiting, logging, and monitoring. The MCP specification includes an authentication framework for remote servers. Container-based deployment (Docker) works well since MCP servers are typically lightweight single-process applications. Many teams deploy MCP servers alongside their existing API infrastructure on platforms like Railway, Fly.io, or AWS ECS.
The complete source code for dozens of reference MCP servers is available on GitHub under the modelcontextprotocol organization. Study the official filesystem, Git, and SQLite servers for production-quality patterns. Once your server is built, you can connect it to Claude Code for terminal-based workflows, or to Claude Desktop for a visual interface. For inspiration, explore our guides to production MCP servers for Notion, Supabase, and Figma.
Last updated: 2026 • Browse all courses