Skip to main content
Glama
ishayoyo

Excel MCP Server

by ishayoyo

aggregate

Calculate sum, average, count, min, or max values from a column in Excel or CSV files to analyze data patterns and extract key metrics.

Instructions

Perform aggregation operations on a column

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesPath to the CSV or Excel file
columnYesColumn name or index (0-based)
operationYesAggregation operation
sheetNoSheet name for Excel files (optional)

Implementation Reference

  • The core handler function that performs aggregation operations (sum, average, count, min, max) on numeric data in a specified column of a CSV or Excel file, with validation for data types and column existence.
    async aggregate(args: ToolArgs): Promise<ToolResponse> {
      try {
        const { filePath, column, operation, sheet } = args;
        const data = await readFileContent(filePath, sheet);
    
        if (data.length <= 1) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  success: false,
                  error: 'File has no data rows',
                }, null, 2),
              },
            ],
          };
        }
    
        const colIndex = isNaN(Number(column))
          ? data[0].indexOf(column)
          : Number(column);
    
        if (colIndex === -1 || colIndex >= (data[0]?.length || 0)) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  success: false,
                  error: `Column "${column}" not found`,
                }, null, 2),
              },
            ],
          };
        }
    
        const numericValues = [];
        const nonNullValues = [];
    
        for (let i = 1; i < data.length; i++) {
          const cellValue = data[i][colIndex];
          if (cellValue !== null && cellValue !== undefined && cellValue !== '') {
            nonNullValues.push(cellValue);
            const numVal = Number(cellValue);
            if (!isNaN(numVal)) {
              numericValues.push(numVal);
            }
          }
        }
    
        // Check if operation requires numeric data but no numeric values found
        if (['sum', 'average', 'min', 'max'].includes(operation) && numericValues.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  success: false,
                  error: 'Column contains no numeric values for numeric operation',
                }, null, 2),
              },
            ],
          };
        }
    
        // Check for mixed data types when doing numeric operations
        if (['sum', 'average', 'min', 'max'].includes(operation) &&
            numericValues.length > 0 &&
            numericValues.length < nonNullValues.length) {
          const mixedCount = nonNullValues.length - numericValues.length;
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  success: false,
                  error: `Column contains mixed data types: ${mixedCount} non-numeric values found among ${nonNullValues.length} total values`,
                }, null, 2),
              },
            ],
          };
        }
    
        let result;
        switch (operation) {
          case 'sum':
            result = numericValues.reduce((a, b) => a + b, 0);
            break;
          case 'average':
            result = numericValues.length > 0 ? numericValues.reduce((a, b) => a + b, 0) / numericValues.length : 0;
            break;
          case 'count':
            result = nonNullValues.length;
            break;
          case 'min':
            result = numericValues.length > 0 ? Math.min(...numericValues) : null;
            break;
          case 'max':
            result = numericValues.length > 0 ? Math.max(...numericValues) : null;
            break;
          default:
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    success: false,
                    error: `Unknown operation: ${operation}`,
                  }, null, 2),
                },
              ],
            };
        }
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: true,
                column: data[0][colIndex],
                operation,
                result,
                validValues: operation === 'count' ? nonNullValues.length : numericValues.length,
              }, null, 2),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: false,
                error: error instanceof Error ? error.message : 'Unknown error occurred',
              }, null, 2),
            },
          ],
        };
      }
    }
  • Input schema definition for the 'aggregate' tool, specifying parameters: filePath (required), column (required), operation (enum: sum, average, count, min, max), sheet (optional).
      name: 'aggregate',
      description: 'Perform aggregation operations on a column',
      inputSchema: {
        type: 'object',
        properties: {
          filePath: {
            type: 'string',
            description: 'Path to the CSV or Excel file',
          },
          column: {
            type: 'string',
            description: 'Column name or index (0-based)',
          },
          operation: {
            type: 'string',
            description: 'Aggregation operation',
            enum: ['sum', 'average', 'count', 'min', 'max'],
          },
          sheet: {
            type: 'string',
            description: 'Sheet name for Excel files (optional)',
          },
        },
        required: ['filePath', 'column', 'operation'],
      },
    },
  • src/index.ts:1211-1212 (registration)
    Tool dispatch registration in the main server request handler, mapping 'aggregate' tool calls to the DataOperationsHandler.aggregate method.
    case 'aggregate':
      return await this.dataOpsHandler.aggregate(toolArgs);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries full burden but only states the operation without behavioral details. It doesn't disclose what the tool returns (e.g., a numeric result), error handling, performance implications, or side effects (e.g., file reading). This is inadequate for a tool with 4 parameters and no output schema.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with zero waste. It's front-loaded and appropriately sized for the tool's complexity, though it could benefit from more detail given the lack of annotations and output schema.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 4 parameters, no annotations, and no output schema, the description is incomplete. It doesn't explain the return value (e.g., a numeric aggregate), file format support beyond what's in the schema, or how it differs from sibling tools. This leaves significant gaps for an AI agent to use it correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents parameters like 'filePath' and 'operation' with enum values. The description adds no meaning beyond the schema, merely restating 'aggregation operations on a column'. Baseline 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose3/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Perform aggregation operations on a column' states the basic action but is vague about the context (data files) and doesn't distinguish from siblings like 'bulk_aggregate_multi_files' or 'statistical_analysis'. It specifies the target ('a column') but lacks detail on the resource (CSV/Excel files mentioned only in schema).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like 'bulk_aggregate_multi_files' for multiple files or 'statistical_analysis' for broader operations. The description implies usage for column aggregation but offers no context, exclusions, or prerequisites.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/ishayoyo/excel-mcp'

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