Skip to main content
Glama
andrewlwn77
by andrewlwn77

aggregate

Calculate summary statistics like count, sum, average, minimum, or maximum values from a NocoDB table column, with optional filtering for specific data subsets.

Instructions

Perform aggregation operations on a column

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
base_idYesThe ID of the base/project
table_nameYesThe name of the table
column_nameYesThe column to aggregate
functionYesAggregation function
whereNoOptional filter condition

Implementation Reference

  • Handler for the 'aggregate' MCP tool. Executes aggregation by delegating to NocoDBClient.aggregate and returns formatted result.
      handler: async (
        client: NocoDBClient,
        args: {
          base_id: string;
          table_name: string;
          column_name: string;
          function: "count" | "sum" | "avg" | "min" | "max";
          where?: string;
        },
      ) => {
        const value = await client.aggregate(args.base_id, args.table_name, {
          column_name: args.column_name,
          func: args.function,
          where: args.where,
        });
        return {
          value,
          aggregation: {
            column: args.column_name,
            function: args.function,
            where: args.where,
          },
        };
      },
    },
  • Input schema defining parameters for the 'aggregate' tool including base_id, table_name, column_name, function, and optional where.
    inputSchema: {
      type: "object",
      properties: {
        base_id: {
          type: "string",
          description: "The ID of the base/project",
        },
        table_name: {
          type: "string",
          description: "The name of the table",
        },
        column_name: {
          type: "string",
          description: "The column to aggregate",
        },
        function: {
          type: "string",
          description: "Aggregation function",
          enum: ["count", "sum", "avg", "min", "max"],
        },
        where: {
          type: "string",
          description: "Optional filter condition",
        },
      },
      required: ["base_id", "table_name", "column_name", "function"],
    },
  • Tool object definition for 'aggregate' within queryTools array, which is included in the main allTools for MCP server registration.
    {
      name: "aggregate",
      description: "Perform aggregation operations on a column",
      inputSchema: {
        type: "object",
        properties: {
          base_id: {
            type: "string",
            description: "The ID of the base/project",
          },
          table_name: {
            type: "string",
            description: "The name of the table",
          },
          column_name: {
            type: "string",
            description: "The column to aggregate",
          },
          function: {
            type: "string",
            description: "Aggregation function",
            enum: ["count", "sum", "avg", "min", "max"],
          },
          where: {
            type: "string",
            description: "Optional filter condition",
          },
        },
        required: ["base_id", "table_name", "column_name", "function"],
      },
      handler: async (
        client: NocoDBClient,
        args: {
          base_id: string;
          table_name: string;
          column_name: string;
          function: "count" | "sum" | "avg" | "min" | "max";
          where?: string;
        },
      ) => {
        const value = await client.aggregate(args.base_id, args.table_name, {
          column_name: args.column_name,
          func: args.function,
          where: args.where,
        });
        return {
          value,
          aggregation: {
            column: args.column_name,
            function: args.function,
            where: args.where,
          },
        };
      },
    },
  • Underlying NocoDBClient.aggregate method implementing client-side aggregation (count, sum, avg, min, max) by fetching records and computing locally.
    async aggregate(
      baseId: string,
      tableName: string,
      options: AggregateOptions,
    ): Promise<number> {
      // For now, implement client-side aggregation
      // as the aggregate endpoint might not be available in all versions
      const records = await this.listRecords(baseId, tableName, {
        where: options.where,
      });
    
      const values = records.list.map((r) => Number(r[options.column_name]) || 0);
    
      switch (options.func) {
        case "count":
          return records.list.length;
        case "sum":
          return values.reduce((a, b) => a + b, 0);
        case "avg":
          return values.length > 0
            ? values.reduce((a, b) => a + b, 0) / values.length
            : 0;
        case "min":
          return Math.min(...values);
        case "max":
          return Math.max(...values);
        default:
          throw new NocoDBError(`Unknown aggregate function: ${options.func}`);
      }
    }
  • Type definition for AggregateOptions used by the NocoDBClient.aggregate method.
    export interface AggregateOptions {
      column_name: string;
      func: "count" | "sum" | "avg" | "min" | "max";
      where?: string;
    }

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/andrewlwn77/nocodb-mcp'

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