Databases·
intermediate
·14 min read·Apr 4, 2026·Updated Oct 10, 2026
By Rad Tome·Lead AI Systems Architect·How we verify this

MongoDB MCP Server Configuration Guide

Connect MongoDB databases to Model Context Protocol agents: Step-by-step setup, read-only Atlas roles, aggregation pipelines, and schema exploration.

mongodbnosqldatabasedocumentsaggregationmcpatlasdata-analysis
Config Target: claude_desktop_config.json
{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-server-mongodb"
      ],
      "env": {
        "MONGODB_URI": "mongodb:///mydb"
      }
    }
  }
}
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

MongoDB MCP Server Configuration Guide

MongoDB is the industry standard for document-oriented NoSQL persistence, powering high-throughput mobile backends, event logging architectures, and dynamic catalog schemas. When developers explore polymorphic JSON payloads or build analytical reporting pipelines, querying unstructured collections traditionally requires switching between code editors and MongoDB Compass or executing raw scripts in the mongosh terminal.

By connecting MongoDB to the Model Context Protocol (MCP), developers can provide AI assistants in Claude Desktop, Cursor, or OpenAI Codex CLI with direct, structured access to NoSQL databases. Agents can dynamically infer document schemas across unstructured collections, construct multi-stage aggregation pipelines ($match, $group, $sort), and query documents using native BSON filter operators.

However, connecting an autonomous reasoning model to document databases requires strict read-only role scoping, field projection filtering, and connection timeout protections to prevent runaway full-collection scans.

This guide provides an end-to-end operational blueprint, multi-client configurations, a complete production TypeScript MCP server, and security hardening procedures for managing MongoDB with the Model Context Protocol.


1. System Architecture: NoSQL Document Queries via MCP

The MongoDB MCP server runs as a local child process that bridges JSON-RPC tool invocations from the AI host into parameterized database queries using the official MongoDB Node.js driver:

diagram

Security and Execution Controls

Mutation Blocked

Valid Read

AI Agent: tools/call run_aggregation_pipeline

MongoDB MCP Server Process

Role: read-only?

-32004 Access Denied: Mutations Prohibited

Field Projection & Limit 50 Capping

MongoDB Node.js Driver Connection Pool

MongoDB 7.0 / Atlas Cluster

BSON Document Result Stream

Token-Efficient JSON Serializer

Protocol Mechanics and Operational Safety Controls

  1. ▸Projection and Token Budgeting: Unstructured documents frequently contain deeply nested objects, arrays, and lengthy text fields. Returning 50 unprojected documents can easily exhaust tens of thousands of tokens. The MCP server enforces mandatory pagination limits (limit: 50) and projection filtering to strip unnecessary keys.
  2. ▸BSON Type Normalization: MongoDB BSON types (such as ObjectId, ISODate, and Decimal128) must be cleanly serialized into standard JSON primitives before crossing the JSON-RPC wire to avoid deserialization crashes in the host model.
  3. ▸Connection Pool Management: Because tool calls from an AI host execute sequentially during interactive dialogue, the connection pool should remain compact (5–10 sockets) with aggressive socket timeouts (serverSelectionTimeoutMS: 5000) to avoid hanging the parent process during network interruptions.

2. Environment Prerequisites and Software Matrix

Verify that your workstation satisfies the following software requirements:

ComponentMinimum VersionInstallation CheckPurpose
Node.js>= 20.0.0node -vServer runtime environment
MongoDB / Atlas>= 6.0Check mongosh or Atlas consoleTarget database cluster
mongodb>= 6.8.0Package manifestOfficial MongoDB Node.js driver
@modelcontextprotocol/sdk>= 1.2.0Package manifestCore Model Context Protocol framework

Verify connectivity to your cluster:

bash
mongosh "mongodb:///analytics" --eval "db.runCommand({ ping: 1 })"

3. Creating a Hardened Read-Only Database User

Never supply cluster administrative credentials to an interactive AI agent. In mongosh, execute the following script to create a user restricted exclusively to read permissions:

javascript
// Switch to target application database
use analytics;

// Create dedicated unprivileged analyst user
db.createUser({
  user: "mcp_analyst_ro",
  pwd: "Secure_NoSQL_Password_2026!",
  roles: [
    { role: "read", db: "analytics" }
  ]
});

On MongoDB Atlas, navigate to Database Access, create a user with the Read Any Database or custom read-only role, and ensure your development IP address is whitelisted under Network Access.


