Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

collection-indexes

Describe indexes for a MongoDB collection to understand query performance and optimize database operations by analyzing index structure and usage.

Instructions

Describe the indexes for a collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name

Implementation Reference

  • The main handler function that connects to MongoDB, fetches regular indexes and optional search indexes, formats them as JSON strings in the tool response.
    protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
        const provider = await this.ensureConnected();
        const indexes = await provider.getIndexes(database, collection);
        const indexDefinitions: IndexStatus[] = indexes.map((index) => ({
            name: index.name as string,
            key: index.key as Document,
        }));
    
        const searchIndexDefinitions: SearchIndexStatus[] = [];
        if (this.isFeatureEnabled("search") && (await this.session.isSearchSupported())) {
            const searchIndexes = await provider.getSearchIndexes(database, collection);
            searchIndexDefinitions.push(...this.extractSearchIndexDetails(searchIndexes));
        }
    
        return {
            content: [
                ...formatUntrustedData(
                    `Found ${indexDefinitions.length} indexes in the collection "${collection}":`,
                    ...indexDefinitions.map((i) => JSON.stringify(i))
                ),
                ...(searchIndexDefinitions.length > 0
                    ? formatUntrustedData(
                          `Found ${searchIndexDefinitions.length} search and vector search indexes in the collection "${collection}":`,
                          ...searchIndexDefinitions.map((i) => JSON.stringify(i))
                      )
                    : []),
            ],
        };
  • Zod schema defining the input arguments 'database' and 'collection' used by the collection-indexes tool and other MongoDB tools.
    export const DbOperationArgs = {
        database: z.string().describe("Database name"),
        collection: z.string().describe("Collection name"),
    };
  • Tool class definition including the unique name 'collection-indexes', description, input schema reference, and operation type.
    export class CollectionIndexesTool extends MongoDBToolBase {
        public name = "collection-indexes";
        protected description = "Describe the indexes for a collection";
        protected argsShape = DbOperationArgs;
        static operationType: OperationType = "metadata";
  • Re-export of the CollectionIndexesTool class from the MongoDB tools barrel file for easy import and registration.
    export { CollectionIndexesTool } from "./metadata/collectionIndexes.js";
  • Helper method to extract relevant details from search index definitions, simplifying status, queryability, and definition for the response.
    protected extractSearchIndexDetails(indexes: Record<string, unknown>[]): SearchIndexStatus[] {
        return indexes.map((index) => ({
            name: (index["name"] ?? "default") as string,
            type: (index["type"] ?? "UNKNOWN") as string,
            status: (index["status"] ?? "UNKNOWN") as string,
            queryable: (index["queryable"] ?? false) as boolean,
            latestDefinition: index["latestDefinition"] as Document,
        }));
    }

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/mongodb-js/mongodb-mcp-server'

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