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
| Name | Required | Description | Default |
|---|---|---|---|
| aggregate | No | Aggregation settings | |
| collection | Yes | Collection name | |
| expand | No | Relations to expand | |
| filter | No | Filter expression | |
| sort | No | Sort expression |
Implementation Reference
- src/tools/handlers/analysis.ts:115-167 (handler)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); } }; }
- src/tools/schemas/analysis.ts:31-56 (schema)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), },
- src/types/index.ts:142-148 (schema)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; }