How to Use the MongoDB MCP Server for NoSQL Data Access
Connect MongoDB databases to AI agents for document querying, aggregation pipelines, and schema exploration using MCP.
Use the MongoDB MCP Server for NoSQL Data Access
The MongoDB MCP server bridges the gap between unstructured document databases and intelligent AI agents. It allows LLMs to query NoSQL data, build complex aggregation pipelines, and infer dynamic schemas on the fly.
Prerequisites
- ▸A MongoDB instance running locally, or a remote MongoDB Atlas cluster.
- ▸Your connection string (URI).
- ▸Node.js 18+ installed on the machine running the MCP server.
Security Best Practices
Before connecting your agent, restrict its permissions to prevent accidental data deletion:
- ▸Create a dedicated read-only user with only
readrole permissions. - ▸Use Atlas IP whitelists to restrict access to the IP running the MCP server.
// Run this in mongosh to create a read-only user
db.createUser({
user: "mcp_reader",
pwd: "secure_password",
roles: [{ role: "read", db: "mydb" }]
});Configuration Setup
Add the following to your Claude Desktop config.
Local MongoDB Connection
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "mcp-server-mongodb"],
"env": {
"MONGODB_URI": "mongodb://mcp_reader:secure_password@/mydb"
}
}
}
}MongoDB Atlas Connection
If connecting to Atlas, make sure to include the mongodb+srv prefix and retryWrites=true&w=majority.
{
"env": {
"MONGODB_URI": "mongodb+srv://mcp_reader:secure_password@cluster.mongodb.net/mydb?retryWrites=true&w=majority"
}
}Available Tools
The agent receives a full suite of NoSQL tools:
| Tool | Description |
|---|---|
find | Query documents with standard BSON filters |
aggregate | Run powerful aggregation pipelines |
list_collections | List all collections in the DB |
collection_schema | Infer the schema by randomly sampling documents |
count | Count matching documents |
(Note: insert_one, update_one, and delete_one are also available, but require the user to have write privileges. We strongly recommend sticking to read-only roles.)
Example Prompts
- ▸Basic Queries: "Use the
findtool to locate all users in the 'users' collection who signed up in the last 30 days." - ▸Aggregation Analysis: "Create an aggregation pipeline to group revenue by product category per month, then execute it and show me the results."
- ▸Schema Exploration: "What collections exist in the database? Run
collection_schemaon the 'transactions' collection so you understand its structure."
Troubleshooting
- ▸
AuthenticationFailed: Double check yourMONGODB_URI. Ensure any special characters in the password are properly URL-encoded. - ▸Connection Timeout: If using Atlas, ensure the IP address of the machine running the MCP server is whitelisted in the Atlas Network Access panel.
- ▸Result Size Too Large: Fetching thousands of massive JSON documents will break the JSON-RPC limits. Instruct the agent to use
limit: 50or projection filters to narrow down the payload.
Using with OpenAI Codex
You can use this MCP server with the OpenAI Codex CLI by adding it to your configuration:
codex mcp add --name mongodb --command "npx -y @modelcontextprotocol/server-mongodb mongodb:///mydb"Note: Replace the connection string with your actual MongoDB URL.
For a full list of recommended servers, see Best MCP Servers for OpenAI Codex.
Related Guides
How to Use the MySQL MCP Server for Database Queries
Connect MySQL databases to your AI agent for querying, schema exploration, and data analysis using the MySQL MCP server.
DatabasesHow to Use the SQLite MCP Server for Local Database Analysis
Learn how to connect SQLite databases to your AI agent for local data analysis, querying, and schema exploration using the MCP protocol.
DatabasesHow to Connect PostgreSQL to AI Agents Using MCP
Step-by-step guide to setting up the PostgreSQL MCP server. Give your AI agent read-only access to query your PostgreSQL databases safely.