Map the WeatherAPI to an MCP Server

Incompatible with
on-prem
Minimum Version
AI Gateway - 2.0
Previous Versions of this page
TL;DR

AI Gateway provides first-class MCP Server entities in Kong Konnect that expose REST APIs as tools for MCP-compatible clients. Create an AI MCP Server entity configured as a conversion-listener to convert REST endpoints into MCP tools that clients can call directly, without managing API credentials.

This tutorial shows you how to set up an AI MCP Server to expose the WeatherAPI in Kong Konnect using kongctl, and how to proxy your first MCP request.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. Run the AI Gateway quickstart script to automatically provision a control plane and data plane in Kong Konnect, and configure your environment:

    curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN 

This sets up a AI Gateway control plane named ai-quickstart, provisions a local data plane, and prints out the following environment variables export:

export AI_GATEWAY_ID=your-gateway-id
export KONNECT_TOKEN=$KONNECT_TOKEN
export KONNECT_CONTROL_PLANE_NAME=ai-quickstart
export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
export KONNECT_PROXY_URL='http://localhost:8000'

Copy and paste these into your terminal to configure your session.

This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.13.0).

  1. Install kongctl from developer.konghq.com/kongctl.
  2. Verify the installation:

    kongctl version
  1. Go to WeatherAPI.
  2. Navigate to your dashboard and copy your API key.
  3. Export your API key by running the following command in your terminal:
    export WEATHERAPI_API_KEY='your-weatherapi-api-key'

Create an MCP Server entity

Create an MCP Server entity that exposes the WeatherAPI through a single MCP tool called get-current-weather, mapped from the WeatherAPI /v1/current.json endpoint. tools[].query.key injects your WeatherAPI credentials automatically, so clients never handle the API key:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_mcp_servers:
  - ref: weather-mcp
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: weather-mcp
    display_name: "Weather API"
    type: conversion-listener
    enabled: true
    policies: []
    access:
      acl_attribute_type: consumer
      acls:
        allow: []
      default_tool_acls:
        deny: []
    config:
      url: https://api.weatherapi.com/v1/current.json
      route:
        paths:
          - /weather
      logging:
        payloads: false
        audits: true
      server:
        timeout: 60000
    tools:
      - name: get-current-weather
        description: Get current weather for a location
        method: GET
        path: /weather
        query:
          key:
            - !env WEATHERAPI_API_KEY
        parameters:
          - name: q
            in: query
            required: true
            schema:
              type: string
            description: Location query. Accepts US Zipcode, UK Postcode, Canada Postalcode, IP address, latitude/longitude, or city name.
EOF

Validate the MCP Server

AI Gateway implements the MCP Streamable HTTP transport. Before you can call a tool, you need to open a session against the MCP Server’s route.

Open a session

Send an initialize request to the route configured on the MCP Server (/weather), capturing the Mcp-Session-Id response header into an environment variable:

SESSION_ID=$(curl -i -X POST "$KONNECT_PROXY_URL/weather/" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json"\
     -H "Accept: application/json, text/event-stream" \
     --json '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "initialize",
       "params": {
         "protocolVersion": "2025-06-18",
         "capabilities": {},
         "clientInfo": {
           "name": "weather-mcp-test",
           "version": "1.0.0"
         }
       }
     }' | grep -i '^mcp-session-id:' | tr -d '\r' | cut -d' ' -f2
)

Complete the handshake with a notifications/initialized notification, carrying the session ID:

curl -i -X POST "$KONNECT_PROXY_URL/weather/" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json"\
     -H "Accept: application/json, text/event-stream"\
     -H "Mcp-Session-Id: $SESSION_ID" \
     --json '{
       "jsonrpc": "2.0",
       "method": "notifications/initialized"
     }'

A 202 Accepted response confirms the session is ready.

Call the tool

List the available tools to confirm the get-current-weather tool exists, and inspect its inputSchema. Include the Mcp-Session-Id header:

curl -X POST "$KONNECT_PROXY_URL/weather/" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json"\
     -H "Accept: application/json, text/event-stream"\
     -H "Mcp-Session-Id: $SESSION_ID" \
     --json '{
       "jsonrpc": "2.0",
       "id": 2,
       "method": "tools/list"
     }'

For conversion-listener and conversion-only MCP Servers, the generated inputSchema names each converted REST parameter {in}_{name}, not the bare configured name. Since you configured the q parameter as name: q and in: query, AI Gateway exposes to MCP clients as query_q. Call the tool with that argument name:

curl -X POST "$KONNECT_PROXY_URL/weather/" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json"\
     -H "Accept: application/json, text/event-stream"\
     -H "Mcp-Session-Id: $SESSION_ID" \
     --json '{
       "jsonrpc": "2.0",
       "id": 3,
       "method": "tools/call",
       "params": {
         "name": "get-current-weather",
         "arguments": {
           "query_q": "London"
         }
       }
     }'

The response includes the current conditions for London:

event: message
data: {"id":3,"result":{"content":[{"type":"text","text":"{\"location\":{\"name\":\"London\",\"region\":\"City of London, Greater London\",\"country\":\"United Kingdom\",...},\"current\":{...,\"condition\":{\"text\":\"Sunny\",...},\"temp_c\":27.3,\"temp_f\":81.1,...}}"}],"isError":false},"jsonrpc":"2.0"}

Cleanup

To clean up all AI Gateway resources created in this guide, run:

curl -Ls https://get.konghq.com/ai | bash -s -- -d

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!