get_time_series_data
Retrieve economic time series data from Peru's Central Reserve Bank database for specific codes within defined date ranges, returning formatted date-value pairs.
Instructions
Get the data for a specific time series within a date range.
This function retrieves time series data from the BCRP (Banco Central de Reserva del Perú) database for a specific time series code within the specified date range. The data is returned as a list of lists with dates formatted as 'YYYY-MM-DD'.
Args: time_series_code (str): The unique code identifier for the time series. start (str): The start date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. end (str): The end date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data.
Returns: List[List[str]]: A list of lists where each inner list contains: [formatted_date, time_series_value] The date is formatted as 'YYYY-MM-DD' and the value is the corresponding data point for that date.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time_series_code | Yes | ||
| start | Yes | ||
| end | Yes |
Implementation Reference
- main.py:71-117 (handler)The @mcp.tool() decorator registers the function, and the function implements the core logic for fetching time series data from the BCRP API endpoint, parsing the JSON response, handling errors, formatting dates to YYYY-MM-DD, and returning a list of [date, value] pairs.@mcp.tool() def get_time_series_data(time_series_code: str, start: str, end: str) -> List[List[str]]: """ Get the data for a specific time series within a date range. This function retrieves time series data from the BCRP (Banco Central de Reserva del Perú) database for a specific time series code within the specified date range. The data is returned as a list of lists with dates formatted as 'YYYY-MM-DD'. Args: time_series_code (str): The unique code identifier for the time series. start (str): The start date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. end (str): The end date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. Returns: List[List[str]]: A list of lists where each inner list contains: [formatted_date, time_series_value] The date is formatted as 'YYYY-MM-DD' and the value is the corresponding data point for that date. """ try: url = f"{API_ENDPOINT}/{time_series_code}/json/{start}/{end}/eng" response = requests.get(url) if response.status_code != 200: return [] # Extract JSON from response that may contain HTML errors response_text = response.text json_start = response_text.find('{\n"config":') if json_start == -1: return [] data = json.loads(response_text[json_start:]) result = [] for period in data["periods"]: date_formatted = pd.to_datetime(period["name"]).strftime('%Y-%m-%d') value = period["values"][0] if period["values"] else "n.d." result.append([date_formatted, str(value) if value != "n.d." else "n.d."]) return result except Exception as e: return []
- main.py:72-92 (schema)Type hints and docstring define the input schema (time_series_code: str, start: str, end: str) and output schema (List[List[str]] with [date, value]).def get_time_series_data(time_series_code: str, start: str, end: str) -> List[List[str]]: """ Get the data for a specific time series within a date range. This function retrieves time series data from the BCRP (Banco Central de Reserva del Perú) database for a specific time series code within the specified date range. The data is returned as a list of lists with dates formatted as 'YYYY-MM-DD'. Args: time_series_code (str): The unique code identifier for the time series. start (str): The start date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. end (str): The end date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. Returns: List[List[str]]: A list of lists where each inner list contains: [formatted_date, time_series_value] The date is formatted as 'YYYY-MM-DD' and the value is the corresponding data point for that date. """
- main.py:71-71 (registration)The tool is registered via the @mcp.tool() decorator on the handler function.@mcp.tool()