본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

File Log Policy로 MCP 트래픽 관찰하기

원문 보기 위키 갱신

File Log Policy로 MCP 트래픽 관찰하기 (Observe MCP traffic with the File Log Policy)

이 문서는 AI Gateway에서 File Log Policy를 사용해 MCP 도구 사용 기록을 로컬 파일에 남기는 방법을 알려줘요. 이전 튜토리얼에서 만든 Petstore AI MCP Server에 File Log Policy를 붙이고, config.logging.audits: true를 설정해 ACL 허용·거부 판정까지 기록해요. MCP Inspector CLI로 허용된 호출과 거부된 호출을 직접 생성해보고, 결과로 남는 ai.mcp 로그 항목을 확인하는 흐름으로 진행돼요.

출처: 문서

본문

TL;DR

AI Gateway로 MCP 도구 사용량은 어떻게 관찰하나요? File Log Policy를 사용해 MCP 도구 활동을 로컬 파일에 기록하고, AI MCP Server에 config.logging.audits: true를 설정해서 모든 ACL 결정이 기록되도록 하세요.

이 튜토리얼에서는 AI Consumer 및 AI Consumer Group ACL로 MCP 도구 접근 제어하기에서 만든 Petstore AI MCP Server에 File Log Policy를 연결하고, MCP Inspector CLI로 허용된 도구 호출과 거부된 도구 호출을 생성한 다음, 결과로 나온 ai.mcp 로그 항목을 읽어 봐요.

필수 조건

시리즈 필수 조건

이 페이지는 AI Gateway로 MCP 도구 접근 제어 및 관찰하기 시리즈의 일부예요.

이 페이지를 완료하기 전에 이전 페이지인 AI Consumer 및 AI Consumer Group ACL로 MCP 도구 접근 제어하기를 먼저 완료하세요.

kongctl   v1.20.2+

이 튜토리얼에서는 kongctl을 사용해 Konnect 리소스를 프로그래밍 방식으로 관리해요. kongctl을 최신 버전(1.20.2)으로 계속 업데이트하는 것을 추천해요.

  1. developer.konghq.com/kongctl에서 kongctl을 설치하세요.

  2. 설치를 확인하세요:

    kongctl version
    

File Log Policy 연결하기

File Log Policy를 생성하고 기존 petstore-acl-mcp AI MCP Server에 연결하세요.

kongctl은 각 AI MCP Server를 선언적으로 관리하기 때문에, 이 설정은 이전 튜토리얼의 엔티티 전체 정의를 두 가지 추가 사항과 함께 반복해요: policies에 새로운 AI Policy를 추가한 것과 config.logging.audits: true예요.

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_policies:
  - ref: my-file-log
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-file-log
    display_name: "my-file-log"
    type: file-log
    enabled: true
    global: false
    config:
      path: /tmp/mcp.json

ai_gateway_mcp_servers:
  - ref: petstore-acl-mcp
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: petstore-acl-mcp
    display_name: "Petstore API"
    type: conversion-listener
    enabled: true
    policies:
      - !ref my-file-log#name
    access:
      auth_strategies:
        - my-key-auth
      acl_attribute_type: consumer
      acls:
        allow: []
      default_tool_acls:
        allow:
          - admin
        deny: []
    config:
      url: http://host.docker.internal:8080/api/v3
      route:
        paths:
          - /petstore-acl
      logging:
        payloads: false
        audits: true
      server:
        timeout: 60000
    tools:
      - name: get-pets-by-status
        description: Find pets by status
        method: GET
        path: /petstore-acl/pet/findByStatus
        access:
          acls:
            allow:
              - admin
              - support
              - eason
        parameters:
          - name: status
            in: query
            required: true
            schema:
              type: string
              enum:
                - available
                - pending
                - sold
            description: Status value to filter pets by
      - name: get-pet-by-id
        description: Get a pet by ID
        method: GET
        path: /petstore-acl/pet/{petId}
        access:
          acls:
            allow:
              - admin
              - support
        parameters:
          - description: ID of the pet to retrieve
            in: path
            name: petId
            required: true
            schema:
              type: integer
      - name: get-inventory
        description: Get pet inventories by status
        method: GET
        path: /petstore-acl/store/inventory
        access:
          acls:
            allow:
              - admin
              - support
      - name: get-order-by-id
        description: Get a purchase order by ID
        method: GET
        path: /petstore-acl/store/order/{orderId}
        access:
          acls:
            allow:
              - admin
              - support
        parameters:
          - description: ID of the order to retrieve
            in: path
            name: orderId
            required: true
            schema:
              type: integer
      - name: delete-pet
        description: Delete a pet
        method: DELETE
        path: /petstore-acl/pet/{petId}
        access:
          acls:
            allow:
              - admin
            deny:
              - support
        parameters:
          - description: ID of the pet to delete
            in: path
            name: petId
            required: true
            schema:
              type: integer
