生产MCP服务器部署指南
多用户访问的HTTP/SSE传输
本指南涵盖了为用户组部署具有HTTP/SSE流传输的企业就绪MCP服务器。
______________________________________________________________________
🎯 概述
您的MCP服务器配置为:
- ✅ HTTP/SSE传输 -HTTP上的流式响应
- ✅ 多用户支持 -并发用户的会话管理
- ✅ 认证 -基于API密钥的安全性
- ✅ CORS支持 -跨源资源共享
- ✅ 生产就绪 -负载平衡、自动扩展、监控
______________________________________________________________________
🔧 快速开始
1.生成API密钥
# Generate secure MCP API key for authentication
python -c "import secrets; print('MCP_API_KEY=' + secrets.token_urlsafe(32))"
# Save output to .env file2.配置环境
# Create .env file
cat > .env > .env所有客户必须包括: Authorization: Bearer YOUR_KEY
2.配置CORS
仅限于特定来源:
# Production setting
export ALLOWED_ORIGINS="https://claude.ai,https://your-app.com"发展(允许所有人):
export ALLOWED_ORIGINS="*"3.使用Nginx的SSL/TLS
# Generate SSL certificate (Let's Encrypt)
certbot certonly --standalone -d your-domain.com
# Copy certificates
cp /etc/letsencrypt/live/your-domain.com/fullchain.pem ./ssl/cert.pem
cp /etc/letsencrypt/live/your-domain.com/privkey.pem ./ssl/key.pem
# Start Nginx proxy
docker-compose up -d nginx4.费率限制
Nginx配置包括:
- 10个请求/秒 按IP
- 爆裂20 请求:
- 最多10个并发连接 按IP
调整 nginx.conf:
limit_req_zone $binary_remote_addr zone=mcp_limit:10m rate=10r/s;______________________________________________________________________
📊 监测和可观察性
健康检查端点
# Simple health check
curl https://your-server.com/health
# Response
{
"status": "healthy",
"service": "nephrology-rag-mcp",
"version": "1.0.0"
}察看连接信息
# Get server status
curl -X POST https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp \
-H "Authorization: Bearer YOUR_KEY" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "get_server_info"
}
}'会话管理
# List active sessions (admin)
curl -X POST https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp \
-H "Authorization: Bearer YOUR_KEY" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "list_sessions"
}
}'普罗米修斯指标
访问指标: http://your-server:9090
需监控的关键指标:
- 请求率
- 响应时间
- 活动会话
- 错误率
- 内存使用
日志
Docker日志:
docker logs -f mcp-serverKubernetes日志:
kubectl logs -f deployment/nephrology-rag-mcp -n mcp-server错误筛选器:
kubectl logs -f deployment/nephrology-rag-mcp -n mcp-server | grep ERROR______________________________________________________________________
🎛️ 配置选项
环境变量
| 变量 | 必填 | 默认 | 描述 |
|---|---|---|---|
MISTRALAI_API_KEY | 是 | - | 用于嵌入的Mistral API密钥 |
MCP_API_KEY | 建议使用 | - | API密钥进行MCP身份验证 |
HOST | 没有 | 0.0.0.0 | 服务器绑定地址 |
PORT | 没有 | 8000 | 服务器端口 |
ALLOWED_ORIGINS | 没有 | * | CORS允许的来源(逗号分隔) |
VECTOR_STORE_PATH | 没有 | vector_store | 通往FAISS指数的路径 |
客户端报头
| 标题 | 必填 | 描述 |
|---|---|---|
Authorization | 是\* | 用于身份验证的承载令牌 |
X-Session-ID | 否 | 用于跟踪的会话标识符 |
Accept | 没有 | text/event-stream 用于SSE流媒体 |
\*如果需要 MCP_API_KEY 已设置
______________________________________________________________________
🧪 测试与验证
1.测试健康终点
curl https://your-server.com/health预期: {"status": "healthy", ...}
2.测试认证
# Without auth (should fail if enabled)
curl -X POST https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp -d '{...}'
# With auth (should succeed)
curl -X POST https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp \
-H "Authorization: Bearer YOUR_KEY" \
-d '{...}'3.测试查询
curl -X POST https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_KEY" \
-H "Accept: text/event-stream" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "query_nephrology_docs",
"arguments": {
"query": "test query",
"k": 2
}
}
}'4.负载测试
# Install Apache Bench
apt-get install apache2-utils
# Test 1000 requests with 10 concurrent
ab -n 1000 -c 10 \
-H "Authorization: Bearer YOUR_KEY" \
-p request.json \
-T "application/json" \
https://nephrology-rag-mcp-tool-785629432566.us-central1.run.app/mcp______________________________________________________________________
🔧 故障排除
问题:“未经授权”错误
原因: API密钥丢失或无效
解决方案:
# Verify API key is set
echo $MCP_API_KEY
# Ensure clients use correct header
Authorization: Bearer YOUR_ACTUAL_KEY问题:CORS错误
原因: 不允许来源
解决方案:
# Update ALLOWED_ORIGINS
export ALLOWED_ORIGINS="https://claude.ai,https://your-domain.com"
# Or allow all (development only)
export ALLOWED_ORIGINS="*"问题:“矢量存储未初始化”
原因: FAISS指数未加载
解决方案:
# Check vector store exists
ls -la vector_store/
# Verify MISTRAL_API_KEY is set
echo $MISTRALAI_API_KEY
# Check server logs
docker logs mcp-server | grep "Vector store"问题:响应缓慢
可能的原因和解决方案:
- 结果太多: 减少
k参数 - 冷启动: 第一个请求可能较慢
- 资源限制: 增加容器内存/CPU
- 网络延迟: 部署在离用户更近的地方
问题:连接超时
解决方案:
# Increase Nginx timeouts
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;______________________________________________________________________
📈 扩展策略
水平缩放
Docker编写:
docker-compose up -d --scale mcp-server=5库贝内特斯:
kubectl scale deployment nephrology-rag-mcp --replicas=5 -n mcp-server垂直缩放
增加资源:
resources:
limits:
memory: "4Gi"
cpu: "2000m"自动缩放
Kubernetes HPA配置为:
- 最小副本数:2
- 最大副本数:10
- 以70%的CPU或80%的内存进行扩展
AWS ECS:
- CPU上的目标跟踪(70%)
- 在2-10个任务之间自动缩放
______________________________________________________________________
🎯 最佳实践
1.安全
- ✅ 在生产环境中始终使用HTTPS
- ✅ 设置强
MCP_API_KEY - ✅ 将CORS限制到特定的源
- ✅ 启用速率限制
- ✅ 使用机密管理(不是生产环境中的环境变量)
2.可靠性
- ✅ 部署至少2个副本
- ✅ 配置健康检查
- ✅ 设置监控和警报
- ✅ 使用负载平衡器
- ✅ 实施断路器
3.性能
- ✅ 缓存频繁查询
- ✅ 将CDN用于静态资产
- ✅ 在多个地区部署
- ✅ 优化矢量存储索引
- ✅ 监控和优化查询模式
4.操作
- ✅ 集中化的日志
- ✅ 自动化部署
- ✅ 基础设施即代码
- ✅ 灾难恢复计划
- ✅ 定期安全更新
______________________________________________________________________
📚 额外资源
- MCP规范: https://spec.modelcontextprotocol.io/
- FastMCP文档: https://github.com/jlowin/fastmcp
- API克劳德: https://docs.anthropic.com/
- SSE协议: https://html.spec.whatwg.org/multipage/server-sent-events.html
______________________________________________________________________
💡 支持
生产支持:
- 检查服务器日志
- 验证配置
- 使用curl/httpie进行测试
- 查看监控仪表板
- 联系您的DevOps团队
服务器现在已准备好进行多用户生产部署! 🚀
