Skip to content

Conversation

@codewithkenzo
Copy link

Summary

Adds | undefined to optional callback properties to support TypeScript projects using exactOptionalPropertyTypes: true.

Fixes #1397

Changes

Transport interface (packages/core/src/shared/transport.ts):

  • onclose?: (() => void) | undefined
  • onerror?: ((error: Error) => void) | undefined
  • onmessage?: (<T extends JSONRPCMessage>(...) => void) | undefined

WebStandardStreamableHTTPServerTransportOptions (packages/server/src/server/streamableHttp.ts):

  • sessionIdGenerator?: (() => string) | undefined
  • onsessioninitialized?: ((sessionId: string) => void | Promise<void>) | undefined
  • onsessionclosed?: ((sessionId: string) => void | Promise<void>) | undefined

Why This Pattern

This is the official TypeScript pattern for exactOptionalPropertyTypes compatibility. Used by Radix UI, Material UI, and other major libraries.

Without this fix, users with strict TypeScript configs get errors like:

error TS2379: Argument of type '{ sessionIdGenerator: undefined; ... }'
is not assignable to parameter of type 'WebStandardStreamableHTTPServerTransportOptions' with
'exactOptionalPropertyTypes: true'.
  Types of property 'sessionIdGenerator' are incompatible.
    Type 'undefined' is not assignable to type '() => string'.

Testing

  • pnpm typecheck:all passes
  • ✅ No new test failures (1 pre-existing failure in authExtensions.test.ts unrelated to this change)

Add `| undefined` to optional callback properties to support TypeScript
projects using `exactOptionalPropertyTypes: true`.

Changes:
- Transport interface: onclose, onerror, onmessage
- WebStandardStreamableHTTPServerTransportOptions: sessionIdGenerator,
  onsessioninitialized, onsessionclosed

This is the official TypeScript pattern for exactOptionalPropertyTypes
compatibility, used by Radix UI, Material UI, and other major libraries.

Fixes modelcontextprotocol#1397
@codewithkenzo codewithkenzo requested a review from a team as a code owner February 2, 2026 14:35
@changeset-bot
Copy link

changeset-bot bot commented Feb 2, 2026

🦋 Changeset detected

Latest commit: e943b7a

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

This PR includes changesets to release 5 packages
Name Type
@modelcontextprotocol/core Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/node Patch
@modelcontextprotocol/express Patch
@modelcontextprotocol/hono 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

pkg-pr-new bot commented Feb 2, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: e943b7a

@codewithkenzo
Copy link
Author

Note: This PR is independent from #1433 (onerror callback fix) - they touch different parts of the code:

No conflicts - both can be merged independently.

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: invalid types for sessionIdGenerator in 1.25.2

1 participant