Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/fix-transport-exact-optional-property-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@modelcontextprotocol/sdk': patch
---

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.

This is a backport of the same fix already merged on `main` in #1766, and resolves #2083 for the v1 line.
16 changes: 8 additions & 8 deletions src/shared/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -189,14 +189,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.
Expand All @@ -205,15 +205,15 @@ export interface Transport {
*
* The requestInfo can be used to get the original request information (headers, etc.)
*/
onmessage?: <T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void;
onmessage?: (<T extends JSONRPCMessage>(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;
}
Loading