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
| Name | Required | Description | Default |
|---|---|---|---|
| connectionString | Yes | MongoDB 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); } - src/tools/mongodb/tools.ts:1-23 (registration)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, - src/tools/mongodb/metadata/connect.ts:75-96 (registration)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, }); } } }