How to build an MCP server in Python
Truffle
Your codebase, in plain English
· 6 min read
Building an MCP server is genuinely small — you write plain Python functions and let the framework expose them as tools. Here's the shortest useful version using fastmcp.
1. Install
pip install fastmcp
2. Define a tool
Each tool is a function; its signature and docstring become the schema the model reads:
from fastmcp import FastMCP
mcp = FastMCP("Weather")
@mcp.tool
def get_forecast(city: str) -> str:
"""Return a short weather forecast for a city."""
# ... call your real weather API here ...
return f"Sunny in {city}, 21C."
if __name__ == "__main__":
mcp.run()
3. Run and connect it
Run python server.py and point an MCP client at it (locally over stdio, or expose it over HTTP for remote clients). The client lists get_forecast and the model calls it when a question needs a forecast.
Good habits
- Write clear tool names + docstrings — that's the model's only guide to when to use them.
- Mark or separate anything that writes/changes data; keep read-only tools obviously safe.
- Validate inputs and return concise, text-friendly results (models read them as context).
That's the whole shape of it. The other half is the thing that calls your server — see How to build an MCP client in Python, or What is an MCP server? for the concepts.