View a markdown version of this page

예제 - Amazon Bedrock AgentCore

예제

다음 예제는 REQUEST 및 RESPONSE 인터셉터 유형을 모두 처리할 수 있는 Python Lambda 함수를 보여줍니다. REQUEST 인터셉터는 MCP 메서드를 로깅하고 요청을 변경되지 않은 상태로 전달하는 반면, RESPONSE 인터셉터는 변경되지 않은 상태로 응답을 전달합니다.

패스스루 인터셉터

이 예제는 REQUEST 인터셉터에 대한 MCP 메서드를 로깅하고 모든 요청 및 응답을 변경되지 않은 상태로 전달하는 간단한 인터셉터를 보여줍니다.

import json import logging # Configure logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): """ Lambda function that handles both REQUEST and RESPONSE interceptor types. For REQUEST interceptors: logs the MCP method and passes request through unchanged For RESPONSE interceptors: passes response through unchanged """ # Extract the MCP data from the event mcp_data = event.get('mcp', {}) # Check if this is a REQUEST or RESPONSE interceptor based on presence of gatewayResponse if 'gatewayResponse' in mcp_data and mcp_data['gatewayResponse'] != None: # This is a RESPONSE interceptor logger.info("Processing RESPONSE interceptor - passing through unchanged") # Pass through the original request and response unchanged response = { "interceptorOutputVersion": "1.0", "mcp": { "transformedGatewayResponse": { "body": mcp_data.get('gatewayResponse', {}).get('body', {}), "statusCode": mcp_data.get('gatewayResponse', {}).get('statusCode', 200) } } } else: # This is a REQUEST interceptor gateway_request = mcp_data.get('gatewayRequest', {}) request_body = gateway_request.get('body', {}) mcp_method = request_body.get('method', 'unknown') # Log the MCP method logger.info(f"Processing REQUEST interceptor - MCP method: {mcp_method}") # Pass through the original request unchanged response = { "interceptorOutputVersion": "1.0", "mcp": { "transformedGatewayRequest": { "body": request_body, } } } return response

이 Lambda 함수는 REQUEST 및 RESPONSE 인터셉터로 구성할 수 있습니다. REQUEST 인터셉터로 구성된 경우 수신 요청에서 MCP 메서드를 로깅합니다. RESPONSE 인터셉터로 구성된 경우 변경되지 않은 상태로 응답을 전달하기만 하면 됩니다. 두 인터셉터 유형 모두 수정 없이 원본 데이터를 반환하므로 "패스-스루" 인터셉터가 됩니다.