MongoDB MCP server

Enables interaction with MongoDB databases through TypeScript and the MongoDB driver, providing seamless integration of MongoDB operations into workflows.
Back to servers
Provider
1RB
Release date
Mar 06, 2025
Language
TypeScript
Stats
11 stars

The MongoDB MCP Server provides a natural language interface for LLMs to interact with MongoDB databases. This server implements the Model Context Protocol (MCP), enabling AI assistants like Claude to query collections, inspect schemas, and manage data through conversation.

Prerequisites

Before installation, ensure you have:

  • Node.js v18 or higher
  • MongoDB instance (local or remote)
  • An MCP client like Claude Desktop or Cursor.ai

Verify your Node.js installation:

node --version  # Should show v18.0.0 or higher

Installation Options

Quick Setup with Claude Desktop

Add this configuration to your Claude Desktop config file:

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

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": [
        "mongo-mcp",
        "mongodb://<username>:<password>@<host>:<port>/<database>?authSource=admin"
      ]
    }
  }
}

Install via Smithery

For automatic installation with Claude Desktop:

npx -y @smithery/cli install mongo-mcp --client claude

Integration with Cursor.ai

  1. Open Cursor.ai and go to Settings > Features
  2. Find "MCP Servers" in the features panel
  3. Add a new MCP server with:
    • Name: mongodb
    • Command: npx
    • Args: mongo-mcp mongodb://<username>:<password>@<host>:<port>/<database>?authSource=admin

Note: Cursor currently supports MCP tools only in the Agent in Composer feature.

Test Environment Setup

If you don't have a MongoDB server available, you can create a test sandbox:

  1. Start MongoDB using Docker Compose:
docker-compose up -d
  1. Seed the database with sample data:
npm run seed
  1. Configure Claude Desktop for local development:

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

{
  "mcpServers": {
    "mongodb": {
      "command": "node",
      "args": [
        "dist/index.js",
        "mongodb://root:example@localhost:27017/test?authSource=admin"
      ]
    }
  }
}

Usage Examples

Try these prompts with Claude to interact with your MongoDB database:

Basic Queries

"What collections are available in the database?"
"Show me the schema for the users collection"
"Find all users in San Francisco"

Advanced Queries

"Find all electronics products that are in stock and cost less than $1000"
"Show me all orders from the user [email protected]"
"List the products with ratings above 4.5"

Index Management

"What indexes exist on the users collection?"
"Create an index on the products collection for the 'category' field"
"List all indexes across all collections"

Document Operations

"Insert a new product with name 'Gaming Laptop' in the products collection"
"Update the status of order with ID X to 'shipped'"
"Find and delete all products that are out of stock"

Available Tools

The MCP server provides these MongoDB interaction tools:

Query Tools

  • listCollections: Lists available collections in the database
  • find: Queries documents with filtering and projection
  • insertOne: Inserts a single document into a collection
  • updateOne: Updates a single document in a collection
  • deleteOne: Deletes a single document from a collection

Index Tools

  • createIndex: Creates a new index on a collection
  • dropIndex: Removes an index from a collection
  • indexes: Lists indexes for a collection

Troubleshooting

Common Issues

  1. Connection Errors

    • Verify your MongoDB connection string is correct
    • Check that your MongoDB server is running and accessible
    • Ensure network permissions allow the connection
  2. Authentication Issues

    • Confirm username and password are correct
    • Verify the authentication database is specified (usually authSource=admin)
    • Check if MongoDB requires TLS/SSL connections
  3. Tool Execution Problems

    • Restart Claude Desktop or Cursor.ai completely
    • Check the logs for detailed error messages:
      # macOS
      tail -n 20 -f ~/Library/Logs/Claude/mcp*.log
      
  4. Performance Issues

    • Consider adding appropriate indexes to frequently queried fields
    • Use projection to limit the data returned in queries
    • Use limit and skip parameters for pagination

Security Considerations

When using this MCP server with MongoDB:

  • Create a dedicated MongoDB user with minimal required permissions
  • Never use admin credentials in production environments
  • Enable access logging for audit purposes
  • Set appropriate read/write permissions on collections
  • Use connection string parameters to restrict access
  • Consider IP allow-listing to restrict database access

How to add this MCP server to 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 > MCP and click "Add new global MCP server".

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

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

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 explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

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