Deploy an MCP Server in Knative

更新时间:
复制 MD 格式

Hosting an MCP Server in Knative allows you to use its serverless architecture for on-demand autoscaling and event-driven processing for AI services.

How it works

This workflow shows how an AI application in Knative uses the MCP protocol to interact with external tools.

image
  1. Request initiation: A user issues a command to an AI agent. The agent's built-in MCP client generates a standard tool-calling request and sends it over SSE or HTTP.

    Knative does not support MCP Servers that use the stdio transport protocol.
  2. Service handling and execution: The Knative Service receives the HTTP request, routes it to an available MCP Server instance, and automatically scales based on the load.

    To build your own MCP Server, see the MCP Server SDK or FastMCP. You can also find existing implementations in the MCP Server GitHub repository.
  3. Result response: The MCP Server returns the result to the AI agent, which then sends the final response to the user.

Prerequisites

You have deployed Knative in the cluster. For more information, see Deploy and manage Knative components.

Step 1: Deploy the MCP Server

In this section, you will deploy a sample MCP service for trip planning in China (Hangzhou). The service packages trip planning, flight search, hotel booking, and weather forecasting functions into standardized tools that an AI agent can call.

  1. Create a file named mcp-server.yaml.

    The following YAML defines a Knative Service that pulls the sample MCP Server image.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
      namespace: default
    spec:
      template:
        spec:
          containers:
            - env:
                - name: TARGET
                  value: Knative
              # Replace {region} in the image path with your actual region ID, such as cn-hangzhou.
              image: >-
                registry-{region}-vpc.ack.aliyuncs.com/acs/knative-samples-mcp-trip:v1.0-6b9fc59
              name: user-container
  2. Create the Knative Service.

    kubectl apply -f mcp-server.yaml

Step 2: Access the service

After you deploy the service, send an MCP request to verify that it works as expected.

  1. On the Services page, obtain the Gateway and Default Domain of the service.

    The Gateway is an ALB address, such as alb-xxx.aliyuncsslb.com. The Default Domain for the helloworld-go service is helloworld-go.default.example.com.

  2. Access the helloworld-go service.

    Replace helloworld-go.default.example.com with your actual domain name and <GATEWAY_IP> with your actual gateway IP address.

    This command sends a tools/list request to the service to list all supported tools.
    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 .

    The command returns a formatted JSON response containing the list of tools from the MCP Server.

    Expected output

    {
      "jsonrpc": "2.0",
      "id": "1",
      "result": {
        "tools": [
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "Book a hotel near West Lake in China (Hangzhou).",
            "inputSchema": {
              "type": "object",
              "properties": {
                "check_in": {
                  "description": "Check-in date (YYYY-MM-DD)",
                  "type": "string"
                },
                "guests": {
                  "description": "Number of guests",
                  "type": "number"
                },
                "nights": {
                  "description": "Number of nights",
                  "type": "number"
                }
              },
              "required": [
                "check_in",
                "guests",
                "nights"
              ]
            },
            "name": "book_hotel"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "Get the weather forecast for the next few days in China (Hangzhou) for trip preparation.",
            "inputSchema": {
              "type": "object",
              "properties": {
                "date_start": {
                  "description": "Start date (YYYY-MM-DD)",
                  "type": "string"
                },
                "days": {
                  "description": "Number of forecast days",
                  "type": "number"
                }
              },
              "required": [
                "date_start",
                "days"
              ]
            },
            "name": "get_weather_forecast"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "Generate a multi-day, in-depth travel itinerary for China (Hangzhou), including daily attractions, cuisine, and cultural experiences.",
            "inputSchema": {
              "type": "object",
              "properties": {
                "days": {
                  "description": "Number of travel days (1-7)",
                  "maximum": 7,
                  "minimum": 1,
                  "type": "number"
                }
              },
              "required": [
                "days"
              ]
            },
            "name": "plan_hangzhou_trip"
          },
          {
            "annotations": {
              "readOnlyHint": false,
              "destructiveHint": true,
              "idempotentHint": false,
              "openWorldHint": true
            },
            "description": "Search for flight information to China (Hangzhou).",
            "inputSchema": {
              "type": "object",
              "properties": {
                "date": {
                  "description": "Departure date (YYYY-MM-DD)",
                  "type": "string"
                },
                "origin": {
                  "description": "Departure city (for example, Beijing, Shanghai)",
                  "type": "string"
                }
              },
              "required": [
                "origin",
                "date"
              ]
            },
            "name": "search_flights"
          }
        ]
      }
    }

Billing

Knative itself does not incur additional charges. However, you are billed for the cloud services that you use, such as computing and network resources. For more information, see Cloud service costs and .