multiple-mongodb-mcp
v1.0.0
Published
An MCP stdio server that provides read-only access to multiple MongoDB instances via a single MCP connection.
Maintainers
Readme
Multiple MongoDB MCP Server
一个 MCP (Model Context Protocol) stdio 服务器,通过单个 MCP 连接提供对多个 MongoDB 实例的访问。
特性
- 单进程管理多个 MongoDB 实例连接
- 默认只读模式,通过环境变量开启写操作
- 支持 MongoDB 3.6+(使用 mongodb driver v4.x)
- 实例健康检查(不依赖 admin 权限)
- 每实例可配置查询文档数上限
- 网络断开后自动重连(driver 内置机制)
项目结构
src/
├── index.ts # 入口:MCP server 启动、请求路由、参数校验
├── types.ts # 类型定义(MongoDBInstanceConfig, ToolResult)
├── config.ts # 配置加载与校验(从 JSON 文件)
├── instance.ts # MongoDBInstance 类 — 单实例连接生命周期
├── instanceManager.ts # InstanceManager 类 — 多实例管理
└── tools/
├── definitions.ts # 所有 tool 的 JSON Schema 定义(读/写分离)
├── read.ts # 查询类 tool(find / aggregate / count / explain)
├── metadata.ts # 元数据类 tool(databases / collections / indexes / schema / stats / logs)
└── write.ts # 写操作 tool(insert / update / delete / collection管理 / index管理)配置
实例配置文件
创建一个 JSON 文件配置你的 MongoDB 实例:
{
"instances": [
{
"name": "prod-mongodb",
"description": "生产环境 MongoDB",
"connectionString": "mongodb://user:password@host:27017/dbname?authSource=admin"
},
{
"name": "stage-mongodb",
"description": "Stage环境 MongoDB",
"connectionString": "mongodb://user:password@stage-host:27017/?authSource=admin",
"maxDocumentsPerQuery": 200
}
]
}配置字段
| 字段 | 必填 | 默认值 | 说明 |
|------|:----:|--------|------|
| name | ✅ | - | 实例唯一标识,调用 tool 时通过此名称指定实例 |
| description | ❌ | "" | 实例描述,list_instances 时展示 |
| connectionString | ✅ | - | MongoDB 连接字符串(mongodb:// 或 mongodb+srv://) |
| connectTimeoutMS | ❌ | 10000 | 连接超时时间(ms) |
| serverSelectionTimeoutMS | ❌ | 10000 | 服务器选择超时时间(ms) |
| maxDocumentsPerQuery | ❌ | 100 | 每次查询返回的最大文档数 |
环境变量
| 变量 | 必填 | 说明 |
|------|:----:|------|
| MONGODB_CONFIG_FILE | ✅ | 实例配置 JSON 文件路径 |
| MONGODB_WRITABLE | ❌ | 设为 true 开启写操作,默认只读 |
Kiro MCP Setting
在 .kiro/settings/mcp.json 或 ~/.kiro/settings/mcp.json 中添加:
{
"mcpServers": {
"multiple-mongodb": {
"command": "node",
"args": ["/path/to/multiple-mongodb-mcp/build/index.js"],
"env": {
"MONGODB_CONFIG_FILE": "/path/to/mongodb-instances.json",
"MONGODB_WRITABLE": "false"
}
}
}
}可用 Tools
读操作(始终可用,共 13 个)
| Tool | 说明 |
|------|------|
| list_instances | 列出所有配置的实例及可用状态 |
| list_databases | 列出指定实例的所有数据库 |
| list_collections | 列出数据库中的所有集合 |
| find | 执行 find 查询 |
| aggregate | 执行集合级聚合管道 |
| aggregate_db | 执行数据库级聚合管道 |
| count | 文档计数(支持 filter) |
| explain | 获取查询执行计划 |
| collection_indexes | 列出集合索引 |
| collection_schema | 采样推断集合 schema |
| collection_storage_size | 获取集合存储大小 |
| db_stats | 获取数据库统计信息 |
| mongodb_logs | 获取 mongod 日志 |
写操作(需要 MONGODB_WRITABLE=true,共 9 个)
| Tool | 说明 |
|------|------|
| insert_many | 批量插入文档 |
| update_many | 批量更新文档 |
| delete_many | 批量删除文档 |
| create_collection | 创建集合 |
| drop_collection | 删除集合 |
| drop_database | 删除数据库 |
| rename_collection | 重命名集合 |
| create_index | 创建索引 |
| drop_index | 删除索引 |
构建
npm install
npm run build开发
npm run watch使用 MCP Inspector 测试
MONGODB_CONFIG_FILE=./mongodb-instances.json npx @modelcontextprotocol/inspector node build/index.js