本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
從 AgentCore 閘道接收進度通知
進度通知可讓 MCP 伺服器目標報告長時間執行工具呼叫的增量進度。當工具呼叫需要時間完成時,伺服器可以傳送notifications/progress事件,讓用戶端了解操作的狀態。AgentCore Gateway 會將這些通知從 MCP 伺服器目標轉送到您的用戶端,做為 Server-Sent Events (SSE) 區塊。
先決條件
若要從閘道接收進度通知:
-
啟用回應串流 — 在開放連線期間,進度通知會以 SSE 區塊的形式傳送。在閘道的
true中streamingConfiguration.enableResponseStreaming將 設定為protocolConfiguration.mcp。 -
MCP 伺服器目標類型 — 進度通知源自 MCP 伺服器目標。
-
用戶端傳送
Accept標頭 — 用戶端必須傳送 ,Accept: application/json, text/event-stream以便閘道可以傳回串流 (SSE) 回應。
進度通知的運作方式
當用戶端在tools/call請求參數progressToken中使用 提出請求時,MCP 伺服器目標可以在執行期間傳送notifications/progress事件。在最終工具結果之前,閘道會以 SSE 區塊的形式將這些事件轉送至用戶端。
每個進度通知都包含:
-
progressToken— 符合原始請求中提供的字符。 -
progress— 目前的進度值 (數值)。 -
total— 指出完成目標的選用總值。 -
message— 目前狀態的選用人類可讀描述。
程式碼範例
範例
- curl (2025-11-25 and earlier)
-
使用進度字符呼叫工具。將
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": "analyzeDataset", "arguments": { "datasetId": "ds-12345" }, "_meta": { "progressToken": "progress-1" } } }'閘道會傳回具有進度通知的 SSE 串流,後面接著最終結果:
event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":1,"total":4,"message":"Loading dataset..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":2,"total":4,"message":"Running analysis..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":3,"total":4,"message":"Generating report..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":4,"total":4,"message":"Complete"}} event: message data: {"jsonrpc":"2.0","id":"tool-call-1","result":{"content":[{"type":"text","text":"Analysis complete. Found 3 anomalies in dataset ds-12345."}]}} - curl (2026-07-28)
-
在版本 上
2026-07-28,在內文中包含Mcp-Method和Mcp-Namerequest-metadata 標頭和_meta版本欄位。繼續progressToken_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: analyzeDataset" \ -d '{ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "analyzeDataset", "arguments": { "datasetId": "ds-12345" }, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": { "name": "my-agent", "version": "1.0.0" }, "io.modelcontextprotocol/clientCapabilities": {}, "progressToken": "progress-1" } } }'閘道會傳回具有進度通知的 SSE 串流,後面接著最終結果:
event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":1,"total":4,"message":"Loading dataset..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":2,"total":4,"message":"Running analysis..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":3,"total":4,"message":"Generating report..."}} event: message data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"progress-1","progress":4,"total":4,"message":"Complete"}} event: message data: {"jsonrpc":"2.0","id":"tool-call-1","result":{"content":[{"type":"text","text":"Analysis complete. Found 3 anomalies in dataset ds-12345."}]}} - 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 with progress token (streaming response) response = requests.post(gateway_url, headers=headers, json={ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "analyzeDataset", "arguments": {"datasetId": "ds-12345"}, "_meta": {"progressToken": "progress-1"} } }, stream=True) # Process SSE events client = sseclient.SSEClient(response) for event in client.events(): data = json.loads(event.data) if data.get("method") == "notifications/progress": params = data["params"] print(f"Progress: {params['progress']}/{params.get('total', '?')} - {params.get('message', '')}") elif "result" in data: print(f"Tool result: {data['result']}") break - Python requests package (2026-07-28)
-
在版本 上
2026-07-28,新增MCP-Protocol-Version、Mcp-Method和Mcp-Name標頭,並在請求內文中包含_meta版本欄位。繼續progressToken_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": "analyzeDataset" } # Call tool with progress token (streaming response) response = requests.post(gateway_url, headers=headers, json={ "jsonrpc": "2.0", "id": "tool-call-1", "method": "tools/call", "params": { "name": "analyzeDataset", "arguments": {"datasetId": "ds-12345"}, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": {"name": "my-agent", "version": "1.0.0"}, "io.modelcontextprotocol/clientCapabilities": {}, "progressToken": "progress-1" } } }, stream=True) # Process SSE events client = sseclient.SSEClient(response) for event in client.events(): data = json.loads(event.data) if data.get("method") == "notifications/progress": params = data["params"] print(f"Progress: {params['progress']}/{params.get('total', '?')} - {params.get('message', '')}") 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 progress_handler(progress_token, progress, total, message=None): """Handle progress notifications.""" print(f"[{progress}/{total}] {message or ''}") async def use_progress(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, progress_handler=progress_handler ) as session: await session.initialize() # Call tool with progress token - notifications handled by callback result = await session.call_tool( name="analyzeDataset", arguments={"datasetId": "ds-12345"} ) print(f"Tool result: {result}") return result asyncio.run(use_progress( 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 progress notifications automatically with mcp_client: agent = Agent(tools=mcp_client.list_tools_sync()) response = agent("Analyze dataset ds-12345") print(response)