Skip to main content
Glama

Advanced Hasura GraphQL MCP Server

by husamabusafa

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

NameRequiredDescriptionDefault
aggregateFunctionYesThe aggregation function...
fieldNoRequired for 'sum', 'avg', 'min', 'max'...
filterNoOptional. A Hasura GraphQL 'where' filter object...
tableNameYesThe 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

  • 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; } }
  • 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)...",

Other Tools

Related 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/husamabusafa/hasura_mcp'

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