From 14974127630b7d80d42a5ca450b2c798f25999fc Mon Sep 17 00:00:00 2001 From: Felix Weinberger Date: Thu, 26 Mar 2026 16:01:52 +0000 Subject: [PATCH] fix(core): add explicit | undefined to Transport interface optional properties Adds | undefined to optional properties on the Transport interface and TransportSendOptions so the interface matches what tsc emits in .d.ts for implementing classes. This fixes TS2420 errors for consumers using exactOptionalPropertyTypes: true without skipLibCheck. Fixes #1314 --- .../fix-transport-exact-optional-property-types.md | 9 +++++++++ packages/core/src/shared/transport.ts | 14 +++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 .changeset/fix-transport-exact-optional-property-types.md diff --git a/.changeset/fix-transport-exact-optional-property-types.md b/.changeset/fix-transport-exact-optional-property-types.md new file mode 100644 index 000000000..c3187db8a --- /dev/null +++ b/.changeset/fix-transport-exact-optional-property-types.md @@ -0,0 +1,9 @@ +--- +'@modelcontextprotocol/core': patch +--- + +Add explicit `| undefined` to optional properties on the `Transport` interface and `TransportSendOptions` (`onclose`, `onerror`, `onmessage`, `sessionId`, `setProtocolVersion`, `setSupportedProtocolVersions`, `onresumptiontoken`). + +This fixes TS2420 errors for consumers using `exactOptionalPropertyTypes: true` without `skipLibCheck`, where the emitted `.d.ts` for implementing classes included `| undefined` but the interface did not. + +Workaround for older SDK versions: enable `skipLibCheck: true` in your tsconfig. diff --git a/packages/core/src/shared/transport.ts b/packages/core/src/shared/transport.ts index 889b319a9..6a78dae80 100644 --- a/packages/core/src/shared/transport.ts +++ b/packages/core/src/shared/transport.ts @@ -66,7 +66,7 @@ export type TransportSendOptions = { * * This allows clients to persist the latest token for potential reconnection. */ - onresumptiontoken?: (token: string) => void; + onresumptiontoken?: ((token: string) => void) | undefined; }; /** * Describes the minimal contract for an MCP transport that a client or server can communicate over. @@ -98,14 +98,14 @@ export interface Transport { * * This should be invoked when {@linkcode Transport.close | close()} is called as well. */ - onclose?: () => void; + onclose?: (() => void) | undefined; /** * Callback for when an error occurs. * * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band. */ - onerror?: (error: Error) => void; + onerror?: ((error: Error) => void) | undefined; /** * Callback for when a message (request or response) is received over the connection. @@ -114,21 +114,21 @@ export interface Transport { * * The {@linkcode MessageExtraInfo.requestInfo | requestInfo} can be used to get the original request information (headers, etc.) */ - onmessage?: (message: T, extra?: MessageExtraInfo) => void; + onmessage?: ((message: T, extra?: MessageExtraInfo) => void) | undefined; /** * The session ID generated for this connection. */ - sessionId?: string; + sessionId?: string | undefined; /** * Sets the protocol version used for the connection (called when the initialize response is received). */ - setProtocolVersion?: (version: string) => void; + setProtocolVersion?: ((version: string) => void) | undefined; /** * Sets the supported protocol versions for header validation (called during connect). * This allows the server to pass its supported versions to the transport. */ - setSupportedProtocolVersions?: (versions: string[]) => void; + setSupportedProtocolVersions?: ((versions: string[]) => void) | undefined; }