Skip to content

Commit 1497412

Browse files
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
1 parent 40174d2 commit 1497412

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
---
4+
5+
Add explicit `| undefined` to optional properties on the `Transport` interface and `TransportSendOptions` (`onclose`, `onerror`, `onmessage`, `sessionId`, `setProtocolVersion`, `setSupportedProtocolVersions`, `onresumptiontoken`).
6+
7+
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.
8+
9+
Workaround for older SDK versions: enable `skipLibCheck: true` in your tsconfig.

packages/core/src/shared/transport.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export type TransportSendOptions = {
6666
*
6767
* This allows clients to persist the latest token for potential reconnection.
6868
*/
69-
onresumptiontoken?: (token: string) => void;
69+
onresumptiontoken?: ((token: string) => void) | undefined;
7070
};
7171
/**
7272
* Describes the minimal contract for an MCP transport that a client or server can communicate over.
@@ -98,14 +98,14 @@ export interface Transport {
9898
*
9999
* This should be invoked when {@linkcode Transport.close | close()} is called as well.
100100
*/
101-
onclose?: () => void;
101+
onclose?: (() => void) | undefined;
102102

103103
/**
104104
* Callback for when an error occurs.
105105
*
106106
* Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
107107
*/
108-
onerror?: (error: Error) => void;
108+
onerror?: ((error: Error) => void) | undefined;
109109

110110
/**
111111
* Callback for when a message (request or response) is received over the connection.
@@ -114,21 +114,21 @@ export interface Transport {
114114
*
115115
* The {@linkcode MessageExtraInfo.requestInfo | requestInfo} can be used to get the original request information (headers, etc.)
116116
*/
117-
onmessage?: <T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void;
117+
onmessage?: (<T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void) | undefined;
118118

119119
/**
120120
* The session ID generated for this connection.
121121
*/
122-
sessionId?: string;
122+
sessionId?: string | undefined;
123123

124124
/**
125125
* Sets the protocol version used for the connection (called when the initialize response is received).
126126
*/
127-
setProtocolVersion?: (version: string) => void;
127+
setProtocolVersion?: ((version: string) => void) | undefined;
128128

129129
/**
130130
* Sets the supported protocol versions for header validation (called during connect).
131131
* This allows the server to pass its supported versions to the transport.
132132
*/
133-
setSupportedProtocolVersions?: (versions: string[]) => void;
133+
setSupportedProtocolVersions?: ((versions: string[]) => void) | undefined;
134134
}

0 commit comments

Comments
 (0)