Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

connect

Read-only

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);
            }
        }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations indicate readOnlyHint=true and destructiveHint=false, which the description aligns with by describing a connection-establishment action without implying data modification. The description adds context about checking existing connections and switching clusters, which is useful beyond annotations. However, it lacks details on error handling, timeout behavior, or authentication requirements that aren't covered by annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and well-structured, consisting of two sentences that efficiently convey purpose and usage guidelines. Every sentence adds value: the first states the core function, and the second provides critical context about when to use or avoid the tool. There is no redundant or unnecessary information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (connection management), the description is reasonably complete. It covers purpose, usage conditions, and existing connection checks. However, without an output schema, it doesn't describe what happens after connection (e.g., success/failure responses), and annotations only cover safety aspects. For a connection tool, more behavioral details (like error cases) would enhance completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with the 'connectionString' parameter fully documented in the schema. The description doesn't add any parameter-specific details beyond what the schema provides, such as format examples or validation rules. With high schema coverage, a baseline score of 3 is appropriate as the description doesn't compensate but doesn't need to.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Connect to a MongoDB instance.' It specifies the verb ('Connect') and resource ('MongoDB instance'), making the intent unambiguous. However, it doesn't explicitly differentiate from sibling tools like 'atlas-connect-cluster', which might serve a similar purpose but for Atlas-specific connections.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool: '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.' It clearly states conditions for usage and non-usage, helping the agent avoid redundant calls.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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