你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:在 Azure 容器应用中将 MCP 服务器与 shell 会话配合使用(预览版)

本教程演示如何使用模型上下文协议 (MCP) 服务器在 Azure 容器应用动态会话中部署 shell 环境并与之交互。

在本教程中,你将:

  • 创建启用了 MCP 服务器的 shell 会话池
  • 设置MCP服务器端点和凭据
  • 使用 JSON-RPC 远程执行 shell 命令

先决条件

在开始本教程之前,需要以下资源。

Requirement Description
Azure 帐户 需要一个具有活动订阅的 Azure 帐户。 如果没有帐户,可以免费创建一个帐户
Azure CLI 安装 Azure CLI

设置

首先,使用最新的更新准备 Azure CLI 并登录到 Azure。

  1. 将 Azure CLI 更新到最新版本。

    az upgrade
    
  2. 注册 Microsoft.App 资源提供程序。

    az provider register --namespace Microsoft.App
    
  3. 安装最新版本的 Azure 容器应用 CLI 扩展。

    az extension add --name containerapp --allow-preview true --upgrade
    
  4. 登录到 Azure。

    az login
    
  5. 查询 Azure 订阅 ID 并将值设置为变量。

    SUBSCRIPTION_ID=$(az account show --query id --output tsv)
    
  6. 设置此过程中使用的变量。

    运行以下命令之前,请确保将 <> 括起来的占位符替换为自己的值。

    RESOURCE_GROUP=<RESOURCE_GROUP_NAME>
    SESSION_POOL_NAME=<SESSION_POOL_NAME>
    LOCATION=<LOCATION>
    

    可以使用这些变量在以下步骤中创建资源。

  7. 设置用于创建资源组的所需订阅。

    az account set -s $SUBSCRIPTION_ID
    
  8. 创建资源组。

    az group create --name $RESOURCE_GROUP --location $LOCATION
    

使用 MCP 服务器创建 shell 会话池

使用 ARM 模板创建启用了 MCP 服务器的 shell 会话池。

  1. 创建名为 deploy.json: 的部署模板文件:

    {
        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "name": { "type": "String" },
            "location": { "type": "String" }
        },
        "resources": [
            {
                "type": "Microsoft.App/sessionPools",
                "apiVersion": "2025-02-02-preview",
                "name": "[parameters('name')]",
                "location": "[parameters('location')]",
                "properties": {
                    "poolManagementType": "Dynamic",
                    "containerType": "Shell", # Set the "containerType" property to "Shell"
                    "scaleConfiguration": {
                        "maxConcurrentSessions": 5
                    },
                    "sessionNetworkConfiguration": {
                        "status": "EgressEnabled"
                    },
                    "dynamicPoolConfiguration": {
                        "lifecycleConfiguration": {
                            "lifecycleType": "Timed",
                            "coolDownPeriodInSeconds": 300
                        }
                    },
                    "mcpServerSettings": { 
                        "isMCPServerEnabled": true # Add the "mcpServerSettings" section to enable the MCP server
                    }
                }
            }
        ]
    }
    
  2. 部署 ARM 模板。

    az deployment group create \
      --resource-group $RESOURCE_GROUP \
      --template-file deploy.json \
      --parameters name=$SESSION_POOL_NAME location=$LOCATION
    

获取 MCP 服务器终结点

从部署的会话池中检索 MCP 服务器终结点。

MCP_ENDPOINT=$(az rest --method GET --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/sessionPools/$SESSION_POOL_NAME?api-version=2025-02-02-preview" --query "properties.mcpServerSettings.mcpServerEndpoint" -o tsv)

获取 MCP 服务器凭据

请求 MCP 服务器的 API 凭据。

API_KEY=$(az rest --method POST --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/sessionPools/$SESSION_POOL_NAME/fetchMCPServerCredentials?api-version=2025-02-02-preview" --query "apiKey" -o tsv)

初始化 MCP 服务器

使用 JSON-RPC 初始化 MCP 服务器连接。

curl -sS -X POST "$MCP_ENDPOINT" \
  -H "Content-Type: application/json" \
  -H "x-ms-apikey: $API_KEY" \
  -d '{ "jsonrpc": "2.0", "id": "1", "method": "initialize" }'

应会看到包含 protocolVersionserverInfo的响应。

启动 shell 环境

在会话池中创建新的 shell 环境。

ENVIRONMENT_RESPONSE=$(curl -sS -X POST "$MCP_ENDPOINT" \
  -H "Content-Type: application/json" \
  -H "x-ms-apikey: $API_KEY" \
  -d '{ "jsonrpc": "2.0", "id": "2", "method": "tools/call", "params": { "name": "launchShell", "arguments": {} } }')

echo $ENVIRONMENT_RESPONSE

从响应中提取环境 ID,以便在后续命令中使用。

执行 shell 命令

在远程 shell 环境中运行命令。 将 <ENVIRONMENT_ID> 替换为上一步返回的 ID。

curl -sS -X POST "$MCP_ENDPOINT" \
  -H "Content-Type: application/json" \
  -H "x-ms-apikey: $API_KEY" \
  -d '{
    "jsonrpc": "2.0",
    "id": "3",
    "method": "tools/call",
    "params": {
      "name": "runShellCommandInRemoteEnvironment",
      "arguments": {
        "environmentId": "<ENVIRONMENT_ID>",
        "shellCommand": "echo Hello from Azure Container Apps Shell Session!"
      }
    }
  }'

你应该在 stdout 字段中看到包含命令结果的输出。

清理资源

本教程中创建的资源对 Azure 帐单有影响。 如果不打算长期使用这些服务,请运行以下命令以删除本教程中创建的所有内容。

az group delete --resource-group $RESOURCE_GROUP

后续步骤