EOF

{: data-test-step="block" }

MCP 트래픽 생성하기

MCP Inspector CLI를 사용해 서로 다른 두 AI Consumer로 AI MCP Server를 호출하고, 각자의 API 키를 apikey 헤더로 전달하세요.

  1. admin에 속한 Alice는 get-pet-by-id를 성공적으로 호출해요:

    npx -y @modelcontextprotocol/inspector@0.22.0 --cli \
      $KONNECT_PROXY_URL/petstore-acl \
      --transport http --method tools/call \
      --tool-name get-pet-by-id \
      --tool-arg path_petId=7 \
      --header "apikey: $ALICE_API_KEY" | jq -r '.content[0].text' | jq -c '.'
    
  2. support에 속한 Bob은 delete-pet 호출이 거부돼요. 이 호출은 HTTP 403 Forbidden으로 거부되며, MCP Inspector CLI는 이를 전송 오류와 0이 아닌 종료 코드로 보고해요:

    npx -y @modelcontextprotocol/inspector@0.22.0 --cli \
      $KONNECT_PROXY_URL/petstore-acl \
      --transport http --method tools/call \
      --tool-name delete-pet \
      --tool-arg path_petId=9 \
      --header "apikey: $BOB_API_KEY"
    

로그 항목 검증하기

/tmp/mcp.json의 각 줄은 완전한 File Log 항목이고, MCP 활동은 표준 request, response, consumer 필드와 함께 ai.mcp 객체에 담겨요.

AI Gateway Docker 컨테이너 안에서 로그를 읽어 보세요. audit 배열을 가진 항목으로 필터링하면 출력이 ACL 결정으로 좁혀져서, 각 MCP Inspector CLI 호출이 함께 생성하는 initialize 및 notifications/initialized 핸드셰이크 요청은 건너뛰게 돼요:

docker exec kong-ai-quickstart-gateway cat /tmp/mcp.json \
  | jq 'select(.ai.mcp.audit) | .ai.mcp'

항목은 네 개가 나와요. 각 AI Consumer의 클라이언트가 도구를 호출하기 전에 도구 목록을 먼저 조회하고, 이 조회 자체도 ACL 결정 중 하나이기 때문이에요. primitive_name이 없는 항목들이 tools/list 결정이고, 도구 이름을 담고 있는 두 항목이 실제 호출이에요.

허용된 호출은 rpc 항목과 audit 항목을 모두 생성해요. Alice의 get-pet-by-id 호출은 다음과 같아요:

{
  "mcp_session_id": "98971c19-9539-4611-9cfe-a9eefc11f702",
  "mcp_server_id": "8c3cd66f-4c4f-4809-a671-2f412a794d01",
  "protocol_version": "2025-11-25",
  "rpc": [
    {
      "method": "tools/call",
      "tool_name": "get-pet-by-id",
      "latency": 46,
      "response_body_size": 290,
      "id": "2"
    }
  ],
  "audit": [
    {
      "primitive": "tool",
      "scope": "primitive",
      "primitive_name": "get-pet-by-id",
      "consumer": {
        "name": "admin",
        "id": "327670e0-4d13-47a8-9453-5b7370bfbba7",
        "identifier": "consumer_group"
      },
      "action": "allow"
    }
  ],
  "tool_list_cache_hit": true
}

{:.no-copy-code}

audit 항목에서 consumer.id는 항상 호출을 수행한 AI Consumer의 UUID이고, consumer.name은 실제로 ACL 규칙에 매칭된 주체예요. consumer.identifier는 그 주체가 어떤 종류였는지 알려줘요:

  • consumer_group: AI Consumer Group이 이 결정을 허가받은 것이고, name은 그 그룹이에요. Alice의 호출은 admin을 통해 매칭됐어요.
  • username: 개별 AI Consumer가 매칭된 것이고, name은 해당 AI Consumer 자신의 이름이에요.

