全部產品
Search
文件中心

Container Service for Kubernetes:在 Knative 中部署 MCP Server

更新時間:Mar 24, 2026

在 Knative 中託管 MCP Server後,可利用其Serverless架構優勢,實現AI服務的按需擴縮容、事件驅動等。

工作原理

在 Knative 中,AI 應用通過 MCP 協議與外部工具進行互動的流程如下。

image
  1. 請求發起:使用者向 AI Agent 發出指令。AI Agent內建的 MCP 用戶端會產生標準化的工具調用請求,並通過 SSE 或 HTTP 協議發送。

    Knative不支援 stdio 傳輸協議的 MCP 伺服器。
  2. 服務處理與執行:Knative Service 接收該 HTTP 要求,將其路由到一個 MCP 伺服器執行個體,並根據負載進行自動擴縮容。

    可參見MCP 伺服器 SDKFastMCP自行開發 MCP 伺服器,也可在 MCP 伺服器 GitHub 程式碼程式庫中尋找現有實現。
  3. 結果返回:任務結果通過 MCP 伺服器回傳給 AI Agent,最終發送至使用者。

準備工作

已在叢集中部署Knative,請參見部署與管理Knative組件

步驟一:部署 MCP Server

本小節部署了一個用於杭州行程規劃的MCP 服務,將行程規劃、機票查詢、酒店預訂和天氣預報功能封裝為可供 AI 智能體調用的標準化工具。

  1. 建立 mcp-server.yaml

    樣本定義了一個 Knative Service,拉取樣本 MCP Server鏡像。
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
      namespace: default
    spec:
      template:
        spec:
          containers:
            - env:
                - name: TARGET
                  value: Knative
              # 將鏡像中 {region} 替換為實際地區,如 cn-hangzhou
              image: >-
                registry-{region}-vpc.ack.aliyuncs.com/acs/knative-samples-mcp-trip:v1.0-6b9fc59
              name: user-container
  2. 建立 Knative Service。

    kubectl apply -f mcp-server.yaml

步驟二:訪問服務

服務部署後,向其發送 MCP 請求,驗證服務是否正常工作。

  1. 服務管理頁面,擷取服務的訪問網關預設網域名稱

    image

  2. 訪問helloworld-go服務。

    替換helloworld-go.default.example.com為實際網域名稱,替換<GATEWAY_IP>為實際網關地址。

    該命令會向服務發送一個 tools/list 請求,用於查詢其支援的所有工具列表。
    curl -H "Host: helloworld-go.default.example.com" http://<GATEWAY_IP>/mcp -H "Content-Type: application/json" \
      -d '{
        "jsonrpc": "2.0",
        "id": "1",
        "method": "tools/list",
        "params": {}
      }' | jq .

    預期將輸出格式化的 JSON 響應,其中包含 MCP Server提供的工具列表。

    展開查看預期輸出

    {
      "jsonrpc": "2.0",
      "id": "1",
      "result": {
        "tools": [
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "預訂杭州西湖附近酒店",
            "inputSchema": {
              "type": "object",
              "properties": {
                "check_in": {
                  "description": "入住日期 (YYYY-MM-DD)",
                  "type": "string"
                },
                "guests": {
                  "description": "住客人數",
                  "type": "number"
                },
                "nights": {
                  "description": "入住晚數",
                  "type": "number"
                }
              },
              "required": [
                "check_in",
                "guests",
                "nights"
              ]
            },
            "name": "book_hotel"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "擷取杭州未來多日天氣預報,用於行程準備",
            "inputSchema": {
              "type": "object",
              "properties": {
                "date_start": {
                  "description": "起始日期 (YYYY-MM-DD)",
                  "type": "string"
                },
                "days": {
                  "description": "預報天數",
                  "type": "number"
                }
              },
              "required": [
                "date_start",
                "days"
              ]
            },
            "name": "get_weather_forecast"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "產生杭州多日深度旅行行程,包含每日景點、美食與文化體驗",
            "inputSchema": {
              "type": "object",
              "properties": {
                "days": {
                  "description": "旅行天數(1-7天)",
                  "maximum": 7,
                  "minimum": 1,
                  "type": "number"
                }
              },
              "required": [
                "days"
              ]
            },
            "name": "plan_hangzhou_trip"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "查詢前往杭州的航班資訊",
            "inputSchema": {
              "type": "object",
              "properties": {
                "date": {
                  "description": "出發日期 (YYYY-MM-DD)",
                  "type": "string"
                },
                "origin": {
                  "description": "出發城市(如 北京、上海)",
                  "type": "string"
                }
              },
              "required": [
                "origin",
                "date"
              ]
            },
            "name": "search_flights"
          }
        ]
      }
    }

計費說明

Knative組件本身不產生額外費用。但在使用過程中產生的計算資源(如ECS)、網路資源(如ALB)等費用,由各雲產品收取。請參見雲產品資源費用