query-table
Retrieve items from a DynamoDB table using specified key conditions, filters, and optional parameters to refine query results.
Instructions
Query items from a DynamoDB table based on conditions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expressionAttributeValues | Yes | Filter expression attribute values (JSON format) | |
| filterExpression | No | Filter expression (optional) | |
| indexName | No | Name of the index to use (optional) | |
| keyConditionExpression | Yes | Key condition expression (e.g: 'PK = :pk') | |
| limit | No | Maximum number of items to return | |
| projectionExpression | No | Projection expression (optional) | |
| tableName | Yes | Name of the table to query |
Implementation Reference
- src/index.ts:142-197 (handler)The handler function for the 'query-table' tool. It constructs a QueryCommandInput object from the input parameters and calls the queryTable helper to execute the DynamoDB query, then formats the response.async ({ tableName, keyConditionExpression, expressionAttributeValues, indexName, filterExpression, limit, projectionExpression, }) => { console.error('# query-table tool has been called'); try { const queryParams: QueryCommandInput = { TableName: tableName, KeyConditionExpression: keyConditionExpression, ExpressionAttributeValues: expressionAttributeValues, ProjectionExpression: projectionExpression, }; if (indexName) { queryParams.IndexName = indexName; } if (filterExpression) { queryParams.FilterExpression = filterExpression; } if (limit) { queryParams.Limit = limit; } if (projectionExpression) { queryParams.ProjectionExpression = projectionExpression; } const items = await queryTable(queryParams); return { content: [ { type: 'text', text: JSON.stringify(items, null, 2), }, ], }; } catch (error: any) { console.error('# Error executing query:', error); return { isError: true, content: [ { type: 'text', text: `Error occurred: ${error.message}`, }, ], }; } }
- src/index.ts:131-141 (schema)Zod schema defining the input parameters for the 'query-table' tool, including table name, key condition, attribute values, and optional filters.{ tableName: z.string().describe('Name of the table to query'), keyConditionExpression: z.string().describe("Key condition expression (e.g: 'PK = :pk')"), expressionAttributeValues: z .record(z.any()) .describe('Filter expression attribute values (JSON format)'), indexName: z.string().optional().describe('Name of the index to use (optional)'), filterExpression: z.string().optional().describe('Filter expression (optional)'), limit: z.number().optional().describe('Maximum number of items to return'), projectionExpression: z.string().optional().describe('Projection expression (optional)'), },
- src/index.ts:128-198 (registration)Registration of the 'query-table' MCP tool using server.tool(), specifying name, description, input schema, and handler function.server.tool( 'query-table', 'Query items from a DynamoDB table based on conditions', { tableName: z.string().describe('Name of the table to query'), keyConditionExpression: z.string().describe("Key condition expression (e.g: 'PK = :pk')"), expressionAttributeValues: z .record(z.any()) .describe('Filter expression attribute values (JSON format)'), indexName: z.string().optional().describe('Name of the index to use (optional)'), filterExpression: z.string().optional().describe('Filter expression (optional)'), limit: z.number().optional().describe('Maximum number of items to return'), projectionExpression: z.string().optional().describe('Projection expression (optional)'), }, async ({ tableName, keyConditionExpression, expressionAttributeValues, indexName, filterExpression, limit, projectionExpression, }) => { console.error('# query-table tool has been called'); try { const queryParams: QueryCommandInput = { TableName: tableName, KeyConditionExpression: keyConditionExpression, ExpressionAttributeValues: expressionAttributeValues, ProjectionExpression: projectionExpression, }; if (indexName) { queryParams.IndexName = indexName; } if (filterExpression) { queryParams.FilterExpression = filterExpression; } if (limit) { queryParams.Limit = limit; } if (projectionExpression) { queryParams.ProjectionExpression = projectionExpression; } const items = await queryTable(queryParams); return { content: [ { type: 'text', text: JSON.stringify(items, null, 2), }, ], }; } catch (error: any) { console.error('# Error executing query:', error); return { isError: true, content: [ { type: 'text', text: `Error occurred: ${error.message}`, }, ], }; } } );
- src/dynamo-helpers.ts:83-110 (helper)Helper function queryTable that executes the actual DynamoDB QueryCommand using the AWS SDK and returns the items.export async function queryTable(params: QueryCommandInput) { console.error('# Starting queryTable function'); console.error('# Query parameters:', params); try { const command = new QueryCommand(params); console.error('# Query command created successfully'); const response = await dynamodb.send(command); console.error('# Query response received:', response); if (!response) { console.error('# Response is undefined'); return []; } if (!response.Items) { console.error('# Response has no Items'); return []; } console.error(`# Found ${response.Items.length} items`); return response.Items; } catch (error) { console.error('# Error in queryTable function:', error); throw error; } }