反向代理MCP
一个容器化的Nginx反向代理管理系统,带有REST API、模型上下文协议(MCP)服务器和Flutter WebUI。为您的反向代理基础架构提供集中控制、监视和配置。
特性
- 🔄 动态配置 -无需重新启动容器即可热重新加载代理规则
- 🌐 REST API -用于完整代理管理的分层(v1)和矩阵(v2)API端点
- 🤖 MCP集成 -通过模型上下文协议控制代理,实现AI/LLM兼容性
- 🎨 Flutter WebUI -用于代理管理的响应式web界面
- 🔐 基于角色的访问控制 -具有细粒度权限的管理员和用户角色
- 📊 监控 -实时指标、历史分析、每后端性能跟踪
- 📝 审计日志 -完整的变更历史和用户活动跟踪
- 🔒 SSL管理 -上传和管理SSL证书,支持通配符、默认证书和过期监控
- 🐳 Docker就绪 -使用docker编写编排的多容器设置
- 📚 文档 -全面阅读文档
建筑
┌─────────────────────────────────────────────────────────────┐
│ Nginx Proxy │
│ (Dynamically configured from DB) │
└─────────────────────────────────────────────────────────────┘
↑ ↑ ↑
┌────────┐ ┌─────────┐ ┌──────────┐
│ API │ │ MCP │ │ WebUI │
│ (FastAPI) │ (FastMCP) │ (Flutter) │
└────────┘ └─────────┘ └──────────┘
↓ ↓ ↓
└─────────────────────────────────────────────────────────┘
↓
┌──────────────────────┐
│ SQLite Database │
│ │
│ - Users │
│ - Backends │
│ - Proxy Rules │
│ - SSL Certificates │
│ - Audit Logs │
│ - Metrics │
└──────────────────────┘快速开始
先决条件
- Docker和Docker Compose
- Python 3.11+(用于本地开发)
uv包管理器(建议快速安装)
使用Docker Compose
# Clone the repository
git clone https://github.com/yourusername/reverse-proxy-mcp.git
cd reverse-proxy-mcp
# Create environment configuration
cp .env.example .env
# Start all services
docker compose up -d
# Access the WebUI
# http://localhost (default credentials: admin / password)
# API documentation
# http://localhost:5100/docs本地开发
# Install uv (fast Python package manager)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies with uv
uv sync --all-groups
# Set up pre-commit hooks
pre-commit install
# Run the API server
uv run python -m reverse_proxy_mcp
# Run tests
uv run pytest
# Run linting and type checking
uv run black src && uv run ruff check src && uv run mypy src项目结构
reverse-proxy-mcp/
├── src/reverse_proxy_mcp/
│ ├── api/ # FastAPI application
│ │ ├── v1/ # REST API v1 endpoints
│ │ ├── v2/ # REST API v2 endpoints
│ │ ├── dependencies.py # FastAPI dependencies (auth, etc)
│ │ └── main.py # API entry point
│ ├── mcp/ # MCP server
│ │ ├── server.py # MCP server implementation
│ │ └── tools.py # MCP tool definitions
│ ├── core/
│ │ ├── config.py # Configuration management
│ │ ├── database.py # Database setup and sessions
│ │ ├── security.py # Authentication and authorization
│ │ └── nginx.py # Nginx config generation and reload
│ ├── models/
│ │ ├── database.py # SQLAlchemy ORM models
│ │ └── schemas.py # Pydantic request/response schemas
│ ├── services/
│ │ ├── backend.py # Backend server management
│ │ ├── proxy_rule.py # Proxy rule management
│ │ ├── certificate.py # SSL certificate management
│ │ ├── user.py # User management
│ │ ├── audit.py # Audit logging
│ │ └── metrics.py # Metrics collection
│ └── migrations/ # Alembic database migrations
├── webui/ # Flutter WebUI project
├── docker-compose.yml # Multi-container orchestration
├── docs/ # Read the Docs documentation
├── tests/ # Test suite
├── Dockerfile.api # API service Dockerfile
├── Dockerfile.mcp # MCP server Dockerfile
└── nginx/
├── Dockerfile # Nginx service Dockerfile
└── nginx.conf.template # Nginx configuration template配置
环境变量
创建 .env 项目根目录中的文件:
# Database
DATABASE_URL=sqlite:///./data/reverse_proxy_mcp.db
# JWT Configuration
SECRET_KEY=your-secret-key-change-this
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=1440
# API Configuration
API_HOST=0.0.0.0
API_PORT=8000
# MCP Configuration
MCP_HOST=0.0.0.0
MCP_PORT=5000
# Nginx Configuration
NGINX_CONFIG_PATH=/etc/nginx/sites-enabled/proxy.conf
NGINX_SOCKET_PATH=/var/run/nginx.sockSSL证书管理
反向代理MCP提供全面的SSL证书管理:
特性
- 命名证书 -为证书对分配友好名称(例如“Wildcard Kempville”、“API证书”)
- 通配符支持 -跨多个子域名使用通配符证书(\*.example.com)
- 默认证书 -为没有显式分配的域设置默认证书
- 证书分配 -为单个代理规则分配特定证书
- 自动分辨率 -证书按以下顺序解析:
1. 代理规则上的显式证书分配 1. 精确的域匹配 1. 通配符域匹配 1. 默认证书回退
- 验证 -上传时验证证书/密钥对
- 到期监测 -跟踪证书过期并获取警报
证书上传
使用API:
# Upload a wildcard certificate as default
curl -X POST http://localhost:5100/api/v1/certificates \
-H "Authorization: Bearer $TOKEN" \
-F "name=Wildcard Kempville" \
-F "domain=*.kempville.com" \
-F "is_default=true" \
-F "cert_file=@/path/to/wildcard.crt" \
-F "key_file=@/path/to/wildcard.key"
# Upload a domain-specific certificate
curl -X POST http://localhost:5100/api/v1/certificates \
-H "Authorization: Bearer $TOKEN" \
-F "name=API Certificate" \
-F "domain=api.example.com" \
-F "is_default=false" \
-F "cert_file=@/path/to/api.crt" \
-F "key_file=@/path/to/api.key"为代理规则分配证书
# Create proxy rule with explicit certificate
curl -X POST http://localhost:5100/api/v1/proxy-rules \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"frontend_domain": "api.kempville.com",
"backend_id": 1,
"certificate_id": 2
}'
# Create proxy rule using default certificate (omit certificate_id)
curl -X POST http://localhost:5100/api/v1/proxy-rules \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"frontend_domain": "app.kempville.com",
"backend_id": 1
}'列出可用证书(用于下拉菜单)
curl -X GET http://localhost:5100/api/v1/certificates/dropdown \
-H "Authorization: Bearer $TOKEN"MCP服务器(模型上下文协议)
反向代理MCP包括一个FastMCP服务器,用于使用拟人模型上下文协议规范进行AI/LLM集成。
特性
- 22工具 -通过MCP工具完成代理管理
- 9资源 -通过URI资源对配置数据进行只读访问
- 5提示 -常见任务的指导工作流程
- HTTP传输 -端口5000上的标准流式HTTP传输
运行MCP服务器
# Start MCP server locally
uv run python -m reverse_proxy_mcp.mcp
# MCP endpoint: http://localhost:5000/mcp连接AI工具
添加到您的AI工具配置中(例如,Claude Desktop、Warp Agent模式):
{
"mcpServers": {
"reverse-proxy": {
"url": "http://localhost:5000/mcp",
"transport": "http"
}
}
}可用工具
后端管理(5个工具)
list_backends-列出所有后端服务器create_backend-创建新的后端服务器update_backend-更新后端配置delete_backend-删除后端服务器get_backend-按ID获取后端详细信息
代理规则管理(6个工具)
list_proxy_rules-列出所有代理规则create_proxy_rule-创建新的代理规则(支持certificate_id)update_proxy_rule-更新代理规则(支持certificate_id)delete_proxy_rule-删除代理规则get_proxy_rule-按ID获取规则详细信息reload_nginx-重新加载Nginx配置
证书管理(5个工具)
list_certificates-列出所有SSL证书create_certificate-上传带有名称和is_default的证书get_certificate-获取证书详细信息set_default_certificate-将证书设置为默认值delete_certificate-删除证书
用户和配置(4个工具)
list_users-列出所有用户(仅限管理员)create_user-创建新用户(仅限管理员)get_config-获取系统配置update_config-更新配置(仅限管理员)
监控(2个工具)
get_health-获取系统运行状况get_metrics-获取性能指标
可用资源
资源提供对配置数据的只读访问:
proxy://backends-列出所有后端proxy://backends/{backend_id}-单一后端详细信息proxy://rules-列出所有代理规则proxy://rules/{rule_id}-单一规则详细信息proxy://certificates-列出所有证书proxy://certificates/{cert_id}-单一证书详细信息proxy://config-当前系统配置proxy://metrics-汇总指标摘要proxy://audit-logs-最近的审核日志条目
可用提示
提示提供指导工作流程:
setup_new_domain(domain, backend_host, backend_port)-完整的域设置指南troubleshoot_proxy(domain)-代理问题的诊断步骤configure_ssl(domain, is_wildcard)-SSL证书设置指南rotate_certificate(cert_id)-证书轮换工作流程create_user_account(username, role)-用户创建指南configure_wildcard_domain(base_domain, subdomains)-多个子域名的通配符设置
示例:使用MCP设置新域
# Via AI assistant with MCP access
# Use the setup_new_domain prompt
Prompt: setup_new_domain(
domain="api.example.com",
backend_host="10.0.0.5",
backend_port=8080
)
# Follow the generated steps:
# 1. create_backend(name="api-example-com-backend", host="10.0.0.5", port=8080)
# 2. create_proxy_rule(domain="api.example.com", backend_id=1)
# 3. reload_nginx()
# 4. get_health()MCP资源示例
# Fetch all backends via resource
Resource: proxy://backends
# Returns JSON array of all backend servers
# Fetch specific backend
Resource: proxy://backends/1
# Returns single backend details
# Fetch all proxy rules
Resource: proxy://rules
# Returns rules with backend and certificate relationshipsAPI 文档
- REST API v1 (分层):
/api/v1/docs - REST API v2 (矩阵):
/api/v2/docs - MCP端点:
http://localhost:5000/mcp
开发命令
使用 uv (快速Python包管理器):
# Format code
uv run black src
# Lint code
uv run ruff check src
# Type check
uv run mypy src
# Run tests
uv run pytest
# Run specific test file
uv run pytest tests/test_auth.py
# Run with coverage
uv run pytest --cov=src --cov-report=html
# Run only unit tests
uv run pytest -m unit
# Build Docker image
docker build -t reverse-proxy-mcp-api -f Dockerfile.api .
# Run Docker Compose
docker-compose up测试
运行完整的测试套件:
pytest运行特定测试类别:
# Unit tests only
pytest -m unit
# Integration tests only
pytest -m integration
# End-to-end tests
pytest -m e2e
# Docker container tests
pytest -m docker文档
完整文档可在 docs/ 目录并部署到读取文档。
关键文档文件:
docs/installation.rst-安装说明docs/architecture.rst-系统架构docs/api-reference.rst-完整的API端点文档docs/user-guide.rst-WebUI使用指南docs/administration.rst-用户管理和权限
许可证
BSD 3条款许可证-有关详细信息,请参阅许可证文件。
作者
布莱恩·肯普(bryan@kempville.com)
贡献
欢迎投稿!拜托:
- 克隆该仓库
- 创建要素分支
- 通过测试进行更改
- 运行代码质量检查
- 提交拉取请求
支持
- 文档: https://reverse-proxy-mcp.readthedocs.io
- 问题:GitHub问题
- 电子邮件: bryan@kempville.com
