WeatherAPI를 MCP 서버로 매핑하기
WeatherAPI를 MCP 서버로 매핑하기 (Map the WeatherAPI to an MCP Server)
이 튜토리얼에서는 Kong Konnect AI Gateway에서 REST API를 MCP 도구로 노출하는 방법을 배워요. AI MCP Server 엔티티를 conversion-listener로 구성해서 WeatherAPI의 /v1/current.json 엔드포인트를 get-current-weather라는 단일 MCP 도구로 변환해요. kongctl로 MCP Server를 만들고 자격 증명을 자동으로 주입한 뒤, 첫 MCP 요청을 프록시해서 결과를 확인하는 과정까지 단계별로 안내해요
출처: 문서
본문
TL;DR
AI Gateway에서 REST API를 MCP 도구로 노출하려면 어떻게 해야 하나요?
AI Gateway는 Kong Konnect에서 퍼스트클래스 MCP Server 엔티티를 제공하며, 이를 통해 REST API를 MCP 호환 클라이언트가 사용할 도구로 노출할 수 있어요.
conversion-listener로 구성된 AI MCP Server 엔티티를 만들면 REST 엔드포인트가 클라이언트가 직접 호출할 수 있는 MCP 도구로 변환돼요. 클라이언트는 API 자격 증명을 관리할 필요가 없어요.
이 튜토리얼에서는 kongctl을 사용해 Kong Konnect에서 WeatherAPI를 노출하는 AI MCP Server를 설정하는 방법과, 첫 MCP 요청을 프록시하는 방법을 알아봐요.
전제 조건
AI Gateway 실행 중
이 튜토리얼은 Konnect 대상 튜토리얼이므로 Konnect 개인 액세스 토큰이 필요해요.
-
Konnect PAT 페이지를 열고 Generate Token을 선택해 새 개인 액세스 토큰을 만드세요.
-
토큰을 환경 변수로 내보내세요:
export KONNECT_TOKEN='YOUR_KONNECT_PAT' -
AI Gateway 퀵스타트 스크립트를 실행해 Kong Konnect에 컨트롤 플레인과 데이터 플레인을 자동으로 프로비저닝하고 환경을 구성하세요:
curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN
이 명령은 ai-quickstart라는 이름의 AI Gateway 컨트롤 플레인을 설정하고, 로컬 데이터 플레인을 프로비저닝하며, 다음 환경 변수 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'
터미널에 복사해 붙여넣어 세션을 구성하세요.
kongctl v1.20.2+
이 튜토리얼에서는 kongctl을 사용해 Konnect 리소스를 프로그래밍 방식으로 관리해요. kongctl은 최신 버전(1.20.2)으로 최신 상태를 유지하는 것을 추천해요.
-
developer.konghq.com/kongctl에서 kongctl을 설치하세요.
-
설치를 확인하세요:
kongctl version
WeatherAPI 계정
- WeatherAPI에 접속하세요.
- 내 대시보드로 이동해 API 키를 복사하세요.
- 터미널에서 다음 명령을 실행해 API 키를 내보내세요:
export WEATHERAPI_API_KEY='your-weatherapi-api-key'
MCP Server 엔티티 만들기
WeatherAPI /v1/current.json 엔드포인트에서 매핑된 get-current-weather라는 단일 MCP 도구를 통해 WeatherAPI를 노출하는 MCP Server 엔티티를 만드세요. tools[].query.key가 WeatherAPI 자격 증명을 자동으로 주입하기 때문에 클라이언트는 API 키를 직접 다루지 않아도 돼요:
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
{: data-test-step="block" }
MCP Server 검증하기
AI Gateway는 MCP Streamable HTTP transport를 구현해요. 도구를 호출하기 전에 MCP Server의 라우트를 대상으로 세션을 열어야 해요.
세션 열기
MCP Server에 구성된 라우트(/weather)로 initialize 요청을 보내고, Mcp-Session-Id 응답 헤더를 환경 변수에 담으세요:
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
)
세션 ID를 담은 notifications/initialized 알림을 보내 핸드셰이크를 완료하세요:
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"
}'
202 Accepted 응답이 돌아오면 세션이 준비된 거예요.
도구 호출하기
사용 가능한 도구를 나열해 get-current-weather 도구가 있는지 확인하고, inputSchema를 살펴보세요. Mcp-Session-Id 헤더를 포함하세요:
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"
}'
conversion-listener 및 conversion-only MCP Server의 경우, 생성된 inputSchema는 변환된 각 REST 파라미터를 설정된 이름 그대로가 아니라 {in}_{name} 형태로 이름 붙여요. 앞서 설정에서 q 파라미터를 name: q, in: query로 구성했으므로, AI Gateway는 MCP 클라이언트에 query_q로 노출해요. 이 인자 이름으로 도구를 호출하세요:
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"
}
}
}'
응답에는 런던의 현재 날씨 상태가 포함돼요:
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"}
{:.no-copy-code.wrap}
정리
AI Gateway 리소스 정리하기
이 가이드에서 생성한 모든 AI Gateway 리소스를 정리하려면 다음 명령을 실행하세요:
curl -Ls https://get.konghq.com/ai | bash -s -- -d
관련 리소스
더 알아보기 (Learn more)
- REST 엔드포인트를 MCP 도구로 변환하는 AI MCP Server 엔티티의 설정과 동작 설명 — AI MCP Server
- API를 MCP 도구로 매핑하는 개념과 패턴을 자세히 다루는 문서 — MAP API to MCP tools
- AI Gateway의 MCP 트래픽 게이트웨이 기능 전반을 소개 — MCP Traffic Gateway
- Konnect 리소스를 프로그래매틱하게 관리하는 kongctl 사용법 안내 — kongctl
- AI Gateway의 전체 기능과 문서 탐색 — AI Gateway
- MCP 트래픽을 OpenTelemetry로 모니터링하는 방법 소개 — Monitor MCP traffic with OpenTelemetry