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..db0b4266b4 --- /dev/null +++ b/.changeset/fix-transport-exact-optional-property-types.md @@ -0,0 +1,10 @@ +--- +'@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).