AgentCore 결제를 브라우저 도구와 통합
에이전트가 x402 유료 콘텐츠에 액세스하는 방법에는 브라우저 도구 또는 표준 HTTP 호출을 통해 액세스할 수 있는 두 가지가 있습니다.
브라우저 도구 통합(Playwright)
AgentCore 브라우저 도구는 에이전트에게 AgentCore 런타임 내에서 관리형 헤드리스 Chromium 브라우저 세션을 제공합니다. 에이전트는 Chrome DevTools 프로토콜(CDP)을 사용하여 WebSocket을 통해 연결하여 페이지를 탐색하고, JavaScript를 실행하고, 네트워크 수준에서 HTTP 응답을 가로챌 수 있습니다. 시작하려면 브라우저 온보딩 가이드를 참조하세요.
이 옵션은 브라우저 도구 및 결제 도구와 함께 Strands SDK를 사용하여 웹 사이트를 검색하고 결제를 처리합니다. 에이전트는 브라우저 도구를 사용하여 웹 사이트를 탐색할 때 Playwright의 응답 가로채기 기능을 사용하여 페이월 사이트를 자동으로 감지합니다.
-
브라우저 탐색 - 에이전트는 Playwright에서 제공하는
browse_with_payment도구를 사용합니다. -
자동 402 감지 - Playwright는 모든 HTTP 응답을 가로채고 에이전트의 응답 핸들러를 트리거하여 402 상태 코드를 감지합니다.
-
x402 추출 - 결제 도구는 응답 헤더/본문에서 x402 결제 세부 정보를 자동으로 구문 분석합니다. 이는 에이전트 비즈니스 로직으로도 수행할 수 있습니다.
-
결제 처리 - AgentCore 결제 프로세서는 구성된 CDP Wallet을 사용하여 암호화 서명을 생성합니다.
-
재시도 - 결제 도구가 권한 부여 헤더를 주입하고 동일한 브라우저 세션에서 요청을 재시도합니다.
from agentcore.payments import PaymentClient from agentcore.browser import browser_tool @tool async def browse_with_payment(url: str, auto_pay: bool = True) -> dict: payment_requirements = None async def handle_response(response): nonlocal payment_requirements if response.status == 402: payment_requirements = await extract_x402_requirements(response) async def add_payment_header(route, request): headers = {**request.headers, **payment_result["authorization"]} await route.continue_(headers=headers) async with async_playwright() as p: # 1. Browser setup browser = await p.chromium.connect_over_cdp(AGENTCORE_BROWSER_WS_URL) page = await browser.contexts[0].new_page() # 2. Register response interceptor page.on("response", handle_response) # 3. Initial navigation (handle_response runs automatically here) response = await page.goto(url) # 4. Payment processing logic (runs AFTER navigation completes) if response.status == 402 and payment_requirements and auto_pay: payment_result = payment_client.process_payment( payment_session, payment_instrument, payment_requirements=payment_requirements ) if payment_result["success"]: await page.route("**/*", add_payment_header) response = await page.goto(url) # Retry with payment else: raise PaymentError(f"Payment failed: {payment_result['error_message']}") # 5. Capture content, cleanup, and return content = await page.content() await browser.close() return {"content": content, "status": response.status}
브라우저가 아닌 HTTP 통합
직접 API 호출 및 헤드리스 시나리오의 경우 개발자는 표준 HTTP 요청 라이브러리와 함께 Strands SDK를 사용하여 사용자 지정 도구 로직을 작성하여 x402 감지로 결제를 처리할 수 있습니다.
-
에이전트는 사용자 지정 도구를 사용하여 직접 HTTP 호출을 수행합니다.
-
도구는 HTTP 응답 상태 코드를 모니터링하여 402를 감지합니다.
-
도구는 x402 요구 사항을 추출하고 결제를 처리합니다.
-
도구는 결제 권한 부여 헤더를 사용하여 HTTP 요청을 재시도합니다.
@tool async def make_payment(method, url, headers, body) -> dict: # Make initial HTTP request response = requests.request(method, url, headers=headers, data=body) # Check for 402 Payment Required if response.status_code == 402: payment_requirements = extract_x402_requirements(response) payment_result = payment_client.process_payment( payment_session, payment_instrument, payment_requirements=payment_requirements ) if payment_result["success"]: # Retry request with payment authorization headers.update(payment_result["authorization"]) response = requests.request(method, url, headers=headers, data=body) else: raise PaymentError(f"Payment failed: {payment_result['error_message']}")