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
| Name | Required | Description | Default |
|---|---|---|---|
| base_id | Yes | The ID of the base/project | |
| table_name | Yes | The name of the table | |
| column_name | Yes | The column to aggregate | |
| function | Yes | Aggregation function | |
| where | No | Optional filter condition |
Implementation Reference
- src/tools/query.ts:115-139 (handler)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, }, }; }, },
- src/tools/query.ts:88-114 (schema)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"], },
- src/tools/query.ts:85-139 (registration)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, }, }; }, },
- src/nocodb-api.ts:343-372 (helper)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}`); } }
- src/types.ts:71-75 (schema)Type definition for AggregateOptions used by the NocoDBClient.aggregate method.export interface AggregateOptions { column_name: string; func: "count" | "sum" | "avg" | "min" | "max"; where?: string; }