例
次の例は、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 インターセプターとして設定すると、変更されずにレスポンスを渡すだけです。どちらのインターセプタータイプも変更せずに元のデータを返し、これを「パススルー」インターセプターにします。