MCP MongoDB服务器
______________________________________________________________________
](https://smithery.ai/server/mcp-mongo-server) 
一个模型上下文协议服务器,使LLM能够与MongoDB数据库交互。该服务器提供通过标准化接口检查收集模式和执行MongoDB操作的功能。
演示

主要特点
智能对象ID处理
- 字符串ID和MongoDB ObjectId之间的智能转换
- 可配置
objectIdMode参数:
- "auto":根据字段名称转换(默认) - "none":无转换 - "force":强制所有字符串ID字段为ObjectId
灵活的配置
- 环境变量:
- MCP_MONGODB_URI:MongoDB连接URI - MCP_MONGODB_READONLY:设置为“true”时启用只读模式
- 命令行选项:
- --read-only 或 -r:以只读模式连接
只读模式
- 防止写入操作(更新、插入、创建索引)
- 使用MongoDB的二次读取首选项以获得最佳性能
- 非常适合安全连接到生产数据库
MongoDB操作
- 读取操作:
- 查询具有可选执行计划分析的文档 - 执行聚合管道 - 统计符合条件的文档 - 获取集合架构信息
- 写入操作 (非只读模式时):
- 更新文档 - 插入新文档 - 创建索引
LLM集成
- 收集完成以增强LLM交互
- 用于改进上下文理解的模式推理
- 数据洞察的收集分析
安装
全球安装
npm install -g mcp-mongo-server为了发展
# Clone repository
git clone https://github.com/kiliczsh/mcp-mongo-server.git
cd mcp-mongo-server
# Install dependencies
npm install
# Build
npm run build
# Development with auto-rebuild
npm run watch用法
基本用法
# Start server with MongoDB URI
npx -y mcp-mongo-server mongodb://muhammed:kilic@localhost:27017/database
# Connect in read-only mode
npx -y mcp-mongo-server mongodb://muhammed:kilic@localhost:27017/database --read-only环境变量
您可以使用环境变量配置服务器,这对于CI/CD管道、Docker容器或当您不想在命令参数中公开连接详细信息时特别有用:
# Set MongoDB connection URI
export MCP_MONGODB_URI="mongodb://muhammed:kilic@localhost:27017/database"
# Enable read-only mode
export MCP_MONGODB_READONLY="true"
# Run server (will use environment variables if no URI is provided)
npx -y mcp-mongo-server在Claude Desktop配置中使用环境变量:
{
"mcpServers": {
"mongodb-env": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server"
],
"env": {
"MCP_MONGODB_URI": "mongodb://muhammed:kilic@localhost:27017/database",
"MCP_MONGODB_READONLY": "true"
}
}
}
}在Docker中使用环境变量:
# Build
docker build -t mcp-mongo-server .
# Run
docker run -it -d -e MCP_MONGODB_URI="mongodb://muhammed:kilic@localhost:27017/database" -e MCP_MONGODB_READONLY="true" mcp-mongo-server
# or edit docker-compose.yml and run
docker-compose up -d与Claude Desktop集成
手动配置
将服务器配置添加到Claude Desktop的配置文件中:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%/Claude/claude_desktop_config.json
命令行参数方法:
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database"
]
},
"mongodb-readonly": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database",
"--read-only"
]
}
}
}环境变量方法:
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server"
],
"env": {
"MCP_MONGODB_URI": "mongodb://muhammed:kilic@localhost:27017/database"
}
},
"mongodb-readonly": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server"
],
"env": {
"MCP_MONGODB_URI": "mongodb://muhammed:kilic@localhost:27017/database",
"MCP_MONGODB_READONLY": "true"
}
}
}
}GitHub包用法:
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": [
"-y",
"github:kiliczsh/mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database"
]
},
"mongodb-readonly": {
"command": "npx",
"args": [
"-y",
"github:kiliczsh/mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database",
"--read-only"
]
}
}
}与Windsurf和Cursor集成
MCP MongoDB服务器可以与Windsurf和Cursor以类似于Claude Desktop的方式一起使用。
风帆配置
将服务器添加到Windsurf配置中:
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database"
]
}
}
}光标配置
对于Cursor,将服务器配置添加到您的设置中:
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": [
"-y",
"mcp-mongo-server",
"mongodb://muhammed:kilic@localhost:27017/database"
]
}
}
}您还可以将环境变量方法用于Windsurf和Cursor,遵循Claude Desktop配置中显示的相同模式。
自动化安装
使用Smithery:
npx -y @smithery/cli install mcp-mongo-server --client claude使用mcp get:
npx @michaellatman/mcp-get@latest install mcp-mongo-server可用工具
查询操作
- 怎么翻译:执行MongoDB查询
{
collection: "users",
filter: { age: { $gt: 30 } },
projection: { name: 1, email: 1 },
limit: 20,
explain: "executionStats" // Optional
}- 合计:运行聚合管道
{
collection: "orders",
pipeline: [
{ $match: { status: "completed" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }
],
explain: "queryPlanner" // Optional
}- 计数:统计匹配的文档
{
collection: "products",
query: { category: "electronics" }
}写入操作
- 更新:修改文档
{
collection: "posts",
filter: { _id: "60d21b4667d0d8992e610c85" },
update: { $set: { title: "Updated Title" } },
upsert: false,
multi: false
}- 插入:添加新文档
{
collection: "comments",
documents: [
{ author: "user123", text: "Great post!" },
{ author: "user456", text: "Thanks for sharing" }
]
}- 创建指数:创建集合索引
{
collection: "users",
indexes: [
{
key: { email: 1 },
unique: true,
name: "email_unique_idx"
}
]
}系统操作
- 服务器信息:获取MongoDB服务器详细信息
{
includeDebugInfo: true // Optional
}调试
由于MCP服务器通过stdio进行通信,调试可能具有挑战性。使用MCP检查器以获得更好的可见性:
npm run inspector这将提供一个URL,用于访问浏览器中的调试工具。
运行评估
evals包加载一个mcp客户端,然后运行index.ts文件,因此不需要在测试之间重建。您可以通过在npx命令前加前缀来加载环境变量。可以找到完整的文档 这里.
OPENAI_API_KEY=your-key npx mcp-eval src/evals/evals.ts src/schemas/tools.ts许可证
此MCP服务器根据MIT许可证获得许可。这意味着您可以根据MIT许可证的条款和条件自由使用、修改和分发软件。有关更多详细信息,请参阅项目存储库中的LICENSE文件。
