feat: Add auto-reconnect logic for Discord client when session gets disconnected
This commit is contained in:
parent
eb2f5a7da3
commit
8d1040ea73
104
src/transport.ts
104
src/transport.ts
|
@ -217,14 +217,50 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
tag: this.toolContext!.client.user.tag,
|
tag: this.toolContext!.client.user.tag,
|
||||||
} : null
|
} : null
|
||||||
})}`);
|
})}`);
|
||||||
return res.json({
|
|
||||||
jsonrpc: '2.0',
|
// Check if we have a token but not ready - try to force reconnect
|
||||||
error: {
|
if (this.toolContext!.client.token) {
|
||||||
code: -32603,
|
info("Has token but not ready - attempting to force reconnect");
|
||||||
message: 'Discord client not logged in. Please use discord_login tool first.',
|
try {
|
||||||
},
|
// Attempt to force login with existing token
|
||||||
id: req.body?.id || null,
|
await this.toolContext!.client.login(this.toolContext!.client.token);
|
||||||
});
|
info(`Force reconnect successful: ${this.toolContext!.client.isReady()}`);
|
||||||
|
|
||||||
|
// If still not ready after reconnect, return error
|
||||||
|
if (!this.toolContext!.client.isReady()) {
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client reconnect failed. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continue with original request as now logged in
|
||||||
|
info("Reconnected successfully, continuing with original request");
|
||||||
|
} catch (reconnectError) {
|
||||||
|
error(`Reconnect failed: ${reconnectError instanceof Error ? reconnectError.message : String(reconnectError)}`);
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client reconnect failed. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client not logged in. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call appropriate handler based on method
|
// Call appropriate handler based on method
|
||||||
|
@ -308,14 +344,50 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
tag: this.toolContext!.client.user.tag,
|
tag: this.toolContext!.client.user.tag,
|
||||||
} : null
|
} : null
|
||||||
})}`);
|
})}`);
|
||||||
return res.json({
|
|
||||||
jsonrpc: '2.0',
|
// Check if we have a token but not ready - try to force reconnect
|
||||||
error: {
|
if (this.toolContext!.client.token) {
|
||||||
code: -32603,
|
info("Has token but not ready - attempting to force reconnect");
|
||||||
message: 'Discord client not logged in. Please use discord_login tool first.',
|
try {
|
||||||
},
|
// Attempt to force login with existing token
|
||||||
id: req.body?.id || null,
|
await this.toolContext!.client.login(this.toolContext!.client.token);
|
||||||
});
|
info(`Force reconnect successful: ${this.toolContext!.client.isReady()}`);
|
||||||
|
|
||||||
|
// If still not ready after reconnect, return error
|
||||||
|
if (!this.toolContext!.client.isReady()) {
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client reconnect failed. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continue with original request as now logged in
|
||||||
|
info("Reconnected successfully, continuing with original request");
|
||||||
|
} catch (reconnectError) {
|
||||||
|
error(`Reconnect failed: ${reconnectError instanceof Error ? reconnectError.message : String(reconnectError)}`);
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client reconnect failed. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return res.json({
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: 'Discord client not logged in. Please use discord_login tool first.',
|
||||||
|
},
|
||||||
|
id: req.body?.id || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the appropriate handler based on tool name
|
// Call the appropriate handler based on tool name
|
||||||
|
|
Loading…
Reference in New Issue