Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlNoMongoDB URL (mongodb://user:pass@host:port/db)
workspaceNoProject workspace path
databaseNoMongoDB database name

Implementation Reference

  • 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);
  • TypeScript interface defining the MongoDB configuration structure used by the connect_mongodb tool.
    interface MongoDBConfig { uri: string; database: string; options?: any; }
  • 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)}` ); } }

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/yaoxiaolinglong/mcp-mongodb-mysql-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server