Skip to content

Conversation

@grimmerk
Copy link
Contributor

@grimmerk grimmerk commented Dec 19, 2025

Replace response.body.cancel() with response.text().catch(() => {}) in client package to prevent indefinite hanging with certain server responses.

Motivation and Context

PRs #1173 and #1214 added body.cancel() calls to fix connection leaks in Cloudflare Workers. However, body.cancel() can hang indefinitely with certain MCP servers (e.g., Sentry MCP during OAuth metadata discovery).

The hang occurs because cancel() attempts to abort the stream, which requires coordination with the server that doesn't always complete.

Using text().catch(() => {}) instead:

  • Always completes - just reads all data
  • Releases the connection - body is fully consumed
  • No stream coordination needed - more reliable than cancel()

In all affected code paths (OAuth metadata discovery, error responses, 202 Accepted, session termination), the response bodies are small, making text() a safe and efficient choice.

This preserves the original fix for Cloudflare Workers connection leaks while eliminating the hanging issue.

How Has This Been Tested?

  • Tested with Sentry MCP (https://mcp.sentry.dev/mcp) which previously hung indefinitely
  • After the fix, OAuth flow proceeds correctly
  • Verified Notion MCP still works correctly

Breaking Changes

None. Drop-in replacement that maintains the same behavior.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

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

Additional context

Fixes regression introduced in #1173 and #1214.

References:

@grimmerk grimmerk requested a review from a team as a code owner December 19, 2025 18:06
@changeset-bot
Copy link

changeset-bot bot commented Dec 19, 2025

⚠️ No Changeset found

Latest commit: 358910d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

mattzcarey
mattzcarey previously approved these changes Dec 19, 2025
Copy link
Contributor

@mattzcarey mattzcarey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

body.cancel() can hang indefinitely with certain server responses
(e.g., Sentry MCP's OAuth flow). Using text?.().catch(() => {}) is safer:
- Always completes (reads all data)
- Releases connection (body consumed)
- No stream coordination needed
- Uses optional chaining for compatibility with mock responses

Fixes regression from modelcontextprotocol#1173 and modelcontextprotocol#1214.
@grimmerk grimmerk force-pushed the fix/replace-body-cancel-with-text-consume branch from c131704 to 358910d Compare December 19, 2025 18:13
@mattzcarey
Copy link
Contributor

Tests are failing here, reckon you could have a look

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 19, 2025

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1320
npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1320

commit: 358910d

@grimmerk
Copy link
Contributor Author

@mattzcarey thanks for the reminder. I've force-pushed it to fix the broken tests.

@grimmerk grimmerk requested a review from mattzcarey December 19, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants