get_weather
Get current weather conditions for any specified city. Provide a city name to receive temperature, humidity, and forecast details for trip planning and daily activities.
Instructions
Get the weather for the given location
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes |
Implementation Reference
- server.py:28-32 (handler)The MCP tool handler for 'get_weather'. Decorated with @mcp.tool(), it accepts a city name, creates a GetWeather instance, and returns the weather string.
@mcp.tool() def get_weather(city: str) -> str: """Get the weather for the given location""" weather = GetWeather(city) return str(weather) - get_weather.py:7-41 (helper)Helper class that queries the WeatherAPI.com API. Uses WEATHER_API_KEY env variable, fetches current weather data for a city, and formats the result with temperature, condition, humidity, and wind info.
class GetWeather: def __init__(self, city: str): self.city = city self.api_key = os.getenv("WEATHER_API_KEY") self.base_url = "http://api.weatherapi.com/v1/current.json" def get_weather(self): if not self.api_key: return "Error: WEATHER_API_KEY not found in environment variables" try: params = { "key": self.api_key, "q": self.city, "aqi": "no" } response = requests.get(self.base_url, params=params) response.raise_for_status() data = response.json() location = data['location'] current = data['current'] weather_info = f"""Weather in {location['name']}, {location['region']}: Temperature: {current['temp_f']}°F (feels like {current['feelslike_f']}°F) Condition: {current['condition']['text']} Humidity: {current['humidity']}% Wind: {current['wind_mph']} mph {current['wind_dir']} """ return weather_info except requests.exceptions.RequestException as e: return f"Error fetching weather data: {str(e)}" def __str__(self): return self.get_weather() - server.py:28-32 (registration)The @mcp.tool() decorator registers 'get_weather' as an MCP tool on the FastMCP server instance.
@mcp.tool() def get_weather(city: str) -> str: """Get the weather for the given location""" weather = GetWeather(city) return str(weather)