/ 目录 / 演练场 / MongoDB MCP Server
● 社区 kiliczsh 🔑 需要你的密钥

MongoDB MCP Server

作者 kiliczsh · kiliczsh/mcp-mongo-server

把 MongoDB 连接字符串交给 Claude,让它查询、聚合、检查 schema——还有一个 --read-only 模式保护生产数据安全。

mcp-mongo-server 是一个简洁的 MongoDB MCP server。只需将连接 URI 作为唯一参数传入,Claude 就可以执行 find、aggregate、检查索引、推断 schema 以及浏览集合结构。内置 --read-only 标志,启用后会禁用所有写操作——在不希望 Claude 修改生产数据时非常有用。

为什么要用

核心特性

实时演示

实际使用效果

mongo-mcp-kiliczsh.replay ▶ 就绪
0/0

安装

选择你的客户端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "mongo-mcp-kiliczsh": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://localhost:27017/mydb"
      ]
    }
  }
}

打开 Claude Desktop → Settings → Developer → Edit Config。保存后重启应用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "mongo-mcp-kiliczsh": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://localhost:27017/mydb"
      ]
    }
  }
}

Cursor 使用与 Claude Desktop 相同的 mcpServers 格式。项目级配置优先于全局。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "mongo-mcp-kiliczsh": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://localhost:27017/mydb"
      ]
    }
  }
}

点击 Cline 侧栏中的 MCP Servers 图标,然后选 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "mongo-mcp-kiliczsh": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://localhost:27017/mydb"
      ]
    }
  }
}

格式与 Claude Desktop 相同。重启 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "mongo-mcp-kiliczsh",
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://localhost:27017/mydb"
      ]
    }
  ]
}

Continue 使用服务器对象数组,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "mongo-mcp-kiliczsh": {
      "command": {
        "path": "npx",
        "args": [
          "-y",
          "mcp-mongo-server",
          "mongodb://localhost:27017/mydb"
        ]
      }
    }
  }
}

加入 context_servers。Zed 保存后热重载。

claude mcp add mongo-mcp-kiliczsh -- npx -y mcp-mongo-server mongodb://localhost:27017/mydb

一行命令搞定。用 claude mcp list 验证,claude mcp remove 卸载。

使用场景

实战用法: MongoDB MCP Server

在不写入任何数据的情况下探索陌生的 MongoDB 数据库

👤 接手新服务的开发者 ⏱ ~15 min beginner

何时使用: 你接手了一个 Mongo 实例,但 schema 完全没有文档。

前置条件
  • 只读 Mongo 用户db.createUser({user:'reader', roles:[{role:'read', db:'mydb'}]})
步骤
  1. 以只读模式连接
    Use the mongo MCP. List all collections and infer the schema for the top 3 by size.✓ 已复制
    → 返回集合列表及每个集合的 JSON schema
  2. 抽样检查
    Show me 5 sample documents from orders — anonymize emails.✓ 已复制
    → 返回 5 条文档,PII 已脱敏
  3. 梳理关系
    Which collections reference each other by ObjectId? Draw a quick text diagram.✓ 已复制
    → 纯文本 ER 图

结果: 10 分钟内建立数据库工作心智模型,无需 DBA 介入。

注意事项
  • 采样可能遗漏高基数字段 — 在推断调用中增大采样量;若数据有时间偏斜,按日期范围采样
搭配使用: filesystem

借助 Claude 的索引建议优化慢聚合查询

👤 后端开发者 ⏱ ~25 min intermediate

何时使用: 一个聚合管道执行需要 30 秒,不清楚哪个阶段最耗时。

步骤
  1. 分析执行计划
    Run this aggregation with explain=true. Show the winning plan.✓ 已复制
    → 返回执行计划,包含各阶段扫描文档数
  2. 诊断瓶颈
    Which stage is the bottleneck? What index would help?✓ 已复制
    → 带理由的具体索引建议
  3. 验证效果
    Re-explain after I add the index. Did totalDocsExamined drop?✓ 已复制
    → 是/否答复,附具体数字

结果: 加一个精准索引,聚合速度提升 10 倍。

注意事项
  • Claude 建议的索引会增加写操作开销 — 应用前先问:「这个索引会带来多大的写性能损耗?」

找出违反隐式 schema 的文档

👤 数据工程师 ⏱ ~20 min intermediate

何时使用: 无 schema 的数据库会积累数据漂移;你需要找出问题文档。

步骤
  1. 采样
    Sample 1000 documents from users. What fields are missing or have unexpected types?✓ 已复制
    → 每个字段的空值率 / 类型频率表
  2. 定位问题
    Find all users where email is null or not a string.✓ 已复制
    → 问题文档数量 + 示例 _id

结果: 可直接交给迁移脚本处理的具体脏数据报告。

注意事项
  • 大集合查询超时 — 先用 $sample,再按范围缩小后续查询

组合

与其他 MCP 搭配,撬动十倍杠杆

mongo-mcp-kiliczsh + filesystem

将聚合结果导出为 JSON 供下游工具使用

Run the aggregation and save results to /tmp/orders-by-month.json.✓ 已复制
mongo-mcp-kiliczsh + github

发现数据漂移后,开一个包含迁移脚本的 PR

We found 1200 users without email. Open a PR with a backfill migration.✓ 已复制

工具

此 MCP 暴露的能力

工具输入参数何时调用成本
find collection, filter, projection?, limit? 查询特定文档 1 query
aggregate collection, pipeline[], explain? 对数据进行分组 / 变换 1 query
list_collections (none) 了解数据库中有哪些集合 free
schema collection, sample_size? 从采样文档推断数据结构 1 sample read
list_indexes collection 查询优化讨论时使用 free
insert_one collection, document 仅在非 --read-only 模式下使用 1 write

成本与限制

运行它的成本

API 配额
受限于你的 Mongo 集群 RU/IOPS
每次调用 Token 数
200-5000,取决于结果集大小
费用
免费(开源)
提示
只 project 需要的字段;尽早设置 limit 上限

安全

权限、密钥、影响范围

最小权限: read on the target db
凭据存储: URI 作为 CLI 参数传入——注意不要写入 shell 历史
数据出站: 仅到你的 Mongo 主机
切勿授予: dbAdmin root

故障排查

常见错误与修复

MongoServerSelectionError

检查网络连通性、IP 白名单、TLS 配置

验证: 用相同 URI 运行 mongosh
Authentication failed

检查 URI 中的 authSource(通常是 admin);确认用户在该 DB 中存在

Tool not found in read-only mode

如果需要写操作,去掉 --read-only;否则使用另一个 MCP 处理写操作

替代方案

MongoDB MCP Server 对比其他方案

替代方案何时用它替代权衡
MongoDB CompassGUI 探索,不需要 AI 介入无 agent 集成;只能手动查询
mongosh in a shell MCP需要 mongosh 的完整能力风险更高;此 MCP 提供的是边界明确的工具集

更多

资源

📖 阅读 GitHub 上的官方 README

🐙 查看未解决的 issue

🔍 浏览全部 400+ MCP 服务器和 Skills