一个统一管理和调用多个MCP (Model Context Protocol) 服务的网关服务器。支持八字算命、天气查询等多种MCP服务,提供统一的REST API接口。
- 🚀 统一网关: 统一管理和路由所有MCP服务请求
- 🔧 热插拔: 支持动态注册和注销MCP服务
- 📊 服务监控: 实时监控服务状态和性能指标
- 🛡️ 安全认证: 支持API密钥认证和访问控制
- 🚦 限流保护: 内置请求限流,防止滥用
- 📝 详细日志: 完整的请求日志和错误追踪
- 🐳 容器化: 支持Docker部署,开箱即用
- 🔄 负载均衡: 支持多种负载均衡策略
- 🏥 健康检查: 自动检测服务健康状态
- 克隆项目
git clone <repository-url>
cd mcp-gateway- 安装依赖
npm install- 配置环境变量
cp .env.example .env
# 编辑 .env 文件,配置必要的环境变量- 启动开发服务器
npm run dev- 验证服务
curl http://localhost:3000/health- 使用 Docker Compose(推荐)
docker-compose up -d- 使用 Docker 命令
# 构建镜像
docker build -t mcp-gateway .
# 运行容器
docker run -d \
--name mcp-gateway \
-p 3000:3000 \
-e API_KEY=your-api-key \
mcp-gateway| 端点 | 方法 | 描述 |
|---|---|---|
/health |
GET | 健康检查 |
/api |
GET | API信息 |
/api/mcp/status |
GET | 获取所有服务状态 |
/api/mcp/servers |
GET | 获取服务列表 |
/api/mcp/servers/:serverName/tools |
GET | 获取服务工具列表 |
POST /api/mcp/:serverName/:toolName
Content-Type: application/json
X-API-Key: your-api-key
X-API-Secret: your-api-secret
{
"birthDate": "1990-01-01T08:00:00Z",
"gender": "male",
"calendarType": "solar"
}curl -X POST http://localhost:3000/api/mcp/bazi/getBaziDetail \
-H "Content-Type: application/json" \
-H "X-API-Key: dev-api-key" \
-d '{
"birthDate": "1990-01-01T08:00:00Z",
"gender": "male",
"calendarType": "solar"
}'curl -X POST http://localhost:3000/api/mcp/bazi/getBaziFortune \
-H "Content-Type: application/json" \
-H "X-API-Key: dev-api-key" \
-d '{
"birthDate": "1990-01-01T08:00:00Z",
"gender": "male",
"calendarType": "solar",
"targetType": "today"
}'curl -X POST http://localhost:3000/api/mcp/bazi/getCompatibility \
-H "Content-Type: application/json" \
-H "X-API-Key: dev-api-key" \
-d '{
"person1": {
"birthDate": "1990-01-01T08:00:00Z",
"gender": "male",
"calendarType": "solar"
},
"person2": {
"birthDate": "1992-05-15T14:30:00Z",
"gender": "female",
"calendarType": "solar"
},
"analysisType": "love"
}'| 变量名 | 描述 | 默认值 | 必填 |
|---|---|---|---|
NODE_ENV |
运行环境 | development |
否 |
PORT |
服务端口 | 3000 |
否 |
HOST |
绑定地址 | 0.0.0.0 |
否 |
API_KEY |
API密钥 | - | 否(开发环境) |
API_SECRET |
API密钥 | - | 否(开发环境) |
RATE_LIMIT_WINDOW_MS |
限流窗口时间 | 900000 |
否 |
RATE_LIMIT_MAX_REQUESTS |
限流请求数 | 100 |
否 |
LOG_LEVEL |
日志级别 | info |
否 |
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ n8n 工作流 │───▶│ MCP Gateway │───▶│ 八字算命服务 │
└─────────────────┘ │ (统一网关) │ └─────────────────┘
└─────────────────┘
│
▼
┌─────────────────┐
│ 其他MCP服务 │
│ (天气/日历...) │
└─────────────────┘
- MCPGateway: 主网关,负责请求路由和中间件处理
- ServerRegistry: 服务注册中心,管理所有MCP服务
- LoadBalancer: 负载均衡器,支持多种策略
- BaziServer: 八字算命服务实现
- 中间件: 认证、日志、限流、错误处理等
-
连接 GitHub 仓库
- 登录 Railway
- 点击 "New Project" → "Deploy from GitHub repo"
- 选择你的仓库
-
配置环境变量
NODE_ENV=production PORT=3000 API_KEY=your-production-api-key API_SECRET=your-production-api-secret -
部署完成
- Railway 会自动构建和部署
- 获得一个类似
https://your-app.railway.app的域名
-
创建 Web Service
- 登录 Render
- 点击 "New" → "Web Service"
- 连接 GitHub 仓库
-
配置构建和启动命令
- Build Command:
npm install && npm run build - Start Command:
npm start
- Build Command:
-
设置环境变量 和 Railway 类似配置必要的环境变量
-
准备服务器
# 安装 Docker 和 Docker Compose curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh -
部署应用
git clone <repository-url> cd mcp-gateway docker-compose up -d
-
配置反向代理(Nginx)
server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
创建 HTTP Request 节点
- Method:
POST - URL:
https://your-domain.com/api/mcp/bazi/getBaziDetail - Headers:
Content-Type:application/jsonX-API-Key:your-api-key
- Method:
-
配置请求体
{ "birthDate": "{{$json.birthDate}}", "gender": "{{$json.gender}}", "calendarType": "solar" } -
处理响应数据
- 响应会包含
data字段,包含完整的八字分析结果
- 响应会包含
curl http://localhost:3000/api/mcp/status# Docker 日志
docker logs mcp-gateway -f
# 开发环境日志
npm run devcurl http://localhost:3000/health- 创建服务类
// src/servers/YourServer.ts
import { IMCPServer } from '@/types';
export class YourServer implements IMCPServer {
name = 'your-service';
version = '1.0.0';
tools = ['tool1', 'tool2'];
async executeTool(toolName: string, params: any): Promise<any> {
// 实现你的工具逻辑
}
getStatus(): ServerStatus {
// 返回服务状态
}
async healthCheck(): Promise<boolean> {
// 健康检查逻辑
}
}- 注册服务
// src/app.ts
gateway.registerServer('your-service', new YourServer());// src/middleware/yourMiddleware.ts
export const yourMiddleware = (req: Request, res: Response, next: NextFunction) => {
// 你的中间件逻辑
next();
};- Fork 本仓库
- 创建特性分支:
git checkout -b feature/amazing-feature - 提交更改:
git commit -m 'Add amazing feature' - 推送分支:
git push origin feature/amazing-feature - 提交 Pull Request
本项目采用 MIT 许可证。
- cantian-ai/bazi-mcp - 八字算命计算逻辑
- n8n - 工作流自动化平台
- Express.js - Web 框架
如有问题或建议,请:
🌟 如果这个项目对你有帮助,请给个 Star!