AI代理部署工具

使用Azure Kubernetes服务、Kaito框架和API管理构建强大的AI代理,支持企业级LLM部署和管理。

  • by ctava-msft
  • Bicep
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 ctava-msft/apim-mcp-aks-kaito

使用说明

使用Azure Kubernetes Service、Kaito和APIM的AI代理

使用(某工具/平台)构建强大的AI代理 Azure Foundry 模型 在Kubernetes上部署,使用 **模型上下文协议(MCP)**这个解决方案采用 Azure Kubernetes 服务 (AKS), 凯托 为了简化大型语言模型(LLM)的部署,并且 Azure API 管理(APIM) 作为智能AI网关。

🎯 本解决方案提供的内容

  • 🤖 人工智能代理后端部署AI代理可以交互的MCP服务器
  • 📦 Azure Foundry 模型在Kubernetes上运行来自Azure Foundry的企业级大型语言模型(LLMs)
  • ⚡ Kaito 框架在支持GPU的AKS上实现模型的自动化部署和扩展
  • 🔧 自定义工具可扩展的MCP工具,用于AI代理(片段存储、自定义集成)
  • 🛡️ 企业安全带有OAuth认证和授权的APIM网关
  • 📊 准备就绪,可投入生产内置监控、日志记录和自动扩展功能

🏗️ 建筑学

AI Agent (Claude Desktop, etc.)
    ↓
Azure API Management (OAuth + Gateway)
    ↓
AKS Cluster
    ├── MCP Server (FastAPI)
    │   └── Tools: hello_mcp, save_snippet, get_snippet
    └── Kaito Workspace
        └── Azure Foundry Model (Phi-3, etc.)
            └── GPU Node Pool (NC-series VMs)

关键组件

  1. AKS 集群带有GPU启用节点池的Kubernetes集群,用于模型推理
  2. “Kaito Operator”可以翻译为“假面骑士(Kaito)操作员”或根据具体语境简化为“假面骑士(Kaito)”。这里,“Kaito”通常指的是日本特摄剧《假面骑士》系列中的主角或特定角色名称,而“Operator”则可能指的是操作该角色或相关设备的人。不过,具体翻译还需结合上下文来确定最准确的表述简化了在Kubernetes上部署和管理大型语言模型(LLMs)的过程
  3. MCP服务器实现模型上下文协议的FastAPI应用程序
  4. Azure Foundry 模型企业级大型语言模型(如Phi-3、Llama等)已容器化,准备部署
  5. **APIM(Application Programming Interface Management,应用程序编程接口管理)**处理身份验证、速率限制和API网关功能
  6. Azure 存储代理数据(代码片段、文档等)的持久化存储

可用的MCP工具

工具 描述 参数
hello_mcp 简单测试工具 无
save_snippet 将文本/代码片段保存到 Azure 存储 snippetname, snippet
get_snippet 检索已保存的代码片段 snippetname

🚀 快速入门

先决条件

第一步:部署基础设施

# Login to Azure
azd auth login
#az login --tenant 

# Deploy all Azure resources (AKS, ACR, APIM, Storage, etc.)
azd up

此命令部署:

  • ✅ 具有GPU节点池的AKS集群
  • ✅ Azure 容器注册表
  • Azure API 管理
  • ✅ Azure 存储帐户
  • ✅ 管理身份和基于角色的访问控制(RBAC)
  • ✅ Application Insights 监控

⏱️ 部署时间大约15-20分钟

步骤2:连接到AKS

# Get cluster credentials
export AKS_CLUSTER_NAME=$(azd env get-values | grep AKS_CLUSTER_NAME | cut -d'=' -f2 | tr -d '"')
export RESOURCE_GROUP=$(azd env get-values | grep AZURE_RESOURCE_GROUP_NAME | cut -d'=' -f2 | tr -d '"')

az aks get-credentials --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER_NAME

步骤3:安装Kaito Operator

# Install Kaito on AKS
./scripts/install-kaito.sh

# Verify installation
kubectl get pods -n kaito-system

步骤4:构建并部署MCP服务器

# Set environment variables
export CONTAINER_REGISTRY=$(azd env get-values | grep CONTAINER_REGISTRY | cut -d'=' -f2 | tr -d '"')
export AZURE_STORAGE_ACCOUNT_URL=$(azd env get-values | grep AZURE_STORAGE_ACCOUNT_URL | cut -d'=' -f2 | tr -d '"')
export AZURE_CLIENT_ID=$(azd env get-values | grep MCP_SERVER_IDENTITY_CLIENT_ID | cut -d'=' -f2 | tr -d '"')

