Skip to content

fix(core): add | undefined to Transport optional callbacks for exactOptionalPropertyTypes compatibility#1661

Open
rechedev9 wants to merge 2 commits intomodelcontextprotocol:mainfrom
rechedev9:fix/exact-optional-property-types-1314
Open

fix(core): add | undefined to Transport optional callbacks for exactOptionalPropertyTypes compatibility#1661
rechedev9 wants to merge 2 commits intomodelcontextprotocol:mainfrom
rechedev9:fix/exact-optional-property-types-1314

Conversation

@rechedev9
Copy link

Summary

Fixes TS2420 errors when implementing the Transport interface in projects with exactOptionalPropertyTypes: true enabled.

The five optional callback/method properties on Transport previously lacked explicit | undefined:

// Before
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: <T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void;
setProtocolVersion?: (version: string) => void;
setSupportedProtocolVersions?: (versions: string[]) => void;

With exactOptionalPropertyTypes: true, TypeScript treats T | undefined and T as distinct in optional property contexts. A concrete class that declares onclose?: (() => void) | undefined is not assignable to an interface that declares onclose?: () => void under this flag, producing TS2420.

The fix adds explicit | undefined to all five optional members:

// After
onclose?: (() => void) | undefined;
onerror?: ((error: Error) => void) | undefined;
onmessage?: (<T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void) | undefined;
setProtocolVersion?: ((version: string) => void) | undefined;
setSupportedProtocolVersions?: ((versions: string[]) => void) | undefined;

This is backwards-compatible: projects not using exactOptionalPropertyTypes are unaffected.

Breaking Changes

None.

Test plan

  • Added packages/core/test/shared/transport.types.test.ts — compile-time type test verifying a class with explicit | undefined callbacks implements Transport without errors
  • pnpm typecheck:all passes clean across all packages
  • All 441 core tests pass

Fixes #1314

…xactOptionalPropertyTypes compatibility

Resolves TS2420 errors when users implement the Transport interface in
projects with `exactOptionalPropertyTypes: true` enabled. Optional
callback properties (onclose, onerror, onmessage, setProtocolVersion,
setSupportedProtocolVersions) now include explicit `| undefined` in
their types, satisfying the stricter assignability rules.

Fixes modelcontextprotocol#1314
@rechedev9 rechedev9 requested a review from a team as a code owner March 11, 2026 18:19
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 11, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 08a2f28

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: 3052513

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

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.

error TS2420: Class 'StreamableHTTPServerTransport' incorrectly implements interface 'Transport'.

1 participant