View a markdown version of this page

AgentCore ゲートウェイからログメッセージを受信する - Amazon Bedrock AgentCore

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AgentCore ゲートウェイからログメッセージを受信する

MCP サーバーターゲットは、 notifications/messageメソッドを使用してツールの実行中にクライアントにログメッセージを送信できます。これらのメッセージは、サーバーの動作をリアルタイムで可視化し、ツールの動作のデバッグ、監査、モニタリングに役立ちます。AgentCore Gateway は、これらのログ通知を MCP サーバーターゲットからサーバー送信イベント (SSE) チャンクとしてクライアントに転送します。

前提条件

ゲートウェイからログメッセージを受信するには:

  • レスポンスストリーミングが有効 — ログメッセージは、オープン接続中に SSE チャンクとして配信されます。ゲートウェイの trueで streamingConfiguration.enableResponseStreamingを に設定しますprotocolConfiguration.mcp。

  • MCP サーバーターゲットタイプ — ログメッセージは MCP サーバーターゲットから送信されます。

  • クライアントが Acceptヘッダーを送信する — ゲートウェイがストリーミング (SSE) レスポンスを返Accept: application/json, text/event-streamすことができるように、クライアントは を送信する必要があります。

ログレベル

MCP では、重要度が高い順に次のログレベルを定義します。

レベル 説明

debug

トラブルシューティングのための詳細な診断情報。

info

通常のオペレーションに関する一般的な情報メッセージ。

notice

正常だが重要なイベント。

warning

オペレーションを妨げない潜在的に有害な状況。

error

特定のオペレーションを妨げたエラー条件。

critical

即時の対応が必要な重大な状態。

alert

アクションはすぐに実行する必要があります。

emergency

システムは利用できません。

ログ記録メッセージの仕組み

ツールの実行notifications/message中に MCP サーバーターゲットが を出力すると、ゲートウェイはそれを SSE イベントとしてクライアントに転送します。各ログメッセージには以下が含まれます。

  • level — メッセージの重要度レベル。

  • logger — ソースコンポーネントを識別するオプションの名前。

  • data — ログコンテンツ (文字列または構造化オブジェクト)。

ログメッセージは情報であり、クライアントからの応答は必要ありません。これらは、進行状況通知や最終的なツール結果など、他の SSE イベントと一緒に配信されます。

コードサンプル

例
curl (2025-11-25 and earlier)

ツールを呼び出し、SSE ストリームでログメッセージを受信します。MCP-Protocol-Version ヘッダーをゲートウェイがサポートするバージョンに設定します。

curl -N -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "MCP-Protocol-Version: 2025-11-25" \ -d '{ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "deployService", "arguments": { "serviceName": "my-api", "environment": "staging" } } }'

ゲートウェイは、ログメッセージとそれに続く最終結果を含む SSE ストリームを返します。

event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Starting deployment of my-api to staging"}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Building container image..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"warning","logger":"deploy-service","data":"Deprecated configuration detected in service manifest"}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Deployment complete"}} event: message data: {"jsonrpc":"2.0","id":"tool-call-1","result":{"content":[{"type":"text","text":"Successfully deployed my-api to staging environment."}]}}
curl (2026-07-28)

バージョン では2026-07-28、クライアントは で を設定して各リクエストのログメッセージをオプトインio.modelcontextprotocol/logLevelします _meta (このバージョンではlogging/setLevelオペレーションは廃止されます)。サーバーは、リクエストされたレベル以上notifications/messageでのみ を送信します。また、 Mcp-Methodおよび Mcp-Name request-metadata ヘッダーと_metaバージョンフィールドを含めます。MCP-Protocol-Version ヘッダーは と一致する必要があります_meta.io.modelcontextprotocol/protocolVersion。ゲートウェイの には が含まれているsupportedVersions必要があります2026-07-28。

curl -N -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "MCP-Protocol-Version: 2026-07-28" \ -H "Mcp-Method: tools/call" \ -H "Mcp-Name: deployService" \ -d '{ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "deployService", "arguments": { "serviceName": "my-api", "environment": "staging" }, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": { "name": "my-agent", "version": "1.0.0" }, "io.modelcontextprotocol/clientCapabilities": {}, "io.modelcontextprotocol/logLevel": "info" } } }'

