MongoDB MCP Server

MongoDB MCP Server

镜像

简介

MongoDB MCP 服务器提供对 MongoDB 数据库的只读访问,允许 LLMs 查询集合模式并执行聚合管道操作。

安装与配置

将以下配置添加到 Claude Desktop 的 claude_desktop_config.json 文件中:

"mongodb": {
  "command": "npx",
  "args": [
    "-y",
    "@pash1986/mcp-server-mongodb"
  ],
  "env": {
    "MONGODB_URI": "mongodb+srv://<yourcluster>"  // 或 "mongodb://localhost:27017"
  }
}

替换 <yourcluster> 为您的实际 MongoDB 连接字符串。

主要功能

1. 执行聚合查询 (aggregate)

{
  "collection": "users",
  "pipeline": [
    { "$match": { "age": { "$gt": 21 } } },
    { "$group": {
      "_id": "$city",
      "avgAge": { "$avg": "$age" },
      "count": { "$sum": 1 }
    }},
    { "$sort": { "count": -1 } },
    { "$limit": 10 }
  ],
  "options": {
    "allowDiskUse": true,
    "maxTimeMS": 60000
  }
}

2. 获取查询执行计划 (explain)

{
  "collection": "users",
  "pipeline": [
    { "$match": { "age": { "$gt": 21 } } }
  ],
  "verbosity": "executionStats"
}

3. 访问集合模式

通过资源 URL 访问:mongodb://<host>/<collection>/schema

注意事项

  • 默认查询限制为 1000 条文档
  • 默认查询超时为 30 秒
  • 仅支持只读操作
  • 模式是从集合样本中自动推断的

安全特性

服务器仅支持只读操作,自动为大型结果集添加文档限制,并为所有操作设置超时限制。


依照 MIT 许可证分发,可自由使用、修改和分发。