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
Interactive Tool
1-Click Export

Generate & Test MongoDB MCP Server Config

One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.

Open in Generator

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.

Ready to Deploy?

Build your full agent toolstack in the Visual Generator

Combine MongoDB with databases, search APIs, and memory graphs in a single configuration file.

Customize in Generator

Related Guides