Skip to content

Commit 75d3061

Browse files
committed
Fixed the issue where transport was overwritten when multiple clients connected to the same server
1 parent e0de082 commit 75d3061

File tree

10 files changed

+556
-46
lines changed

10 files changed

+556
-46
lines changed

src/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ async function runServer(port: number | null) {
9191
console.log('Received message');
9292

9393
const sessionId = req.query.sessionId as string;
94-
const transport = servers.map(s => s.transport as SSEServerTransport).find(t => t.sessionId === sessionId);
94+
95+
const transport = servers.reduce((pre, val) => pre.concat(Array.from(val.transportMap.values()) as SSEServerTransport[]), [] as SSEServerTransport[]).find(t => t.sessionId === sessionId);
9596
if (!transport) {
9697
res.status(404).send('Session not found');
9798
return;

src/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Client<
103103
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
104104
*/
105105
public registerCapabilities(capabilities: ClientCapabilities): void {
106-
if (this.transport) {
106+
if (this.transportMap.size > 0) {
107107
throw new Error('Cannot register capabilities after connecting to transport');
108108
}
109109

src/inMemory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class InMemoryTransport implements Transport {
1818
onerror?: (error: Error) => void;
1919
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
2020
sessionId?: string;
21+
onsessionclosed?: ((sessionId: string) => void | Promise<void>) | undefined;
22+
onsessioninitialized?: ((sessionId: string) => void | Promise<void>) | undefined;
2123

2224
/**
2325
* Creates a pair of linked in-memory transports that can communicate with each other. One should be passed to a Client and one to a Server.
@@ -31,6 +33,7 @@ export class InMemoryTransport implements Transport {
3133
}
3234

3335
async start(): Promise<void> {
36+
this.onsessioninitialized?.(this.sessionId!);
3437
// Process any messages that were queued before start was called
3538
while (this._messageQueue.length > 0) {
3639
const queuedMessage = this._messageQueue.shift()!;
@@ -39,6 +42,7 @@ export class InMemoryTransport implements Transport {
3942
}
4043

4144
async close(): Promise<void> {
45+
this.onsessionclosed?.(this.sessionId!);
4246
const other = this._otherTransport;
4347
this._otherTransport = undefined;
4448
await other?.close();

src/server/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class Server<
131131
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
132132
*/
133133
public registerCapabilities(capabilities: ServerCapabilities): void {
134-
if (this.transport) {
134+
if (this.transportMap.size > 0) {
135135
throw new Error('Cannot register capabilities after connecting to transport');
136136
}
137137
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
@@ -277,8 +277,8 @@ export class Server<
277277
return this._capabilities;
278278
}
279279

280-
async ping() {
281-
return this.request({ method: 'ping' }, EmptyResultSchema);
280+
async ping(sessionId?: string) {
281+
return this.request({ method: 'ping' }, EmptyResultSchema, { sessionId });
282282
}
283283

284284
async createMessage(params: CreateMessageRequest['params'], options?: RequestOptions) {
@@ -327,7 +327,7 @@ export class Server<
327327
async sendLoggingMessage(params: LoggingMessageNotification['params'], sessionId?: string) {
328328
if (this._capabilities.logging) {
329329
if (!this.isMessageIgnored(params.level, sessionId)) {
330-
return this.notification({ method: 'notifications/message', params });
330+
return this.notification({ method: 'notifications/message', params }, { sessionId});
331331
}
332332
}
333333
}

src/server/mcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ export class McpServer {
902902
* @returns True if the server is connected
903903
*/
904904
isConnected() {
905-
return this.server.transport !== undefined;
905+
return this.server.transportMap.size > 0;
906906
}
907907

908908
/**

0 commit comments

Comments
 (0)