MongoDB MCP
MongoDB MCP Server is a Model Context Protocol server enabling secure read-only interaction with MongoDB and Atlas databases. It supports Atlas API tools and database operations for streamlined data access and management.
How to Install and Use MongoDB MCP
MongoDB MCP is a tool that helps you work with MongoDB databases and MongoDB Atlas. It acts as a server for different clients to connect and interact with your MongoDB data safely. Here’s a simple guide to install and use it with examples.
Prerequisites for MongoDB MCP
Before you install MongoDB MCP, make sure you have everything ready. First, you need Node.js installed on your computer. The recommended versions are at least 20.19.0 or newer. You can check your Node.js version by typing this command in your terminal:
node -v
Also, you must have either a MongoDB connection string or MongoDB Atlas API credentials. The server won’t start without these. You can find how to get Atlas API credentials in the MongoDB Atlas dashboard by creating a service account with the right permissions.
Quick Start Setup of MongoDB MCP
Once the prerequisites are done, you can start setting up MongoDB MCP. Here are easy ways to do it:
Option 1: Using MongoDB Connection String
You can run MongoDB MCP by passing your connection string as an environment variable. For example, add your connection string with the username and password as shown below:
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server@latest", "--readOnly"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb://localhost:27017/myDatabase"
}
}
}
}
This will start the MCP server and connect it to your MongoDB database. The --readOnly flag makes sure the server only allows safe, read-only access.
Option 2: Using Atlas API Credentials
Alternatively, if you use MongoDB Atlas, you can use your API service account credentials. Set the client ID and secret as environment variables:
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server@latest", "--readOnly"],
"env": {
"MDB_MCP_API_CLIENT_ID": "your-atlas-service-accounts-client-id",
"MDB_MCP_API_CLIENT_SECRET": "your-atlas-service-accounts-client-secret"
}
}
}
}
You need to create these credentials in the Atlas UI by adding a service account with minimal permissions.
Option 3: Running Standalone with Environment Variables
You can also start MongoDB MCP from the command line by exporting your credentials first, then starting the server like this:
export MDB_MCP_API_CLIENT_ID="your-atlas-service-accounts-client-id"
export MDB_MCP_API_CLIENT_SECRET="your-atlas-service-accounts-client-secret"
npx -y mongodb-mcp-server@latest --readOnly
This method is simple and uses environment variables to keep your secrets safe.
Using Docker to Run MongoDB MCP
If you prefer Docker, you don’t need Node.js installed. Just run the following commands:
For a connection string:
export MDB_MCP_CONNECTION_STRING="mongodb+srv://username:[email protected]/myDatabase"
docker run --rm -i \
-e MDB_MCP_CONNECTION_STRING \
-e MDB_MCP_READ_ONLY="true" \
mongodb/mongodb-mcp-server:latest
For Atlas API credentials:
export MDB_MCP_API_CLIENT_ID="your-atlas-service-accounts-client-id"
export MDB_MCP_API_CLIENT_SECRET="your-atlas-service-accounts-client-secret"
docker run --rm -i \
-e MDB_MCP_API_CLIENT_ID \
-e MDB_MCP_API_CLIENT_SECRET \
-e MDB_MCP_READ_ONLY="true" \
mongodb/mongodb-mcp-server:latest
This keeps your environment clean and isolated.
Starting MongoDB MCP as an HTTP Server
MongoDB MCP can also run as an HTTP server. This is useful if you want to access it over the web or within a network. To start it with HTTP transport, run:
npx -y mongodb-mcp-server@latest --transport=http
By default, it listens on http://127.0.0.1:3000. You can change the host and port like this:
npx -y mongodb-mcp-server@latest --transport=http --httpHost=0.0.0.0 --httpPort=8080
Remember to add proper security if you expose it over the internet.
This guide helps you get started with MongoDB MCP quickly and securely, using connection strings, Atlas API credentials, or Docker. Use environment variables to keep your credentials safe, and choose the setup that fits your needs.