connect_mongodb
Establish a connection to a MongoDB database using a connection URL or configuration parameters to enable database operations through the MCP server.
Instructions
Connect to MongoDB database using URL or config
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "connect_mongodb"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | No | MongoDB URL (mongodb://user:pass@host:port/db) | |
| workspace | No | Project workspace path | |
| database | No | MongoDB database name |
Implementation Reference
- src/index.ts:876-928 (handler)The main handler function for the connect_mongodb tool. Processes input arguments to establish a MongoDB connection, supporting URL, workspace .env, or env vars. Closes prior connections and uses MongoClient.
private async handleConnectMongoDB(args: any) { let config: MongoDBConfig | null = null; // 优先使用URL if (args.url) { config = this.parseMongoConnectionUrl(args.url); } // 其次使用工作区配置 else if (args.workspace) { this.currentWorkspace = args.workspace; config = await this.loadMongoWorkspaceConfig(args.workspace); } // 最后使用单独的参数 else if (args.database) { config = { uri: process.env.MONGODB_URI || 'mongodb://localhost:27017', database: args.database }; } if (!config) { throw new McpError( ErrorCode.InvalidParams, 'No valid MongoDB configuration provided. Please provide either a URL, workspace path, or database name.' ); } // 关闭现有连接 if (this.mongoClient) { await this.mongoClient.close(); this.mongoClient = null; this.mongoDB = null; } this.mongoConfig = config; try { await this.ensureMongoConnection(); return { content: [ { type: 'text', text: `Successfully connected to MongoDB database ${config.database}` } ] }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to MongoDB: ${getErrorMessage(error)}` ); } } - src/index.ts:234-258 (registration)Registers the connect_mongodb tool in the server's tool list for ListTools requests, defining its name, description, and input schema.
{ name: 'connect_mongodb', description: 'Connect to MongoDB database using URL or config', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'MongoDB URL (mongodb://user:pass@host:port/db)', optional: true }, workspace: { type: 'string', description: 'Project workspace path', optional: true }, database: { type: 'string', description: 'MongoDB database name', optional: true } }, // No required fields - will try different connection methods }, }, - src/index.ts:552-553 (registration)Dispatcher switch case that routes connect_mongodb tool calls to the handleConnectMongoDB handler.
case 'connect_mongodb': return await this.handleConnectMongoDB(request.params.arguments); - src/index.ts:27-31 (schema)TypeScript interface defining the MongoDB configuration structure used by the connect_mongodb tool.
interface MongoDBConfig { uri: string; database: string; options?: any; } - src/index.ts:175-203 (helper)Helper function to parse a MongoDB connection URL into a MongoDBConfig object.
private parseMongoConnectionUrl(url: string): MongoDBConfig { try { const parsed = parseUrl(url); if (!parsed.hostname) { throw new McpError( ErrorCode.InvalidParams, 'Invalid MongoDB connection URL' ); } const database = parsed.pathname?.slice(1); if (!database) { throw new McpError( ErrorCode.InvalidParams, 'Database name must be specified in MongoDB URL' ); } return { uri: url, database: database }; } catch (error) { throw new McpError( ErrorCode.InvalidParams, `Invalid MongoDB connection URL: ${getErrorMessage(error)}` ); } }