Databases·
intermediate
·11 min read·Apr 4, 2026

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.

MongoDBNoSQLdatabasedocumentsaggregationarchitecture

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

  1. A MongoDB instance running locally, or a remote MongoDB Atlas cluster.
  2. Your connection string (URI).
  3. 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:

  1. Create a dedicated read-only user with only read role permissions.
  2. Use Atlas IP whitelists to restrict access to the IP running the MCP server.
javascript
// 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

json
{
  "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.

json
{
  "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:

ToolDescription
findQuery documents with standard BSON filters
aggregateRun powerful aggregation pipelines
list_collectionsList all collections in the DB
collection_schemaInfer the schema by randomly sampling documents
countCount 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 find tool 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_schema on the 'transactions' collection so you understand its structure."

Troubleshooting

  • AuthenticationFailed: Double check your MONGODB_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: 50 or 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:

bash
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