Skip to content

Commit 54d6929

Browse files
committed
don't need to re-initialize when we reconnect a session
1 parent 1359e9f commit 54d6929

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/client/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ export class Client<
126126

127127
override async connect(transport: Transport, options?: RequestOptions): Promise<void> {
128128
await super.connect(transport);
129-
129+
// When transport sessionId is already set this means we are trying to reconnect.
130+
// In this case we don't need to initialize again.
131+
if (transport.sessionId !== undefined) {
132+
return;
133+
}
130134
try {
131135
const result = await this.request(
132136
{

src/server/streamableHttp.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,10 @@ export class StreamableHTTPServerTransport implements Transport {
331331
const isInitializationRequest = messages.some(
332332
msg => 'method' in msg && msg.method === 'initialize'
333333
);
334-
const mcpSessionId = req.headers["mcp-session-id"] as string | undefined;
335334
if (isInitializationRequest) {
336335
// If it's a server with session management and the session ID is already set we should reject the request
337336
// to avoid re-initialization.
338-
if (this._initialized && this.sessionId !== undefined && mcpSessionId !== this.sessionId) {
337+
if (this._initialized) {
339338
res.writeHead(400).end(JSON.stringify({
340339
jsonrpc: "2.0",
341340
error: {
@@ -357,7 +356,7 @@ export class StreamableHTTPServerTransport implements Transport {
357356
}));
358357
return;
359358
}
360-
this.sessionId = mcpSessionId ?? this.sessionIdGenerator();
359+
this.sessionId = this.sessionIdGenerator();
361360
this._initialized = true;
362361

363362
}

0 commit comments

Comments
 (0)