模型上下文协议服务器部署方案

提供基于Azure Web Apps和Azure API Management的MCP服务器生产级部署架构,支持基础设施即代码、API管理和安全监控。

  • by Vinny614
  • Shell
  • 本地部署
  • 更新于
在 GitHub 查看 Vinny614/MCP-build

使用说明

Azure上带有API管理的MCP服务器

用于将模型上下文协议(MCP)服务器部署为Azure Web应用程序的生产就绪架构,通过Azure API管理(APIM)进行管理。

🏗️ 架构概述

此项目提供了一个完整的基础设施即代码解决方案,用于在Azure上部署具有企业级功能的MCP服务器:

  • Azure Web应用程序:使用Python运行时托管MCP服务器应用程序
  • Azure API管理:用于管理、安全和监控的集中式API网关
  • 应用洞察:全面的测井和遥测
  • Azure密钥库:安全的秘密管理
  • 地形:可重复部署的基础设施即代码

看 建筑.md 获取详细的架构文档。

📁 项目结构

.
├── ARCHITECTURE.md              # Detailed architecture documentation
├── README.md                    # This file
├── docker-compose.yml           # Local development with Docker
├── .github/
│   └── workflows/
│       └── deploy.yml          # CI/CD pipeline
├── infrastructure/
│   ├── terraform/              # Terraform infrastructure code
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   ├── outputs.tf
│   │   ├── resources.tf
│   │   ├── terraform.tfvars.example
│   │   └── README.md
│   └── apim-policies/          # APIM policy configurations
│       ├── rate-limiting-policy.xml
│       ├── jwt-validation-policy.xml
│       ├── ip-whitelist-policy.xml
│       ├── caching-policy.xml
│       ├── transformation-policy.xml
│       ├── production-policy.xml
│       └── README.md
├── scripts/
│   ├── deploy.sh               # Automated deployment script
│   ├── test-api.sh             # API testing script
│   └── cleanup.sh              # Resource cleanup script
└── src/
    ├── mcp_server.py           # MCP server implementation
    ├── app.py                  # Flask web wrapper
    ├── requirements.txt        # Python dependencies
    └── Dockerfile              # Container image definition

🚀 快速开始

先决条件

  1. Azure命令行界面: 安装Azure命令行界面
  2. 地形: 安装Terraform
  3. Azure订阅:活动Azure订阅
  4. Python 3.11+:促进地方发展

1.克隆和设置

# Clone the repository
git clone <your-repo-url>
cd MCP-build

# Login to Azure
az login
az account set --subscription <subscription-id>

2.配置部署

# Create Terraform variables file
cd infrastructure/terraform
cp terraform.tfvars.example terraform.tfvars

# Edit terraform.tfvars with your settings
nano terraform.tfvars

3.部署

选项A:自动部署(推荐)

# Run the deployment script from project root
./scripts/deploy.sh

选项B:手动部署

# Deploy infrastructure
cd infrastructure/terraform
terraform init
terraform plan
terraform apply

# Build and deploy application
cd ../../src
zip -r deploy.zip . -x "*.pyc" -x "__pycache__/*"

# Get deployment details
RG_NAME=$(terraform output -raw resource_group_name)
APP_NAME=$(terraform output -raw web_app_name)

# Deploy to Web App
az webapp deployment source config-zip \
  --resource-group $RG_NAME \
  --name $APP_NAME \
  --src deploy.zip

4.测试部署

# Run automated tests
./scripts/test-api.sh

# Or test manually
curl https://<your-apim>.azure-api.net/mcp/api/v1/tools \
  -H "Ocp-Apim-Subscription-Key: <your-key>"

🔧 本地开发

使用Docker

# Start the server
docker-compose up

# The server will be available at http://localhost:8000
curl http://localhost:8000/health

使用Python虚拟环境

# Create virtual environment
cd src
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run the server
python app.py

# Test locally
curl http://localhost:8000/health

📊 监控

查看应用程序日志

# Stream logs
az webapp log tail \
  --resource-group <resource-group> \
  --name <webapp-name>

# Download logs
az webapp log download \
  --resource-group <resource-group> \
  --name <webapp-name>

应用洞察

通过Azure门户访问应用程序洞察或使用KQL进行查询:

// Request performance
requests
| where timestamp > ago(1h)
| summarize count(), avg(duration) by bin(timestamp, 5m)
| render timechart

// Error analysis
exceptions
| where timestamp > ago(24h)
| summarize count() by type, outerMessage

🔒 安全

身份验证选项

  1. API密钥(默认):APIM中的订阅密钥
  2. Azure AD/Outh2.0:JWT令牌验证
  3. 客户端证书:双向TLS身份验证

应用策略

# Apply a policy to your API
az apim api policy create \
  --resource-group <rg> \
  --service-name <apim-name> \
  --api-id <api-id> \
  --xml-policy @infrastructure/apim-policies/production-policy.xml

看 基础设施/apim策略/README.md 详细的政策文件。

💰 成本估算

开发环境

  • APIM开发人员:约50美元/月
  • 应用服务B1:约13美元/月
  • 应用洞察:约5美元/月
  • 总计:约68美元/月

生产环境

  • APIM标准:约750美元/月
  • 应用服务S1(x2):约140美元/月
  • 应用洞察:约20美元/月
  • 钥匙库:约$5/月
  • 总计:约915美元/月

🔄 CI/CD管道

GitHub Actions工作流包含在 .github/workflows/deploy.yml.

设置GitHub机密

# Create Azure service principal
az ad sp create-for-rbac \
  --name "mcp-server-github" \
  --role contributor \
  --scopes /subscriptions/<subscription-id> \
  --sdk-auth

# Add to GitHub Secrets:
# - AZURE_CREDENTIALS (output from above command)
# - AZURE_RESOURCE_GROUP
# - AZURE_WEBAPP_NAME

📚 可用的MCP工具

示例服务器包括以下工具:

  1. 回声:回复回复消息
  2. 健康检查:返回服务器运行状况
  3. get_environment:列出环境变量
  4. 计算:执行算术运算

示例:调用工具

curl -X POST https://<apim-url>/mcp/api/v1/tools/call \
  -H "Ocp-Apim-Subscription-Key: <key>" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "calculate",
    "arguments": {
      "operation": "add",
      "a": 10,
      "b": 5
    }
  }'

🧹 清理

要销毁所有资源,请执行以下操作:

# Using cleanup script
./scripts/cleanup.sh

# Or manually with Terraform
cd infrastructure/terraform
terraform destroy

📖 文档

🤝 贡献

  1. 克隆该仓库
  2. 创建要素分支
  3. 进行更改
  4. 提交拉取请求

📝 许可证

该项目根据MIT许可证获得许可。

🆘 支持

对于问题和疑问:

🔗 参考文献