get_weather_forecast
Retrieve weather forecasts for specific locations and date ranges using Malaysia Government's Open API data.
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 |
|---|---|---|---|
| location_name | Yes | ||
| date_start | No | ||
| date_end | No |
Implementation Reference
- weather.py:82-116 (handler)The core handler function that validates input dates, queries the weather forecast API using the location and date range, processes the data with format_weather, and returns formatted forecasts separated by ---.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:81-81 (registration)The @mcp.tool() decorator registers this function as an MCP tool named 'get_weather_forecast', with schema inferred from signature and docstring.@mcp.tool()
- weather.py:82-89 (schema)Type annotations and docstring provide the input schema: required location_name (str), optional date_start/date_end (str, YYYY-MM-DD), returns formatted str.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. """