Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

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
NameRequiredDescriptionDefault
connectionStringYesMongoDB 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)"), };
  • Exports the ConnectTool class, making it available for registration in the overall tools collection.
    export { ConnectTool } from "./connect/connect.js";
  • 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, });
  • 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; }

Other Tools

Related 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