MongoDB MCP Server
Enables AI agents to securely connect to MongoDB databases via MCP, with deployment modes from basic to RBAC-gated with Keycloak authentication.
README
🍃 MongoDB MCP Server
A ready-to-use integration of the MongoDB MCP Server with multiple deployment modes — from a simple local launcher to a fully secured RBAC gateway with Keycloak authentication.
Explore how to connect AI agents and LLM clients (Claude, VS Code Copilot, Cursor, etc.) to your MongoDB databases through the Model Context Protocol.

📋 Table of Contents
| # | Section | Description |
|---|---|---|
| 1 | Deployment Modes | Compare the three available modes |
| 2 | Quick Start | Get running in 2 minutes (basic mode) |
| 3 | Project Structure | Directory layout overview |
| 4 | Test Client | Built-in MCP diagnostic client |
| 5 | VS Code Copilot | Connect VS Code to the MCP Server |
| 6 | Docker Compose | Run the full stack with containers |
| 7 | Documentation | Links to detailed guides |
| 8 | npm Scripts | All available commands at a glance |
| 9 | References | Official docs and specifications |
🔀 Deployment Modes
This project supports three deployment modes, each adding a layer of security on top of the previous one:
🟢 Basic Mode — Direct Connection
The simplest setup. The MCP Server runs locally and accepts connections without authentication. Best for local development and quick testing.
┌────────────┐ ┌──────────────┐
│ Client │──────────>│ MCP Server │
│ (Agent) │ :8008 │ (MongoDB) │
└────────────┘ └──────────────┘
When to use: Solo development, local experiments, testing tools.
📖 See Quick Start below.
🟡 Proxy Mode — OAuth 2.1 Authentication
Adds Keycloak as an OAuth 2.1 Authorization Server and a reverse proxy that validates Bearer tokens before forwarding to the MCP Server. Implements the Delegated Authorization pattern.
┌────────────┐ Bearer ┌──────────┐ ┌──────────────┐
│ Client │──token──>│ Proxy │──────────>│ MCP Server │
│ (Agent) │ │ (RS) │ (no auth) │ (MongoDB) │
└────────────┘ └────┬─────┘ └──────────────┘
│ JWKS
┌────┴─────┐
│ Keycloak │
│ (AS) │
└──────────┘
When to use: Shared environments, multiple users, audit requirements.
📖 See
iac/auth-proxy/for the proxy implementation.
🔴 Gateway Mode — RBAC (Role-Based Tool Filtering)
The most secure mode. Extends Proxy Mode by inspecting MCP messages and filtering tools based on the user's Keycloak realm role. Different users see and can execute different sets of tools.
┌────────────┐ Bearer ┌──────────────┐ ┌──────────────┐
│ Client │──token──>│ RBAC Gateway │──────────>│ MCP Server │
│ (Agent) │ │ :4040 │ (no auth) │ (MongoDB) │
└────────────┘ └──────┬───────┘ └──────────────┘
│ JWKS + roles
┌────┴─────┐
│ Keycloak │
│ (AS) │
└──────────┘
| Role | Mode | Access Level |
|---|---|---|
🔑 mcp-admin |
allow | Full access — all tools (RW) |
📊 mcp-analyst |
allow | 14 specific tools (RO) |
👁️ mcp-viewer |
allow | 5 specific tools (RW) |
👤 mcp-guest |
deny | All except atlas category (RO) |
When to use: Production, teams with different access levels, compliance.
📖 See doc/gateway.md for the full guide: architecture, step-by-step setup, Keycloak curl examples, token inspection, and RBAC configuration.
🚀 Quick Start
Prerequisites
- Node.js v18+
- A MongoDB connection string (Atlas or local)
1. Install dependencies
npm install
2. Configure environment
cp .env.example .env
Edit .env and set your MongoDB connection string:
MDB_MCP_CONNECTION_STRING=mongodb+srv://user:password@cluster.mongodb.net
3. Start the MCP Server
npm run mcp:wrapper:start
========================================
MongoDB MCP Server
========================================
Status : running
URL : http://127.0.0.1:8008
Endpoint : http://127.0.0.1:8008/mcp
========================================
4. Verify with the test client
npm run mcp:client:start
All 7 diagnostic checks should pass (connect, ping, list tools, call tool, list resources, read resource, shutdown).
📁 Project Structure
mongodb-mcp/
├── src/
│ ├── wrapper/ # 🟢 MCP Server launcher
│ │ ├── index.js # Entry point
│ │ └── McpServerLauncher.js # Process manager (env, spawn, shutdown)
│ ├── gateway/ # 🔴 RBAC Gateway (OOP, SOLID/GRASP)
│ │ ├── index.js # Entry point — config + startup
│ │ ├── GatewayServer.js # Controller — HTTP server orchestration
│ │ ├── TokenVerifier.js # JWT/JWKS token verification
│ │ ├── RoleResolver.js # Role resolution + tool permissions
│ │ ├── McpInterceptor.js # MCP message filtering/blocking
│ │ └── ProxyHandler.js # HTTP reverse proxy to upstream
│ └── client/index.js # 🧪 MCP test client (direct & auth modes)
├── cfg/
│ └── roles.json # 🔧 Role-to-tools mapping config
├── iac/
│ ├── keycloak/
│ │ └── realm-export.json # 🔐 Keycloak realm (roles, users, scopes)
├── doc/
│ └── gateway.md # 📖 Full RBAC gateway guide
├── docker-compose.yml # 🐳 Keycloak + MCP Server + Gateway
├── .env # ⚙️ Environment configuration
└── .vscode/mcp.json # 🆚 VS Code Copilot MCP config
🧪 Test Client
The built-in test client implements the full MCP lifecycle and supports both direct and authenticated modes:
# 🟢 Direct — no authentication
npm run mcp:client:start
# 🔴 Via Gateway — with Keycloak token (uses .env defaults)
npm run mcp:client:gateway
# 🔴 Via Gateway — override user from the command line
npm run mcp:client:gateway -- --user mcp-admin --pass admin123
The client runs a diagnostic suite: initialize, ping, list tools, call a tool, list resources, read a resource, and graceful shutdown.
🆚 VS Code Copilot Integration
The .vscode/mcp.json file provides two server entries:
| Server | URL | Auth |
|---|---|---|
mongodb |
http://127.0.0.1:8008/mcp |
None |
mongodb-gateway |
http://127.0.0.1:4040/mcp |
Bearer |
For mongodb-gateway, VS Code will prompt you to paste a Keycloak access token.
Obtain one first (replace user/password as needed):
curl -s -X POST http://localhost:8080/realms/mcp/protocol/openid-connect/token \
-d "grant_type=password" \
-d "client_id=mcp-client" \
-d "username=mcp-admin" \
-d "password=admin123" \
-d "scope=openid mcp:access" | node -e "
let d=''; process.stdin.on('data',c=>d+=c);
process.stdin.on('end',()=>console.log(JSON.parse(d).access_token));
"
Copy the token and paste it when VS Code prompts. See doc/gateway.md for more details and all available users.
🐳 Docker Compose
Run the complete stack with a single command:
npm run mcp:docker:start
This starts three services:
| Service | Port | Description |
|---|---|---|
keycloak |
:8080 |
🔐 OAuth 2.1 Authorization Server |
mongodb-mcp |
:8008 |
🍃 MongoDB MCP Server |
mcp-gateway |
:4040 |
🛡️ RBAC Gateway (validates JWT + roles) |
Stop everything:
npm run mcp:docker:stop
📜 npm Scripts
| Script | Mode | Description |
|---|---|---|
npm run mcp:wrapper:start |
🟢 | Start the MCP Server locally |
npm run mcp:client:start |
🟢 | Run diagnostics — direct (no auth) |
npm run mcp:gateway:start |
🔴 | Start the RBAC Gateway locally |
npm run mcp:client:gateway |
🔴 | Run diagnostics — via gateway (with auth) |
npm run mcp:client:auth |
🟡 | Run diagnostics — via proxy (with auth) |
npm run mcp:docker:start |
🐳 | Start all Docker services |
npm run mcp:docker:stop |
🐳 | Stop all Docker services |
📖 Documentation
| Document | Description |
|---|---|
| doc/gateway.md | 🔴 RBAC Gateway, full guide with Keycloak examples |
| doc/remote.md | 🟢 Securing Remote MongoDB MCP Servers: An RBAC Gateway Architecture |
📚 References
- MongoDB MCP Server: Overview & Use Cases
- MongoDB MCP Server: Get Started (Self-Managed)
- MongoDB MCP Server: Security Best Practices
- MongoDB MCP Server: Tools Reference
- MCP Specification: Transports
- MCP Specification: Lifecycle
- MCP Authorization Tutorial
- LM Studio: Using MCP via API
📄 License
ISC
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。