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; }

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

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