Skip to content

Commit c725192

Browse files
corvid-agentclaude
andcommitted
fix: add | undefined to optional properties for exactOptionalPropertyTypes support
When TypeScript's `exactOptionalPropertyTypes` is enabled, optional properties typed as `prop?: T` cannot be assigned `undefined` unless the type explicitly includes `| undefined`. This caused TS2420 errors when implementing the Transport interface. Update all optional callback/property types in the Transport interface, TransportSendOptions, and WebStandardStreamableHTTPServerTransportOptions to use the `| undefined` union pattern (e.g., `onclose?: (() => void) | undefined`). Fixes modelcontextprotocol#1314 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 65bbcea commit c725192

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
'@modelcontextprotocol/server': patch
4+
---
5+
6+
Add explicit `| undefined` to optional properties in Transport interface and related option types to support TypeScript's `exactOptionalPropertyTypes` compiler option

‎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 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 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
}

‎packages/server/src/server/streamableHttp.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface WebStandardStreamableHTTPServerTransportOptions {
7777
*
7878
* If not provided, session management is disabled (stateless mode).
7979
*/
80-
sessionIdGenerator?: () => string;
80+
sessionIdGenerator?: (() => string) | undefined;
8181

8282
/**
8383
* A callback for session initialization events
@@ -86,7 +86,7 @@ export interface WebStandardStreamableHTTPServerTransportOptions {
8686
* and need to keep track of them.
8787
* @param sessionId The generated session ID
8888
*/
89-
onsessioninitialized?: (sessionId: string) => void | Promise<void>;
89+
onsessioninitialized?: ((sessionId: string) => void | Promise<void>) | undefined;
9090

9191
/**
9292
* A callback for session close events
@@ -98,7 +98,7 @@ export interface WebStandardStreamableHTTPServerTransportOptions {
9898
* session open/running.
9999
* @param sessionId The session ID that was closed
100100
*/
101-
onsessionclosed?: (sessionId: string) => void | Promise<void>;
101+
onsessionclosed?: ((sessionId: string) => void | Promise<void>) | undefined;
102102

103103
/**
104104
* If true, the server will return JSON responses instead of starting an SSE stream.

0 commit comments

Comments
 (0)