Skip to content

Commit 48aba0d

Browse files
fix(core): add explicit | undefined to Transport interface optional properties (#1766)
1 parent 9d924b1 commit 48aba0d

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ export type TransportSendOptions = {
5252
/**
5353
* If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
5454
*/
55-
relatedRequestId?: RequestId;
55+
relatedRequestId?: RequestId | undefined;
5656

5757
/**
5858
* The resumption token used to continue long-running requests that were interrupted.
5959
*
6060
* This allows clients to reconnect and continue from where they left off, if supported by the transport.
6161
*/
62-
resumptionToken?: string;
62+
resumptionToken?: string | undefined;
6363

6464
/**
6565
* A callback that is invoked when the resumption token changes, if supported by the transport.
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)