Skip to content

fix(core): skip cancellation notification for initialize requests#1932

Open
ameenalkhaldi wants to merge 1 commit intomodelcontextprotocol:mainfrom
ameenalkhaldi:fix/initialize-cancellation-notification
Open

fix(core): skip cancellation notification for initialize requests#1932
ameenalkhaldi wants to merge 1 commit intomodelcontextprotocol:mainfrom
ameenalkhaldi:fix/initialize-cancellation-notification

Conversation

@ameenalkhaldi
Copy link
Copy Markdown

Summary

Fixes #998.

Per the MCP specification, clients must not cancel the initialize request:

A client MUST NOT attempt to cancel its initialize request.
(cancellation.mdx §2, mirrored in packages/core/src/types/types.ts)

Before this change, when Client.connect() received an AbortSignal that later aborted, or when the initialize request timed out, Protocol._requestWithSchema unconditionally sent notifications/cancelled to the server for the in-flight initialize request.

Fix

In cancel() inside _requestWithSchema (packages/core/src/shared/protocol.ts), guard the outbound notifications/cancelled send on request.method !== 'initialize'. The abort/reject path is unchanged — the caller's promise still rejects with the supplied reason — only the wire notification is suppressed for initialize.

Tests

Added three cases in packages/core/test/shared/protocol.test.ts under a new initialize request cancellation describe block:

  • aborting an initialize request does not send notifications/cancelled
  • timing out an initialize request does not send notifications/cancelled
  • aborting a non-initialize request still sends notifications/cancelled (regression guard)

Test plan

  • pnpm typecheck:all
  • pnpm lint:all
  • pnpm test:all (all packages green, 492/492 in core)
  • pnpm build:all

Per the MCP spec, clients must not cancel the `initialize` request.
When the caller aborts or the request times out during `connect()`,
the promise still rejects locally, but `notifications/cancelled` is
no longer emitted on the wire.

Fixes modelcontextprotocol#998
@ameenalkhaldi ameenalkhaldi requested a review from a team as a code owner April 19, 2026 19:22
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 19, 2026

🦋 Changeset detected

Latest commit: 52478f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/core Patch

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 19, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@1932

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@1932

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@1932

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@1932

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@1932

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@1932

commit: 52478f7

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.

Bug Report: Initialize Request Cancellation Violation

1 participant