Purpose
MongoDB Lens is a local Model Context Protocol (MCP) server that provides comprehensive access to MongoDB databases. Users can use natural language, with the help of a large language model (LLM), to execute queries, run aggregations, optimize performance, and more. As an MCP server, it must be used with MCP-compatible clients (such as Claude Desktop, Cursor, etc.).
Main Capabilities
MongoDB Lens provides a large number of "tools" covering everyday management and development scenarios for MongoDB:
- Connection and database management: add-connection-alias, connect-mongodb, connect-original, use-database, create-database, current-database, list-connections, list-databases, list-collections, databases, collections.
- Data and queries: find-documents, count-documents, distinct-values, text-search, geo-query, collation-query, aggregate-data, explain-query, analyze-query-patterns, aggregation-builder, query-builder.
- Writes and modifications: insert-document, update-document, delete-document (confirmation required), bulk-operations (destructive operations require confirmation), transaction (ACID transactions).
- Collections and indexes: create-collection, drop-collection (confirmation required), rename-collection (confirmation required when deleting the target), create-index, drop-index (confirmation required), collection-indexes, drop-database (confirmation required).
- Schema and validation: analyze-schema, compare-schemas, collection-schema, generate-schema-validator, validate-collection, collection-validation, schema-analysis, schema-versioning.
- Users and security: create-user, drop-user (confirmation required), database-users, security-audit.
- Performance and status: get-stats, collection-stats, performance-metrics, server-status, replica-status, shard-status, database-health-check, index-recommendation, query-optimizer.
- Data import/export and files: export-data (JSON/CSV), gridfs-operation.
- Real-time monitoring: watch-changes, database-triggers.
- Other assistance: clear-cache, stored-functions, backup-strategy, migration-guide, mongo-shell, multi-tenant-design, sql-to-mongodb.
In addition, it also includes resources (such as collection-indexes, collection-schema, collection-stats, collection-validation, collections, database-triggers, database-users, databases, performance-metrics, replica-status, server-status, stored-functions) and prompts (such as aggregation-builder, backup-strategy, data-modeling, database-health-check, index-recommendation, migration-guide, mongo-shell, multi-tenant-design, query-builder, query-optimizer, schema-analysis, schema-versioning, security-audit, sql-to-mongodb).
Other Features
MongoDB Lens also provides:
- Configuration file: customize via ~/.mongodb-lens.[jsonc|json], supporting both .json and .jsonc formats.
- Environment variable overrides: override configuration in the form CONFIG_*, with environment variables taking precedence over the configuration file.
- Confirmation system: destructive operations use two-step verification.
- Multiple connections: define and switch between named URI aliases.
- Component disabling: selectively disable tools, prompts, or resources.
- Connection resilience: automatic reconnection with exponential backoff.
- Query protection: configurable limits and performance safeguards.
- Error handling: robust JSONRPC error codes and messages.
- Schema inference: efficient schema analysis through intelligent sampling.
- Credential protection: obfuscation of connection string passwords in logs.
- Memory management: automatic monitoring and cleanup for large operations.
- Smart caching: optimized caching for schemas, indexes, fields, and collections.
- Backward compatibility: supports both modern and legacy MongoDB.
MongoDB Lens inserts a metadata collection into every database it creates, storing a document containing context information as a permanent record of the database's origin, while also ensuring that new empty databases persist in MongoDB storage. Once the user adds their own collections to the new database, this metadata collection can be safely removed using the drop-collection tool (e.g., "Drop the new database's metadata collection" ➥ use the drop-collection tool, confirmation required).
Usage
Installation and Running
MongoDB Lens can be installed and run in the following ways:
- NPX (Node.js must be installed): npx -y mongodb-lens; if you encounter permission errors, first run npx clear-npx-cache.
- Docker Hub (Docker required): docker run --rm -i --network=host furey/mongodb-lens (the original text only provides the method,for the specific image run command, refer to the official source).
- Node.js source:
- git clone https://github.com/furey/mongodb-lens.git
- cd /path/to/mongodb-lens
- Check the Node.js version: node --version (ideally >= v22.x, compatible with >= v18.x)
- Install dependencies: npm ci
- Start: node mongodb-lens.js (default connection to mongodb://localhost:27017) or node mongodb-lens.js mongodb://your-connection-string
- Docker source:
- Check Docker: docker --version (ideally >= v27.x)
- Build the image: docker build -t mongodb-lens .
- Run: docker run --rm -i --network=host mongodb-lens (default connection) or docker run --rm -i --network=host mongodb-lens mongodb://your-connection-string
Installation verification: paste and run a JSONRPC message into the server's stdio, and the server should return a list of databases in the MongoDB instance.
Legacy MongoDB
If the connected MongoDB version is < 4.0, the Node.js driver used by the latest MongoDB Lens is incompatible (driver 4.0.0+ requires MongoDB 4.0+). You need to use the 3.x series driver (e.g., 3.7.4 is compatible with MongoDB 3.6):
- Modify package.json: change "mongodb": "^6.15.0" to "mongodb": "^3.7.4" (or a compatible version)
- Install dependencies: npm install
- Start: node mongodb-lens.js mongodb://older-mongodb-instance
- You may also need to revert a certain commit to restore the useNewUrlParser and useUnifiedTopology configuration options.
- If using NPX or Docker, you need to use an older version of Mon released with a compatible drivergoDB Lens, such as MongoDB Lens 8.3.0 using driver 3.7.4, is specified via a version tag.
Configuration
- Connection string: The server accepts a MongoDB connection string as its only argument. Examples: mongodb://localhost:27017; mongodb://username:password@hostname:27017/mydatabase?authSource=admin; mongodb://hostname:27017/mydatabase?retryWrites=true&w=majority. If not provided, it attempts a local connection.
- Configuration file: Supports ~/.mongodb-lens.jsonc (preferred) or ~/.mongodb-lens.json, optional. The path can be customized via the CONFIG_PATH environment variable. Use the config:create script to automatically generate a configuration and specify the output location.
- Multiple connections: In the configuration file, set mongoUri to an alias-URI object; the first item in the list is the default connection at startup; you can switch using natural language (e.g. "Connect to backup", "Connect to atlas"), and you can also use add-connection-alias to add an alias at runtime.
- Environment variable overrides: Named with CONFIG_*, they take precedence over the configuration file; use the strings 'true'/'false' for booleans, strings for numbers, and JSON strings for nested objects or arrays. cross-env can be used cross-platform.
MCP client setup
Using Claude Desktop as an example: install Claude Desktop; open claude_desktop_config.json (create it if it does not exist): macOS path ~/Library/Application Support/Claude/claude_desktop_config.json; Windows path %APPDATA%\Claude\claude_desktop_config.json. For other MCP clients (such as Cursor), refer to their respective documentation.
Applicable scenarios
Developers, DBAs, or data analysts who need to explore, query,optimize, and manage MongoDB databases using natural language; scenarios where you want to use LLMs to quickly perform tasks such as aggregation, schema analysis, performance diagnostics, SQL to MongoDB conversion, and multi-tenant design.
Limitations
- Destructive operations (deletes, dropping collections/databases/indexes/users, etc.) require confirmation.
- Older MongoDB versions (< 4.0) require manually switching to the 3.x driver and may need fallback code adjustments, or use an older version of MongoDB Lens.
- Must be used with a client that supports MCP.
- Specific pricing, cost, and similar information is not yet provided.