Skip to main content
Glama
officialpraise

MongoDB MCP Server

create-index

Create indexes for MongoDB collections to improve query performance and enable efficient data retrieval.

Instructions

Create an index for a collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
nameNoThe name of the index
definitionYesThe index definition. Use 'classic' for standard indexes.

Implementation Reference

  • The main handler function that connects to MongoDB and calls createIndexes to create the specified index on the collection.
    protected async execute({
        database,
        collection,
        keys,
        name,
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
        const provider = await this.ensureConnected();
        const indexes = await provider.createIndexes(database, collection, [
            {
                key: keys,
                name,
            },
        ]);
    
        return {
            content: [
                {
                    text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}"`,
                    type: "text",
                },
            ],
        };
    }
  • Zod schema defining the input arguments for the create-index tool, including database, collection (from DbOperationArgs), keys for the index, and optional name.
    protected argsShape = {
        ...DbOperationArgs,
        keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
        name: z.string().optional().describe("The name of the index"),
    };
  • Registers CreateIndexTool (imported from ./create/createIndex.js) in the array of all MongoDB tools.
    export const MongoDbTools = [
        ConnectTool,
        ListCollectionsTool,
        ListDatabasesTool,
        CollectionIndexesTool,
        CreateIndexTool,
        CollectionSchemaTool,
        FindTool,
        InsertManyTool,
        DeleteManyTool,
        CollectionStorageSizeTool,
        CountTool,
        DbStatsTool,
        AggregateTool,
        UpdateManyTool,
        RenameCollectionTool,
        DropDatabaseTool,
        DropCollectionTool,
        ExplainTool,
        CreateCollectionTool,
        LogsTool,
    ];
  • Defines the tool name as 'create-index' within the CreateIndexTool class.
    protected name = "create-index";

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/officialpraise/mcp'

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