AgentCore ゲートウェイからログメッセージを受信する
MCP サーバーターゲットは、 notifications/messageメソッドを使用してツールの実行中にクライアントにログメッセージを送信できます。これらのメッセージは、サーバーの動作をリアルタイムで可視化し、ツールの動作のデバッグ、監査、モニタリングに役立ちます。AgentCore Gateway は、これらのログ通知を MCP サーバーターゲットからサーバー送信イベント (SSE) チャンクとしてクライアントに転送します。
前提条件
ゲートウェイからログメッセージを受信するには:
-
レスポンスストリーミングが有効 — ログメッセージは、オープン接続中に SSE チャンクとして配信されます。ゲートウェイの trueで streamingConfiguration.enableResponseStreamingを に設定しますprotocolConfiguration.mcp。
-
MCP サーバーターゲットタイプ — ログメッセージは MCP サーバーターゲットから送信されます。
-
クライアント送信Accept: text/event-streamヘッダー — クライアントはストリーミングイベントを受信するために SSE レスポンスをリクエストする必要があります。
ログレベル
MCP では、重要度が高い順に次のログレベルを定義します。
| レベル |
説明 |
|
debug
|
トラブルシューティングのための詳細な診断情報。
|
|
info
|
通常のオペレーションに関する一般的な情報メッセージ。
|
|
notice
|
正常だが重要なイベント。
|
|
warning
|
オペレーションを妨げない潜在的に有害な状況。
|
|
error
|
特定のオペレーションを妨げたエラー条件。
|
|
critical
|
即時の対応が必要な重大な状態。
|
|
alert
|
アクションはすぐに実行する必要があります。
|
|
emergency
|
システムは利用できません。
|
ログ記録メッセージの仕組み
ツールの実行notifications/message中に MCP サーバーターゲットが を出力すると、ゲートウェイはそれを SSE イベントとしてクライアントに転送します。各ログメッセージには以下が含まれます。
ログメッセージは情報であり、クライアントからの応答は必要ありません。これらは、進行状況通知や最終的なツール結果など、他の SSE イベントと一緒に配信されます。
コードサンプル
例
- curl
-
-
ツールを呼び出し、SSE ストリームでログメッセージを受信します。
curl -N -X POST \
https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-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."}]}}
- Python requests package
-
-
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": "text/event-stream",
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
}
# 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
- 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)