Skip to main content
Glama
officialpraise

MongoDB MCP Server

connect

Establish a connection to a MongoDB cluster using a connection string. Use this tool to initiate or switch database connections for performing operations through the MongoDB MCP Server.

Instructions

Connect to a MongoDB instance. The config resource captures if the server is already connected to a MongoDB cluster. If the user has configured a connection string or has previously called the connect tool, a connection is already established and there's no need to call this tool unless the user has explicitly requested to switch to a new MongoDB cluster.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionStringYesMongoDB connection string (in the mongodb:// or mongodb+srv:// format)

Implementation Reference

  • The execute method implements the core logic of the 'connect' tool, handling connection or switching based on current state.
    protected async execute({ connectionString }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
        switch (this.name) {
            case disconnectedName:
                assert(connectionString, "Connection string is required");
                break;
            case connectedName:
                connectionString ??= this.config.connectionString;
                assert(
                    connectionString,
                    "Cannot switch to a new connection because no connection string was provided and no default connection string is configured."
                );
                break;
        }
    
        await this.connectToMongoDB(connectionString);
        this.updateMetadata();
        return {
            content: [{ type: "text", text: "Successfully connected to MongoDB." }],
        };
    }
  • Zod schemas for input validation: 'disconnectedSchema' for initial 'connect', 'connectedSchema' for 'switch-connection'.
    const disconnectedSchema = z
        .object({
            connectionString: z.string().describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"),
        })
        .describe("Options for connecting to MongoDB.");
    
    const connectedSchema = z
        .object({
            connectionString: z
                .string()
                .optional()
                .describe("MongoDB connection string to switch to (in the mongodb:// or mongodb+srv:// format)"),
        })
        .describe(
            "Options for switching the current MongoDB connection. If a connection string is not provided, the connection string from the config will be used."
        );
  • src/server.ts:142-144 (registration)
    Server-wide registration loop that instantiates ConnectTool (via MongoDbTools) and calls its register method on the MCP server.
    for (const tool of [...AtlasTools, ...MongoDbTools]) {
        new tool(this.session, this.userConfig, this.telemetry).register(this.mcpServer);
    }
  • Export of MongoDbTools array including ConnectTool, imported into server.ts for registration.
    import { ConnectTool } from "./metadata/connect.js";
    import { ListCollectionsTool } from "./metadata/listCollections.js";
    import { CollectionIndexesTool } from "./read/collectionIndexes.js";
    import { ListDatabasesTool } from "./metadata/listDatabases.js";
    import { CreateIndexTool } from "./create/createIndex.js";
    import { CollectionSchemaTool } from "./metadata/collectionSchema.js";
    import { FindTool } from "./read/find.js";
    import { InsertManyTool } from "./create/insertMany.js";
    import { DeleteManyTool } from "./delete/deleteMany.js";
    import { CollectionStorageSizeTool } from "./metadata/collectionStorageSize.js";
    import { CountTool } from "./read/count.js";
    import { DbStatsTool } from "./metadata/dbStats.js";
    import { AggregateTool } from "./read/aggregate.js";
    import { UpdateManyTool } from "./update/updateMany.js";
    import { RenameCollectionTool } from "./update/renameCollection.js";
    import { DropDatabaseTool } from "./delete/dropDatabase.js";
    import { DropCollectionTool } from "./delete/dropCollection.js";
    import { ExplainTool } from "./metadata/explain.js";
    import { CreateCollectionTool } from "./create/createCollection.js";
    import { LogsTool } from "./metadata/logs.js";
    
    export const MongoDbTools = [
        ConnectTool,
  • Dynamic tool registration/update: switches between 'connect' and 'switch-connection' based on connection status.
        public register(server: McpServer): void {
            super.register(server);
    
            this.updateMetadata();
        }
    
        private updateMetadata(): void {
            if (this.config.connectionString || this.session.serviceProvider) {
                this.update?.({
                    name: connectedName,
                    description: connectedDescription,
                    inputSchema: connectedSchema,
                });
            } else {
                this.update?.({
                    name: disconnectedName,
                    description: disconnectedDescription,
                    inputSchema: disconnectedSchema,
                });
            }
        }
    }

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