connect
Establish a connection to a MongoDB instance using a specific connection string, enabling interaction with MongoDB Atlas resources for database operations and management tasks.
Instructions
Connect to a MongoDB instance
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionString | Yes | MongoDB connection string (in the mongodb:// or mongodb+srv:// format) |
Implementation Reference
- The main handler function for the 'connect' tool. It connects to a MongoDB instance using the provided 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." }], }; }
- Zod schema defining the input arguments for the 'connect' tool, specifically the connectionString parameter.protected override argsShape = { connectionString: z.string().describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"), };
- src/tools/mongodb/tools.ts:1-1 (registration)Exports the ConnectTool class, making it available for registration in the overall tools collection.export { ConnectTool } from "./connect/connect.js";
- src/tools/index.ts:7-11 (registration)Collects all tools including those from MongoDB (which includes ConnectTool) into AllTools array used for server registration.export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
- src/tools/mongodb/connect/connect.ts:30-40 (registration)Custom registration logic for the ConnectTool, which disables itself once connected to MongoDB in favor of switchConnection tool.public override register(server: Server): boolean { const registrationSuccessful = super.register(server); /** * When connected to mongodb we want to swap connect with * switch-connection tool. */ if (registrationSuccessful && this.session.isConnectedToMongoDB) { this.disable(); } return registrationSuccessful; }