don't need to re-initialize when we reconnect a session

This commit is contained in:
ihrpr 2025-04-15 11:58:43 +01:00
commit 54d6929149
No known key found for this signature in database
2 changed files with 7 additions and 4 deletions

View file

@ -126,7 +126,11 @@ export class Client<
override async connect(transport: Transport, options?: RequestOptions): Promise<void> {
await super.connect(transport);
// When transport sessionId is already set this means we are trying to reconnect.
// In this case we don't need to initialize again.
if (transport.sessionId !== undefined) {
return;
}
try {
const result = await this.request(
{

View file

@ -331,11 +331,10 @@ export class StreamableHTTPServerTransport implements Transport {
const isInitializationRequest = messages.some(
msg => 'method' in msg && msg.method === 'initialize'
);
const mcpSessionId = req.headers["mcp-session-id"] as string | undefined;
if (isInitializationRequest) {
// If it's a server with session management and the session ID is already set we should reject the request
// to avoid re-initialization.
if (this._initialized && this.sessionId !== undefined && mcpSessionId !== this.sessionId) {
if (this._initialized) {
res.writeHead(400).end(JSON.stringify({
jsonrpc: "2.0",
error: {
@ -357,7 +356,7 @@ export class StreamableHTTPServerTransport implements Transport {
}));
return;
}
this.sessionId = mcpSessionId ?? this.sessionIdGenerator();
this.sessionId = this.sessionIdGenerator();
this._initialized = true;
}