# Build and push Docker image
./scripts/build-and-push.sh

# Deploy to Kubernetes
export IMAGE_TAG=latest
envsubst < k8s/mcp-server-deployment.yaml | kubectl apply -f -

步骤5:部署Azure Foundry模型

# Deploy Phi-3 model using Kaito
kubectl apply -f k8s/kaito-foundry-workspace.yaml

# Wait for model to be ready (5-10 minutes)
kubectl wait --for=condition=Ready workspace/azure-foundry-model --timeout=15m

# Check status
kubectl get workspace

步骤6:测试您的设置

# Run tests
python tests/test_mcp_fixed_session.py

# Or use MCP Inspector
npx @modelcontextprotocol/inspector

🔧 配置

使用不同的Azure Foundry模型

编辑 k8s/kaito-foundry-workspace.yaml:

spec:
  inference:
    preset:
      name: phi-3-mini-128k-instruct  # Change model here
  resource:
    instanceType: Standard_NC6s_v3  # Adjust VM size for larger models

支持的模型:

  • phi-3-mini-4k-instruct (建议用于测试)
  • phi-3-mini-128k-instruct
  • phi-3-medium
  • 其他Azure Foundry模型

定制的MCP工具

添加新工具到 src/mcp_server.py:

TOOLS.append(
    MCPTool(
        name="my_tool",
        description="What this tool does",
        inputSchema={
            "type": "object",
            "properties": {
                "param1": {"type": "string", "description": "..."}
            },
            "required": ["param1"]
        }
    )
)

# Implement in execute_tool()
async def execute_tool(tool_name: str, arguments: Dict[str, Any]):
    if tool_name == "my_tool":
        # Your implementation
        return MCPToolResult(content=[{"type": "text", "text": "Result"}])

🧪 测试

自动化测试

# Run MCP server tests
python tests/test_mcp_fixed_session.py

# Test Kaito deployment
kubectl get workspace
kubectl describe workspace azure-foundry-model

使用MCP Inspector进行手动测试

npx @modelcontextprotocol/inspector

在MCP检查器中:

  1. 设置传输为 上海证券交易所
  2. 输入: https://<your-apim>.azure-api.net/mcp/sse
  3. 添加授权头
  4. 点击 连接 → 列出工具

📊 监控

# MCP server logs
kubectl logs -n mcp-server -l app=mcp-server --follow

# Kaito operator logs
kubectl logs -n kaito-system -l app=kaito --follow

# Model logs
kubectl logs -l kaito.sh/workspace=azure-foundry-model

🛠️ 故障排除

AKS问题

# Check nodes
kubectl get nodes

# Check GPU availability
kubectl describe node -l workload=gpu

凯托问题

# Check Kaito status
kubectl get pods -n kaito-system

# Check workspace
kubectl describe workspace azure-foundry-model

# Scale GPU nodes manually if needed
az aks nodepool scale --resource-group $RESOURCE_GROUP \
  --cluster-name $AKS_CLUSTER_NAME --name gpupool --node-count 1

MCP服务器问题

# Check pods
kubectl get pods -n mcp-server

# View logs
kubectl logs -n mcp-server -l app=mcp-server

# Restart deployment
kubectl rollout restart deployment/mcp-server -n mcp-server

💰 成本优化

自动缩放GPU至零

GPU节点在空闲时自动缩放到0(默认配置):

# In kaito-foundry-workspace.yaml
spec:
  resource:
    count: 0  # Scales to zero when not in use

使用按需实例(Spot Instances)

编辑 infra/core/aks/aks-cluster.bicep 使用按需虚拟机(成本降低70%-90%):

scaleSetPriority: 'Spot'
scaleSetEvictionPolicy: 'Delete'
spotMaxPrice: -1  # Pay up to regular price

🔐 安全

  • 认证通过 Azure Entra ID 使用 OAuth 2.0
  • 工作负载身份Pod级别的托管身份
  • 网络可选的VNet隔离(vnetEnabled=true)
  • **RBAC(Role-Based Access Control)**Kubernetes和Azure RBAC已启用

📚 了解更多

🤝 贡献(或“参与贡献”)

欢迎投稿!详情请见 `CONTRIBUTING.md` 翻译成中文是:“贡献指南.md” 或 “贡献规范.md”。这个文件通常用于说明如何向项目贡献代码或内容,包括贡献的流程、规范、要求等 作为指导方针。

📄 许可证

这个项目遵循MIT许可证授权 - 详见 LICENSE.md(许可证文件).