Quickly build a Q&A agent with kagent

Updated at:

Use kagent to deploy a simple agent. The agent uses the qwen3-coder-plus model provided by Alibaba Cloud Model Studio and the fetch MCP (Model Context Protocol) server, which retrieves web page content from a URL. The agent answers questions by summarizing that content in real time.

Prerequisites

  1. Create a kagent namespace in the ACK cluster.

  2. Use the ACK App Marketplace or the ACK cluster Applications > Helm page to install the kagent-crds and kagent applications in the kagent namespace.

  3. Activate Alibaba Cloud Model Studio and Create an API key.

Step 1: Create a ModelConfig

  1. Connect to an ACK cluster using kubectl.

  2. Export the Model Studio API key as an environment variable, and then store it in a Secret for the ModelConfig to use.

    export PROVIDER_API_KEY=${Your_Model_Studio_API_Key}
    kubectl create secret generic bailian-apikey -n kagent --from-literal credential="$PROVIDER_API_KEY"
  3. Create a ModelConfig.

    kubectl -n kagent apply -f - <<EOF
    apiVersion: kagent.dev/v1alpha2
    kind: ModelConfig
    metadata:
      name: bailian-provider-config
    spec:
      model: qwen3-coder-plus
      apiKeySecret: bailian-apikey
      apiKeySecretKey: credential
      openAI:
        baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
      provider: OpenAI
    EOF

Step 2: Deploy the fetch MCP server

  1. Deploy the fetch MCP server in the ACK cluster.

    kubectl -n kagent apply -f - <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: streamablehttp-fetch-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: streamablehttp-fetch
      template:
        metadata:
          labels:
            app: streamablehttp-fetch
        spec:
          containers:
          - name: streamablehttp-fetch-container
            image: registry-cn-hangzhou.ack.aliyuncs.com/dev/streamablehttp-fetch:latest
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: streamablehttp-fetch-service
    spec:
      selector:
        app: streamablehttp-fetch
      ports:
        - protocol: TCP
          port: 3000
          targetPort: 3000
      type: ClusterIP
    EOF

Step 3: Register the MCP server

  1. Create a RemoteMCPServer resource.

    kubectl -n kagent apply -f - <<EOF
    apiVersion: kagent.dev/v1alpha2
    kind: RemoteMCPServer
    metadata:
      name: streamablehttp-fetch
    spec:
      description: A Model Context Protocol server that provides web content fetching capabilities. This server enables LLMs to retrieve and process content from web pages, converting HTML to markdown for easier consumption.
      protocol: STREAMABLE_HTTP
      sseReadTimeout: 5m0s
      terminateOnClose: true
      timeout: 30s
      url: http://streamablehttp-fetch-service:3000/
    EOF
  2. Check the creation status of the RemoteMCPServer.

    kubectl get RemoteMCPServer -n kagent

    Expected output:

    NAME                   PROTOCOL          URL                                         ACCEPTED
    streamablehttp-fetch   STREAMABLE_HTTP   http://streamablehttp-fetch-service:3000/   True

Step 4: Create the agent

  1. Create the Q&A agent.

    kubectl -n kagent apply -f - <<EOF
    apiVersion: kagent.dev/v1alpha2
    kind: Agent
    metadata:
      name: gateway-api-professor
      namespace: kagent
    spec:
      declarative:
        modelConfig: bailian-provider-config
        stream: true
        systemMessage: |-
          You are a friendly and helpful agent that uses the streamablehttp-fetch tool to fetch GatewayAPI information from the following addresses to answer user questions about GatewayAPI.
          # Link
          - intro: https://gateway-api.sigs.k8s.io/
          - api-overview: https://gateway-api.sigs.k8s.io/concepts/api-overview/
          - use-case: https://gateway-api.sigs.k8s.io/concepts/use-cases/
          - servicemesh: https://gateway-api.sigs.k8s.io/mesh/
          - implementations: https://gateway-api.sigs.k8s.io/implementations/
          - 1.4 version support overview: https://gateway-api.sigs.k8s.io/implementations/v1.4/
          - Full spec, large page: https://gateway-api.sigs.k8s.io/reference/spec/
          # Instructions
          - If the user's question is unclear, ask for clarification before running any tool
          - Be friendly and warm in your responses to users
          - If you do not know how to answer a question, do not make up an answer
            Answer "Sorry, I do not know how to answer this question" and ask the user to clarify the question further
          - If the user's question asks for a summary or an overview, make sure you read the full text before answering.
          # Response format
          - Always respond in Markdown format.
          - Your response must include a summary of the actions you performed and an explanation of the results.
        tools:
        - type: McpServer
          mcpServer:
            apiGroup: kagent.dev
            kind: RemoteMCPServer
            name: streamablehttp-fetch
            toolNames:
            - fetch
      description: This agent primarily answers questions about the GatewayAPI Spec.
      type: Declarative
    EOF
  2. Check the creation status of the agent.

    kubectl get pod -n kagent

    Expected output:

    NAME                                               READY   STATUS    RESTARTS   AGE
    gateway-api-professor-7cb6496b9d-l7nlj             1/1     Running   0          3m1s
    kagent-controller-794fc765df-hqswt                 1/1     Running   0          45m
    kagent-ui-569cb875c6-h55mg                         1/1     Running   0          45m
    streamablehttp-fetch-deployment-75d5cd86cf-h9rvp   1/1     Running   0          4m3s

Step 5: Access the agent

By default, kagent provides a web UI that lets you interact with the agent directly.

  1. Use kubectl port-forward to expose the kagent-ui Service on your local machine.

    kubectl port-forward -n kagent service/kagent-ui 8082:8080
  2. In a browser, access the agent at localhost:8082.

    1. Q&A example 1: What APIs are available in Gateway API?

      After receiving the question, the AI agent made two fetch tool calls to access the official Gateway API documentation. The target URL of the first call was https://gateway-api.sigs.k8s.io/concepts/api-overview/, and both calls show the Completed status.

      The AI agent (kagent/gateway-api-professor) answered: The main Gateway API resource types include GatewayClass (defines a group of gateways that share a common configuration and behavior), Gateway (describes how traffic is routed to Services within the cluster), and the Route resource (defines the protocol-specific routing rules from a Gateway to Kubernetes Services). The Route resource includes HTTPRoute, TLSRoute, TCPRoute, UDPRoute, and GRPCRoute. These resources work together to form the load balancing configuration, and the extension points include BackendRefs, HTTPRouteFilter, and custom Routes.

    2. Q&A example 2: Which implementation currently provides the most comprehensive support for Gateway API features?

      After receiving the question, the AI assistant made three fetch tool calls with the target URL https://gateway-api.sigs.k8s.io/implementations/. All three calls show the Completed status, which indicates that the content of the official Gateway API implementations list page was fetched.

      The AI assistant answered: Gateway API supports five Route types: HTTPRoute (HTTP/HTTPS), TLSRoute (SNI-based TLS), TCPRoute (TCP), UDPRoute (UDP), and GRPCRoute (gRPC). Based on the core conformance tests and the extension feature tests, the implementations with the most comprehensive feature support are Envoy Gateway (good support for HTTPRoute, GRPCRoute, and TLSRoute, and many extension features), Istio (which performs well under both the Gateway and Mesh profiles), and Traefik (good support for multiple Route types). The summary notes that Envoy Gateway and Istio are generally considered the most feature-rich, and the final choice depends on your specific requirements and environment.