MongoDB MCP Server

MongoDB MCP Server

未知

概述

MongoDB MCP 服务器提供对 MongoDB 数据库的只读访问,让 LLMs 能够检查集合模式并执行聚合查询。

设置

在 Claude Desktop 中添加服务器配置:

  1. 打开 claude_desktop_config.json 文件
  2. 在 "mcpServers" 部分添加以下配置:
"mongodb": {
  "command": "npx",
  "args": ["-y", "@pash1986/mcp-server-mongodb"],
  "env": {
    "MONGODB_URI": "mongodb+srv://<yourcluster>"  // 或 'mongodb://localhost:27017'
  }
}

核心功能

1. 执行聚合查询

使用 aggregate 工具运行 MongoDB 聚合管道:

{
  "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. 查看集合模式

每个集合的模式信息可通过模型上下文资源访问: mongodb://<host>/<collection>/schema

注意事项

  • 默认限制返回 1000 个文档
  • 操作默认超时为 30 秒
  • 仅支持只读操作
  • 根据 MIT 许可证开源使用