Skip to content

Commit 3f3222c

Browse files
Adding support for multiple instances of streamable http tracking the same MCP server. Needed for a multi-instance deployment of MCP using this transport
1 parent fb4d07f commit 3f3222c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/server/streamableHttp.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export interface StreamableHTTPServerTransportOptions {
7979
* Default is false for backwards compatibility.
8080
*/
8181
enableDnsRebindingProtection?: boolean;
82+
83+
/**
84+
* Enables support for multiple instances of the transport.
85+
*/
86+
multiInstance?: boolean;
8287
}
8388

8489
/**
@@ -130,6 +135,7 @@ export class StreamableHTTPServerTransport implements Transport {
130135
private _allowedHosts?: string[];
131136
private _allowedOrigins?: string[];
132137
private _enableDnsRebindingProtection: boolean;
138+
private _multiInstance: boolean;
133139

134140
sessionId?: string;
135141
onclose?: () => void;
@@ -144,6 +150,7 @@ export class StreamableHTTPServerTransport implements Transport {
144150
this._allowedHosts = options.allowedHosts;
145151
this._allowedOrigins = options.allowedOrigins;
146152
this._enableDnsRebindingProtection = options.enableDnsRebindingProtection ?? false;
153+
this._multiInstance = options.multiInstance ?? false;
147154
}
148155

149156
/**
@@ -674,6 +681,9 @@ export class StreamableHTTPServerTransport implements Transport {
674681
const streamId = this._requestToStreamMapping.get(requestId);
675682
const response = this._streamMapping.get(streamId!);
676683
if (!streamId) {
684+
if (this._multiInstance) {
685+
return; // If multi-instance is enabled, we can ignore messages without a stream
686+
}
677687
throw new Error(`No connection established for request ID: ${String(requestId)}`);
678688
}
679689

@@ -701,6 +711,10 @@ export class StreamableHTTPServerTransport implements Transport {
701711

702712
if (allResponsesReady) {
703713
if (!response) {
714+
if (this._multiInstance) {
715+
// If multi-instance is enabled, we can ignore messages without a stream
716+
return;
717+
}
704718
throw new Error(`No connection established for request ID: ${String(requestId)}`);
705719
}
706720
if (this._enableJsonResponse) {

0 commit comments

Comments
 (0)