diff --git a/src/main/index.ts b/src/main/index.ts index 816695bb..51022257 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,9 +3,11 @@ 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 mcpService from './services/MCPService' import { CHERRY_STUDIO_PROTOCOL, handleProtocolUrl, registerProtocolClient } from './services/ProtocolClient' import { registerShortcuts } from './services/ShortcutService' import { TrayService } from './services/TrayService' @@ -92,6 +94,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 ecfa14a8..782b9cc9 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -39,6 +39,7 @@ class McpService { this.removeServer = this.removeServer.bind(this) this.restartServer = this.restartServer.bind(this) this.stopServer = this.stopServer.bind(this) + this.cleanup = this.cleanup.bind(this) } async initClient(server: MCPServer): Promise { @@ -205,6 +206,16 @@ class McpService { await this.initClient(server) } + async cleanup() { + for (const [key] of this.clients) { + try { + await this.closeClient(key) + } catch (error) { + Logger.error(`[MCP] Failed to close client: ${error}`) + } + } + } + async listTools(_: Electron.IpcMainInvokeEvent, server: MCPServer) { const client = await this.initClient(server) const serverKey = this.getServerKey(server) @@ -324,4 +335,5 @@ class McpService { } } -export default new McpService() +const mcpService = new McpService() +export default mcpService