Skip to main content
Glama
datastax

Astra DB MCP Server

Official

ListRecords

Retrieve records from a specified collection in Astra DB, with options to limit the number of results returned for efficient data management.

Instructions

List records from a collection in the database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionNameYesName of the collection to list records from
limitNoMaximum number of records to return

Implementation Reference

  • Core handler function that executes the ListRecords tool: fetches records from the specified collection using db.collection().find(), applies limit, sanitizes output, handles errors.
    export async function ListRecords(params: {
      collectionName: string;
      limit?: number;
    }) {
      const { collectionName, limit = 10 } = params;
    
      const collection = db.collection(collectionName);
      
      try {
        // Try to use the limit method if available
        const cursor = collection.find({});
        
        // Handle the case when toArray is not available
        if (!cursor || typeof cursor.toArray !== 'function') {
          console.warn(`cursor.toArray is not available for collection '${collectionName}'`);
          return sanitizeRecordData([]);
        }
        
        if (typeof cursor.limit === 'function') {
          const records = await cursor.limit(limit).toArray();
          return sanitizeRecordData(records);
        } else {
          // Fallback if limit is not available
          const allRecords = await cursor.toArray();
          const limitedRecords = allRecords.slice(0, limit);
          return sanitizeRecordData(limitedRecords);
        }
      } catch (error) {
        console.error(`Error listing records from collection '${collectionName}':`, error);
        return sanitizeRecordData([]);
      }
    }
  • Input schema definition for the ListRecords tool, specifying parameters collectionName (required) and optional limit.
    {
      name: "ListRecords",
      description: "List records from a collection in the database",
      inputSchema: {
        type: "object",
        properties: {
          collectionName: {
            type: "string",
            description: "Name of the collection to list records from",
          },
          limit: {
            type: "number",
            description: "Maximum number of records to return",
            default: 10,
          },
        },
        required: ["collectionName"],
      },
    },
  • index.ts:140-154 (registration)
    Registration and dispatch logic in the MCP server's CallToolRequestSchema handler: imports ListRecords, calls it with arguments, sanitizes, and returns JSON response.
    case "ListRecords":
      const records = await ListRecords({
        collectionName: args.collectionName as string,
        limit: args.limit as number | undefined,
      });
      // Sanitize records to prevent prompt injection
      const sanitizedRecords = sanitizeRecordData(records);
      return {
        content: [
          {
            type: "text",
            text: JSON.stringify(sanitizedRecords, null, 2),
          },
        ],
      };

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/datastax/astra-db-mcp'

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