@mcp.tool()
async def get_weather(city: str, forecast_days: Optional[int] = None) -> dict:
"""Get weather data - current conditions or forecast up to 10 days."""
api_key = get_api_key_from_request()
if not check_rate_limit(api_key):
return {"error": "Rate limit exceeded", "status": 429}
if not await validate_api_key(api_key):
return {"error": "Invalid API key", "status": 401}
# Determine behavior from parameters
if forecast_days is None:
# Current weather
endpoint = f"{WEATHER_API_BASE}/current.json"
params = {"key": api_key, "q": city, "aqi": "yes"}
else:
# Forecast
endpoint = f"{WEATHER_API_BASE}/forecast.json"
params = {"key": api_key, "q": city, "days": forecast_days, "aqi": "yes"}
# Proxy to WeatherAPI.com
async with httpx.AsyncClient() as client:
response = await client.get(endpoint, params=params)
return response.json()