fix(McpService): improve client connection handling with error logging

This commit is contained in:
Vaayne 2025-04-08 15:01:43 +08:00 committed by 亢奋猫
parent 97c1d67cbf
commit 037027f1f4

View File

@ -46,18 +46,22 @@ class McpService {
// Check if we already have a client for this server configuration // Check if we already have a client for this server configuration
const existingClient = this.clients.get(serverKey) const existingClient = this.clients.get(serverKey)
if (existingClient) { if (existingClient) {
// Check if the existing client is still connected try {
const pingResult = await existingClient.ping() // Check if the existing client is still connected
Logger.info(`[MCP] Ping result for ${server.name}:`, pingResult) const pingResult = await existingClient.ping()
// If the ping fails, remove the client from the cache Logger.info(`[MCP] Ping result for ${server.name}:`, pingResult)
// and create a new one // If the ping fails, remove the client from the cache
if (!pingResult) { // and create a new one
if (!pingResult) {
this.clients.delete(serverKey)
} else {
return existingClient
}
} catch (error) {
Logger.error(`[MCP] Error pinging server ${server.name}:`, error)
this.clients.delete(serverKey) this.clients.delete(serverKey)
} else {
return existingClient
} }
} }
// Create new client instance for each connection // Create new client instance for each connection
const client = new Client({ name: 'Cherry Studio', version: app.getVersion() }, { capabilities: {} }) const client = new Client({ name: 'Cherry Studio', version: app.getVersion() }, { capabilities: {} })