Build a real MCP server: not a look-alike REST endpoint
The original tutorial manually created /tools/call with FastAPI. That teaches HTTP, but it is not the Model Context Protocol. The current Python SDK v2 implements the 2026-07-28 protocol directly.
pip install mcp anyio. The stable 2.x SDK uses MCPServer; the old FastMCP name belongs to SDK v1.Create one typed tool
Type hints become the input schema. The docstring helps clients and models understand when to use the capability.
# server.py
from mcp.server import MCPServer
mcp = MCPServer("Weather Demo")
@mcp.tool()
def get_weather(location: str) -> dict:
"""Return demo weather data for a location."""
return {
"location": location,
"temperature_c": 21,
"condition": "clear",
}
if __name__ == "__main__":
mcp.run(transport="streamable-http", port=3001)Connect with the first-class client
The v2 client negotiates the protocol and can work with modern 2026 servers while retaining compatibility logic for older deployments.
# client.py
import anyio
from mcp import Client
async def main() -> None:
async with Client("http://localhost:3001/mcp") as client:
tools = await client.list_tools()
print([tool.name for tool in tools.tools])
result = await client.call_tool(
"get_weather", {"location": "Seattle"}
)
print(result.structured_content)
if __name__ == "__main__":
anyio.run(main)Run and inspect
Start server
python server.py
Run client
python client.py
Inspector
uv run mcp dev server.py
http://127.0.0.1:3001/mcp. Do not start new deployments on the legacy SSE transport.Current reference sources
Add controls before adding more tools
Authorization
Authenticate users/agents and scope access per capability.
Validation
Constrain inputs, outputs, timeouts and backend effects.
Observability
Trace tool calls and business outcomes.
Evaluation
Test tool selection, arguments, errors and policy behavior.
