@@ -18,7 +18,7 @@ const activeClients: Map<string, MCPClient> = new Map()
18
18
export function initMCPHandlers ( ) : void {
19
19
// Test connection to a server
20
20
ipcMain . handle ( 'mcp:test-connection' , async ( _ , serverConfig : MCPServer , serverName : string ) => {
21
- console . log ( `Testing connection to MCP server: ${ serverName } ` )
21
+ console . log ( `[MCP] Testing connection to MCP server: ${ serverName } ` )
22
22
try {
23
23
const client = new MCPClient ( )
24
24
const connected = await client . connectToServer ( serverConfig )
@@ -60,16 +60,16 @@ export function initMCPHandlers(): void {
60
60
61
61
// Call a tool on a specific server
62
62
ipcMain . handle ( 'mcp:call-tool' , async ( _ , serverName : string , toolName : string , toolArgs : any ) => {
63
- console . log ( `Calling tool ${ toolName } on server ${ serverName } ` )
63
+ console . log ( `[MCP] Calling tool ${ toolName } on server ${ serverName } ` )
64
64
65
65
try {
66
66
const client = activeClients . get ( serverName )
67
67
if ( ! client ) {
68
- // 如果服务器未连接,尝试自动连接
68
+ // If server is not connected, try to auto-connect
69
69
await autoConnectToServer ( serverName )
70
70
const reconnectedClient = activeClients . get ( serverName )
71
71
if ( ! reconnectedClient ) {
72
- throw new Error ( `No active connection to server: ${ serverName } and auto-reconnect failed` )
72
+ throw new Error ( `[MCP] No active connection to server: ${ serverName } and auto-reconnect failed` )
73
73
}
74
74
75
75
const result = await reconnectedClient . callTool ( toolName , toolArgs )
@@ -80,7 +80,7 @@ export function initMCPHandlers(): void {
80
80
}
81
81
82
82
if ( ! client . isConnected ( ) ) {
83
- throw new Error ( `Connection to server ${ serverName } has been lost` )
83
+ throw new Error ( `[MCP] Connection to server ${ serverName } has been lost` )
84
84
}
85
85
86
86
const result = await client . callTool ( toolName , toolArgs )
@@ -89,7 +89,7 @@ export function initMCPHandlers(): void {
89
89
result,
90
90
}
91
91
} catch ( error ) {
92
- console . error ( `Error calling tool ${ toolName } on server ${ serverName } :` , error )
92
+ console . error ( `[MCP] Error calling tool ${ toolName } on server ${ serverName } :` , error )
93
93
return {
94
94
success : false ,
95
95
message : error instanceof Error ? error . message : String ( error ) ,
@@ -99,7 +99,7 @@ export function initMCPHandlers(): void {
99
99
100
100
// Disconnect from a server
101
101
ipcMain . handle ( 'mcp:disconnect' , async ( _ , serverName : string ) => {
102
- console . log ( `Disconnecting from MCP server: ${ serverName } ` )
102
+ console . log ( `[MCP] Disconnecting from MCP server: ${ serverName } ` )
103
103
104
104
try {
105
105
const client = activeClients . get ( serverName )
@@ -116,81 +116,81 @@ export function initMCPHandlers(): void {
116
116
message : `No active connection to server: ${ serverName } ` ,
117
117
}
118
118
} catch ( error ) {
119
- console . error ( `Error disconnecting from MCP server: ${ serverName } ` , error )
119
+ console . error ( `[MCP] Error disconnecting from MCP server: ${ serverName } ` , error )
120
120
return {
121
121
success : false ,
122
122
message : error instanceof Error ? error . message : String ( error ) ,
123
123
}
124
124
}
125
125
} )
126
126
127
- // 自动连接已启用的 MCP 服务器
127
+ // Auto-connect to enabled MCP servers
128
128
autoConnectToEnabledServers ( )
129
129
}
130
130
131
131
/**
132
- * 自动连接到启用的 MCP 服务器
132
+ * Automatically connect to enabled MCP servers
133
133
*/
134
134
async function autoConnectToEnabledServers ( ) : Promise < void > {
135
- console . log ( 'Checking for enabled MCP servers to auto-connect' )
135
+ console . log ( '[MCP] Checking for enabled MCP servers to auto-connect' )
136
136
137
137
try {
138
- // 从本地存储中读取 MCP 配置
138
+ // Read MCP configuration from local storage
139
139
const Store = require ( 'electron-store' )
140
140
const store = new Store ( )
141
141
142
- // 检查 MCP 是否启用
142
+ // Check if MCP is enabled
143
143
const mcpEnabled = store . get ( 'mcpEnabled' )
144
144
if ( mcpEnabled !== 'true' ) {
145
- console . log ( 'MCP is not enabled, skipping auto-connect' )
145
+ console . log ( '[MCP] MCP is not enabled, skipping auto-connect' )
146
146
return
147
147
}
148
148
149
- // 获取 MCP 配置
149
+ // Get MCP configuration
150
150
const mcpConfigStr = store . get ( 'mcpConfig' )
151
151
if ( ! mcpConfigStr ) {
152
- console . log ( 'No MCP configuration found, skipping auto-connect' )
152
+ console . log ( '[MCP] No MCP configuration found, skipping auto-connect' )
153
153
return
154
154
}
155
155
156
156
let mcpConfig
157
157
try {
158
158
mcpConfig = JSON . parse ( mcpConfigStr )
159
159
} catch ( err ) {
160
- console . error ( 'Failed to parse MCP configuration:' , err )
160
+ console . error ( '[MCP] Failed to parse MCP configuration:' , err )
161
161
return
162
162
}
163
163
164
164
if ( ! mcpConfig . mcpServers ) {
165
- console . log ( 'No MCP servers configured, skipping auto-connect' )
165
+ console . log ( '[MCP] No MCP servers configured, skipping auto-connect' )
166
166
return
167
167
}
168
168
169
- // 连接所有启用的服务器
169
+ // Connect to all enabled servers
170
170
for ( const [ serverName , serverConfig ] of Object . entries ( mcpConfig . mcpServers ) ) {
171
171
const serverEnabled = store . get ( `mcpServerEnabled:${ serverName } ` )
172
172
if ( serverEnabled === 'true' ) {
173
173
await autoConnectToServer ( serverName , serverConfig as MCPServer )
174
174
}
175
175
}
176
176
177
- console . log ( `Auto-connected to ${ activeClients . size } MCP servers` )
177
+ console . log ( `[MCP] Auto-connected to ${ activeClients . size } MCP servers` )
178
178
} catch ( error ) {
179
- console . error ( 'Error during MCP auto-connect:' , error )
179
+ console . error ( '[MCP] Error during MCP auto-connect:' , error )
180
180
}
181
181
}
182
182
183
183
/**
184
- * 自动连接到指定的 MCP 服务器
184
+ * Automatically connect to a specific MCP server
185
185
*
186
- * @param serverName 服务器名称
187
- * @param serverConfig 服务器配置(可选)
186
+ * @param serverName Server name
187
+ * @param serverConfig Server configuration (optional)
188
188
*/
189
189
async function autoConnectToServer ( serverName : string , serverConfig ?: MCPServer ) : Promise < boolean > {
190
- console . log ( `Auto-connecting to MCP server: ${ serverName } ` )
190
+ console . log ( `[MCP] Auto-connecting to MCP server: ${ serverName } ` )
191
191
192
192
try {
193
- // 如果没有提供服务器配置,从存储中获取
193
+ // If server configuration is not provided, get it from storage
194
194
if ( ! serverConfig ) {
195
195
const Store = require ( 'electron-store' )
196
196
const store = new Store ( )
@@ -205,35 +205,35 @@ async function autoConnectToServer(serverName: string, serverConfig?: MCPServer)
205
205
serverConfig = mcpConfig . mcpServers [ serverName ] as MCPServer
206
206
207
207
if ( ! serverConfig ) {
208
- console . error ( `Server configuration for ${ serverName } not found` )
208
+ console . error ( `[MCP] Server configuration for ${ serverName } not found` )
209
209
return false
210
210
}
211
211
} catch ( err ) {
212
- console . error ( 'Failed to parse MCP configuration:' , err )
212
+ console . error ( '[MCP] Failed to parse MCP configuration:' , err )
213
213
return false
214
214
}
215
215
}
216
216
217
- // 检查是否已经有连接
217
+ // Check if connection already exists
218
218
if ( activeClients . has ( serverName ) ) {
219
219
const existingClient = activeClients . get ( serverName )
220
220
if ( existingClient && existingClient . isConnected ( ) ) {
221
- console . log ( `Server ${ serverName } is already connected` )
221
+ console . log ( `[MCP] Server ${ serverName } is already connected` )
222
222
return true
223
223
}
224
224
225
- // 如果存在但断开连接,先清理
225
+ // If exists but disconnected, clean up first
226
226
if ( existingClient ) {
227
227
try {
228
228
await existingClient . disconnect ( )
229
229
} catch ( err ) {
230
- console . error ( `Error disconnecting existing client for ${ serverName } :` , err )
230
+ console . error ( `[MCP] Error disconnecting existing client for ${ serverName } :` , err )
231
231
}
232
232
activeClients . delete ( serverName )
233
233
}
234
234
}
235
235
236
- // 创建并连接新客户端
236
+ // Create and connect new client
237
237
const client = new MCPClient ( )
238
238
const connected = await client . connectToServer ( serverConfig )
239
239
@@ -245,11 +245,11 @@ async function autoConnectToServer(serverName: string, serverConfig?: MCPServer)
245
245
activeClients . set ( serverName , client )
246
246
return true
247
247
} else {
248
- console . error ( `Failed to connect to MCP server: ${ serverName } ` )
248
+ console . error ( `[MCP] Failed to connect to MCP server: ${ serverName } ` )
249
249
return false
250
250
}
251
251
} catch ( error ) {
252
- console . error ( `Error auto-connecting to MCP server ${ serverName } :` , error )
252
+ console . error ( `[MCP] Error auto-connecting to MCP server ${ serverName } :` , error )
253
253
return false
254
254
}
255
255
}
@@ -258,14 +258,14 @@ async function autoConnectToServer(serverName: string, serverConfig?: MCPServer)
258
258
* Clean up all active connections
259
259
*/
260
260
export async function cleanupMCPConnections ( ) : Promise < void > {
261
- console . log ( `Cleaning up ${ activeClients . size } MCP connections` )
261
+ console . log ( `[MCP] Cleaning up ${ activeClients . size } MCP connections` )
262
262
263
263
for ( const [ name , client ] of activeClients . entries ( ) ) {
264
264
try {
265
265
await client . disconnect ( )
266
- console . log ( `Disconnected from MCP server: ${ name } ` )
266
+ console . log ( `[MCP] Disconnected from MCP server: ${ name } ` )
267
267
} catch ( error ) {
268
- console . error ( `Error disconnecting from MCP server: ${ name } ` , error )
268
+ console . error ( `[MCP] Error disconnecting from MCP server: ${ name } ` , error )
269
269
}
270
270
}
271
271
0 commit comments