From 827f90ba0c13edb546028df42fadc9f1211a4ff2 Mon Sep 17 00:00:00 2001 From: Guglielmo Colombo Date: Fri, 17 Jul 2026 13:40:39 +0200 Subject: [PATCH] mcp: allow opt-in to cancel handler ctx on aborted POST (#1099) Fixes #1093 --- mcp/streamable.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mcp/streamable.go b/mcp/streamable.go index 7e0dddd..37adfda 100644 --- a/mcp/streamable.go +++ b/mcp/streamable.go @@ -207,6 +207,17 @@ type StreamableHTTPOptions struct { // A negative value disables the limit entirely; do not use this on // servers exposed to untrusted clients. MaxRequestBodyBytes int64 + + // PropagateRequestCancellation, when true, ties the in-flight handler + // context to the originating HTTP request's context. Only applies to + // requests using the >= 2026-07-28 protocol, where the POST is the whole + // request lifecycle. + // The handler context cancels whenever the HTTP request context does as the + // response can no longer be delivered, so cancelling the handler is safe. + // + // Requests using older protocol versions (including those routed through + // the allowsessionsinstateless compatibility path) are unaffected. + PropagateRequestCancellation bool } // DefaultMaxRequestBodyBytes is the default value used for @@ -417,7 +428,7 @@ func (h *StreamableHTTPHandler) serveStateless(w http.ResponseWriter, req *http. EventStore: h.opts.EventStore, jsonResponse: h.opts.JSONResponse, logger: h.opts.Logger, - shouldPropagateCancellation: info.isSubscriptionsListen && info.usesNewProtocol, + shouldPropagateCancellation: info.usesNewProtocol && (info.isSubscriptionsListen || h.opts.PropagateRequestCancellation), } session, err := connectStreamable(req.Context(), server, transport, info.opts)