Skip to main content
Glama
jjikky

DynamoDB Read-Only MCP

by jjikky

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
NameRequiredDescriptionDefault
expressionAttributeValuesYesFilter expression attribute values (JSON format)
filterExpressionNoFilter expression (optional)
indexNameNoName of the index to use (optional)
keyConditionExpressionYesKey condition expression (e.g: 'PK = :pk')
limitNoMaximum number of items to return
projectionExpressionNoProjection expression (optional)
tableNameYesName of the table to query

Implementation Reference

  • 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}`,
            },
          ],
        };
      }
    }
  • 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}`,
              },
            ],
          };
        }
      }
    );
  • 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;
      }
    }
Install Server

Other Tools

Related 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/jjikky/dynamo-readonly-mcp'

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