즉, 어떤 규칙이 해당 요청을 결정했는지에 따라 같은 AI Consumer라도 두 식별자 중 어느 쪽으로든 표시될 수 있어요. 전체 필드 목록은 감사 로그 레퍼런스를 참고하세요.

Bob의 거부된 delete-pet 호출에는 rpc 항목이 전혀 없고, ACL 결정을 기록하는 audit 항목만 있어요:

{
  "mcp_session_id": "1d8ca25a-50b5-4871-b4e9-81d0f2f3fa89",
  "mcp_server_id": "8c3cd66f-4c4f-4809-a671-2f412a794d01",
  "protocol_version": "2025-11-25",
  "tool_list_cache_hit": true,
  "audit": [
    {
      "primitive": "tool",
      "scope": "primitive",
      "primitive_name": "delete-pet",
      "consumer": {
        "name": "support",
        "id": "ae05d1a5-1a43-4176-a882-af5c2d0b2e78",
        "identifier": "consumer_group"
      },
      "action": "deny"
    }
  ]
}

{:.no-copy-code}

거부가 기록됐는지 확인하세요:

docker exec kong-ai-quickstart-gateway cat /tmp/mcp.json \
  | jq -r '.ai.mcp.audit[]? | select(.primitive_name == "delete-pet") | .action' \
  | sort -u

Konnect에서의 MCP 트래픽

File Log Policy를 설정하지 않고도 Konnect Analytics를 사용해 도구 사용량과 ACL 거부를 볼 수 있어요. Observability > Dashboards로 이동해서 Agentic analytics 템플릿으로 대시보드를 만들고, 도구 이름이나 AI Consumer로 필터링하세요.

원시 로그 항목 대신 메트릭 기반 관찰이 필요하다면, OpenTelemetry로 MCP 트래픽 모니터링하기를 참고하세요.

정리

Petstore API 중지하기

docker rm -f swagger-petstore

{: data-test-cleanup="block" }

AI Gateway 리소스 정리하기

이 가이드에서 생성한 모든 AI Gateway 리소스를 정리하려면 다음을 실행하세요:

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

자주 묻는 질문(FAQ)

  • 거부된 호출에 rpc 항목이 없는 이유는 무엇인가요? ACL 검사는 요청이 RPC로 디스패치되기 전에 실행되기 때문에, 거부된 호출은 절대 RPC가 되지 않아요. ai.mcp.audit 항목만이 이를 기록해요. 이 때문에 rpc 지연 시간과 크기 데이터는 업스트림 MCP 서버에 도달한 호출만을 다뤄요.

  • 도구 호출 한 번이 여러 로그 항목을 기록하는 이유는 무엇인가요? MCP Inspector CLI는 호출할 때마다 새로운 MCP 세션을 열기 때문에, tools/call 명령 하나가 initialize, notifications/initialized, tools/list, 그리고 도구 호출 자체로 이어지는 짧은 요청 시퀀스를 만들어요. 각각은 AI Gateway를 거치는 별개의 요청이므로 각자 자체 File Log 항목을 받고, 모두 하나의 ai.mcp.mcp_session_id를 공유해요.

    tools/list와 도구 호출 모두 ai.mcp.audit 배열을 담고 있는데, 이는 조회와 호출이 별개의 ACL 결정이기 때문이에요. 조회 항목에는 primitive_name이 없는데, 하나의 도구가 아니라 도구 목록 전체를 다루기 때문이에요.

  • ai.mcp.protocol_version에는 어떤 MCP 프로토콜 리비전이 표시되나요? AI MCP Server에서 설정하는 값이 아니라, 클라이언트가 initialize 중에 협상한 리비전이에요. 이 튜토리얼에서 사용한 MCP Inspector CLI는 2025-11-25를 협상하므로 항목에 ai.mcp.mcp_session_id가 포함돼요. 2026-07-28을 협상하는 클라이언트는 세션 개념이 없고, AI Gateway는 해당 트래픽에 대해 mcp_session_id를 생략해요. 자세한 내용은 MCP 버전 지원을 참고하세요.

관련 리소스

더 알아보기 (Learn more)