Skip to content

Commit e943b7a

Browse files
committed
fix: support exactOptionalPropertyTypes for optional callbacks
Add `| undefined` to optional callback properties to support TypeScript projects using `exactOptionalPropertyTypes: true`. Changes: - Transport interface: onclose, onerror, onmessage - WebStandardStreamableHTTPServerTransportOptions: sessionIdGenerator, onsessioninitialized, onsessionclosed This is the official TypeScript pattern for exactOptionalPropertyTypes compatibility, used by Radix UI, Material UI, and other major libraries. Fixes #1397
1 parent b0ef89f commit e943b7a

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@modelcontextprotocol/core": patch
3+
"@modelcontextprotocol/server": patch
4+
---
5+
6+
fix: support `exactOptionalPropertyTypes` TypeScript config
7+
8+
Adds `| undefined` to optional callback properties in the `Transport` interface
9+
and `WebStandardStreamableHTTPServerTransportOptions` to support projects using
10+
`exactOptionalPropertyTypes: true` in their tsconfig.
11+
12+
This allows explicitly passing `undefined` for optional callbacks like
13+
`sessionIdGenerator`, `onclose`, `onerror`, and `onmessage`.
14+
15+
Fixes #1397

packages/core/src/shared/transport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +114,7 @@ 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.

packages/server/src/server/streamableHttp.ts

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

8484
/**
8585
* A callback for session initialization events
@@ -88,7 +88,7 @@ export interface WebStandardStreamableHTTPServerTransportOptions {
8888
* and need to keep track of them.
8989
* @param sessionId The generated session ID
9090
*/
91-
onsessioninitialized?: (sessionId: string) => void | Promise<void>;
91+
onsessioninitialized?: ((sessionId: string) => void | Promise<void>) | undefined;
9292

9393
/**
9494
* A callback for session close events
@@ -100,7 +100,7 @@ export interface WebStandardStreamableHTTPServerTransportOptions {
100100
* session open/running.
101101
* @param sessionId The session ID that was closed
102102
*/
103-
onsessionclosed?: (sessionId: string) => void | Promise<void>;
103+
onsessionclosed?: ((sessionId: string) => void | Promise<void>) | undefined;
104104

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

0 commit comments

Comments
 (0)