README
🚀 MCP 数据库文档
MCP 数据库是一个遵循 MongoDB 协议设计的 JSON - RPC 服务,它支持多种数据库后端,如 PostgreSQL 和 MySQL。用户能够通过 JSON 格式的请求来执行 SQL 查询或 MongoDB 操作,为数据库操作提供了便利。
🚀 快速开始
MCP 数据库允许你通过 JSON 格式的请求执行 SQL 查询或 MongoDB 操作。下面将为你介绍如何进行安装和启动服务。
📦 安装指南
使用 Node.js 进行安装
npm install mcp-dbs
启动服务器
node index.js
默认监听地址为 http://localhost:3000,配置文件位于 config.json。更多详细信息请参考 GitHub。
💻 使用示例
数据库支持
PostgreSQL 连接示例
{
"connectionString": "postgresql://username:password@localhost:5432/mydb"
}
MySQL 连接示例
{
"connectionString": "mysql://username:password@localhost:3006/mydb"
}
更多配置选项请参考 config.json。
资源和服务
执行查询
基础用法
以下是 SQL 查询示例:
{
"connectionId": "my-postgres-db",
"query": "SELECT * FROM users WHERE age > $1",
"params": [21]
}
高级用法
以下是不同格式的 MongoDB 查询示例:
{
"connectionId": "my-mongodb-db",
"query": "[{\"$match\": {\"age\": {\"$gt\": 21}}}, {\"$sort\": {\"name\": 1}}]",
"params": ["users"]
}
{
"connectionId": "my-mongodb-db",
"query": "{\"collection\": \"users\", \"pipeline\": [{\"$match\": {\"age\": {\"$gt\": 21}}}, {\"$sort\": {\"name\": 1}}]}"
}
{
"connectionId": "my-mongodb-db",
"query": "db.getCollection('users').find({\"age\": {\"$gt\": 21}})"
}
{
"connectionId": "my-mongodb-db",
"query": "db.users.find({\"age\": {\"$gt\": 21}})"
}
{
"connectionId": "my-mongodb-db",
"query": "{\"find\": \"users\", \"filter\": {\"age\": {\"$gt\": 21}}}"
}
执行更新操作
基础用法
以下是 SQL 更新示例:
{
"connectionId": "my-postgres-db",
"query": "UPDATE users SET age = $1 WHERE id = $2",
"params": [25, 1]
}
高级用法
以下是 MongoDB 更新示例:
{
"connectionId": "my-mongodb-db",
"query": "db.users.update({\"_id\": \"1\"}, {\"$set\": {\"age\": 25}})"
}
其他方法
executeQuery: 执行查询并返回结果。executeUpdate: 执行更新操作并返回执行结果。executeBatch: 批量执行多个 SQL 查询。
资源
JSON - RPC 方法
execute 方法
参数:
{
"method": "execute",
"params": {
"connectionId": string,
"query": string,
"params": array (可选)
}
}
返回结果:
- 如果查询成功,返回
{ status: 'success', result: [...] } - 如果失败,返回
{ status: 'error', error: {...} }
executeUpdate 方法
参数:
{
"method": "executeUpdate",
"params": {
"connectionId": string,
"query": string,
"params": array (可选)
}
}
返回结果:
- 如果更新成功,返回
{ status: 'success', result: [...] } - 如果失败,返回
{ status: 'error', error: {...} }
📚 详细文档
测试
运行测试:
npm test
更多详细信息请参考 tests。
支持项目
如果这个项目对您有帮助,请考虑通过以下方式支持我们:
- 贡献代码:GitHub
- 购买一杯咖啡:Buy Me a Coffee
📄 许可证
本项目采用 MIT License,更多信息请参考 LICENSE。
Scan to contact