

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 从您的 AgentCore 网关接收进度通知
<a name="gateway-mcp-progress"></a>

进度通知允许 MCP 服务器目标报告长时间运行的工具调用的增量进度。当工具调用需要一段时间才能完成时，服务器可以发送`notifications/progress`事件让客户端随时了解操作的状态。 AgentCore Gateway 将这些来自 MCP 服务器目标的通知作为 Server-Sent 事件 (SSE) 区块转发到您的客户端。

## 先决条件
<a name="gateway-mcp-progress-prereqs"></a>

要接收来自网关的进度通知，请执行以下操作：
+  **启用响应流 **-在打开的连接期间，进度通知以 SSE 区块的形式传送。`streamingConfiguration.enableResponseStreaming`在网关`true`中设置为`protocolConfiguration.mcp`。
+  **MCP 服务器目标类型 **-进度通知来自 MCP 服务器目标。
+  **客户端发送标`Accept`头 **-客户端必须发送，`Accept: application/json, text/event-stream`这样网关才能返回流式传输 (SSE) 响应。

## 进度通知的工作原理
<a name="gateway-mcp-progress-how"></a>

当客户端使用`tools/call`请求参数`progressToken`中的请求时，MCP 服务器目标可以在执行期间发送`notifications/progress`事件。在最终工具结果出来之前，网关将这些事件作为 SSE 区块转发给客户端。

每份进度通知包括：
+  `progressToken`— 与原始请求中提供的令牌相匹配。
+  `progress`— 当前进度值（数字）。
+  `total`— 表示完成目标的可选总值。
+  `message`— 对当前状态的可选人类可读描述。

## 代码示例
<a name="gateway-mcp-progress-examples"></a>

**Example**  
使用进度令牌调用工具。将标`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."}]}}
```
在版本上`2026-07-28`，在正文中包括`Mcp-Method`和`Mcp-Name`请求元数据标头以及`_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."}]}}
```
将标`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
```
在版本上`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
```

```
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"
))
```

```
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)
```