Skip to main content
Glama
fadlee

PocketBase MCP Server

by fadlee

query_collection

Query PocketBase collections using filters, sorting, and aggregation to retrieve specific data records based on defined criteria.

Instructions

Advanced query with filtering, sorting, and aggregation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
aggregateNoAggregation settings
collectionYesCollection name
expandNoRelations to expand
filterNoFilter expression
sortNoSort expression

Implementation Reference

  • Core handler implementation for the 'query_collection' tool. Creates a ToolHandler that executes PocketBase queries with filter, sort, expand, and aggregate (sum, avg, count) capabilities.
    export function createQueryCollectionHandler(pb: PocketBase): ToolHandler {
      return async (args: QueryCollectionArgs) => {
        try {
          const collection = pb.collection(args.collection);
          const options: any = {};
          
          if (args.filter) options.filter = args.filter;
          if (args.sort) options.sort = args.sort;
          if (args.expand) options.expand = args.expand;
          
          const records = await collection.getList(1, 100, options);
          
          const result: any = { items: records.items };
          
          if (args.aggregate) {
            const aggregations: any = {};
            for (const [name, expr] of Object.entries(args.aggregate)) {
              const [func, field] = (expr as string).split("(");
              const cleanField = field.replace(")", "");
              
              switch (func) {
                case "sum":
                  aggregations[name] = records.items.reduce(
                    (sum: number, record: any) => sum + (record[cleanField] || 0),
                    0
                  );
                  break;
                case "avg":
                  aggregations[name] =
                    records.items.reduce(
                      (sum: number, record: any) => sum + (record[cleanField] || 0),
                      0
                    ) / records.items.length;
                  break;
                case "count":
                  aggregations[name] = records.items.length;
                  break;
                default:
                  throw new McpError(
                    ErrorCode.InvalidParams,
                    `Unsupported aggregation function: ${func}`
                  );
              }
            }
            result.aggregations = aggregations;
          }
          
          return createJsonResponse(result);
        } catch (error: unknown) {
          throw handlePocketBaseError("query collection", error);
        }
      };
    }
  • JSON schema defining the input parameters for the 'query_collection' tool, including collection, filter, sort, aggregate, and expand.
    export const queryCollectionSchema = {
      type: "object",
      properties: {
        collection: {
          type: "string",
          description: "Collection name",
        },
        filter: {
          type: "string",
          description: "Filter expression",
        },
        sort: {
          type: "string",
          description: "Sort expression",
        },
        aggregate: {
          type: "object",
          description: "Aggregation settings",
        },
        expand: {
          type: "string",
          description: "Relations to expand",
        },
      },
      required: ["collection"],
    };
  • src/server.ts:172-176 (registration)
    Registration of the 'query_collection' tool in the MCP server, specifying name, description, input schema, and handler factory.
      name: "query_collection",
      description: "Advanced query with filtering, sorting, and aggregation",
      inputSchema: queryCollectionSchema,
      handler: createQueryCollectionHandler(pb),
    },
  • TypeScript interface defining the argument types for the query_collection handler, matching the JSON schema.
    export interface QueryCollectionArgs {
      collection: string;
      filter?: string;
      sort?: string;
      aggregate?: Record<string, string>;
      expand?: string;
    }
Behavior2/5

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

No annotations are provided, so the description carries full burden for behavioral disclosure. It mentions 'advanced query' but doesn't clarify if this is read-only, has side effects, requires authentication, or handles pagination/rate limits. For a query tool with complex parameters and no annotations, this leaves critical behavioral traits unspecified.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's capabilities. It's front-loaded with the core purpose ('Advanced query') and lists key features without unnecessary words. However, it could be slightly more structured by explicitly mentioning the resource (collections).

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 (5 parameters, nested objects, no output schema) and lack of annotations, the description is incomplete. It doesn't explain return values, error handling, or provide enough context for an agent to use it effectively alongside siblings. For an advanced query tool in a data management system, more guidance is needed.

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 5 parameters adequately. The description adds minimal value by naming the capabilities (filtering, sorting, aggregation) that correspond to some parameters, but doesn't provide additional syntax, format details, or examples beyond what's in the schema. This meets the baseline for high schema coverage.

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

Purpose3/5

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

The description states the tool performs an 'advanced query' with specific capabilities (filtering, sorting, aggregation), which gives a general sense of purpose. However, it doesn't specify what resource is being queried (collections) or distinguish it clearly from sibling tools like 'list_records' or 'analyze_collection_data'. The purpose is somewhat vague about scope and differentiation.

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?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites, when it's appropriate compared to simpler tools like 'list_records', or any exclusions. With multiple sibling tools for data operations, this lack of contextual guidance is a significant gap.

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/fadlee/dynamic-pocketbase-mcp'

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