4. Multi-Host Configuration Snippets

Configure your MCP clients to launch the MongoDB server over standard input and output (stdio). Provide your connection string through environment variables.

Claude Desktop Configuration (claude_desktop_config.json)

On macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
On Windows: %APPDATA%\Claude\claude_desktop_config.json

json
{
  "mcpServers": {
    "mongodb": {
      "command": "node",
      "args": ["/opt/mcp-servers/mongodb/dist/index.js"],
      "env": {
        "MONGODB_URI": "mongodb://mcp_analyst_ro:Secure_NoSQL_Password_2026!@/analytics?authSource=analytics",
        "DATABASE_NAME": "analytics"
      }
    }
  }
}

Cursor IDE Configuration (.cursor/mcp.json)

json
{
  "mcpServers": {
    "mongodb": {
      "command": "node",
      "args": ["./dist/index.js"],
      "env": {
        "MONGODB_URI": "mongodb://mcp_analyst_ro:Secure_NoSQL_Password_2026!@/analytics?authSource=analytics",
        "DATABASE_NAME": "analytics"
      }
    }
  }
}

OpenAI Codex CLI Configuration (~/.codex/config.toml)

toml
[mcp_servers.mongodb]
command = "node"
args = ["/opt/mcp-servers/mongodb/dist/index.js"]

[mcp_servers.mongodb.env]
MONGODB_URI = "mongodb://mcp_analyst_ro:Secure_NoSQL_Password_2026!@/analytics?authSource=analytics"
DATABASE_NAME = "analytics"

5. Production TypeScript MongoDB MCP Server Implementation

The following implementation provides schema discovery (list_collections, infer_schema), document filtering (find_documents), and aggregation pipeline execution (run_aggregation). Save this as src/index.ts:

typescript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { MongoClient, Document } from 'mongodb';

const uri = process.env.MONGODB_URI || 'mongodb:///analytics';
const dbName = process.env.DATABASE_NAME || 'analytics';

const client = new MongoClient(uri, {
  maxPoolSize: 10,
  serverSelectionTimeoutMS: 5000
});

const server = new Server(
  { name: 'mongodb-mcp-server', version: '1.0.0' },
  { capabilities: { tools: {} } }
);

server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: 'list_collections',
        description: 'List all collections in the configured database.',
        inputSchema: { type: 'object', properties: {} }
      },
      {
        name: 'find_documents',
        description: 'Query documents in a collection using BSON filter expressions. Maximum 50 documents returned.',
        inputSchema: {
          type: 'object',
          properties: {
            collection: { type: 'string', description: 'Collection name' },
            filter: { type: 'object', description: 'MongoDB query filter expression (e.g. {"status": "active"})' },
            projection: { type: 'object', description: 'Field projection map (e.g. {"email": 1, "created_at": 1})' },
            limit: { type: 'number', default: 20, maximum: 50, description: 'Number of documents to return' }
          },
          required: ['collection']
        }
      },
      {
        name: 'run_aggregation',
        description: 'Execute a multi-stage aggregation pipeline across a collection.',
        inputSchema: {
          type: 'object',
          properties: {
            collection: { type: 'string', description: 'Collection name' },
            pipeline: { type: 'array', items: { type: 'object' }, description: 'Array of aggregation stages ($match, $group, $sort)' }
          },
          required: ['collection', 'pipeline']
        }
      }
    ]
  };
});

server.setRequestHandler(CallToolRequestSchema, async (request) => {
  const { name, arguments: args } = request.params;
  const db = client.db(dbName);

  try {
    if (name === 'list_collections') {
      const collections = await db.listCollections().toArray();
      const names = collections.map(c => c.name);
      return {
        content: [{ type: 'text', text: JSON.stringify(names, null, 2) }]
      };
    }

    if (name === 'find_documents') {
      const collName = args?.collection as string;
      const filter = (args?.filter as Document) || {};
      const projection = (args?.projection as Document) || {};
      const limit = Math.min(Number(args?.limit) || 20, 50);

      const docs = await db.collection(collName)
        .find(filter, { projection })
        .limit(limit)
        .toArray();

      return {
        content: [{ type: 'text', text: JSON.stringify(docs, null, 2) }]
      };
    }

    if (name === 'run_aggregation') {
      const collName = args?.collection as string;
      const pipeline = (args?.pipeline as Document[]) || [];

      // Append safety limit stage to prevent runaway pipeline outputs
      pipeline.push({ $limit: 50 });

      const results = await db.collection(collName).aggregate(pipeline).toArray();

      return {
        content: [{ type: 'text', text: JSON.stringify(results, null, 2) }]
      };
    }

    throw new Error(`Tool '${name}' is not recognized.`);
  } catch (err: any) {
    return {
      isError: true,
      content: [{ type: 'text', text: `MongoDB Error: ${err.message}` }]
    };
  }
});

