MongoDB MCP server

Query and analyze MongoDB databases.
Back to servers
Setup instructions
Provider
Muhammed Kılıç
Release date
Dec 04, 2024
Language
TypeScript
Package
Stats
14.9K downloads
249 stars

The MCP MongoDB Server enables LLMs to interact with MongoDB databases through a standardized interface. It provides capabilities for inspecting collection schemas and executing MongoDB operations with features like smart ObjectId handling and read-only mode protection.

Installation

Global Installation

npm install -g mcp-mongo-server

Usage

Basic Usage

# Start server with MongoDB URI
npx -y mcp-mongo-server mongodb://username:password@localhost:27017/database

# Connect in read-only mode
npx -y mcp-mongo-server mongodb://username:password@localhost:27017/database --read-only

Configuration with Environment Variables

Environment variables provide a secure way to configure the server without exposing connection details:

# Set MongoDB connection URI
export MCP_MONGODB_URI="mongodb://username:password@localhost:27017/database"

# Enable read-only mode
export MCP_MONGODB_READONLY="true"

# Run server (will use environment variables if no URI is provided)
npx -y mcp-mongo-server

Integration with LLM Applications

Claude Desktop Configuration

Add the server to Claude Desktop's config file:

MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%/Claude/claude_desktop_config.json

Using Command-line Arguments:

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://username:password@localhost:27017/database"
      ]
    },
    "mongodb-readonly": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://username:password@localhost:27017/database",
        "--read-only"
      ]
    }
  }
}

Using Environment Variables:

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server"
      ],
      "env": {
        "MCP_MONGODB_URI": "mongodb://username:password@localhost:27017/database"
      }
    },
    "mongodb-readonly": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server"
      ],
      "env": {
        "MCP_MONGODB_URI": "mongodb://username:password@localhost:27017/database",
        "MCP_MONGODB_READONLY": "true"
      }
    }
  }
}

Automated Installation

# Using Smithery
npx -y @smithery/cli install mcp-mongo-server --client claude

# Using mcp-get
npx @michaellatman/mcp-get@latest install mcp-mongo-server

MongoDB Operations

Query Operations

  • query: Execute MongoDB queries

    {
      collection: "users",
      filter: { age: { $gt: 30 } },
      projection: { name: 1, email: 1 },
      limit: 20,
      explain: "executionStats"  // Optional
    }
    
  • aggregate: Run aggregation pipelines

    {
      collection: "orders",
      pipeline: [
        { $match: { status: "completed" } },
        { $group: { _id: "$customerId", total: { $sum: "$amount" } } }
      ],
      explain: "queryPlanner"  // Optional
    }
    
  • count: Count matching documents

    {
      collection: "products",
      query: { category: "electronics" }
    }
    

Write Operations

  • update: Modify documents

    {
      collection: "posts",
      filter: { _id: "60d21b4667d0d8992e610c85" },
      update: { $set: { title: "Updated Title" } },
      upsert: false,
      multi: false
    }
    
  • insert: Add new documents

    {
      collection: "comments",
      documents: [
        { author: "user123", text: "Great post!" },
        { author: "user456", text: "Thanks for sharing" }
      ]
    }
    
  • createIndex: Create collection indexes

    {
      collection: "users",
      indexes: [
        {
          key: { email: 1 },
          unique: true,
          name: "email_unique_idx"
        }
      ]
    }
    

System Operations

  • serverInfo: Get MongoDB server details
    {
      includeDebugInfo: true  // Optional
    }
    

Key Features

ObjectId Handling

Configure with objectIdMode parameter:

  • "auto": Convert based on field names (default)
  • "none": No conversion
  • "force": Force all string ID fields to ObjectId

Read-Only Mode

  • Protects against write operations
  • Uses MongoDB's secondary read preference
  • Ideal for safely connecting to production databases

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "mongodb" '{"command":"npx","args":["-y","mcp-mongo-server","mongodb://muhammed:kilic@localhost:27017/database"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "mongodb": {
            "command": "npx",
            "args": [
                "-y",
                "mcp-mongo-server",
                "mongodb://muhammed:kilic@localhost:27017/database"
            ]
        },
        "mongodb-readonly": {
            "command": "npx",
            "args": [
                "-y",
                "mcp-mongo-server",
                "mongodb://muhammed:kilic@localhost:27017/database",
                "--read-only"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "mongodb": {
            "command": "npx",
            "args": [
                "-y",
                "mcp-mongo-server",
                "mongodb://muhammed:kilic@localhost:27017/database"
            ]
        },
        "mongodb-readonly": {
            "command": "npx",
            "args": [
                "-y",
                "mcp-mongo-server",
                "mongodb://muhammed:kilic@localhost:27017/database",
                "--read-only"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later