Skip to content

fix(protocol): add RequestAborted error code for AbortSignal cancellation#2177

Open
sakthiveltofficial wants to merge 1 commit into
modelcontextprotocol:mainfrom
sakthiveltofficial:main
Open

fix(protocol): add RequestAborted error code for AbortSignal cancellation#2177
sakthiveltofficial wants to merge 1 commit into
modelcontextprotocol:mainfrom
sakthiveltofficial:main

Conversation

@sakthiveltofficial
Copy link
Copy Markdown

Aborting a request via AbortSignal produced SdkError with code
REQUEST_TIMEOUT — the same code as a genuine timeout — making it
impossible for callers to distinguish a deliberate cancellation from a
real timeout expiry.

Motivation and Context

The cancel() closure inside Protocol._requestWithSchema() wrapped
any non-SdkError abort reason unconditionally as
SdkErrorCode.RequestTimeout. When a caller does:

controller.abort(new DOMException('User cancelled', 'AbortError'));

the DOMException is not an SdkError, so it fell straight into that
generic fallback. The result was that user-initiated cancellations and
real timeouts were indistinguishable:

// Before this fix — fires on BOTH timeout AND manual abort
if (error.code === SdkErrorCode.RequestTimeout) {
  showRetryMessage(); // incorrectly shown on user cancellation too
}

Fixes #2165

How Has This Been Tested?

  • Added a unit test in
    packages/core/test/shared/protocol.test.ts:
    • Sends a request with a 60 s timeout (well beyond the test duration)
    • Aborts immediately via controller.abort(new DOMException('User cancelled', 'AbortError'))
    • Asserts error.code === SdkErrorCode.RequestAborted
    • Asserts error.code !== SdkErrorCode.RequestTimeout
  • Existing timeout test (timeout: 0) continues to assert
    RequestTimeout, confirming the timeout path is unaffected.
  • Full test suite run: 553 tests passed, 0 failed
    pnpm --filter @modelcontextprotocol/core test
    

Breaking Changes

None. RequestAborted is a new enum member. No existing code
checks for it, and the timeout path (SdkErrorCode.RequestTimeout)
is completely unchanged — the timeout handler already passes a typed
SdkError(RequestTimeout, …) which hits the instanceof SdkError
branch and is passed through unmodified.

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

Files changed (3, minimal diff):

packages/core/src/errors/sdkErrors.ts — added one enum member:

/** Request was aborted via AbortSignal */
RequestAborted = 'REQUEST_ABORTED',

packages/core/src/shared/protocol.ts — updated cancel() closure
to distinguish three cases instead of one:

// before
const error = reason instanceof SdkError
  ? reason
  : new SdkError(SdkErrorCode.RequestTimeout, String(reason));

// after
let error: SdkError;
if (reason instanceof SdkError) {
  error = reason;                                          // timeout path unchanged
} else if (reason instanceof DOMException && reason.name === 'AbortError') {
  error = new SdkError(SdkErrorCode.RequestAborted, reason.message);
} else {
  error = new SdkError(SdkErrorCode.RequestAborted, String(reason));
}

packages/core/test/shared/protocol.test.ts — one new test covering
the abort path.

@sakthiveltofficial sakthiveltofficial requested a review from a team as a code owner May 29, 2026 07:38
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 29, 2026

⚠️ No Changeset found

Latest commit: 667b4bc

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 29, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/codemod@2177

@modelcontextprotocol/server

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

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@2177

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/fastify@2177

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@2177

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@2177

commit: 667b4bc

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.

AbortSignal cancellation throws SdkErrorCode.RequestTimeout instead of a distinct abort error

1 participant