Building a Weather MCP Server with Python
Introduction
The Model Context Protocol (MCP) is an open standard that allows AI models like Claude to connect with external tools and data sources. In this article, I'll show you how to build a Weather MCP Server using Python that any MCP client can use.
What We'll Build
A simple MCP Server with one tool: get_weather — it receives a city name and returns current temperature, wind speed, and humidity using the Open-Meteo API (free, no API key required).
Requirements
- Python 3.12+
- mcp[cli]
- httpx
Installation
pip install "mcp[cli]" httpx
The Code
from mcp.server.fastmcp import FastMCP
import httpx
mcp = FastMCP("Weather MCP Server")
@mcp.tool()
async def get_weather(city: str) -> str:
"""Get current weather for a city."""
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
async with httpx.AsyncClient() as client:
geo_data = (await client.get(geo_url)).json()
if not geo_data.get("results"):
return f"City '{city}' not found."
r = geo_data["results"][0]
lat, lon = r["latitude"], r["longitude"]
weather_url = (
f"https://api.open-meteo.com/v1/forecast"
f"?latitude={lat}&longitude={lon}"
f"¤t=temperature_2m,wind_speed_10m,relative_humidity_2m"
f"&timezone=auto"
)
async with httpx.AsyncClient() as client:
current = (await client.get(weather_url)).json()["current"]
return (
f"Weather in {r['name']}, {r['country']}:\n"
f"Temperature: {current['temperature_2m']}°C\n"
f"Wind Speed: {current['wind_speed_10m']} km/h\n"
f"Humidity: {current['relative_humidity_2m']}%"
)
if __name__ == "__main__":
mcp.run()
Testing with MCP Inspector
Run the development server:
mcp dev server.py
This opens the MCP Inspector in your browser where you can test the get_weather tool with any city name.
Result
When tested with "Lima", the server returned:
- Temperature: 19.0°C
- Wind Speed: 12.2 km/h
- Humidity: 81%
Repository
The full source code is available on GitHub:
weather-mcp-server
Conclusion
MCP makes it easy to extend AI capabilities with real-world data. With just a few lines of Python, we built a working weather tool that any MCP client like Claude or VSCode can use.
Top comments (1)
muy útil la forma en que se explica paso a paso cómo construir un servidor MCP con Python. El ejemplo del clima usando Open-Meteo es práctico, sencillo y demuestra muy bien cómo MCP permite conectar modelos de IA con datos reales externos. También me parece interesante que no se necesite una API key, lo que facilita mucho la prueba para quienes están empezando. Buen aporte para entender cómo crear herramientas MCP reutilizables en clientes como Claude o VS Code.