MCP MongoDB 服务器
模型上下文协议 (MLM) 服务器,使 LLM 能够与 MongoDB 数据库交互。该服务器提供通过标准化接口检查集合架构和执行 MongoDB 操作的功能。
演示

Related MCP server: MongoDB MCP Server for LLMs
主要特点
智能 ObjectId 处理
字符串ID与MongoDB ObjectId之间的智能转换
可使用
objectIdMode参数配置:"auto":根据字段名称转换(默认)"none":无转换"force":强制所有字符串 ID 字段为 ObjectId
灵活配置
环境变量:
MCP_MONGODB_URI:MongoDB 连接 URIMCP_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 Windows : %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 }count :统计匹配的文档数量
{ 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" } ] }createIndex :创建集合索引
{ collection: "users", indexes: [ { key: { email: 1 }, unique: true, name: "email_unique_idx" } ] }
系统操作
serverInfo :获取 MongoDB 服务器详细信息
{ includeDebugInfo: true // Optional }
调试
由于 MCP 服务器通过 stdio 进行通信,调试起来可能比较困难。使用 MCP 检查器可以更好地了解情况:
npm run inspector这将提供一个 URL 来访问浏览器中的调试工具。
执照
此 MCP 服务器采用 MIT 许可证。这意味着您可以自由使用、修改和分发该软件,但须遵守 MIT 许可证的条款和条件。更多详情,请参阅项目仓库中的 LICENSE 文件。