Skip to main content
Glama

plot_chart

Generate professional charts from BCRP economic data series. Plot multiple indicators with custom date ranges, titles, and legends, saving results as PNG files for analysis.

Instructions

Generate a professional chart for BCRP series data. Returns the path to the saved PNG file.

Args: series_codes: List of BCRP series codes to plot period: Date range in format 'YYYY-MM/YYYY-MM' (optional) title: Custom chart title (optional, uses series name if not provided) names: Custom names for each series in legend (optional) output_path: Custom output path for the chart (optional)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
series_codesYes
periodNo
titleNo
namesNo
output_pathNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The `plot_chart` tool is defined here with an `@mcp.tool()` decorator and contains the logic for fetching data, parsing Spanish-formatted dates, and generating a plot using Matplotlib.
    @mcp.tool()
    async def plot_chart(
        series_codes: list[str], 
        period: str = None, 
        title: str = None,
        names: list[str] = None,
        output_path: str = None
    ) -> str:
        """
        Generate a professional chart for BCRP series data.
        Returns the path to the saved PNG file.
        
        Args:
            series_codes: List of BCRP series codes to plot
            period: Date range in format 'YYYY-MM/YYYY-MM' (optional)
            title: Custom chart title (optional, uses series name if not provided)
            names: Custom names for each series in legend (optional)
            output_path: Custom output path for the chart (optional)
        """
        try:
            # 1. Fetch Data
            data_json = await _get_data(series_codes, period)
            if data_json.startswith("Error") or data_json.startswith("No data"):
                return data_json
                
            import pandas as pd
            df = pd.read_json(data_json, orient='records')
            if df.empty:
                return "No data found to plot."
            
            # 2. Setup plot style
            import matplotlib
            matplotlib.use('Agg')
            import matplotlib.pyplot as plt
            import matplotlib.dates as mdates
            
            plt.style.use('seaborn-v0_8-whitegrid')
            fig, ax = plt.subplots(figsize=(12, 6), dpi=120)
            
            # 3. Parse time column (BCRP uses Spanish month abbreviations)
            if 'time' in df.columns:
                # Spanish month mapping
                spanish_months = {
                    'Ene': 'Jan', 'Feb': 'Feb', 'Mar': 'Mar', 'Abr': 'Apr',
                    'May': 'May', 'Jun': 'Jun', 'Jul': 'Jul', 'Ago': 'Aug',
                    'Sep': 'Sep', 'Oct': 'Oct', 'Nov': 'Nov', 'Dic': 'Dec'
                }
                
                def parse_spanish_date(date_str):
                    """Convert BCRP Spanish date format to datetime."""
                    try:
                        for es, en in spanish_months.items():
                            date_str = date_str.replace(es, en)
                        return pd.to_datetime(date_str, format='%b.%Y')
                    except Exception:
                        return pd.to_datetime(date_str)
                
                df['time'] = df['time'].apply(parse_spanish_date)
                df = df.set_index('time')
            
            # 4. Resolve Names if not provided
            if not names:
                await metadata_client.load()
                names = metadata_client.get_series_names(series_codes)
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MaykolMedrano/mcp_bcrp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server