Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

connect

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

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 that handles the tool logic: connects to MongoDB using the session's connectToMongoDB with the given connection string and returns a success message.
    protected override async execute({ connectionString }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
        await this.session.connectToMongoDB({ connectionString });
    
        return {
            content: [{ type: "text", text: "Successfully connected to MongoDB." }],
        };
    }
  • Input schema definition using Zod for the connectionString argument.
    // Here the default is empty just to trigger registration, but we're going to override it with the correct
    // schema in the register method.
    protected override argsShape = {
        connectionString: z.string().describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"),
    };
  • Exports the ConnectTool class from its implementation file.
    export { ConnectTool } from "./connect/connect.js";
  • Collects all tools including MongoDbTools (which includes ConnectTool) into AllTools array used by the server.
    export const AllTools: ToolClass[] = Object.values({
        ...MongoDbTools,
        ...AtlasTools,
        ...AtlasLocalTools,
    });
  • src/server.ts:277-291 (registration)
    Registers all tools from toolConstructors (defaults to AllTools) by instantiating and calling register on each.
    private registerTools(): void {
        for (const toolConstructor of this.toolConstructors) {
            const tool = new toolConstructor({
                category: toolConstructor.category,
                operationType: toolConstructor.operationType,
                session: this.session,
                config: this.userConfig,
                telemetry: this.telemetry,
                elicitation: this.elicitation,
                uiRegistry: this.uiRegistry,
            });
            if (tool.register(this)) {
                this.tools.push(tool);
            }
        }

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