在 Knative 中部署 MCP Server

在 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)等费用,由各云产品收取。请参见云产品资源费用