Build an MCP Server with Python
Build a production-ready MCP server in Python using the FastMCP framework with decorator-based tool registration
MCPgee Team
MCP Expert
Build an MCP Server with Python
Introduction
Python is one of the most popular languages for AI development, and building MCP servers in Python is straightforward thanks to the FastMCP framework. In this tutorial, you will learn how to create a fully functional MCP server using Python's decorator-based approach, which makes defining tools, resources, and prompts intuitive and clean.
If you have not already read the basics, start with our What is MCP tutorial first. For the TypeScript approach, see our first MCP server tutorial.
Prerequisites
Before starting, make sure you have:
- Python 3.10+ installed (check with
python --version) - pip or uv package manager
- A code editor (VS Code recommended)
- Basic familiarity with Python decorators
Setting Up Your Project
Step 1: Create the Project Structure
Step 2: Install Dependencies
Or if you prefer uv:
Building Your First FastMCP Server
The FastMCP Framework
FastMCP is the high-level Python framework for building MCP servers. It uses decorators to register tools, resources, and prompts, making your code clean and declarative.
Important: Always import frommcp.server.fastmcp, not from mcp.server. The FastMCP class is the recommended high-level API.
Defining Tools with Decorators
Tools are functions that AI clients can invoke. Use the @mcp.tool() decorator:
FastMCP automatically extracts:
- The function name as the tool name
- The docstring as the tool description
- Type hints and docstring Args as the input schema
Defining Resources
Resources represent data that AI clients can read. Use the @mcp.resource() decorator:
Resource URIs can include template parameters (like {user_id}) for dynamic content.
Defining Prompts
Prompts are reusable templates that help AI clients structure their interactions:
Building a Real-World Example: File Manager Server
Let us build a practical MCP server that provides file management capabilities:
Async Tools and Context
FastMCP supports async functions and provides a context object for advanced features:
The Context parameter is automatically injected by FastMCP when included as a type hint. It provides:
ctx.info(),ctx.warning(),ctx.error()for loggingctx.report_progress()for progress updates- Access to request metadata
Error Handling and Validation
Robust error handling is essential for production MCP servers:
Running Your Server
stdio Transport (Default)
The simplest way to run your server for local use with clients like Claude Desktop:
This starts the server using stdio transport, which is ideal for local integrations.
Configuring Claude Desktop
Add your server to Claude Desktop's configuration file:
For more on connecting to different clients, see our Claude integration tutorial and our clients directory.
Streamable HTTP Transport
For remote deployments, use Streamable HTTP transport instead of the deprecated SSE:
Learn more about remote deployment and transports in our advanced MCP tutorial.
Testing Your Server
Manual Testing with MCP Inspector
The MCP Inspector is a helpful tool for testing servers:
This opens a web UI where you can:
- Browse available tools and resources
- Execute tools with sample inputs
- View responses and debug issues
Unit Testing
Write unit tests for your tool functions:
Project Structure for Larger Servers
For more complex servers, organize your code into modules:
Deployment Options
Once your server is working locally, you can deploy it in several ways:
- Docker containers for consistent environments
- Kubernetes for orchestration at scale
- AWS Lambda for serverless deployments
For security best practices when deploying, read our security fundamentals tutorial.
Conclusion
You have built a fully functional MCP server in Python using FastMCP. The decorator-based approach makes it easy to define tools, resources, and prompts with minimal boilerplate. FastMCP handles protocol serialization, transport management, and schema generation automatically.
For more detailed guidance, check out our blog post on building Python MCP servers and explore the advanced MCP patterns tutorial for streaming, authentication, and more.
Code Examples
Key Takeaways
- Use FastMCP from mcp.server.fastmcp for the recommended Python MCP development experience
- The @mcp.tool() decorator automatically generates schemas from type hints and docstrings
- Resources use URI templates for dynamic content with @mcp.resource()
- The Context parameter provides logging, progress reporting, and request metadata
- Always validate inputs and handle errors gracefully in tool implementations
Troubleshooting
ImportError: cannot import FastMCP from mcp.server
Make sure you import from mcp.server.fastmcp, not mcp.server. The correct import is: from mcp.server.fastmcp import FastMCP. Also ensure you have the latest mcp package installed with pip install --upgrade mcp.
Tools not appearing in Claude Desktop
Verify your Claude Desktop configuration file path is correct and the Python path points to the right interpreter. Restart Claude Desktop after making configuration changes. Check the logs for connection errors.
Server crashes with async tools
Ensure you are using Python 3.10+ which has better async support. Make sure all async dependencies (like httpx) are installed. Use try/except blocks in async tools to handle network errors gracefully.
Next Steps
- Explore advanced patterns in the advanced MCP tutorial
- Deploy your server with Docker for production use
- Add authentication and security to your server
- Connect your server to Claude Desktop or VS Code
Was this helpful?
Stay Updated with MCP Insights
Join 5,000+ developers and get weekly insights on MCP development, new server releases, and implementation strategies delivered to your inbox.
We respect your privacy. Unsubscribe at any time.
MCPgee Team
We write in-depth guides, tutorials, and reviews to help developers get the most out of the Model Context Protocol ecosystem.
Frequently Asked Questions
Related Tutorials
Setting Up Your First MCP Server
Step-by-step guide to creating and running your first MCP server
Advanced MCP Patterns
Master advanced MCP patterns including streaming, Streamable HTTP transport, authentication, and production server architecture
Containerize MCP Servers with Docker
Containerize MCP servers with Docker for consistent, portable, and secure deployments
Recommended MCP Servers
Popular servers related to this tutorial that you can start using right away.
n8n-mcp
A comprehensive MCP server that provides full control over n8n automation workflows through natural language. It offers
Dify MCP Server
Production-ready platform for agentic workflow development.
gemini-cli-mcp
A secure MCP server that wraps the Google Gemini CLI, allowing clients to query Gemini models using local OAuth sessions
Awesome Mcp Servers MCP Server
⭐ Curated list of Model Context Protocol (MCP) servers - tools that extend Claude Desktop, Cursor, Windsurf, and other M
Slack MCP Server
Enables interaction with Slack workspaces through comprehensive channel management, messaging, user management, file upl
Servers MCP Server
Model Context Protocol Servers
Explore MCP Servers
Browse our directory of 33,000+ MCP servers. Find the perfect tools for your AI-powered workflows.