> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SEP-1699：通过服务器端断开支持 SSE 轮询

> 通过服务器端断开支持 SSE 轮询

<div className="flex items-center gap-2 mb-4">
  <Badge color="green" shape="pill">
    最终版
  </Badge>

  <Badge color="gray" shape="pill">
    标准轨道
  </Badge>
</div>

| 字段       | 值                                                                               |
| -------- | ------------------------------------------------------------------------------- |
| **SEP**  | 1699                                                                            |
| **标题**   | 通过服务器端断开支持 SSE 轮询                                                               |
| **状态**   | 最终版                                                                             |
| **类型**   | 标准轨道                                                                            |
| **创建日期** | 2025-10-22                                                                      |
| **作者**   | Jonathan Hefner ([@jonathanhefner](https://github.com/jonathanhefner))          |
| **赞助人**  | 无                                                                               |
| **PR**   | [#1699](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/1699) |

***

## 摘要

本 SEP 提议对 Streamable HTTP 传输进行更改，以缓解有关长连接和可恢复性的问题。

## 动机

Streamable HTTP 传输规范 [不允许](https://github.com/modelcontextprotocol/modelcontextprotocol/blob/04c6e1f0ea6544c7df307fb2d7c637efe34f58d3/docs/specification/draft/basic/transports.mdx?plain=1#L109-L111) 服务器在计算结果时关闭连接。换句话说，除非客户端断开连接，否则服务器必须维持可能长期运行的连接。

## 规范

当服务器启动 SSE 流时，它必须立即发送一个包含 [`id`](https://html.spec.whatwg.org/multipage/server-sent-events.html#:~:text=field%20name%20is%20%22id%22) 和空 [`data`](https://html.spec.whatwg.org/multipage/server-sent-events.html#:~:text=field%20name%20is%20%22data%22) 字符串的 SSE 事件，以便让客户端准备好使用该事件 ID 作为 `Last-Event-ID` 进行重连。

请注意，SSE 标准明确 [允许将 `data` 设置为空字符串](https://html.spec.whatwg.org/multipage/server-sent-events.html#:~:text=data%20buffer%20is%20an%20empty%20string)，并指出适当的客户端处理是记录 `id` 用于 `Last-Event-ID`，但否则忽略该事件（即，不调用事件处理回调）。

在服务器向客户端发送事件 ID 后的任何时间点，服务器可以随时断开连接。具体来说，[MCP 规范的这一部分](https://github.com/modelcontextprotocol/modelcontextprotocol/blob/04c6e1f0ea6544c7df307fb2d7c637efe34f58d3/docs/specification/draft/basic/transports.mdx?plain=1#L109-L111) 将从：

> 服务器在发送收到的 JSON-RPC *请求* 的 JSON-RPC *响应* 之前 **不应** 关闭 SSE 流

改为：

> 如果服务器已向客户端发送了带有事件 ID 的 SSE 事件，则 **可以** 在发送 JSON-RPC *响应* 之前关闭连接

如果服务器断开连接，客户端将把断开连接解释为网络故障，并尝试重新连接。为了防止客户端过度重新连接/轮询，服务器应该发送一个带有 [`retry`](https://html.spec.whatwg.org/multipage/server-sent-events.html#:~:text=field%20name%20is%20%22retry%22) 字段的 SSE 事件，指示客户端在重新连接之前应该等待多长时间。客户端必须遵守 `retry` 字段。

## 理由

服务器可以随时断开连接，避免长连接。发送 `retry` 字段将防止客户端用不适当的重新连接尝试冲击服务器。

## 向后兼容性

* **新客户端 + 旧服务器**：无变化。无向后不兼容性。
* **旧客户端 + 新服务器**：客户端应将随时断开连接解释为网络故障。`retry` 字段是 SSE 标准的一部分。如果客户端已经实现了正确的 SSE 恢复逻辑，则没有向后不兼容性。

## 附加信息

本 SEP 部分取代 [SEP-1335](https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1335)。
