一个暴露关系数据库(PostgreSQL/MySQL)给AI代理并支持自然语言查询的MCP(模型上下文协议)服务器。将自然语言问题转换为SQL查询,并获取结构化结果。
| 端点 | 方法 | 描述 |
|---|---|---|
/health | GET | 健康检查和服务状态 |
/mcp/list_tables | GET | 列出所有可用表及其列数 |
/mcp/describe/{table_name} | GET | 获取特定表的详细模式 |
/mcp/query | POST | 执行自然语言查询 |
/mcp/tables/{table_name}/sample | GET | 获取表中的样本数据 |
克隆并启动服务:
git clone https://github.com/Souhar-dya/mcp-db-server.git
cd mcp-db-server
docker-compose up --build
测试端点:
# 健康检查
curl http://localhost:8000/health
# 列出表
curl http://localhost:8000/mcp/list_tables
# 描述一个表
curl http://localhost:8000/mcp/describe/customers
# 自然语言查询
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "显示按总订单量排名前五的客户"}'
前提条件:
安装依赖项:
pip install -r requirements.txt
设置环境变量:
export DATABASE_URL="postgresql+asyncpg://user:password@localhost:5432/dbname"
# 或对于MySQL:
# export DATABASE_URL="mysql+pymysql://user:password@localhost:3306/dbname"
运行服务器:
python -m app.server
项目包括一个具有真实电子商务数据的示例数据库:
服务器可以理解各种类型的自然语言查询:
# 获取所有客户
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "显示所有客户"}'
# 按状态计数订单
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type:_application/json" \
-d '{"nl_query": "按状态计数订单"}'
# 按订单金额排名前五的客户
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "按总订单金额排名前五的客户"}'
# 最近的订单
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "显示上周的最近订单"}'
| 变量 | 描述 | 默认值 |
|---|---|---|
DATABASE_URL | 完整的数据库连接URL | postgresql+asyncpg://postgres:postgres@localhost:5432/postgres |
DB_HOST | 数据库主机 | localhost |
DB_PORT | 数据库端口 | 5432 |
DB_USER | 数据库用户名 | postgres |
DB_PASSWORD | 数据库密码 | postgres |
DB_NAME | 数据库名称 | postgres |
HOST | 服务器主机 | 0.0.0.0 |
PORT | 服务器端口 | 8000 |
# PostgreSQL
DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/mydb
# MySQL
DATABASE_URL=mysql+pymysql://user:pass@localhost:3306/mydb
# PostgreSQL with SSL
DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/mydb?sslmode=require
mcp-db-server/
├── app/
│ ├── __init__.py # 包初始化
│ ├── server.py # FastAPI应用程序和端点
│ ├── db.py # 数据库连接和操作
│ └── nl_to_sql.py # 自然语言到SQL转换
├── .github/workflows/
│ └── docker-publish.yml # CI/CD流水线
├── docker-compose.yml # Docker Compose配置
├── Dockerfile # 容器定义
├── init_db.sql # 示例数据库模式和数据
├── requirements.txt # Python依赖项
└── README.md # 本文件
此服务器旨在与兼容MCP的AI代理无缝工作:
/docs处提供OpenAPI/Swagger文档# 拉取最新镜像
docker pull souhardyak/mcp-db-server:latest
# 使用您的数据库运行
docker run -d \
-p 8000:8000 \
-e DATABASE_URL="your_database_url_here" \
souhardyak/mcp-db-server:latest
apiVersion: apps/v1
kind: Deployment
metadata:
name: mcp-db-server
spec:
replicas: 3
selector:
matchLabels:
app: mcp-db-server
template:
metadata:
labels:
app: mcp-db-server
spec:
containers:
- name: mcp-db-server
image: souhardyak/mcp-db-server:latest
ports:
- containerPort: 8000
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
---
apiVersion: v1
kind: Service
metadata:
name: mcp-db-server-service
spec:
selector:
app: mcp-db-server
ports:
- port: 80
targetPort: 8000
type: LoadBalancer
# 启动测试数据库
docker-compose up postgres -d
# 等待数据库准备好
sleep 10
# 运行测试
python -m pytest tests/ -v
# 测试健康端点
curl http://localhost:8000/health
# 测试表列出
curl http://localhost:8000/mcp/list_tables
# 测试自然语言查询
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "显示来自加利福尼亚的所有客户"}'
git checkout -b feature/amazing-feature)git commit -m '添加一些惊人的功能')git push origin feature/amazing-feature)本项目根据Apache许可证2.0发布 - 查看LICENSE文件以获取详细信息。
str不能用于'await'表达式的错误⭐ 如果这个项目帮助了您,请考虑给它一个星!