Proposal: Export callback to StreamableHTTPHandler for closed transports
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
- #480 @fgrosse による — マージされずにクローズ
評価
調査の方向性
mcp/streamable.go から始め、StreamableHTTPHandler.ServeHTTP と StreamableServerTransport を読みます。特に、既存の transport 削除時のクリーンアップと DELETE の処理を確認してください。export された接続クローズコールバックが、クライアントによる明示的な DELETE リクエストと中断された接続の両方を対象とし、transport と session のリソースが解放されれば、変更は完了です。
索引モデルが issue の本文から書いたものです。
説明
Is your feature request related to a problem? Please describe
I'm using the StreamableHTTPHandler to implement an MCP server. Just as the underlying mcp.Server, my server is stateful, and I need to clean up resources when a session ends (e.g., deleting entries from a map). I am currently performing this cleanup by wrapping the StreamableHTTPHandler in my own HTTP handler implementation, which allows me to directly handle DELETE requests from the client. While this works fine, I'm still not able to clean up sessions when the connection breaks due to timeouts or other network errors.
Describe the solution you'd like
Browsing around the SDK internals, I found there is already such a cleanup, but it's not yet exported: StreamableHTTPHandler.onTransportDeletion(...). I experimented with this locally to see if it fits my use case and was able to make it work. However, I found out that this cleanup isn't actually called yet when the client closes the session with a DELETE call explicitly. Ideally, I would like to replace my current HTTP handler for DELETE requests with a single cleanup function, so it would be beneficial if the StreamableHTTPHandler would actually close the transport connection and session when handling the DELETE call.
I've attached a full working diff in the details below:
diff --git a/mcp/streamable.go b/mcp/streamable.go
index f359b7c..9c563cf 100644
--- a/mcp/streamable.go
+++ b/mcp/streamable.go
@@ -40,8 +40,6 @@ type StreamableHTTPHandler struct {
getServer func(*http.Request) *Server
opts StreamableHTTPOptions
- onTransportDeletion func(sessionID string) // for testing only
-
mu sync.Mutex
// TODO: we should store the ServerSession along with the transport, because
// we need to cancel keepalive requests when closing the transport.
@@ -67,6 +65,11 @@ type StreamableHTTPOptions struct {
//
// [§2.1.5]: https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#sending-messages-to-the-server
JSONResponse bool
+
+ // OnConnectionClose is a callback function that is invoked when a [Connection]
+ // is closed. A connection is closed when the session is ended explicitly by
+ // the client or when it is interrupted due to a timeout or other errors.
+ OnConnectionClose func(sessionID string)
}
// NewStreamableHTTPHandler returns a new [StreamableHTTPHandler].
@@ -153,7 +156,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
h.mu.Lock()
delete(h.transports, transport.SessionID)
h.mu.Unlock()
- transport.connection.Close()
+ _ = transport.Close()
}
w.WriteHeader(http.StatusNoContent)
return
@@ -286,8 +289,8 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
h.mu.Lock()
delete(h.transports, transport.SessionID)
h.mu.Unlock()
- if h.onTransportDeletion != nil {
- h.onTransportDeletion(transport.SessionID)
+ if h.opts.OnConnectionClose != nil {
+ h.opts.OnConnectionClose(transport.SessionID)
}
},
}
@@ -307,6 +310,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
} else {
// Otherwise, save the transport so that it can be reused
h.mu.Lock()
+ transport.session = ss
h.transports[transport.SessionID] = transport
h.mu.Unlock()
}
@@ -369,6 +373,9 @@ type StreamableServerTransport struct {
// connection is non-nil if and only if the transport has been connected.
connection *streamableServerConn
+
+ // the server session associated with this transport
+ session *ServerSession
}
// Connect implements the [Transport] interface.
@@ -550,6 +557,19 @@ func (t *StreamableServerTransport) ServeHTTP(w http.ResponseWriter, req *http.R
}
}
+// Close releases resources related to this transport if it has already been connected.
+func (t *StreamableServerTransport) Close() error {
+ var sessionErr, connErr error
+ if t.session != nil {
+ sessionErr = t.session.Close()
+ }
+ if t.connection != nil {
+ connErr = t.connection.Close()
+ }
+
+ return errors.Join(sessionErr, connErr)
+}
+
// serveGET streams messages to a hanging http GET, with stream ID and last
// message parsed from the Last-Event-ID header.
//
Describe alternatives you've considered
Cleaning up resources just with a DELETE handler, but this alternative doesn't allow cleanup in error cases, which means my server will leak memory over time.
Additional context
none
- 主要言語
- Go
- スター
- 5.4k
- フォーク
- 623
- 平均マージ
- 22時間 10分
- マージ済み PR(30日)
- 30
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/go-sdk のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
modelcontextprotocol/go-sdk#1557 ·
メンテナーはふだん 1 日以内に返信
-
Streamable HTTP (2026-07-28): tools/call with an out-of-range number in arguments fails because extractName decodes Arguments into any再び着手できるかも @beiryu が 10 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
modelcontextprotocol/go-sdk#1534 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensions再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンneeds investigation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
modelcontextprotocol/go-sdk#745 · コメント 13 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 18/100
modelcontextprotocol/go-sdk#1555 ·
メンテナーはふだん 1 日以内に返信
-
mcp: 2026-07-28 client runs elicitation and sampling handlers for standalone server requests再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
modelcontextprotocol/go-sdk#1552 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/go-sdk の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
apache/dubbo-admin#1659 ·
メンテナーはふだん 8 日以内に返信
-
taskmanager: a failed v1alpha1 backup leaves the partial backup behind対応中かも @arshil42 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100