Skip to content

Commit 827f90b

Browse files
mcp: allow opt-in to cancel handler ctx on aborted POST (#1099)
Fixes #1093
1 parent 5cb8339 commit 827f90b

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

mcp/streamable.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ type StreamableHTTPOptions struct {
207207
// A negative value disables the limit entirely; do not use this on
208208
// servers exposed to untrusted clients.
209209
MaxRequestBodyBytes int64
210+
211+
// PropagateRequestCancellation, when true, ties the in-flight handler
212+
// context to the originating HTTP request's context. Only applies to
213+
// requests using the >= 2026-07-28 protocol, where the POST is the whole
214+
// request lifecycle.
215+
// The handler context cancels whenever the HTTP request context does as the
216+
// response can no longer be delivered, so cancelling the handler is safe.
217+
//
218+
// Requests using older protocol versions (including those routed through
219+
// the allowsessionsinstateless compatibility path) are unaffected.
220+
PropagateRequestCancellation bool
210221
}
211222

212223
// DefaultMaxRequestBodyBytes is the default value used for
@@ -417,7 +428,7 @@ func (h *StreamableHTTPHandler) serveStateless(w http.ResponseWriter, req *http.
417428
EventStore: h.opts.EventStore,
418429
jsonResponse: h.opts.JSONResponse,
419430
logger: h.opts.Logger,
420-
shouldPropagateCancellation: info.isSubscriptionsListen && info.usesNewProtocol,
431+
shouldPropagateCancellation: info.usesNewProtocol && (info.isSubscriptionsListen || h.opts.PropagateRequestCancellation),
421432
}
422433

423434
session, err := connectStreamable(req.Context(), server, transport, info.opts)

0 commit comments

Comments
 (0)