Skip to main content
Glama
K02D

MCP Tabular Data Analysis Server

by K02D

group_aggregate

Group tabular data by specified columns and compute aggregations like sum, mean, count, or max to analyze patterns and summarize datasets.

Instructions

Group data and compute aggregations.

Args:
    file_path: Path to CSV or SQLite file
    group_by: Columns to group by
    aggregations: Dictionary mapping column names to list of aggregation functions
                 (e.g., {"sales": ["sum", "mean"], "quantity": ["count", "max"]})
                 Supported: sum, mean, median, min, max, count, std, var

Returns:
    Dictionary containing grouped and aggregated data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
group_byYes
aggregationsYes

Implementation Reference

  • The main handler function for the 'group_aggregate' tool. It loads data using _load_data, validates inputs, performs pandas groupby with specified aggregations, flattens multi-level columns, and returns the results as a dictionary with metadata.
    @mcp.tool()
    def group_aggregate(
        file_path: str,
        group_by: list[str],
        aggregations: dict[str, list[str]],
    ) -> dict[str, Any]:
        """
        Group data and compute aggregations.
        
        Args:
            file_path: Path to CSV or SQLite file
            group_by: Columns to group by
            aggregations: Dictionary mapping column names to list of aggregation functions
                         (e.g., {"sales": ["sum", "mean"], "quantity": ["count", "max"]})
                         Supported: sum, mean, median, min, max, count, std, var
        
        Returns:
            Dictionary containing grouped and aggregated data
        """
        df = _load_data(file_path)
        
        # Validate group_by columns
        invalid = [c for c in group_by if c not in df.columns]
        if invalid:
            raise ValueError(f"Group-by columns not found: {invalid}")
        
        # Validate aggregation columns
        for col in aggregations:
            if col not in df.columns:
                raise ValueError(f"Aggregation column '{col}' not found")
        
        # Perform groupby
        grouped = df.groupby(group_by).agg(aggregations)
        
        # Flatten column names
        grouped.columns = ["_".join(col).strip() for col in grouped.columns]
        grouped = grouped.reset_index()
        
        return {
            "group_by": group_by,
            "aggregations": aggregations,
            "group_count": len(grouped),
            "result": grouped.to_dict(orient="records"),
        }

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/K02D/mcp-tabular'

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