Skip to content

fix: support exactOptionalPropertyTypes in Transport interface#1736

Open
naman10parikh wants to merge 1 commit intomodelcontextprotocol:mainfrom
naman10parikh:fix/exact-optional-property-types
Open

fix: support exactOptionalPropertyTypes in Transport interface#1736
naman10parikh wants to merge 1 commit intomodelcontextprotocol:mainfrom
naman10parikh:fix/exact-optional-property-types

Conversation

@naman10parikh
Copy link

Summary

Adds explicit | undefined to all optional callback properties on the Transport interface and all implementing classes/test mocks. This allows consumers with exactOptionalPropertyTypes: true in their tsconfig.json to use the SDK without type errors.

Problem

When a consumer enables exactOptionalPropertyTypes (a strict TypeScript config), class properties declared as onclose?: () => void are initialized to undefined by default. Under exactOptionalPropertyTypes, this means the property type becomes (() => void) | undefined, which is not assignable to () => void — causing TS2420 errors:

error TS2420: Class 'StreamableHTTPServerTransport' incorrectly implements interface 'Transport'.
  Types of property 'onclose' are incompatible.
    Type '(() => void) | undefined' is not assignable to type '() => void'.

Solution

Add | undefined to all optional properties in the Transport interface and all implementing classes:

// Before
onclose?: () => void;

// After
onclose?: (() => void) | undefined;

This is fully backwards-compatible — existing code that doesn't use exactOptionalPropertyTypes is unaffected.

Files Changed

  • packages/core/src/shared/transport.ts — Transport interface (6 properties)
  • packages/client/src/client/sse.ts — SSEClientTransport
  • packages/client/src/client/streamableHttp.ts — StreamableHTTPClientTransport
  • packages/client/src/client/websocket.ts — WebSocketClientTransport
  • packages/client/src/client/stdio.ts — StdioClientTransport
  • packages/core/src/util/inMemory.ts — InMemoryTransport
  • packages/server/src/server/stdio.ts — StdioServerTransport
  • packages/server/src/server/streamableHttp.ts — WebStandardStreamableHTTPServerTransport
  • packages/core/test/shared/protocol.test.ts — MockTransport
  • packages/core/test/shared/protocolTransportHandling.test.ts — MockTransport

Testing

  • pnpm build:all passes with 0 errors
  • No runtime behavior changes — this is a type-only fix

Fixes #1314

Adds explicit `| undefined` to all optional callback properties on the
Transport interface and all implementing classes. This allows consumers
with `exactOptionalPropertyTypes: true` in their tsconfig.json to use
the SDK without type errors.

Without this fix, class properties initialized as `undefined` are
incompatible with the interface under exactOptionalPropertyTypes, since
`T | undefined` is not assignable to `T` when the property is present.

Fixes modelcontextprotocol#1314
@naman10parikh naman10parikh requested a review from a team as a code owner March 23, 2026 21:46
@changeset-bot
Copy link

changeset-bot bot commented Mar 23, 2026

⚠️ No Changeset found

Latest commit: 70178e6

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

pkg-pr-new bot commented Mar 23, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 70178e6

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