MCP生产服务器
用于与Claude和其他AI模型集成的生产级模型上下文协议(MCP)服务器实现。
特性
- 生产准备就绪:通过适当的错误处理、超时和弹性模式为规模而构建
- FastAPI框架:高性能异步web框架
- 数据库集成:带有SQLAlchemy ORM的PostgreSQL
- 缓存层:Redis用于高性能缓存
- 可观测性普罗米修斯指标、结构化日志记录、分布式跟踪
- 安全:身份验证、授权、速率限制、输入验证
- 韧性:断路器,指数回退重试
- 测试:全面的单元、集成和负载测试
- 部署:Docker、Kubernetes、蓝绿色部署
快速开始
先决条件
- Python 3.11+
- PostgreSQL 15+
- Redis 7+
- Docker和Docker Compose(用于容器化开发)
地方发展
- 克隆存储库:
git clone https://github.com/afrankenstine/mcp-production-server.git
cd mcp-production-server- 创建虚拟环境:
python3.11 -m venv venv
source venv/bin/activate # On Windows: venv\\Scripts\\activate- 安装依赖项:
pip install -r requirements.txt- 复制环境变量:
cp .env.example .env
# Edit .env with your configuration- 使用Docker Compose启动服务:
docker-compose up -d- 运行服务器:
python main.py服务器将在以下时间可用 http://localhost:8000
项目结构
mcp-production-server/
├── src/
│ ├── api/ # FastAPI server
│ ├── mcp/ # MCP server implementation
│ ├── config/ # Configuration management
│ ├── database/ # Database models and connections
│ ├── errors/ # Error handling
│ ├── cache/ # Redis caching
│ ├── security/ # Auth, validation, rate limiting
│ ├── resilience/ # Circuit breakers, retries
│ ├── metrics.py # Prometheus metrics
│ ├── logging_config.py # Structured logging
│ └── context.py # Request context
├── tests/
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── load/ # Load tests
├── deploy/
│ ├── kubernetes/ # Kubernetes manifests
│ ├── docker/ # Docker configurations
│ └── terraform/ # Infrastructure as code
├── scripts/ # Operational scripts
├── docs/ # Documentation
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── main.py # Entry point
API终点
POST /mcp-MCP消息端点(JSON-RPC 2.0)GET /health-健康检查GET /metrics-普罗米修斯指标
配置
所有配置都是通过环境变量完成的。看 .env.example 查看可用选项。
关键环境变量:
DATABASE_URL-PostgreSQL连接字符串REDIS_URL-Redis连接字符串CLAUDE_API_KEY-API密钥LOG_LEVEL-日志记录级别(调试、信息、警告、错误)ENVIRONMENT-环境(开发、分期、生产)
测试
运行测试:
# Unit tests
pytest tests/unit/
# Integration tests
pytest tests/integration/
# Load tests
locust -f tests/load/locustfile.py --host=http://localhost:8000部署
码头工人
构建并运行:
docker build -t mcp-server .
docker run -p 8000:8000 mcp-serverKubernetes
部署到Kubernetes:
kubectl apply -f deploy/kubernetes/mcp-server.yaml蓝绿部署
./scripts/deploy.sh监控
- 指标:普罗米修斯指标
/metrics - 日志:结构化JSON日志到stdout
- 仪表盘:Grafana仪表板位于
http://localhost:3000
文档
许可证
Apache许可证2.0-请参阅 许可证 文件以获取详细信息。
贡献
欢迎投稿!请先阅读我们的投稿指南。
支持
有关问题和疑问,请打开GitHub问题。
