MongoDB MCP Server - 官方文档数据库和 Atlas 集群管理工具
官方实现 | Stars: 676 | TypeScript | Apache-2.0
概述
MongoDB MCP Server 是 MongoDB 官方提供的 Model Context Protocol 实现,为 AI 应用提供完整的 MongoDB 数据库和 Atlas 云集群管理能力。它支持双连接模式(本地 MongoDB 和 Atlas 云集群),提供了从数据库连接、CRUD 操作到 Atlas 集群管理的全套工具。
该服务器内置安全控制和只读模式,支持环境变量配置敏感信息,并为危险操作提供确认机制。特别适合需要 AI 辅助的数据库查询、Schema 探索、Atlas 集群管理等场景,让 AI 助手能够安全高效地访问和操作 MongoDB 数据。
核心特性
- ✅ MongoDB 官方实现,与数据库和 Atlas 深度集成
- 🔌 双连接模式:支持本地 MongoDB 和 Atlas 云集群
- 🛠️ 35+ 强大工具:覆盖数据库、集合、文档、索引、Atlas 管理
- 🔒 只读模式:可配置禁用所有写入操作
- 🛡️ 安全确认机制:危险操作需要用户确认
- 🌐 Atlas 全功能管理:组织、项目、集群、用户、访问控制
- 📊 完整 CRUD 操作:数据库、集合、文档的完整生命周期管理
- 🔍 查询优化工具:支持 explain 执行计划和性能顾问
- 🚀 支持多种传输协议:stdio、HTTP
工具列表
MongoDB 连接工具
1. connect
功能:连接到 MongoDB 实例
参数:
connectionString(string, 可选) - MongoDB 连接字符串
示例:
1 | { |
MongoDB 数据查询工具
2. find
功能:在 MongoDB 集合上运行查询
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称filter(object, 可选) - 查询过滤条件,EJSON 格式projection(object, 可选) - 字段投影limit(number, 可选, 默认: 10) - 返回文档的最大数量sort(object, 可选) - 排序规则 (1=升序, -1=降序)responseBytesLimit(number, 可选, 默认: 1048576) - 响应的最大字节数
示例:
1 | { |
3. aggregate
功能:执行聚合管道查询
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称pipeline(array, 必需) - 聚合管道阶段limit(number, 可选) - 返回文档的最大数量responseBytesLimit(number, 可选) - 响应的最大字节数
示例:
1 | { |
4. count
功能:计算集合中的文档数量
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称filter(object, 可选) - 查询过滤条件
示例:
1 | { |
5. export
功能:导出集合数据
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称filter(object, 可选) - 导出过滤条件
MongoDB 数据写入工具
6. insert-many
功能:向集合插入多个文档
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称documents(array, 必需) - 要插入的文档数组
示例:
1 | { |
7. update-many
功能:更新多个文档
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称filter(object, 必需) - 匹配要更新文档的过滤条件update(object, 必需) - 更新操作
示例:
1 | { |
8. delete-many
功能:删除多个文档
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称filter(object, 必需) - 匹配要删除文档的过滤条件
示例:
1 | { |
MongoDB 集合管理工具
9. create-collection
功能:创建新集合
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称options(object, 可选) - 集合选项
示例:
1 | { |
10. drop-collection
功能:删除集合
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称
11. rename-collection
功能:重命名 MongoDB 集合
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 当前集合名称newName(string, 必需) - 新集合名称
12. list-collections
功能:列出数据库中的所有集合
参数:
database(string, 必需) - 数据库名称
MongoDB 索引管理工具
13. create-index
功能:为集合创建索引
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称keys(object, 必需) - 索引键定义options(object, 可选) - 索引选项
示例:
1 | { |
14. collection-indexes
功能:查看集合的索引
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称
MongoDB 元数据工具
15. list-databases
功能:列出所有数据库
参数:无
16. drop-database
功能:删除数据库
参数:
database(string, 必需) - 数据库名称
17. collection-schema
功能:获取集合的 schema 信息
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称
18. collection-storage-size
功能:获取集合的存储大小
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称
19. db-stats
功能:获取数据库统计信息
参数:
database(string, 必需) - 数据库名称
20. logs
功能:查看 MongoDB 日志
参数:
lines(number, 可选) - 返回的日志行数
MongoDB 查询分析工具
21. explain
功能:解释查询执行计划
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称query(object, 必需) - 要解释的查询
示例:
1 | { |
22. list-search-indexes
功能:列出 Atlas Search 索引
参数:
database(string, 必需) - 数据库名称collection(string, 必需) - 集合名称
MongoDB Atlas 组织和项目管理
23. atlas-list-orgs
功能:列出 MongoDB Atlas 组织
参数:无
24. atlas-list-projects
功能:列出 MongoDB Atlas 项目
参数:
orgId(string, 可选) - 组织 ID
25. atlas-create-project
功能:创建 MongoDB Atlas 项目
参数:
orgId(string, 必需) - 组织 IDname(string, 必需) - 项目名称
MongoDB Atlas 集群管理
26. atlas-list-clusters
功能:列出 MongoDB Atlas 集群
参数:
projectId(string, 可选) - 项目 ID
27. atlas-inspect-cluster
功能:检查 Atlas 集群详情
参数:
projectId(string, 必需) - 项目 IDclusterName(string, 必需) - 集群名称
28. atlas-create-free-cluster
功能:创建免费的 Atlas 集群
参数:
projectId(string, 必需) - 项目 IDclusterName(string, 必需) - 集群名称
示例:
1 | { |
29. atlas-connect-cluster
功能:连接到 Atlas 集群
参数:
projectId(string, 必需) - 项目 IDclusterName(string, 必需) - 集群名称
MongoDB Atlas 访问控制
30. atlas-inspect-access-list
功能:查看 Atlas IP 访问列表
参数:
projectId(string, 必需) - 项目 ID
31. atlas-create-access-list
功能:创建 Atlas IP 访问列表条目
参数:
projectId(string, 必需) - 项目 IDipAddress(string, 必需) - IP 地址或 CIDR
示例:
1 | { |
MongoDB Atlas 用户管理
32. atlas-list-db-users
功能:列出 Atlas 数据库用户
参数:
projectId(string, 必需) - 项目 ID
33. atlas-create-db-user
功能:创建 Atlas 数据库用户
参数:
projectId(string, 必需) - 项目 IDusername(string, 必需) - 用户名password(string, 必需) - 密码
示例:
1 | { |
MongoDB Atlas 监控和性能
34. atlas-list-alerts
功能:列出 Atlas 项目的告警
参数:
projectId(string, 必需) - 项目 ID
35. atlas-get-performance-advisor
功能:获取性能顾问建议
参数:
projectId(string, 必需) - 项目 ID
配置方式
环境变量
1 | # MongoDB 连接字符串(本地连接) |
Claude Desktop 配置
在 claude_desktop_config.json 中添加:
本地 MongoDB 配置
1 | { |
Atlas 集群管理配置
1 | { |
双模式配置(本地 + Atlas)
1 | { |
NPX 直接运行
1 | # 使用环境变量 |
获取 Atlas API 凭证
- 登录 MongoDB Atlas 控制台
- 进入 Access Manager > API Keys
- 点击 Create API Key
- 设置权限(建议:Project Read Only 用于只读,Project Owner 用于完整管理)
- 保存 Client ID 和 Client Secret
使用场景
1. AI 驱动的数据库查询
使用自然语言查询 MongoDB 数据,AI 自动转换为正确的查询语法。
示例流程:
- 用户:「查找所有年龄大于 25 岁的活跃用户」
- AI 使用
find工具查询 - 返回结构化的查询结果
适用于:
- 数据分析和报表生成
- 快速数据探索
- 非技术人员的数据访问
2. Atlas 云集群管理
通过 AI 对话管理 Atlas 云集群,无需登录控制台。
示例流程:
- AI 使用
atlas-list-clusters查看现有集群 - 使用
atlas-create-free-cluster创建新的免费集群 - 使用
atlas-create-db-user创建数据库用户 - 使用
atlas-create-access-list配置 IP 访问控制
适用于:
- 快速集群部署
- 多环境管理
- 自动化运维任务
3. Schema 探索和数据分析
AI 辅助的数据库结构分析和数据洞察。
示例流程:
- 使用
list-databases和list-collections了解数据库结构 - 使用
collection-schema查看集合 schema - 使用
aggregate执行复杂的数据分析 - 使用
explain优化查询性能
适用于:
- 数据库审计
- 性能优化
- 数据迁移准备
4. 查询性能优化
使用 AI 辅助的查询分析和索引建议。
示例流程:
- 用户提供慢查询
- AI 使用
explain分析执行计划 - 使用
atlas-get-performance-advisor获取优化建议 - 使用
create-index创建推荐的索引
适用于:
- 查询性能诊断
- 索引策略规划
- 数据库性能调优
5. 自动化数据运维
智能化的数据库维护和日常操作。
示例流程:
- 定期使用
db-stats检查数据库状态 - 使用
logs监控数据库日志 - 使用
atlas-list-alerts检查告警 - 自动执行数据清理和归档
适用于:
- 数据库监控
- 自动化备份
- 数据生命周期管理
6. 开发和测试辅助
AI 辅助的数据库操作和测试数据管理。
示例流程:
- 使用
create-collection创建测试集合 - 使用
insert-many批量插入测试数据 - 执行测试后使用
drop-collection清理 - 使用
export导出测试结果
适用于:
- 单元测试数据准备
- 集成测试环境搭建
- 数据模拟和演示
技术架构
连接模式
1 | ┌─────────────────────────────────────────────────────────┐ |
工具分类
MongoDB 本地操作 (22 个工具)
- 连接: connect
- 查询: find, aggregate, count, export
- 写入: insert-many, update-many, delete-many
- 集合: create-collection, drop-collection, rename-collection, list-collections
- 索引: create-index, collection-indexes
- 元数据: list-databases, drop-database, collection-schema, collection-storage-size, db-stats, logs
- 分析: explain, list-search-indexes
Atlas 云管理 (13 个工具)
- 组织: atlas-list-orgs
- 项目: atlas-list-projects, atlas-create-project
- 集群: atlas-list-clusters, atlas-inspect-cluster, atlas-create-free-cluster, atlas-connect-cluster
- 访问: atlas-inspect-access-list, atlas-create-access-list
- 用户: atlas-list-db-users, atlas-create-db-user
- 监控: atlas-list-alerts, atlas-get-performance-advisor
安全机制
1 | ┌─────────────────────────────────────────────────────────┐ |
与其他数据库 MCP 服务器对比
| 特性 | MongoDB MCP | Qdrant MCP | PostgreSQL MCP | Redis MCP |
|---|---|---|---|---|
| 数据模型 | 文档型 | 向量 | 关系型 | 键值 |
| 官方实现 | ✅ | ✅ | ❌ | ❌ |
| 云管理 | ✅ Atlas | ✅ Cloud | ❌ | ❌ |
| 查询语言 | MQL | Vector | SQL | Commands |
| 工具数量 | 35+ | 2 | 15+ | 10+ |
| 只读模式 | ✅ | ❌ | ✅ | ✅ |
| 适用场景 | 文档存储、JSON | 向量搜索 | 事务处理 | 缓存 |
| 扩展性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
最佳实践
1. 安全配置
生产环境使用只读模式
1 | { |
分离读写环境
1 | { |
2. 性能优化
限制返回数据量
1 | { |
使用投影减少数据传输
1 | { |
3. Atlas API 权限最小化
1 | 推荐权限级别: |
4. 查询优化流程
1 | 1. 使用 find/aggregate 执行查询 |
5. 监控和告警
1 | # 定期检查集群状态 |
常见问题
Q: 如何同时连接本地和 Atlas?
同时配置 MONGODB_URI 和 Atlas API 凭证即可。本地操作使用 MongoDB 工具,云管理使用 Atlas 工具。
Q: 只读模式会影响哪些操作?
只读模式会禁用以下操作:
- insert-many
- update-many
- delete-many
- drop-collection
- drop-database
- create-collection
- create-index
- 所有 Atlas 创建操作
Q: 如何处理大量数据查询?
使用以下策略:
- 设置合理的
limit参数 - 配置
responseBytesLimit限制 - 使用
projection减少字段 - 使用
export工具导出到文件
Q: Atlas API 凭证如何获取?
- 登录 MongoDB Atlas
- 进入 Access Manager > API Keys
- 创建新的 API Key
- 设置适当的权限级别
- 保存 Client ID 和 Secret
Q: 支持 MongoDB 哪些版本?
支持 MongoDB 4.0+ 的所有版本,包括:
- MongoDB Community Server
- MongoDB Enterprise Server
- MongoDB Atlas (所有层级)
Q: 如何处理连接字符串中的特殊字符?
使用 URL 编码处理特殊字符:
1 | # 错误 |
评分详情
| 维度 | 评分 | 说明 |
|---|---|---|
| 功能性 | 4.8/5.0 | 工具丰富,覆盖全面,支持双连接模式 |
| 文档质量 | 4.5/5.0 | 官方文档清晰,示例充足 |
| 社区活跃度 | 4.5/5.0 | MongoDB 官方维护,社区活跃 |
| 维护状态 | 4.8/5.0 | 定期更新,响应及时 |
| 代码质量 | 4.2/5.0 | TypeScript 实现,代码规范 |
| 综合评分 | 4.5/5.0 | 优秀的 MongoDB MCP 实现 |
总结
MongoDB MCP Server 是 AI 应用访问 MongoDB 的理想选择。它将强大的文档数据库能力和 Atlas 云管理与 MCP 协议完美结合,为开发者提供了全面、安全、易用的接口。
推荐指数: ⭐⭐⭐⭐⭐ (5/5)
适合你的情况:
- ✅ 使用 MongoDB 存储数据
- ✅ 需要 AI 辅助的数据库查询
- ✅ 管理 Atlas 云集群
- ✅ 需要 Schema 探索和分析
- ✅ 查询性能优化需求
- ✅ 自动化数据库运维
不适合的情况:
- ❌ 只需要关系型数据库
- ❌ 需要复杂的事务处理
- ❌ 需要向量搜索能力
- ❌ 需要实时流式处理
相关资源
- GitHub: https://github.com/mongodb-js/mongodb-mcp-server
- MongoDB 文档: https://www.mongodb.com/docs/
- Atlas 文档: https://www.mongodb.com/docs/atlas/
- MCP 协议: https://modelcontextprotocol.io/
- MongoDB University: https://university.mongodb.com/
更新时间: 2025-10-14
数据来源: GitHub, 质量评分: 4.5/5.0