From 644995dd7690a559997c10c086450a4974657d98 Mon Sep 17 00:00:00 2001 From: 1600822305 <1600822305@qq.com> Date: Fri, 11 Apr 2025 03:53:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.ts | 11 +++++++++++ src/main/services/MCPService.ts | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/index.ts b/src/main/index.ts index ded41250cc..d9f92fb348 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,10 +3,12 @@ import { replaceDevtoolsFont } from '@main/utils/windowUtil' import { IpcChannel } from '@shared/IpcChannel' import { app, ipcMain } from 'electron' import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer' +import Logger from 'electron-log' import { registerIpc } from './ipc' import { configManager } from './services/ConfigManager' import { registerMsTTSIpcHandlers } from './services/MsTTSIpcHandler' +import mcpService from './services/MCPService' import { CHERRY_STUDIO_PROTOCOL, handleProtocolUrl, registerProtocolClient } from './services/ProtocolClient' import { registerShortcuts } from './services/ShortcutService' import { TrayService } from './services/TrayService' @@ -96,6 +98,15 @@ if (!app.requestSingleInstanceLock()) { app.isQuitting = true }) + app.on('will-quit', async () => { + // event.preventDefault() + try { + await mcpService.cleanup() + } catch (error) { + Logger.error('Error cleaning up MCP service:', error) + } + }) + // In this file you can include the rest of your app"s specific main process // code. You can also put them in separate files and require them here. } diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index ecfa14a83c..63a4ffb240 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -322,6 +322,30 @@ class McpService { // 转换回字符串 return Array.from(existingPaths).join(pathSeparator) } + + /** + * 清理所有MCP客户端连接 + */ + public async cleanup(): Promise { + Logger.info('[MCP] Cleaning up all MCP clients...') + const closePromises: Promise[] = [] + + // 关闭所有客户端连接 + for (const [serverKey, client] of this.clients.entries()) { + try { + Logger.info(`[MCP] Closing client for server: ${serverKey}`) + closePromises.push(client.close()) + } catch (error) { + Logger.error(`[MCP] Error closing client for server: ${serverKey}`, error) + } finally { + this.clients.delete(serverKey) + } + } + + // 等待所有关闭操作完成 + await Promise.allSettled(closePromises) + Logger.info('[MCP] All MCP clients cleaned up') + } } export default new McpService()