async function main() {
  await client.connect();
  const transport = new StdioServerTransport();
  await server.connect(transport);
}

main().catch((err) => {
  process.stderr.write(`Fatal MongoDB server error: ${err.message}\n`);
  process.exit(1);
});

6. JSON-RPC 2.0 Wire Payloads

Inspecting the underlying protocol framing confirms clean message serialization:

Tool Invocation Request (tools/call)

json
{
  "jsonrpc": "2.0",
  "id": "mongo-find-01",
  "method": "tools/call",
  "params": {
    "name": "find_documents",
    "arguments": {
      "collection": "events",
      "filter": { "type": "signup" },
      "limit": 2
    }
  }
}

Result Payload

json
{
  "jsonrpc": "2.0",
  "id": "mongo-find-01",
  "result": {
    "content": [
      {
        "type": "text",
        "text": "[\n  {\"_id\": \"660ab18f921a4f00\", \"type\": \"signup\", \"user_id\": \"usr_401\"},\n  {\"_id\": \"660ab18f921a4f01\", \"type\": \"signup\", \"user_id\": \"usr_402\"}\n]"
      }
    ]
  }
}

7. Common Errors and Production Troubleshooting

When querying MongoDB via automated language model tools, monitor for these failure modes:

Error: MongoServerError: AuthenticationFailed

  • ▸Root Cause: The provided username or password is incorrect, or the user was created in a different database than the one declared in authSource.
  • ▸Resolution: If your user was created in the admin database, append ?authSource=admin to your connection URI. Also ensure passwords containing special characters (such as @, :, /) are properly percent-encoded.

Error: MongoServerSelectionError: connect ECONNREFUSED

  • ▸Root Cause: The MongoDB server process is not running, or network firewall policies block access to port 27017.
  • ▸Resolution: Verify the MongoDB daemon status (sudo systemctl status mongod) and check your IP access list on MongoDB Atlas.

Excessive Token Consumption from Sprawling Documents

  • ▸Root Cause: The model requested documents with unconstrained fields containing deep event telemetry or embedded binary arrays.
  • ▸Resolution: Prompt the agent to supply a projection argument excluding heavy fields (e.g. {"raw_payload": 0}).

8. External Citations and Security Guardrails

Operating NoSQL databases with AI agents requires strict administrative safeguards:

Technical Changelog

Weekly Protocol Diffs & Verified Drops

New server guides, benchmarks, and security audits. No spam. Unsubscribe anytime.

Zero marketing spam. 1-click unsubscribe anytime.

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
Developer Verification & Feedback
0 reports

Did this setup guide work with your AI host?

Help test this configuration across Claude, Cursor, and Codex CLI. Be the first to report:

MongoDB MCP Server Configuration Guide FAQ

What is the MongoDB MCP Server Configuration Guide?

Connect MongoDB databases to Model Context Protocol agents: Step-by-step setup, read-only Atlas roles, aggregation pipelines, and schema exploration.

How do I configure MongoDB MCP Server Configuration Guide in Claude Desktop or Cursor?

You can copy the configuration JSON from our guide or launch the interactive MCP Codex Config Generator at https://mcp-codex.com/generator to export valid configs in 1 click.

Can I use MongoDB MCP Server Configuration Guide with the OpenAI Codex CLI?

Yes, OpenAI Codex CLI supports Model Context Protocol. You can add it directly to ~/.codex/config.toml or pass arguments to codex mcp add.

RT

Written by Rad Tome

Lead AI Systems Architect & Maintainer, MCP Codex

@RadTome

Specializing in Model Context Protocol (MCP) integrations, autonomous AI agent orchestration, and distributed developer toolchains. Researches and benchmarks production MCP client-server architectures across OpenAI Codex, Claude, and Cursor.

Editorial Integrity: Package names are checked against the npm registry and repositories against the GitHub API. Where a package could not be verified, the guide says so instead of giving you a command to run.

Related Guides