Skip to content

fix(transport): cancel pending modern HTTP requests - #1194

Draft
lucarlig wants to merge 1 commit into
modelcontextprotocol:mainfrom
lucarlig:agent/fix-modern-http-cancellation
Draft

fix(transport): cancel pending modern HTTP requests#1194
lucarlig wants to merge 1 commit into
modelcontextprotocol:mainfrom
lucarlig:agent/fix-modern-http-cancellation

Conversation

@lucarlig

Copy link
Copy Markdown

Allow modern Streamable HTTP requests to be cancelled before their POST returns the first response event. The client worker stays responsive while request POSTs are in flight, and the same per-request cancellation token continues to own any returned SSE stream.

Motivation and Context

Fixes #1193.

RequestHandle::cancel() currently waits forever when a 2026-07-28 request POST is still waiting for its response stream. The worker awaits the POST inline, so it cannot process the cancellation that should close that same request.

This is required by the modern MCP transport rules:

How Has This Been Tested?

The new integration test uses an RMCP client and server over a real Axum HTTP listener. Before the fix, it fails after five seconds because RequestHandle::cancel() remains pending. With the fix, cancellation returns and the server's RequestContext::ct fires.

  • cargo +nightly fmt --all -- --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-features — 783 passed, 24 ignored
  • Full RMCP suite with all non-local features — 1,093 passed, 13 ignored
  • Focused disconnect suite — 3 passed

Breaking Changes

None. This fixes modern Streamable HTTP cancellation behavior without changing the public API. Legacy lifecycle, session, and re-initialization paths are unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The implementation uses RMCP's existing Tokio JoinSet and CancellationToken; it adds no dependency. A modern request's token is registered before its POST starts. Cancelling the request drops the pending HTTP future, and if the POST returns an SSE stream first, that same token controls the stream as before. Response-versus-cancellation races keep the existing behavior of ignoring a response after cancellation.

Issues #857 and PR #967 added the matching server-side disconnect handling. This change makes RMCP's public client cancellation API trigger that path even before response headers or the first SSE event arrive.

Signed-off-by: lucarlig <luca.carlig@ibm.com>
@github-actions github-actions Bot added T-dependencies Dependencies related changes T-test Testing related changes T-config Configuration file changes T-core Core library changes T-transport Transport layer changes labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-config Configuration file changes T-core Core library changes T-dependencies Dependencies related changes T-test Testing related changes T-transport Transport layer changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streamable-http-client: RequestHandle::cancel hangs before response stream starts

1 participant