🛠 MCP in Action: A Developer’s Perspective
Model Context Protocol (MCP) isn’t just a theoretical standard—developers can implement it today using modern Python frameworks like FastAPI. In this guide, we’ll walk through a practical MCP implementation, showing how to:
- Spin up a FastAPI-based MCP server
- Register and invoke tools
- Handle stateful sessions
- Enable LLMs to interact with real-world tools
Step 1: Project Structure
project-root/
├── main.py # FastAPI server
├── tools/
│ └── weather.py # Tool logic
├── sessions/ # In-memory or persisted session store
├── schemas.py # JSON-RPC schemas
└── config.py # Config and setup
Step 2: Define Your Tool (e.g., Weather API)
# tools/weather.py
def get_weather(city: str):
# Normally call external API
return {"forecast": "Sunny", "temperature": "27°C"}
Step 3: Set Up FastAPI MCP Server
# main.py
from fastapi import FastAPI, Request
from tools.weather import get_weather
app = FastAPI()
@app.post("/mcp")
async def handle_request(request: Request):
data = await request.json()
method = data.get("method")
params = data.get("params", {})
if method == "weather.get":
result = get_weather(**params)
return {"jsonrpc": "2.0", "result": result, "id": data.get("id")}
return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": data.get("id")}
Step 4: Session Management
# sessions/session_store.py
session_store = {}
def get_or_create_session(session_id):
if session_id not in session_store:
session_store[session_id] = {"tools_used": []}
return session_store[session_id]
🧠 Why This Matters
This is the core of MCP: models interact via a standard (JSON-RPC 2.0), access external tools, and maintain long-term memory across sessions—enabling agentic behavior.
Diagram: MCP Session and Tool Execution Flow

Explanation:
- LLM sends JSON-RPC
weather.getvia client - MCP server routes it to the tool
- Response flows back with updated session state
Where to Go From Here
- Add OpenAI key to simulate an agent
- Register more tools (e.g.,
search,calendar.add) - Move session management to Redis or a DB
This forms the base for building full-fledged MCP-powered agentic systems.
Next Up: Beyond Chatbots: MCP and Agentic AI →