feat: Add auto-reconnect logic for Discord client when session gets disconnected
This commit is contained in:
parent
eb2f5a7da3
commit
8d1040ea73
|
@ -217,6 +217,41 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
tag: this.toolContext!.client.user.tag,
|
tag: this.toolContext!.client.user.tag,
|
||||||
} : null
|
} : null
|
||||||
})}`);
|
})}`);
|
||||||
|
|
||||||
|
// Check if we have a token but not ready - try to force reconnect
|
||||||
|
if (this.toolContext!.client.token) {
|
||||||
|
info("Has token but not ready - attempting to force reconnect");
|
||||||
|
try {
|
||||||
|
// Attempt to force login with existing token
|
||||||
|
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({
|
return res.json({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
error: {
|
error: {
|
||||||
|
@ -226,6 +261,7 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
id: req.body?.id || null,
|
id: req.body?.id || null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Call appropriate handler based on method
|
// Call appropriate handler based on method
|
||||||
switch (method) {
|
switch (method) {
|
||||||
|
@ -308,6 +344,41 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
tag: this.toolContext!.client.user.tag,
|
tag: this.toolContext!.client.user.tag,
|
||||||
} : null
|
} : null
|
||||||
})}`);
|
})}`);
|
||||||
|
|
||||||
|
// Check if we have a token but not ready - try to force reconnect
|
||||||
|
if (this.toolContext!.client.token) {
|
||||||
|
info("Has token but not ready - attempting to force reconnect");
|
||||||
|
try {
|
||||||
|
// Attempt to force login with existing token
|
||||||
|
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({
|
return res.json({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
error: {
|
error: {
|
||||||
|
@ -317,6 +388,7 @@ export class StreamableHttpTransport implements MCPTransport {
|
||||||
id: req.body?.id || null,
|
id: req.body?.id || null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Call the appropriate handler based on tool name
|
// Call the appropriate handler based on tool name
|
||||||
switch (toolName) {
|
switch (toolName) {
|
||||||
|
|
Loading…
Reference in New Issue