From bef4b728ec12c619df6f7e15ddbd0b1f0978cf80 Mon Sep 17 00:00:00 2001 From: Kishor Morol Date: Mon, 14 Sep 2026 02:03:00 -0400 Subject: [PATCH 1/3] fix: add explicit | undefined to Transport optional properties 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 #1766, already merged on main. Fixes #2083 for the v1 line. --- .../fix-transport-exact-optional-property-types.md | 9 +++++++++ src/shared/transport.ts | 8 ++++---- 2 files changed, 13 insertions(+), 4 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 0000000000..c79240ebaf --- /dev/null +++ b/.changeset/fix-transport-exact-optional-property-types.md @@ -0,0 +1,9 @@ +--- +'@modelcontextprotocol/sdk': patch +--- + +Add explicit `| undefined` to the optional properties of the `Transport` interface (`onclose`, `onerror`, `onmessage`, `sessionId`). + +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 were not assignable to the SDK's own `Transport` interface, and `server.connect(new StreamableHTTPServerTransport(...))` failed with TS2379. Consumers had to either cast at the call site or disable the flag for their whole project. + +This is a backport of the same fix already merged on `main` in #1766, and resolves #2083 for the v1 line. diff --git a/src/shared/transport.ts b/src/shared/transport.ts index f9b21bed32..447a379434 100644 --- a/src/shared/transport.ts +++ b/src/shared/transport.ts @@ -98,14 +98,14 @@ export interface Transport { * * This should be invoked when 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,12 +114,12 @@ export interface Transport { * * The 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). From 2450dfe1ff02a9145085d60dff1da1b737b4859b Mon Sep 17 00:00:00 2001 From: Kishor Morol Date: Mon, 14 Sep 2026 02:14:43 -0400 Subject: [PATCH 2/3] chore: format changeset with prettier --- .changeset/fix-transport-exact-optional-property-types.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/fix-transport-exact-optional-property-types.md b/.changeset/fix-transport-exact-optional-property-types.md index c79240ebaf..db0b4266b4 100644 --- a/.changeset/fix-transport-exact-optional-property-types.md +++ b/.changeset/fix-transport-exact-optional-property-types.md @@ -4,6 +4,7 @@ Add explicit `| undefined` to the optional properties of the `Transport` interface (`onclose`, `onerror`, `onmessage`, `sessionId`). -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 were not assignable to the SDK's own `Transport` interface, and `server.connect(new StreamableHTTPServerTransport(...))` failed with TS2379. Consumers had to either cast at the call site or disable the flag for their whole project. +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 were not assignable to the +SDK's own `Transport` interface, and `server.connect(new StreamableHTTPServerTransport(...))` failed with TS2379. Consumers had to either cast at the call site or disable the flag for their whole project. This is a backport of the same fix already merged on `main` in #1766, and resolves #2083 for the v1 line. From 4d622126831f4ef31a217dd14b5364adc601ec38 Mon Sep 17 00:00:00 2001 From: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> Date: Mon, 5 Oct 2026 12:53:35 +0000 Subject: [PATCH 3/3] fix: cover the remaining optional members, as on main Adds '| undefined' to setProtocolVersion on Transport and to the three members of TransportSendOptions, so the v1 interface matches what #1766 did on main. The changeset names all of them. --- .changeset/fix-transport-exact-optional-property-types.md | 2 +- src/shared/transport.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.changeset/fix-transport-exact-optional-property-types.md b/.changeset/fix-transport-exact-optional-property-types.md index db0b4266b4..9b735ff1cd 100644 --- a/.changeset/fix-transport-exact-optional-property-types.md +++ b/.changeset/fix-transport-exact-optional-property-types.md @@ -2,7 +2,7 @@ '@modelcontextprotocol/sdk': patch --- -Add explicit `| undefined` to the optional properties of the `Transport` interface (`onclose`, `onerror`, `onmessage`, `sessionId`). +Add explicit `| undefined` to the optional properties of the `Transport` interface (`onclose`, `onerror`, `onmessage`, `sessionId`, `setProtocolVersion`) and of `TransportSendOptions` (`relatedRequestId`, `resumptionToken`, `onresumptiontoken`). 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 were not assignable to the SDK's own `Transport` interface, and `server.connect(new StreamableHTTPServerTransport(...))` failed with TS2379. Consumers had to either cast at the call site or disable the flag for their whole project. diff --git a/src/shared/transport.ts b/src/shared/transport.ts index a1c2d3151e..2be18ae85b 100644 --- a/src/shared/transport.ts +++ b/src/shared/transport.ts @@ -143,21 +143,21 @@ export type TransportSendOptions = { /** * If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with. */ - relatedRequestId?: RequestId; + relatedRequestId?: RequestId | undefined; /** * The resumption token used to continue long-running requests that were interrupted. * * This allows clients to reconnect and continue from where they left off, if supported by the transport. */ - resumptionToken?: string; + resumptionToken?: string | undefined; /** * A callback that is invoked when the resumption token changes, if supported by the transport. * * 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. @@ -215,5 +215,5 @@ export interface Transport { /** * Sets the protocol version used for the connection (called when the initialize response is received). */ - setProtocolVersion?: (version: string) => void; + setProtocolVersion?: ((version: string) => void) | undefined; }