get_weather_forecast
Retrieve weather forecasts for a specific location and date range using Malaysia's open API data. Input location details and optional start/end dates to access accurate forecasts.
Instructions
Retrieve a weather forecast for a specific location within a given date range.
Args:
location_name: The name or identifier of the location for which the forecast is retrieved.
date_start: The earliest date (inclusive) to begin retrieving the weather forecast. If omitted, defaults to the current date.
date_end: The latest date (inclusive) to stop retrieving the weather forecast. If omitted, defaults to the current date.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date_end | No | ||
| date_start | No | ||
| location_name | Yes |
Implementation Reference
- weather.py:81-116 (handler)The core handler function for the 'get_weather_forecast' tool. It validates input dates, makes an API request to the Malaysian government weather forecast endpoint, processes the data, formats each forecast using the 'format_weather' helper, and joins them with separators.@mcp.tool() async def get_weather_forecast(location_name: str, date_start: str = None, date_end: str = None) -> str: """Retrieve a weather forecast for a specific location within a given date range. Args: location_name: The name or identifier of the location for which the forecast is retrieved. date_start: The earliest date (inclusive) to begin retrieving the weather forecast. If omitted, defaults to the current date. date_end: The latest date (inclusive) to stop retrieving the weather forecast. If omitted, defaults to the current date. """ if not date_start: date_start = current_date() elif not validate_date(date_start): return "Wrong `date_start` format given. Accepted format is 'YYYY-MM-DD'." if not date_end: date_end = current_date() elif not validate_date(date_end): return "Wrong `date_end` format given. Accepted format is 'YYYY-MM-DD'." forecast_url = f"{GOV_API_BASE}/weather/forecast" forecast_data = await make_api_request(forecast_url, { "meta": "true", "sort": "-date", "icontains": f"{location_name}@location__location_name", "date_start": f"{date_start}@date", "date_end": f"{date_end}@date", }) if not forecast_data or "data" not in forecast_data: return "Unable to fetch weather forecast data for this location." if not forecast_data["data"]: return "No active weather forecast data for this time period." forecasts = [format_weather(forecast) for forecast in forecast_data["data"]] return "\n---\n".join(forecasts)
- weather.py:82-89 (schema)Input schema and documentation for the tool, defining parameters location_name (required str), date_start/date_end (optional str, YYYY-MM-DD format), with descriptive Args in docstring used by FastMCP for tool schema.async def get_weather_forecast(location_name: str, date_start: str = None, date_end: str = None) -> str: """Retrieve a weather forecast for a specific location within a given date range. Args: location_name: The name or identifier of the location for which the forecast is retrieved. date_start: The earliest date (inclusive) to begin retrieving the weather forecast. If omitted, defaults to the current date. date_end: The latest date (inclusive) to stop retrieving the weather forecast. If omitted, defaults to the current date. """
- weather.py:81-81 (registration)Registration of the tool using the @mcp.tool() decorator on the FastMCP server instance.@mcp.tool()
- helpers.py:6-18 (helper)Helper function used by the handler to format each weather forecast item into a human-readable string.def format_weather(weather_res: dict) -> str: """Format a weather json response into a readable string.""" location = weather_res["location"] return f""" Location ID: {location.get('location_id', 'Unknown')} Location Name: {location.get('location_name', 'Unknown')} Date: {weather_res.get('date', 'Unknown')} Morning Forecast: {weather_res.get('morning_forecast', 'Unknown')} Afternoon Forecast: {weather_res.get('afternoon_forecast', 'Unknown')} Night Forecast: {weather_res.get('night_forecast', 'Unknown')} Minimum Temperature: {weather_res.get('min_temp', 'Unknown')} Maximum Temperature: {weather_res.get('max_temp', 'Unknown')} """
- helpers.py:76-86 (helper)Async helper for making HTTP requests to the API endpoint, used by the handler to fetch forecast data.async def make_api_request(url: str, params: dict[str, str]) -> dict[str, Any] | None: """Make a request to the Malaysia Government API with proper error handling.""" async with httpx.AsyncClient() as client: try: response = await client.get(url, params=params, timeout=30.0, follow_redirects=True) print(response) response.raise_for_status() return response.json() except Exception as ex: return None