MongoDB MCP Server

Official
by mongodb-js
Apache 2.0
303
14
  • Apple
  • Linux

Integrations

  • Enables GitHub Copilot to interact with MongoDB databases through the MCP server, allowing natural language queries and operations against MongoDB resources.

  • Provides tools for interacting with MongoDB Atlas resources and databases, supporting operations like querying, aggregation, document creation/modification, collection management, index creation, and database administration.

MongoDB MCP 服务器

用于与 MongoDB 数据库和 MongoDB Atlas 交互的模型上下文协议服务器。

📚 目录

先决条件

  • Node.js(v20 或更高版本)
node -v
  • MongoDB 连接字符串或 Atlas API 凭据,除非配置,否则服务器将不会启动
    • 使用 Atlas 工具需要Atlas API 凭证。您可以在 MongoDB Atlas 中创建一个服务帐户,并使用其凭证进行身份验证。更多详情,请参阅Atlas API 访问
    • 如果您有 MongoDB 连接字符串,则可以直接使用它连接到您的 MongoDB 实例。

设置

快速入门

大多数 MCP 客户端需要创建或修改配置文件来添加 MCP 服务器。

选项 1:连接字符串参数

您可以通过 args 传递连接字符串,请确保使用有效的用户名和密码。

{ "servers": { "MongoDB": { "command": "npx", "args": [ "-y", "mongodb-mcp-server", "--connectionString", "mongodb+srv://username:password@cluster.mongodb.net/myDatabase" ] } } }
选项 2:Atlas API 凭证参数

使用您的 Atlas API 服务帐户凭据。更多详情请参阅Atlas API 访问部分。

{ "servers": { "MongoDB": { "command": "npx", "args": [ "-y", "mongodb-mcp-server", "--apiClientId", "your-atlas-client-id", "--apiClientSecret", "your-atlas-client-secret" ] } } }
其他选择

或者,您可以在配置文件中使用环境变量或设置它们并通过 npx 运行服务器。

  • MCP 文件示例中通过环境变量的连接字符串
  • 通过 MCP 文件示例中的环境变量获取 Atlas API 凭据

🛠️ 支持的工具

工具清单

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 工具才可用。

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 服务器可以使用多种方法进行配置,其优先级如下(从高到低):

  1. 命令行参数
  2. 环境变量

配置选项

选项描述
apiClientId用于身份验证的 Atlas API 客户端 ID
apiClientSecret用于身份验证的 Atlas API 客户端密钥
connectionStringMongoDB 连接字符串用于直接数据库连接(可选用户可以选择在每次工具调用时通知它)
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 中创建一个服务帐户:

  1. 创建服务帐户:
    • 登录cloud.mongodb.com上的 MongoDB Atlas
    • 导航至访问管理器 > 组织访问
    • 单击“添加新”>“应用程序”>“服务帐户”
    • 输入您的服务帐户的名称、描述和到期日期(例如,“MCP、MCP 服务器访问、7 天”)
    • 选择适当的权限(如需完全访问权限,请使用组织所有者)
    • 点击“创建”

要了解有关服务帐户的更多信息,请查看MongoDB Atlas 文档

  1. 保存客户端凭证:
    • 创建后,您将看到客户端 ID 和客户端密钥
    • **重要提示:**立即复制并保存客户端密钥,因为它不会再次显示
  2. 添加访问列表条目(可选但建议):
    • 将您的 IP 地址添加到 API 访问列表
  3. 配置 MCP 服务器:
    • 使用以下配置方法之一设置您的apiClientIdapiClientSecret

配置方法

环境变量

使用前缀MDB_MCP_后跟大写带下划线的选项名称来设置环境变量:

# Set Atlas API credentials export MDB_MCP_API_CLIENT_ID="your-atlas-client-id" export MDB_MCP_API_CLIENT_SECRET="your-atlas-client-secret" # Set a custom MongoDB connection string export MDB_MCP_CONNECTION_STRING="mongodb+srv://username:password@cluster.mongodb.net/myDatabase" export MDB_MCP_LOG_PATH="/path/to/logs"
MCP 配置文件示例
带有环境变量的连接字符串
{ "servers": { "MongoDB": { "command": "npx", "args": ["-y", "mongodb-mcp-server"], "env": { "MDB_MCP_CONNECTION_STRING": "mongodb+srv://username:password@cluster.mongodb.net/myDatabase" } } } }
带有环境变量的 Atlas API 凭据
{ "servers": { "MongoDB": { "command": "npx", "args": ["-y", "mongodb-mcp-server"], "env": { "MDB_MCP_API_CLIENT_ID": "your-atlas-client-id", "MDB_MCP_API_CLIENT_SECRET": "your-atlas-client-secret" } } } }
命令行参数

启动服务器时将配置选项作为命令行参数传递:

npx -y mongodb-mcp-server --apiClientId="your-atlas-client-id" --apiClientSecret="your-atlas-client-secret" --connectionString="mongodb+srv://username:password@cluster.mongodb.net/myDatabase" --logPath=/path/to/logs
MCP 配置文件示例
带有命令行参数的连接字符串
{ "servers": { "MongoDB": { "command": "npx", "args": [ "-y", "mongodb-mcp-server", "--connectionString", "mongodb+srv://username:password@cluster.mongodb.net/myDatabase" ] } } }
带有命令行参数的 Atlas API 凭据
{ "servers": { "MongoDB": { "command": "npx", "args": [ "-y", "mongodb-mcp-server", "--apiClientId", "your-atlas-client-id", "--apiClientSecret", "your-atlas-client-secret" ] } } }

🤝 贡献

有兴趣贡献代码吗?太棒了!请查看我们的贡献指南,了解代码贡献指南、标准、添加新工具以及故障排除信息。

-
security - not tested
A
license - permissive license
-
quality - not tested

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管理功能。

  1. 📚 Table of Contents
    1. Prerequisites
      1. Setup
        1. Quick Start
      2. 🛠️ Supported Tools
        1. Tool List
      3. Configuration
        1. Configuration Options
        2. Atlas API Access
        3. Configuration Methods
      4. 🤝 Contributing

        Related MCP Servers

        • A
          security
          A
          license
          A
          quality
          A 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 -
          14
          9
          2
          JavaScript
          MIT License
          • Apple
        • -
          security
          A
          license
          -
          quality
          An MCP server that enables users to create and manage MongoDB Atlas clusters, users, and network access through natural language commands.
          Last updated -
          254
          2
          JavaScript
          MIT License
          • Apple
          • Linux
        • -
          security
          A
          license
          -
          quality
          An MCP server that enables large language models to interact directly with MongoDB databases, allowing them to query collections, inspect schemas, and manage data through natural language.
          Last updated -
          340
          MIT License
          • Apple
        • -
          security
          -
          license
          -
          quality
          A 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 -
          6
          JavaScript
          MIT License

        View all related MCP servers

        ID: o13w6dioja