
How to Build and Connect a Custom Model Context Protocol (MCP) Server in 5 Minutes
Quick Start (TL;DR)
The Model Context Protocol (MCP) is an open standard created by Anthropic that lets AI assistants securely query external tools and data sources.
To build a functional MCP server in Python right now, install FastMCP:
pip install "mcp[cli]" fastmcp
Create server.py:
from fastmcp import FastMCP
mcp = FastMCP("SystemUtilities")
@mcp.tool()
def get_disk_usage(path: str = "/") -> str:
"""Return disk usage statistics for the given filesystem path."""
import shutil
total, used, free = shutil.disk_usage(path)
return f"Total: {total // (2**30)}GB | Used: {used // (2**30)}GB | Free: {free // (2**30)}GB"
if __name__ == "__main__":
mcp.run()
Run and test it immediately in your terminal inspector:
mcp dev server.py
1. Connecting Your MCP Server to AI Clients
Once your server script runs locally, you can register it with any MCP-compatible AI client.
Configuration for Claude Code / Claude Desktop
Add the following block to your configuration file (~/.claude/mcp.json or claude_desktop_config.json):
{
"mcpServers": {
"system-utils": {
"command": "python3",
"args": ["/absolute/path/to/server.py"]
}
}
}
Configuration for Antigravity & Cursor
In your workspace configuration or mcp_config.json:
{
"mcpServers": {
"system-utils": {
"type": "stdio",
"command": "python3",
"args": ["/absolute/path/to/server.py"]
}
}
}
2. FastMCP vs Standard Low-Level SDK Comparison
| Feature | FastMCP (Recommended) | Official Low-Level MCP SDK |
|---|---|---|
| Setup Time | Under 2 minutes | 15–20 minutes boilerplate |
| Type Hinting & Schema Generation | Automatic from Python docstrings | Manual JSON Schema definitions |
| Transport Support | STDIO and SSE built-in | Separate transport wrappers required |
| Best For | Rapid custom tools & internal APIs | Enterprise custom transport protocols |
3. Best Practices for Production MCP Tools
- Write Descriptive Docstrings: AI agents inspect function docstrings to decide when and how to call a tool. Ambiguous docstrings cause hallucinated argument values.
- Handle Exceptions Gracefully: Return formatted error strings rather than crashing the process, allowing the agent to self-heal.
- Keep Tool Payloads Concise: Truncate overly long JSON responses to prevent exhausting the agent’s context window.
Summary Checklist
- Install
fastmcpfor zero-boilerplate tool definitions. - Expose functions using the
@mcp.tool()decorator with clean docstrings. - Test with
mcp dev server.pybefore linking to your main agent. - Add absolute file paths in your client config (
mcp.json).