有效的MongoDB-MCP服务器
一个可靠的MongoDB MCP(模型上下文协议)服务器,为Claude Desktop提供无缝的MongoDB集成,内置模式发现和字段验证。
特性
- 🔍 架构发现:自动分析集合结构
- ✅ 现场验证:防止字段名错误
- 📊 完全支持MongoDB:查找、聚合、插入、更新、删除操作
- 🚀 高性能:高效的连接池和查询优化
- 🔐 安全:支持MongoDB Atlas和身份验证
- 🎯 类型安全:使用TypeScript和Zod验证构建
安装
从npm安装
npm install -g @sourabhshegane/mongodb-mcp-that-works配置
添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "@sourabhshegane/mongodb-mcp-that-works@latest"],
"env": {
"MONGODB_URI": "mongodb+srv://username:password@cluster.mongodb.net/database",
"MONGODB_DATABASE": "your_database_name"
}
}
}
}配置选项
MONGODB_URI:您的MongoDB连接字符串(必填)MONGODB_DATABASE:默认数据库名称(可选)
可用工具
1. listCollections
列出数据库中的所有集合。
// Example
mcp.listCollections({ filter: {} })2. find
通过过滤、排序和分页在集合中查找文档。
// Example
mcp.find({
collection: "users",
filter: { status: "active" },
sort: { createdAt: -1 },
limit: 10
})3. findOne
查找单个文档。
// Example
mcp.findOne({
collection: "users",
filter: { email: "user@example.com" }
})4. aggregate
运行聚合管道。
// Example
mcp.aggregate({
collection: "orders",
pipeline: [
{ $match: { status: "completed" } },
{ $group: { _id: "$userId", total: { $sum: "$amount" } } }
]
})5. count
统计与筛选器匹配的文档。
// Example
mcp.count({
collection: "products",
filter: { inStock: true }
})6. distinct
获取字段的不同值。
// Example
mcp.distinct({
collection: "orders",
field: "status"
})7. insertOne
插入单个文档。
// Example
mcp.insertOne({
collection: "users",
document: { name: "John Doe", email: "john@example.com" }
})8. updateOne
更新单个文档。
// Example
mcp.updateOne({
collection: "users",
filter: { _id: "123" },
update: { $set: { status: "active" } }
})9. deleteOne
删除单个文档。
// Example
mcp.deleteOne({
collection: "users",
filter: { _id: "123" }
})10. getSchema
分析集合结构并发现字段名称。
// Example
mcp.getSchema({
collection: "users",
sampleSize: 100
})
// Returns:
{
"collection": "users",
"sampleSize": 100,
"fields": {
"_id": {
"types": ["ObjectId"],
"examples": ["507f1f77bcf86cd799439011"],
"frequency": "100/100",
"percentage": 100
},
"email": {
"types": ["string"],
"examples": ["user@example.com"],
"frequency": "100/100",
"percentage": 100
}
}
}最佳实践
- 首先使用架构发现:查询前,运行
getSchema了解字段名称 - 处理对象ID:服务器自动将字符串ID转换为ObjectId
- 使用预测:限制返回的字段以提高性能
- 批量操作:对复杂查询使用聚合管道
例子
基本用法
// Get schema first to avoid field name mistakes
const schema = await mcp.getSchema({ collection: "reports" });
// Use correct field names from schema
const reports = await mcp.find({
collection: "reports",
filter: { organization_id: "64ba7374f8b63db2083b2665" },
limit: 10
});高级聚合
const analytics = await mcp.aggregate({
collection: "orders",
pipeline: [
{ $match: { createdAt: { $gte: new Date("2024-01-01") } } },
{ $group: {
_id: { $dateToString: { format: "%Y-%m", date: "$createdAt" } },
revenue: { $sum: "$amount" },
count: { $sum: 1 }
}},
{ $sort: { _id: 1 } }
]
});故障排除
连接问题
- 验证您的MongoDB URI是否正确
- 检查与MongoDB Atlas的网络连接
- 确保IP白名单包括您当前的IP
字段名称错误
- 始终使用
getSchema查找正确的字段名称 - 记住MongoDB区分大小写
- 检查嵌套字段路径中的拼写错误(例如,“user.profile.name”)
演出
- 对频繁查询的字段使用索引
- 限制结果集
limit参数 - 使用投影只返回所需的字段
许可证
MIT许可证-有关详细信息,请参阅许可证文件
更新日志
v0.1.0
- 初始版本
- 完整的MongoDB CRUD操作
- 架构发现工具
- 自动对象ID转换
- TypeScript支持
______________________________________________________________________
由于官方的MongoDB MCP对我不起作用,我感到很痛苦
