Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

collection-indexes

Retrieve and describe indexes for a specified MongoDB collection to optimize query performance and database management.

Instructions

Describe the indexes for a collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name

Implementation Reference

  • The main handler function that connects to MongoDB, retrieves regular and search indexes for the specified database and collection, formats them as JSON strings, and returns a CallToolResult.
    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 for the tool: database and collection names. Referenced as argsShape in the tool class.
    export const DbOperationArgs = { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), };
  • Tool class definition including the tool 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";
  • Helper function to extract and simplify search index details for the tool 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, })); }
  • Re-export of the CollectionIndexesTool for central import in the MongoDB tools module.
    export { CollectionIndexesTool } from "./metadata/collectionIndexes.js";

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