使用说明
MongoDB MCP服务器
一个强大的模型上下文协议(MCP)服务器,提供与MongoDB数据库交互的工具和提示。基于Node.js和MongoDB构建,具有优雅的关机处理和全面的错误管理功能。
特性
- 使用Mongoose集成MongoDB
- 用户管理工具:
create-user:创建新用户get-user:通过电子邮件检索用户list-users:列出所有分页用户
- 引导操作的交互式提示
- 优雅的停机处理
- 全面的错误管理
- 干净的单文件实现
先决条件
- Node.js(最新LTS版本)
- MongoDB(v8.0或更高版本)
- 克劳德桌面版(最新版本)
- 带游标扩展名的Visual Studio代码(用于开发)
开发设置
-
安装MongoDB:
# Using Homebrew on macOS brew tap mongodb/brew brew install mongodb-community # Start MongoDB service brew services start mongodb-community -
在VS代码中安装游标:
- 打开VS代码
- 转到扩展(Ctrl+Shift+X)
- 搜索“光标”
- 单击安装
-
克隆和设置:
git clone <repository-url> cd learn-mcp-mongo npm install -
配置环境:
cp .env.example .env # Edit .env with your MongoDB URI if different from default
快速开始
-
克隆或下载此存储库
-
安装依赖项:
npm install -
配置MongoDB:
- 确保MongoDB在本地运行(默认:mongodb://localhost:27017)
- 或更新
.env带有MongoDB连接字符串的文件:MONGODB_URI=your_mongodb_connection_string
-
为桌面配置Claude:
- 打开或创建
~/Library/Application Support/Claude/claude_desktop_config.json - 添加以下配置:
{ "mcpServers": { "mcp-mongo": { "command": "node", "args": ["/absolute/path/to/server.js"] } } }
- 打开或创建
-
启动Claude桌面版
- MCP服务器将自动启动
- 查找“搜索和工具”图标以访问工具
在MCP服务器上使用游标
-
安装光标(AI代码编辑器):
- 下载自 https://www.cursor.so/
- 在您的计算机上安装并打开Cursor
-
将MCP服务器添加到游标:
- 打开的游标
- 首选
Settings>Integrations>MCP Servers - 点击
Add MCP Server - 填写:
- 姓名: 蒙古mcp
- 命令: 节点
- 论据: /absolute/path/to/server.js
- 工作目录: /绝对/路径/学习mcp蒙古语
- 保存并启用集成
-
使用Cursor的AI功能:
- 在Cursor中打开项目文件夹
- 使用
/help在命令面板中显示可用的AI命令 - 使用
/edit,/fix,/doc,以及其他与代码和MCP工具交互的AI功能 - 现在,您可以在人工智能的帮助下直接在Cursor中测试、调试和开发MCP服务器
使用示例
创建用户
Create a new user with:
- name: "John Doe"
- email: "john@example.com"
- age: 30
查找用户
Get user information for email: john@example.com
项目结构
learn-mcp-mongo/
├── server.js # Main server file with all functionality
├── .env # Environment variables
└── package.json # Project dependencies and scripts
可用工具
创建用户
在数据库中创建新用户。
- 参数:
- name:用户的全名(字符串,必填)
- email:用户的电子邮件地址(字符串,必填,唯一)
- age:用户的年龄(数字,必填)
- 答复:
{ "_id": "user_id", "name": "John Doe", "email": "john@example.com", "age": 30, "createdAt": "2025-06-26T00:00:00.000Z" }
获取用户
通过用户的电子邮件地址检索用户。
- 参数:
- email:用户的电子邮件地址(字符串,必填)
- 答复:
{ "_id": "user_id", "name": "John Doe", "email": "john@example.com", "age": 30, "createdAt": "2025-06-26T00:00:00.000Z" }
列出用户
列出数据库中所有分页的用户。
- 参数:
- limit:返回的最大用户数(数字,可选,默认值:10)
- 答复:
[ { "_id": "user_id", "name": "John Doe", "email": "john@example.com", "age": 30, "createdAt": "2025-06-26T00:00:00.000Z" }, // ... more users ]
可用提示
创建新用户
一个交互式提示,通过询问以下内容来引导您完成创建新用户的过程:
- 全名
- 电子邮件地址
- 年龄
开发指南
运行服务器
-
启动开发模式:
# Run with inspector for debugging npx @modelcontextprotocol/inspector node mcp-server.js # Or run directly npm start -
在VS代码中使用游标:
- 在VS Code中打开项目
- 使用Cursor的AI功能:
- 类型
/help用于光标命令 - 使用
/edit获取代码建议 - 使用
/doc生成文档 - 使用
/fix获取错误修复
- 类型
调试
-
检查服务器日志:
# Watch server logs in real-time tail -f ~/Library/Logs/Claude/mcp*.log -
MongoDB操作:
# Check MongoDB status mongosh use mcp-mongo db.users.find() # List all users -
手动测试工具:
# Using curl to test tools (when running in HTTP mode) curl -X POST http://localhost:3000/tools/list-users
服务器功能
-
优雅关机:
- 处理信号情报、信号处理、信号处理
- 正确关闭MongoDB连接
- 记录关机过程
-
错误处理:
- MongoDB连接错误
- 工具执行错误
- 无异常
- 未经处理的拒绝
-
性能选项:
- MongoDB连接超时:5s
- 心跳频率:2s
- 用户列表分页
故障排除
- 确保MongoDB正在运行且可访问
- 在以下网址查看Claude的桌面日志
~/Library/Logs/Claude/mcp*.log - 验证claude_desktop_config.json中的server.js路径是否正确
- 配置更改后重新启动Claude for Desktop