mcp-server-mongodb
Provides read-only access to a MongoDB database, enabling collection listing, schema inference from samples, document queries with filters/projections/sorting, read-only aggregations, and counts, with configurable allowlists and operation limits.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mcp-server-mongodbdescribe the movies collection and show a few sample documents"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
mcp-server-mongodb
MCP server that gives LLMs safe, read-only access to MongoDB.
What it does
A Model Context Protocol server over stdio that lets an MCP client (Claude Code, Claude Desktop, ...) explore and query one MongoDB database without being able to change it.
Tool | Purpose |
| Collections the server exposes (respects the allowlist) |
| Inferred field types and presence from a random sample |
| Query documents, hard-capped at 100 |
| Read-only aggregation, hard-capped at 100 results |
| Count matching documents |
It also serves the resource template mongodb://schema/{collection} with the same inferred schema.
Related MCP server: MongoDB Mongoose MCP
Architecture
flowchart LR
Client[MCP client] <-- stdio --> Server[McpServer]
Server --> Tools[tools/*]
Tools --> Guard[validate.ts<br/>operator denylist, allowlist, limit]
Tools --> Infer[schema.ts]
Guard --> Driver[MongoDB driver<br/>maxTimeMS]
Infer --> Driver
Driver --> DB[(MongoDB)]Every tool body runs through respond(), which turns any failure (validation, allowlist, driver error) into a tool result with isError: true so the model can read the message and retry.
Quickstart
npm install
# the server does not load .env: export the variables or set them in your MCP client config
npm run build
MONGODB_URI=mongodb://localhost:27017 MONGODB_DB=sample node dist/index.jsVariable | Required | Meaning |
| yes | Connection string |
| yes | Database to expose |
| no | Comma-separated collections; unset exposes every non- |
| no | Per-operation server-side time limit, default |
Missing required variables abort startup with a message naming the variable. Logs are JSON on stderr only; stdout is the protocol.
Client configuration: examples/claude-code.mcp.json (local Node) and examples/claude-desktop.json (Docker, built with docker build -t mcp-server-mongodb .).
Usage
Example tool calls an LLM might issue (synthetic movies collection):
{ "name": "find", "arguments": { "collection": "movies", "filter": { "year": { "$gte": 2000 } }, "sort": { "year": -1 }, "limit": 5 } }
{ "name": "aggregate", "arguments": { "collection": "movies", "pipeline": [{ "$group": { "_id": "$year", "n": { "$sum": 1 } } }] } }Results are relaxed Extended JSON, and filters accept it too ({"_id": {"$oid": "..."}}, {"$date": "..."}), so ObjectIds and dates round-trip.
Rejected, with an explanatory error: $out / $merge, $function / $accumulator / $where at any depth (also in filters and projections), and $lookup / $graphLookup / $unionWith into collections outside the allowlist.
Design decisions & trade-offs
Denylist plus allowlist, enforced by a deep walk. Validation walks the whole pipeline (including
$facetand$lookupsub-pipelines) rather than inspecting only top-level stages, because the dangerous operators can hide at any depth. The cost is that it can reject a legitimate document whose field happens to be named like an operator.Not a substitute for database permissions. The server is read-only by construction, but the right defence in depth is a MongoDB user with the
readrole only. Use one.Limits are clamped, not rejected.
findlimits above 100 are lowered and aggregations always end in a$limitof at most 100, so a model asking for "everything" gets a useful capped answer. Every operation also carriesmaxTimeMS.Schema inference samples.
describe_collectionuses$sampleand reports presence as a fraction, which is cheap on large collections but can miss rare fields.Tool errors, not protocol errors. Failures return
isError: truewith a short message; collection names outside the allowlist are reported as "not available" without confirming they exist.Shared tool helpers live in
src/tools/shared.tsso each tool file stays a single registration function.
Testing
npm testUnit tests cover pipeline/filter validation, limit enforcement, schema inference and env loading. test/tools.test.ts starts a mongodb-memory-server, seeds 150 synthetic documents and drives the server through the SDK's InMemoryTransport client (limit capping, allowlist, $out and $lookup rejection, schema resource). The first run downloads a mongod binary. CI runs lint, typecheck and tests on every push.
Limitations & next steps
Single database per server instance;
$lookupwith a cross-database{db, coll}reference is rejected.Stdio transport only; no Streamable HTTP or auth layer.
Aggregation stages that expose server internals (for example
$collStats) are not blocked beyond the allowlist; rely on a least-privilege database user.Results are not paginated; the 100-document cap is fixed.
License
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Astra DB tools for agents: schema, find, vector search, guarded writes, and interactive views.
Read-only MCP tools for AI agent discovery, structured resources, and NIULAI information.
Related MCP Servers
- FlicenseNot gradedqualityFmaintenanceProvides read-only access to MongoDB databases for LLMs to inspect collection schemas and execute aggregation pipelines.3 npm27-
- AlicenseNot gradedqualityDmaintenanceEnables Claude to interact with MongoDB databases through natural language, supporting queries, aggregations, CRUD operations, and index management with optional Mongoose schema validation.8 npm3MIT
- FlicenseNot gradedqualityDmaintenanceEnables AI agents to query and introspect MongoDB databases through find, aggregate, and schema discovery operations. Supports read-only default mode with optional write capabilities and works with any MCP-compatible client via stdio or HTTP transports.-
- AlicenseBqualityDmaintenanceEnables AI assistants to interact with MongoDB 3.6+ databases, supporting collection listing, document queries, counting, and aggregations through natural language.51MIT