query-records
Retrieve PowerPlatform records by applying OData filter expressions to specific entities like accounts or contacts.
Instructions
Query records using an OData filter expression
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| entityNamePlural | Yes | The plural name of the entity (e.g., 'accounts', 'contacts') | |
| filter | Yes | OData filter expression (e.g., "name eq 'test'" or "createdon gt 2023-01-01") | |
| maxRecords | No | Maximum number of records to retrieve (default: 50) |
Implementation Reference
- src/index.ts:580-618 (registration)Full registration of the 'query-records' tool, including schema definition and handler function that delegates to PowerPlatformService.queryRecords.server.tool( "query-records", "Query records using an OData filter expression", { entityNamePlural: z.string().describe("The plural name of the entity (e.g., 'accounts', 'contacts')"), filter: z.string().describe("OData filter expression (e.g., \"name eq 'test'\" or \"createdon gt 2023-01-01\")"), maxRecords: z.number().optional().describe("Maximum number of records to retrieve (default: 50)"), }, async ({ entityNamePlural, filter, maxRecords }) => { try { // Get or initialize PowerPlatformService const service = getPowerPlatformService(); const records = await service.queryRecords(entityNamePlural, filter, maxRecords || 50); // Format the records as a string for text display const recordsStr = JSON.stringify(records, null, 2); const recordCount = records.value?.length || 0; return { content: [ { type: "text", text: `Retrieved ${recordCount} records from '${entityNamePlural}' with filter '${filter}':\n\n${recordsStr}`, }, ], }; } catch (error: any) { console.error("Error querying records:", error); return { content: [ { type: "text", text: `Failed to query records: ${error.message}`, }, ], }; } } );
- src/index.ts:583-587 (schema)Zod schema defining input parameters for the 'query-records' tool: entityNamePlural, filter, and optional maxRecords.{ entityNamePlural: z.string().describe("The plural name of the entity (e.g., 'accounts', 'contacts')"), filter: z.string().describe("OData filter expression (e.g., \"name eq 'test'\" or \"createdon gt 2023-01-01\")"), maxRecords: z.number().optional().describe("Maximum number of records to retrieve (default: 50)"), },
- src/index.ts:588-617 (handler)The handler function for executing the 'query-records' tool logic, formatting results as text content.async ({ entityNamePlural, filter, maxRecords }) => { try { // Get or initialize PowerPlatformService const service = getPowerPlatformService(); const records = await service.queryRecords(entityNamePlural, filter, maxRecords || 50); // Format the records as a string for text display const recordsStr = JSON.stringify(records, null, 2); const recordCount = records.value?.length || 0; return { content: [ { type: "text", text: `Retrieved ${recordCount} records from '${entityNamePlural}' with filter '${filter}':\n\n${recordsStr}`, }, ], }; } catch (error: any) { console.error("Error querying records:", error); return { content: [ { type: "text", text: `Failed to query records: ${error.message}`, }, ], }; } }
- src/PowerPlatformService.ts:260-262 (helper)Supporting helper method in PowerPlatformService that performs the actual OData API request for querying records.async queryRecords(entityNamePlural: string, filter: string, maxRecords: number = 50): Promise<ApiCollectionResponse<any>> { return this.makeRequest<ApiCollectionResponse<any>>(`api/data/v9.2/${entityNamePlural}?$filter=${encodeURIComponent(filter)}&$top=${maxRecords}`); }