aggregate_data
Perform data aggregations (count, sum, avg, min, max) on specified tables using a Hasura GraphQL filter. Supported by the Advanced Hasura GraphQL MCP Server for efficient data analysis.
Instructions
Performs a simple aggregation (count, sum, avg, min, max)...
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| aggregateFunction | Yes | The aggregation function... | |
| field | No | Required for 'sum', 'avg', 'min', 'max'... | |
| filter | No | Optional. A Hasura GraphQL 'where' filter object... | |
| tableName | Yes | The exact name of the table... |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"aggregateFunction": {
"description": "The aggregation function...",
"enum": [
"count",
"sum",
"avg",
"min",
"max"
],
"type": "string"
},
"field": {
"description": "Required for 'sum', 'avg', 'min', 'max'...",
"type": "string"
},
"filter": {
"additionalProperties": {},
"description": "Optional. A Hasura GraphQL 'where' filter object...",
"type": "object"
},
"tableName": {
"description": "The exact name of the table...",
"type": "string"
}
},
"required": [
"tableName",
"aggregateFunction"
],
"type": "object"
}
Implementation Reference
- src/index.ts:379-435 (handler)The handler function that implements the core logic of the 'aggregate_data' tool, constructing and executing a GraphQL aggregation query on a Hasura table.async ({ tableName, aggregateFunction, field, filter }) => { console.log(`[INFO] Executing tool 'aggregate_data': ${aggregateFunction} on ${tableName}...`); if (aggregateFunction !== 'count' && !field) { throw new Error(`The 'field' parameter is required for '${aggregateFunction}' aggregation.`); } if (aggregateFunction === 'count' && field) { console.warn(`[WARN] 'field' parameter is ignored for 'count' aggregation.`); } const aggregateTableName = `${tableName}_aggregate`; let aggregateSelection = ''; if (aggregateFunction === 'count') { aggregateSelection = `{ count }`; } else if (field) { aggregateSelection = `{ ${aggregateFunction} { ${field} } }`; } else { throw new Error(`'field' parameter is missing for '${aggregateFunction}' aggregation.`); } const boolExpTypeName = `${tableName}_bool_exp`; const filterVariableDefinition = filter ? `($filter: ${boolExpTypeName}!)` : ""; const whereClause = filter ? `where: $filter` : ""; const query = gql` query AggregateData ${filterVariableDefinition} { ${aggregateTableName}(${whereClause}) { aggregate ${aggregateSelection} } } `; const variables = filter ? { filter } : {}; try { const rawResult = await makeGqlRequest(query, variables); let finalResult = null; if (rawResult && rawResult[aggregateTableName] && rawResult[aggregateTableName].aggregate) { finalResult = rawResult[aggregateTableName].aggregate; } else { console.warn('[WARN] Unexpected result structure from aggregation query:', rawResult); finalResult = rawResult; } return { content: [{ type: "text", text: JSON.stringify(finalResult, null, 2) }] }; } catch (error: any) { if (error instanceof ClientError && error.response?.errors) { const gqlErrors = error.response.errors.map(e => e.message).join(', '); console.error(`[ERROR] Tool 'aggregate_data' failed: ${gqlErrors}`, error.response); throw new Error(`GraphQL aggregation failed: ${gqlErrors}. Check table/field names and filter syntax.`); } console.error(`[ERROR] Tool 'aggregate_data' failed: ${error.message}`); throw error; } }
- src/index.ts:373-378 (schema)Zod schema for input validation of the 'aggregate_data' tool parameters.{ tableName: z.string().describe("The exact name of the table..."), aggregateFunction: z.enum(["count", "sum", "avg", "min", "max"]).describe("The aggregation function..."), field: z.string().optional().describe("Required for 'sum', 'avg', 'min', 'max'..."), filter: z.record(z.unknown()).optional().describe("Optional. A Hasura GraphQL 'where' filter object..."), },
- src/index.ts:370-372 (registration)Registration of the 'aggregate_data' tool with the MCP server using server.tool().server.tool( "aggregate_data", "Performs a simple aggregation (count, sum, avg, min, max)...",