MapAI PostgreSQL MCP服务器-Docker设置
一种模型上下文协议(MCP)服务器,通过HTTP端点提供对PostgreSQL数据库的只读访问。
快速开始
1.配置环境
复制示例环境文件并配置数据库:
cp .env.example .env编辑 .env 并设置您的 DATABASE_URL:
DATABASE_URL=postgresql://username:password@host:port/database_name2.使用Docker Compose运行
# Build and start the server
docker-compose up --build
# Run in background
docker-compose up -d --build3.测试服务器
# Health check
curl http://localhost:8833/health
# Server info
curl http://localhost:8833/
# Test MCP query (example)
curl -X POST http://localhost:8833/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "query",
"arguments": {
"sql": "SELECT version();"
}
}
}'配置
环境变量
DATABASE_URL:PostgreSQL连接字符串(必需)PORT:服务器端口(默认值:8833)NODE_ENV:环境模式(默认:生产)
数据库URL示例
# Local PostgreSQL
DATABASE_URL=postgresql://user:password@localhost:5432/database
# Docker host access (from container to host)
DATABASE_URL=postgresql://user:password@host.docker.internal:5432/database
# AWS RDS
DATABASE_URL=postgresql://user:password@your-rds.region.rds.amazonaws.com:5432/database
# Google Cloud SQL
DATABASE_URL=postgresql://user:password@your-cloud-sql-ip:5432/databaseDocker命令
# Build the image
docker build -t mapai-mcp-server .
# Run container with environment variables
docker run -p 8833:8833 \
-e DATABASE_URL="postgresql://user:pass@host:5432/db" \
mapai-mcp-server
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Rebuild and restart
docker-compose up --build --force-recreateAPI终点
GET /-服务器信息GET /health-健康检查POST /mcp-MCP协议端点
安全功能
- 只读数据库事务
- SQL注入保护(仅限SELECT)
- 容器中的非root用户
- 健康检查包括
- 为web客户端启用CORS
故障排除
连接问题
- 数据库连接失败:检查您的
DATABASE_URL格式 - 端口已在使用中:更改
PORT在……里面.env或使用不同的端口映射 - 容器无法访问数据库:使用
host.docker.internal用于本地数据库
调试模式
运行调试日志:
docker-compose up --build
# Check logs for connection details发展
# Install dependencies locally
npm install
# Run in development mode
npm run dev