MongoDB MCP 服务器
用于与 MongoDB 数据库和 MongoDB Atlas 交互的模型上下文协议服务器。
📚 目录
先决条件
- Node.js(v20.10.0 或更高版本)
- MongoDB 连接字符串或 Atlas API 凭据,除非配置,否则服务器将不会启动。
- 使用 Atlas 工具需要服务帐户 Atlas API 凭证。您可以在 MongoDB Atlas 中创建服务帐户,并使用其凭证进行身份验证。更多详情,请参阅Atlas API 访问。
- 如果您有 MongoDB 连接字符串,则可以直接使用它连接到您的 MongoDB 实例。
设置
快速入门
大多数 MCP 客户端需要创建或修改配置文件来添加 MCP 服务器。
注意:不同客户端的配置文件语法可能有所不同。请参阅以下链接了解最新的预期语法:
- 风帆冲浪: https://docs.windsurf.com/windsurf/mcp
- VSCode : https://code.visualstudio.com/docs/copilot/chat/mcp-servers
- 克劳德桌面: https://modelcontextprotocol.io/quickstart/user
- 光标: https://docs.cursor.com/context/model-context-protocol
选项 1:连接字符串参数
您可以通过 args 传递连接字符串,请确保使用有效的用户名和密码。
注意:可以配置连接字符串以连接到任何 MongoDB 集群,无论它是本地实例还是 Atlas 集群。
选项 2:Atlas API 凭证参数
使用您的 Atlas API 服务帐户凭证。必须遵循Atlas API 访问部分中的所有步骤。
选项 3:使用命令参数的独立服务
使用 npx 命令启动服务器:
- 有关参数的完整列表,请参阅配置选项
- 要配置您的 Atlas 服务帐户凭据,请参阅Atlas API 访问
选项 4:使用环境变量的独立服务
您可以在配置文件中使用环境变量或设置它们并通过 npx 运行服务器。
🛠️ 支持的工具
工具清单
MongoDB Atlas 工具
atlas-list-orgs
- 列出 MongoDB Atlas 组织atlas-list-projects
- 列出 MongoDB Atlas 项目atlas-create-project
- 创建一个新的 MongoDB Atlas 项目atlas-list-clusters
- 列出 MongoDB Atlas 集群atlas-inspect-cluster
- 检查特定的 MongoDB Atlas 集群atlas-create-free-cluster
- 创建免费的 MongoDB Atlas 集群atlas-connect-cluster
- 连接到 MongoDB Atlas 集群atlas-inspect-access-list
- 检查可以访问 MongoDB Atlas 集群的 IP/CIDR 范围atlas-create-access-list
- 为 MongoDB Atlas 集群配置 IP/CIDR 访问列表atlas-list-db-users
- 列出 MongoDB Atlas 数据库用户atlas-create-db-user
- 创建 MongoDB Atlas 数据库用户atlas-list-alerts
- 列出项目的 MongoDB Atlas 警报
注意:只有在配置部分设置凭据时,Atlas 工具才可用。
MongoDB 数据库工具
connect
- 连接到 MongoDB 实例find
- 对 MongoDB 集合运行查找查询aggregate
- 针对 MongoDB 集合运行聚合count
获取 MongoDB 集合中的文档数量insert-one
- 将单个文档插入 MongoDB 集合insert-many
- 将多个文档插入 MongoDB 集合create-index
- 为 MongoDB 集合创建索引update-one
- 更新 MongoDB 集合中的单个文档update-many
- 更新 MongoDB 集合中的多个文档rename-collection
- 重命名 MongoDB 集合delete-one
- 从 MongoDB 集合中删除单个文档delete-many
- 从 MongoDB 集合中删除多个文档drop-collection
从 MongoDB 数据库中删除一个集合drop-database
删除 MongoDB 数据库list-databases
- 列出 MongoDB 连接的所有数据库list-collections
- 列出给定数据库的所有集合collection-indexes
- 描述集合的索引collection-schema
- 描述集合的模式collection-storage-size
- 获取集合的大小(以 MB 为单位)db-stats
- 返回有关 MongoDB 数据库的统计信息
配置
MongoDB MCP 服务器可以使用多种方法进行配置,其优先级如下(从高到低):
- 命令行参数
- 环境变量
配置选项
选项 | 描述 |
---|---|
apiClientId | 用于身份验证的 Atlas API 客户端 ID |
apiClientSecret | 用于身份验证的 Atlas API 客户端密钥 |
connectionString | MongoDB 连接字符串用于直接数据库连接(可选用户可以选择在每次工具调用时通知它) |
logPath | 存储日志的文件夹 |
disabledTools | 将被禁用的工具名称、操作类型和/或工具类别的数组 |
readOnly | 设置为 true 时,仅允许读取和元数据操作类型,禁用创建/更新/删除操作 |
telemetry | 设置为禁用时,禁用遥测收集 |
日志路径
默认日志位置如下:
- Windows:
%LOCALAPPDATA%\mongodb\mongodb-mcp\.app-logs
- macOS/Linux:
~/.mongodb/mongodb-mcp/.app-logs
禁用工具
您可以使用disabledTools
选项禁用特定工具或特定类别的工具。此选项接受一个字符串数组,其中每个字符串可以是工具名称、操作类型或类别。
数组的构造方式取决于您使用的配置方法的类型:
- 对于环境变量配置,使用逗号分隔的字符串:
export MDB_MCP_DISABLED_TOOLS="create,update,delete,atlas,collectionSchema"
。 - 对于命令行参数配置,使用空格分隔的字符串:
--disabledTools create update delete atlas collectionSchema
。
工具类别:
atlas
- MongoDB Atlas 工具,例如列出集群、创建集群等。mongodb
- MongoDB 数据库工具,例如 find、aggregate 等。
操作类型:
create
- 创建资源的工具,例如创建集群、插入文档等。update
- 更新资源的工具,例如更新文档、重命名集合等。delete
- 删除资源的工具,例如删除文档、删除集合等。read
- 读取资源的工具,例如查找、聚合、列出集群等。metadata
——读取元数据的工具,例如列表数据库、列表集合、集合模式等。
只读模式
readOnly
配置选项允许您限制 MCP 服务器仅使用具有“读取”和“元数据”操作类型的工具。启用后,所有具有“创建”、“更新”或“删除”操作类型的工具都不会注册到服务器。
这对于您想要提供对 MongoDB 数据的访问以进行分析而又不允许对数据或基础设施进行任何修改的情况很有用。
您可以使用以下方式启用只读模式:
- 环境变量:
export MDB_MCP_READ_ONLY=true
- 命令行参数:
--readOnly
当只读模式处于活动状态时,您将在服务器日志中看到一条消息,指示由于此限制而导致哪些工具无法注册。
遥测
telemetry
配置选项允许您禁用遥测收集。启用后,MCP 服务器将收集使用情况数据并将其发送到 MongoDB。
您可以使用以下方式禁用遥测:
- 环境变量:
export MDB_MCP_TELEMETRY=disabled
- 命令行参数:
--telemetry disabled
- DO_NOT_TRACK 环境变量:
export DO_NOT_TRACK=1
Atlas API 访问
要使用 Atlas API 工具,您需要在 MongoDB Atlas 中创建一个服务帐户:
- 创建服务帐户:
- 登录cloud.mongodb.com上的 MongoDB Atlas
- 导航至访问管理器 > 组织访问
- 单击“添加新”>“应用程序”>“服务帐户”
- 输入您的服务帐户的名称、描述和到期日期(例如,“MCP、MCP 服务器访问、7 天”)
- 选择适当的权限(如需完全访问权限,请使用组织所有者)
- 点击“创建”
要了解有关服务帐户的更多信息,请查看MongoDB Atlas 文档。
- 保存客户端凭证:
- 创建后,您将看到客户端 ID 和客户端密钥
- **重要提示:**立即复制并保存客户端密钥,因为它不会再次显示
- 添加访问列表条目:
- 将您的 IP 地址添加到 API 访问列表
- 配置 MCP 服务器:
- 使用以下配置方法之一设置您的
apiClientId
和apiClientSecret
- 使用以下配置方法之一设置您的
配置方法
环境变量
使用前缀MDB_MCP_
后跟大写带下划线的选项名称来设置环境变量:
MCP 配置文件示例
带有环境变量的连接字符串
带有环境变量的 Atlas API 凭据
命令行参数
启动服务器时将配置选项作为命令行参数传递:
MCP 配置文件示例
带有命令行参数的连接字符串
带有命令行参数的 Atlas API 凭据
🤝 贡献
有兴趣贡献代码吗?太棒了!请查看我们的贡献指南,了解代码贡献指南、标准、添加新工具以及故障排除信息。
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
模型上下文协议服务器,使AI助手能够通过自然语言与MongoDB Atlas资源进行交互,支持数据库操作和Atlas管理功能。
Related MCP Servers
- AsecurityAlicenseAqualityA Model Context Protocol server that provides read-only access to MongoDB databases, enabling AI assistants to directly query and analyze MongoDB data while maintaining data safety.Last updated -1492JavaScriptMIT License
- AsecurityAlicenseAqualityA Model Context Protocol server that enables AI models to interact with both MySQL and MongoDB databases through a standardized interface, supporting comprehensive database operations including queries, schema management, and CRUD operations.Last updated -146JavaScriptMIT License
- -securityFlicense-qualityA comprehensive Model Context Protocol server implementation that enables AI assistants to interact with file systems, databases, GitHub repositories, web resources, and system tools while maintaining security and control.Last updated -16TypeScript
- AsecurityAlicenseAqualityA Model Context Protocol server implementation that enables AI assistants to interact with Apollo.io data, performing operations like people/organization enrichment, searching, and retrieving job postings through natural language commands.Last updated -73TypeScriptMIT License