Skip to main content
Glama
andrewlwn77
by andrewlwn77

group_by

Organize database records by grouping them based on a specific column and calculate counts for each group to analyze data patterns and distributions.

Instructions

Group records by a column and get counts

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
base_idYesThe ID of the base/project
table_nameYesThe name of the table
column_nameYesThe column to group by
whereNoOptional filter condition
sortNoSort order for groups
limitNoMaximum number of groups to return
offsetNoNumber of groups to skip

Implementation Reference

  • Handler function that executes the group_by tool logic by calling the NocoDB client's groupBy method with user-provided arguments and formatting the response.
    handler: async (
      client: NocoDBClient,
      args: {
        base_id: string;
        table_name: string;
        column_name: string;
        where?: string;
        sort?: string;
        limit?: number;
        offset?: number;
      },
    ) => {
      const groups = await client.groupBy(
        args.base_id,
        args.table_name,
        args.column_name,
        {
          where: args.where,
          sort: args.sort,
          limit: args.limit,
          offset: args.offset,
        },
      );
      return {
        groups,
        count: groups.length,
        column: args.column_name,
      };
    },
  • Input schema defining the parameters for the group_by tool, including required base_id, table_name, column_name, and optional where, sort, limit, offset.
    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 group by",
        },
        where: {
          type: "string",
          description: "Optional filter condition",
        },
        sort: {
          type: "string",
          description: "Sort order for groups",
        },
        limit: {
          type: "number",
          description: "Maximum number of groups to return",
        },
        offset: {
          type: "number",
          description: "Number of groups to skip",
        },
      },
      required: ["base_id", "table_name", "column_name"],
    },
  • The group_by tool object definition, including name, description, schema, and handler, exported as part of queryTools array.
      {
        name: "group_by",
        description: "Group records by a column and get counts",
        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 group by",
            },
            where: {
              type: "string",
              description: "Optional filter condition",
            },
            sort: {
              type: "string",
              description: "Sort order for groups",
            },
            limit: {
              type: "number",
              description: "Maximum number of groups to return",
            },
            offset: {
              type: "number",
              description: "Number of groups to skip",
            },
          },
          required: ["base_id", "table_name", "column_name"],
        },
        handler: async (
          client: NocoDBClient,
          args: {
            base_id: string;
            table_name: string;
            column_name: string;
            where?: string;
            sort?: string;
            limit?: number;
            offset?: number;
          },
        ) => {
          const groups = await client.groupBy(
            args.base_id,
            args.table_name,
            args.column_name,
            {
              where: args.where,
              sort: args.sort,
              limit: args.limit,
              offset: args.offset,
            },
          );
          return {
            groups,
            count: groups.length,
            column: args.column_name,
          };
        },
      },
    ];
  • src/index.ts:55-62 (registration)
    Top-level registration where queryTools (including group_by) is combined into allTools for MCP server tool list and call handlers.
    const allTools = [
      ...databaseTools,
      ...tableTools,
      ...recordTools,
      ...viewTools,
      ...queryTools,
      ...attachmentTools,
    ];
  • Underlying helper method in NocoDBClient that performs client-side grouping by fetching records, counting occurrences per value, applying sort/limit/offset.
    async groupBy(
      baseId: string,
      tableName: string,
      columnName: string,
      options?: QueryOptions,
    ): Promise<any[]> {
      // Implement client-side grouping
      const records = await this.listRecords(baseId, tableName, options);
    
      const groups = new Map<any, number>();
      records.list.forEach((record) => {
        const value = record[columnName];
        groups.set(value, (groups.get(value) || 0) + 1);
      });
    
      const result = Array.from(groups.entries()).map(([value, count]) => ({
        [columnName]: value,
        count,
      }));
    
      // Apply sorting if specified
      if (options?.sort) {
        const sortField = Array.isArray(options.sort)
          ? options.sort[0]
          : options.sort;
        const desc = sortField.startsWith("-");
        result.sort((a, b) => {
          const aVal = a[columnName];
          const bVal = b[columnName];
          return desc ? (bVal > aVal ? 1 : -1) : aVal > bVal ? 1 : -1;
        });
      }
    
      // Apply limit and offset
      const start = options?.offset || 0;
      const end = options?.limit ? start + options.limit : undefined;
    
      return result.slice(start, end);
    }
Behavior2/5

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

With no annotations provided, the description carries full burden but only states the basic operation without disclosing behavioral traits like whether it's read-only, performance characteristics, error handling, or output format. It mentions 'get counts' but doesn't detail what the counts represent or the structure of results.

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, clearly front-loading the core purpose. Every word earns its place, making it easy to parse quickly.

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 the complexity of a 7-parameter tool with no annotations and no output schema, the description is incomplete. It doesn't explain the return values, error conditions, or how the grouping interacts with other parameters like 'where' or 'sort', leaving significant gaps for an AI agent.

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 already documents all 7 parameters thoroughly. The description adds no additional meaning beyond implying grouping and counting, which aligns with the schema but doesn't provide extra context like examples or constraints.

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

Purpose4/5

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

The description clearly states the verb ('group') and resource ('records'), specifying the operation as grouping by a column and getting counts. It distinguishes from siblings like 'aggregate' or 'query' by focusing specifically on grouping with counts, though it doesn't explicitly compare to them.

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 'aggregate' or 'query', which also perform data analysis operations. The description lacks context about prerequisites, typical use cases, or exclusions.

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

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