Azure容器应用程序上的Azure ML MCP服务器
部署a 模型上下文协议(MCP) 服务器到Azure容器应用程序,将Azure ML端点作为AI代理的工具公开。
🎯 这有什么作用
此项目创建了一个MCP服务器,该服务器:
- 继续运行 Azure容器应用 (无服务器,可扩展到零)
- 暴露 Azure ML管理的在线端点 作为MCP工具
- 使用 Azure AI Foundry代理, Copilot工作室,以及任何兼容MCP的客户端
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ AI Foundry │────▶│ MCP Server │────▶│ Azure ML │
│ Agent │ MCP │ (Container App)│HTTP │ Endpoint │
└─────────────────┘ └─────────────────┘ └─────────────────┘📋 先决条件
- Azure订阅 具有创建资源的权限
- Azure ML托管在线端点 已部署并正在运行
- Azure命令行界面 已安装(安装指南)
- 码头工人 安装用于当地开发和建筑图像
- Python 3.11+ 用于本地测试
🚀 快速开始
1.克隆和配置
# Clone the repository
git clone
cd
# Copy environment template
cp .env.sample .env # Linux/macOS
copy .env.sample .env # Windows CMD
Copy-Item .env.sample .env # Windows PowerShell
# Edit .env with your Azure ML endpoint details
# Get these from Azure ML Studio > Endpoints > Your endpoint > Consume tab2.本地测试
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Run the server
python server.py卷曲测试:
curl -X POST http://localhost:8080/mcp/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'3.部署到Azure
Bash(Linux/macOS/WSL)
# Login to Azure
az login
# Create resource group
az group create --name rg-mcp-server --location eastus
# Update parameters with your Azure ML credentials
# Edit infra/main.parameters.bicepparam
# Deploy infrastructure
az deployment group create \
--resource-group rg-mcp-server \
--template-file infra/main.bicep \
--parameters infra/main.parameters.bicepparam
# Get the ACR name from the output
ACR_NAME=$(az deployment group show -g rg-mcp-server -n main --query properties.outputs.acrName.value -o tsv)
# Build and push Docker image
az acr login --name $ACR_NAME
docker build -t $ACR_NAME.azurecr.io/mcp-server:latest .
docker push $ACR_NAME.azurecr.io/mcp-server:latest
# Restart the container app to pull the new image
az containerapp update \
--name mcp-server \
--resource-group rg-mcp-serverPowerShell(Windows)
# Login to Azure
az login
# Create resource group
az group create --name rg-mcp-server --location eastus
# Update parameters with your Azure ML credentials
# Edit infra/main.parameters.bicepparam
# Deploy infrastructure
az deployment group create `
--resource-group rg-mcp-server `
--template-file infra/main.bicep `
--parameters infra/main.parameters.bicepparam
# Get the ACR name from the output
$ACR_NAME = az deployment group show -g rg-mcp-server -n main --query properties.outputs.acrName.value -o tsv
# Build and push Docker image
az acr login --name $ACR_NAME
docker build -t "$ACR_NAME.azurecr.io/mcp-server:latest" .
docker push "$ACR_NAME.azurecr.io/mcp-server:latest"
# Restart the container app to pull the new image
az containerapp update `
--name mcp-server `
--resource-group rg-mcp-server4.获取您的MCP端点
az deployment group show \
--resource-group rg-mcp-server \
--name main \
--query properties.outputs.mcpEndpoint.value -o tsv您的MCP终点将是: https://.azurecontainerapps.io/mcp/mcp
🤖 连接到Azure AI Foundry
- 首选 Azure AI 铸造厂
- 导航到您的项目> 代理 > 工具
- 点击 +新工具 > MCP工具
- 配置:
- 名字: AzureMLScoring (或您喜欢的名字) - 服务器URL: https://.azurecontainerapps.io/mcp/mcp - 认证类型:无(应用程序内部使用托管机密)
- 在代理中保存并测试该工具
📁 项目结构
├── server.py # MCP server with Azure ML tool
├── requirements.txt # Python dependencies
├── Dockerfile # Container image definition
├── .env.sample # Environment variables template
├── .gitignore # Git ignore rules
├── .dockerignore # Docker build exclusions
└── infra/
├── main.bicep # Azure infrastructure definition
└── main.parameters.bicepparam # Deployment parameters🔧 自定义工具
⚠️ 重要:示例代码是为一个任意预测模型配置的。你 必须 修改 server.py 以匹配您自己的Azure ML模型的预期输入模式(列名、数据类型)和输出格式。编辑 server.py 要修改MCP工具:
- 更改功能参数 以匹配模型的输入
- 更新DataFrame列 以匹配模型的预期架构
- 修改文档字符串 准确描述你的工具(这就是AI代理看到的)
@mcp.tool()
def invoke_azure_ml_endpoint(
# Change these parameters to match your model's inputs
your_param_1: float,
your_param_2: str,
) -> float:
"""
Update this docstring to describe your tool - AI agents use this to understand
when and how to call your tool.
"""
# Modify the DataFrame columns to match your model's expected schema
df = pd.DataFrame(
[[float(your_param_1), your_param_2]],
columns=["YourColumn1", "YourColumn2"] # Change to your model's column names
)
# The payload structure may need adjustment - test your model in the
# Azure ML Studio 'Test' tab to see the expected format
data = {"input_data": df.to_dict(orient='split')}
# ... rest of the function🐛 故障排除
421错误请求错误
如果在日志中看到此错误,请确保已禁用DNS重新绑定保护:
from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
mcp = FastMCP(
"your-server-name",
stateless_http=True,
transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False)
)请参阅:
容器未启动
检查日志:
az containerapp logs show \
--name mcp-server \
--resource-group rg-mcp-server \
--followAzure ML终结点错误
验证您的端点是否可访问:
curl -X POST $AML_SCORE_URL \
-H "Authorization: Bearer $AML_API_KEY" \
-H "Content-Type: application/json" \
-d '{"input_data": {"columns": ["col1"], "data": [[1]]}}'📚 资源
⚠️ 生产注意事项
此示例旨在用于学习和原型制作。在部署到生产环境之前,请考虑以下事项:
🔐 身份验证和授权
- 无端点身份验证:MCP端点可公开访问。对于生产,请考虑:
- Azure容器应用程序身份验证 (轻松认证) - 应用程序代码中的API密钥验证 - Azure API管理 作为网关 - OAuth 2.0/Microsoft Entra ID集成
🌐 网络安全
- 公众入口:容器应用程序暴露在互联网上。对于企业场景,请考虑:
- Direct3D集成 用于专用网络 - 专用端点 限制访问 - 控制流量的网络安全组(NSG) - 通过私有端点连接到Azure ML端点
🔑 秘密管理
- 内联秘密:秘密直接存储在容器应用程序配置中。生产:
- 使用 Azure密钥库 具有管理身份 - 定期轮换机密 - 避免在参数文件中存储机密(使用Azure DevOps/GitHub机密进行CI/CD)
📊 监测和可观察性
- 仅用于基本日志记录:考虑添加:
- 应用洞察 用于分布式跟踪 - 模型推理延迟和错误率的自定义指标 - 故障和性能下降警报
🏗️ 基础设施
- 单一区域:此示例部署到一个区域。为了实现高可用性:
- 使用流量管理器部署到多个区域 - 考虑 Azure前门 用于全局负载平衡 - 实施健康探测和故障转移策略
🛡️ 其他企业要求
- 跨域资源共享:目前允许所有来源(
*).仅限于生产中的特定域。 - 速率限制:未配置速率限制。考虑API管理或应用程序级节流。
- 合规:确保部署符合组织的合规要求(SOC 2、HIPAA等)
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
