这是一个专为 MongoDB 数据库设计的模型上下文协议 (MCP) 服务器。它能深入分析 MongoDB 数据库的结构,并借助人工智能技术生成精准的查询,极大提升数据库操作效率。
本服务器使用前需进行安装与运行,以下是详细步骤:
npm install
npm run build
node dist/index.js "mongodb://localhost:27017"
此功能可分析数据库的集合结构并生成实体关系图 (ERD)。
{
"database": "string",
"collections": ["string"], // 可选
"format": "mermaid" | "json" // 默认值: "mermaid"
}
{
"database": "test",
"format": "mermaid"
}
基于自然语言描述生成 MongoDB 查询,通过分析数据库的实际结构,提供准确的查询。
{
"database": "string",
"collection": "string",
"description": "string",
"type": "mongodb" | "mongoose", // 默认值: "mongodb"
"includeExplanation": boolean // 默认值: true
}
{
"database": "test",
"collection": "users",
"description": "查找今天新注册的用户",
"type": "mongodb"
}
npm install
npm run build
node dist/index.js "mongodb://localhost:27017"
在 macOS 上与 Claude Desktop 一起使用,请按照以下步骤配置:
~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcp": {
"servers": [
{
"name": "mongodb",
"command": "node /path/to/mongo-mcp-server/dist/index.js mongodb://localhost:27017/test"
}
]
}
}
src/
├── index.ts # 主服务器代码
├── tools/
│ ├── generateErd.ts # ERD 生成工具
│ └── generateQuery.ts # 查询生成工具
src/tools
目录下创建新的工具文件。export const newTool = {
parameters: {
// 参数定义
},
async execute(params: any, context: { client: MongoClient }) {
// 工具逻辑实现
},
};
src/index.ts
注册新工具。