Skip to content

fix: add explicit | undefined to Transport optional properties (v1.x backport of #1766) - #2814

Open
kishormorol wants to merge 2 commits into
modelcontextprotocol:v1.xfrom
kishormorol:fix/v1-transport-exact-optional-property-types
Open

fix: add explicit | undefined to Transport optional properties (v1.x backport of #1766)#2814
kishormorol wants to merge 2 commits into
modelcontextprotocol:v1.xfrom
kishormorol:fix/v1-transport-exact-optional-property-types

Conversation

@kishormorol

Copy link
Copy Markdown

Backport of #1766 to the v1.x line. Fixes #2083 for v1.

Problem

Under exactOptionalPropertyTypes: true, onclose?: () => void means the property may be absent, but never explicitly undefined. The concrete transports declare these members as accessors typed (() => void) | undefined, so the SDK's own transports are not assignable to the SDK's own Transport interface:

TS2379: Argument of type 'StreamableHTTPServerTransport' is not assignable to parameter
of type 'Transport' with 'exactOptionalPropertyTypes: true'.
  Types of property 'onclose' are incompatible.
    Type '(() => void) | undefined' is not assignable to type '() => void'.

Reproduce with strict + exactOptionalPropertyTypes: true, then server.connect(new StreamableHTTPServerTransport({ sessionIdGenerator: () => randomUUID() })).

Consumers currently have to cast at the call site or disable the flag for their whole project.

Change

Four optional members of Transport gain an explicit | undefined:

onclose?: (() => void) | undefined;
onerror?: ((error: Error) => void) | undefined;
onmessage?: (<T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void) | undefined;
sessionId?: string | undefined;

Source-compatible for every existing consumer: it only permits an explicit undefined that the interface previously rejected. This is the same change already accepted on main in #1766, which has not reached v1.x — so 1.30.0 still ships the issue.

Verification

Happy to adjust or close if a v1 patch release isn't planned — filing it because #2083 notes a backport is what's needed.

Under `exactOptionalPropertyTypes: true`, `onclose?: () => void` means the
property may be absent but never explicitly `undefined`. The concrete transports
declare these members as accessors typed `(() => void) | undefined`, so the SDK's
own transports are not assignable to the SDK's own `Transport` interface and
`server.connect(new StreamableHTTPServerTransport(...))` fails with TS2379.

Consumers currently have to cast at the call site or turn the flag off for their
whole project.

Backport of modelcontextprotocol#1766, already merged on main. Fixes modelcontextprotocol#2083 for the v1 line.
@kishormorol
kishormorol requested a review from a team as a code owner September 14, 2026 06:04
@changeset-bot

changeset-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2450dfe

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

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

pkg-pr-new Bot commented Sep 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@2814

commit: 2450dfe

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.

1 participant