@@ -17,6 +17,8 @@ import {
17
17
ServerStatusResponse ,
18
18
} from "./types" ;
19
19
20
+ const JSON_INDENT = 2 ;
21
+
20
22
const logger = new MCPClientLogger ( "MCP Actions" ) ;
21
23
22
24
const getConfigPath = async ( ) => {
@@ -29,6 +31,7 @@ const getConfigPath = async () => {
29
31
} ;
30
32
31
33
const clientsMap = new Map < string , McpClientData > ( ) ;
34
+ const toolToClientMap = new Map < string , string > ( ) ;
32
35
33
36
// 获取客户端状态
34
37
export async function getClientsStatus ( ) : Promise <
@@ -133,6 +136,13 @@ async function initializeSingleClient(
133
136
`Supported tools for [${ clientId } ]: ${ JSON . stringify ( tools , null , 2 ) } ` ,
134
137
) ;
135
138
clientsMap . set ( clientId , { client, tools, errorMsg : null } ) ;
139
+ if ( tools ?. tools ) {
140
+ for ( const tool of tools . tools ) {
141
+ if ( tool . name ) {
142
+ toolToClientMap . set ( tool . name , clientId ) ;
143
+ }
144
+ }
145
+ }
136
146
logger . success ( `Client [${ clientId } ] initialized successfully` ) ;
137
147
} )
138
148
. catch ( ( error ) => {
@@ -250,6 +260,13 @@ export async function resumeMcpServer(clientId: string): Promise<void> {
250
260
const client = await createClient ( clientId , serverConfig ) ;
251
261
const tools = await listTools ( client ) ;
252
262
clientsMap . set ( clientId , { client, tools, errorMsg : null } ) ;
263
+ if ( tools ?. tools ) {
264
+ for ( const tool of tools . tools ) {
265
+ if ( tool . name ) {
266
+ toolToClientMap . set ( tool . name , clientId ) ;
267
+ }
268
+ }
269
+ }
253
270
logger . success ( `Client [${ clientId } ] initialized successfully` ) ;
254
271
255
272
// 初始化成功后更新配置
@@ -354,9 +371,10 @@ export async function executeMcpAction(
354
371
) {
355
372
// Use a tool-to-client mapping that's maintained when tools are initialized
356
373
const toolName = request . params . name ;
357
- client = [ ...clientsMap . values ( ) ] . find (
358
- ( c ) => c . tools ?. tools && c . tools . tools . some ( ( t ) => t . name == toolName ) ,
359
- ) ;
374
+ const toolClientId = toolToClientMap . get ( toolName ) ;
375
+ if ( toolClientId ) {
376
+ client = clientsMap . get ( toolClientId ) ;
377
+ }
360
378
}
361
379
if ( ! client ?. client ) {
362
380
throw new Error ( `Client ${ clientId } not found` ) ;
@@ -417,7 +435,10 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
417
435
const path = await import ( "path" ) ;
418
436
// 确保目录存在
419
437
await fs . mkdir ( path . dirname ( await getConfigPath ( ) ) , { recursive : true } ) ;
420
- await fs . writeFile ( await getConfigPath ( ) , JSON . stringify ( config , null , 2 ) ) ;
438
+ await fs . writeFile (
439
+ await getConfigPath ( ) ,
440
+ JSON . stringify ( config , null , JSON_INDENT ) ,
441
+ ) ;
421
442
}
422
443
}
423
444
0 commit comments