ゲートウェイは、ログメッセージとそれに続く最終結果を含む SSE ストリームを返します。

event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Starting deployment of my-api to staging"}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Building container image..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"warning","logger":"deploy-service","data":"Deprecated configuration detected in service manifest"}} event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","logger":"deploy-service","data":"Deployment complete"}} event: message data: {"jsonrpc":"2.0","id":"tool-call-1","result":{"content":[{"type":"text","text":"Successfully deployed my-api to staging environment."}]}}
Python requests package (2025-11-25 and earlier)

MCP-Protocol-Version ヘッダーをゲートウェイがサポートするバージョンに設定します。

import requests import json import sseclient gateway_url = "https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp" headers = { "Content-Type": "application/json", "Accept": "application/json, text/event-stream", "Authorization": "Bearer YOUR_ACCESS_TOKEN", "MCP-Protocol-Version": "2025-11-25" } # Call tool (streaming response) response = requests.post(gateway_url, headers=headers, json={ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "deployService", "arguments": {"serviceName": "my-api", "environment": "staging"} } }, stream=True) # Process SSE events client = sseclient.SSEClient(response) for event in client.events(): data = json.loads(event.data) if data.get("method") == "notifications/message": params = data["params"] print(f"[{params['level'].upper()}] {params.get('logger', '')}: {params['data']}") elif "result" in data: print(f"Tool result: {data['result']}") break
Python requests package (2026-07-28)

バージョン では2026-07-28、 で を設定してログメッセージをオプトインio.modelcontextprotocol/logLevelします _meta (このバージョンではlogging/setLevelオペレーションは廃止されます)。またMCP-Protocol-Version、、Mcp-Method、および Mcp-Nameヘッダーと_metaバージョンフィールドを追加します。ゲートウェイの には が含まれているsupportedVersions必要があります2026-07-28。

import requests import json import sseclient gateway_url = "https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp" headers = { "Content-Type": "application/json", "Accept": "application/json, text/event-stream", "Authorization": "Bearer YOUR_ACCESS_TOKEN", "MCP-Protocol-Version": "2026-07-28", "Mcp-Method": "tools/call", "Mcp-Name": "deployService" } # Call tool (streaming response) response = requests.post(gateway_url, headers=headers, json={ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "deployService", "arguments": {"serviceName": "my-api", "environment": "staging"}, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": {"name": "my-agent", "version": "1.0.0"}, "io.modelcontextprotocol/clientCapabilities": {}, "io.modelcontextprotocol/logLevel": "info" } } }, stream=True) # Process SSE events client = sseclient.SSEClient(response) for event in client.events(): data = json.loads(event.data) if data.get("method") == "notifications/message": params = data["params"] print(f"[{params['level'].upper()}] {params.get('logger', '')}: {params['data']}") elif "result" in data: print(f"Tool result: {data['result']}") break
MCP Client
from mcp import ClientSession from mcp.client.streamable_http import streamablehttp_client import asyncio async def log_handler(level, logger, data): """Handle log messages from the server.""" print(f"[{level.upper()}] {logger or 'server'}: {data}") async def use_logging(url, token): headers = {"Authorization": f"Bearer {token}"} async with streamablehttp_client(url=url, headers=headers) as ( read_stream, write_stream, _ ): async with ClientSession( read_stream, write_stream, logging_handler=log_handler ) as session: await session.initialize() # Call tool - log messages handled by callback result = await session.call_tool( name="deployService", arguments={"serviceName": "my-api", "environment": "staging"} ) print(f"Tool result: {result}") return result asyncio.run(use_logging( url="https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp", token="YOUR_ACCESS_TOKEN" ))
Strands MCP Client
from mcp.client.streamable_http import streamablehttp_client from strands import Agent from strands.tools.mcp import MCPClient mcp_url = "https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp" access_token = "YOUR_ACCESS_TOKEN" mcp_client = MCPClient( lambda: streamablehttp_client( mcp_url, headers={"Authorization": f"Bearer {access_token}"} ) ) # Strands handles streaming and log messages automatically with mcp_client: agent = Agent(tools=mcp_client.list_tools_sync()) response = agent("Deploy my-api to staging") print(response)