@@ -285,35 +285,6 @@ export class MCPServerSSE extends BaseMCPServerSSE {
285
285
* Fetches and flattens all tools from multiple MCP servers.
286
286
* Logs and skips any servers that fail to respond.
287
287
*/
288
- export async function getAllMcpFunctionTools < TContext = UnknownContext > (
289
- mcpServers : MCPServer [ ] ,
290
- runContext : RunContext < TContext > ,
291
- agent : Agent < any , any > ,
292
- convertSchemasToStrict = false ,
293
- ) : Promise < Tool < TContext > [ ] > {
294
- const allTools : Tool < TContext > [ ] = [ ] ;
295
- const toolNames = new Set < string > ( ) ;
296
- for ( const server of mcpServers ) {
297
- const serverTools = await getFunctionToolsFromServer (
298
- server ,
299
- runContext ,
300
- agent ,
301
- convertSchemasToStrict ,
302
- ) ;
303
- const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
304
- const intersection = [ ...serverToolNames ] . filter ( ( n ) => toolNames . has ( n ) ) ;
305
- if ( intersection . length > 0 ) {
306
- throw new UserError (
307
- `Duplicate tool names found across MCP servers: ${ intersection . join ( ', ' ) } ` ,
308
- ) ;
309
- }
310
- for ( const t of serverTools ) {
311
- toolNames . add ( t . name ) ;
312
- allTools . push ( t ) ;
313
- }
314
- }
315
- return allTools ;
316
- }
317
288
318
289
const _cachedTools : Record < string , MCPTool [ ] > = { } ;
319
290
/**
@@ -327,12 +298,17 @@ export async function invalidateServerToolsCache(serverName: string) {
327
298
/**
328
299
* Fetches all function tools from a single MCP server.
329
300
*/
330
- async function getFunctionToolsFromServer < TContext = UnknownContext > (
331
- server : MCPServer ,
332
- runContext : RunContext < TContext > ,
333
- agent : Agent < any , any > ,
334
- convertSchemasToStrict : boolean ,
335
- ) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
301
+ async function getFunctionToolsFromServer < TContext = UnknownContext > ( {
302
+ server,
303
+ convertSchemasToStrict,
304
+ runContext,
305
+ agent,
306
+ } : {
307
+ server : MCPServer ;
308
+ convertSchemasToStrict : boolean ;
309
+ runContext ?: RunContext < TContext > ;
310
+ agent ?: Agent < any , any > ;
311
+ } ) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
336
312
if ( server . cacheToolsList && _cachedTools [ server . name ] ) {
337
313
return _cachedTools [ server . name ] . map ( ( t ) =>
338
314
mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -341,52 +317,54 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
341
317
return withMCPListToolsSpan (
342
318
async ( span ) => {
343
319
const fetchedMcpTools = await server . listTools ( ) ;
344
- const mcpTools : MCPTool [ ] = [ ] ;
345
- const context = {
346
- runContext,
347
- agent,
348
- serverName : server . name ,
349
- } ;
350
- for ( const tool of fetchedMcpTools ) {
351
- const filter = server . toolFilter ;
352
- if ( filter ) {
353
- if ( filter && typeof filter === 'function' ) {
354
- const filtered = await filter ( context , tool ) ;
355
- if ( ! filtered ) {
356
- globalLogger . debug (
357
- `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the callable filter.` ,
358
- ) ;
359
- continue ; // skip this tool
360
- }
361
- } else {
362
- const allowedToolNames = filter . allowedToolNames ?? [ ] ;
363
- const blockedToolNames = filter . blockedToolNames ?? [ ] ;
364
- if ( allowedToolNames . length > 0 || blockedToolNames . length > 0 ) {
365
- const allowed =
366
- allowedToolNames . length > 0
367
- ? allowedToolNames . includes ( tool . name )
368
- : true ;
369
- const blocked =
370
- blockedToolNames . length > 0
371
- ? blockedToolNames . includes ( tool . name )
372
- : false ;
373
- if ( ! allowed || blocked ) {
374
- if ( blocked ) {
375
- globalLogger . debug (
376
- `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the static filter.` ,
377
- ) ;
378
- } else if ( ! allowed ) {
379
- globalLogger . debug (
380
- `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is not allowed by the static filter.` ,
381
- ) ;
320
+ let mcpTools : MCPTool [ ] = fetchedMcpTools ;
321
+
322
+ if ( runContext && agent ) {
323
+ const context = { runContext, agent, serverName : server . name } ;
324
+ const filteredTools : MCPTool [ ] = [ ] ;
325
+ for ( const tool of fetchedMcpTools ) {
326
+ const filter = server . toolFilter ;
327
+ if ( filter ) {
328
+ if ( typeof filter === 'function' ) {
329
+ const filtered = await filter ( context , tool ) ;
330
+ if ( ! filtered ) {
331
+ globalLogger . debug (
332
+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the callable filter.` ,
333
+ ) ;
334
+ continue ;
335
+ }
336
+ } else {
337
+ const allowedToolNames = filter . allowedToolNames ?? [ ] ;
338
+ const blockedToolNames = filter . blockedToolNames ?? [ ] ;
339
+ if ( allowedToolNames . length > 0 || blockedToolNames . length > 0 ) {
340
+ const allowed =
341
+ allowedToolNames . length > 0
342
+ ? allowedToolNames . includes ( tool . name )
343
+ : true ;
344
+ const blocked =
345
+ blockedToolNames . length > 0
346
+ ? blockedToolNames . includes ( tool . name )
347
+ : false ;
348
+ if ( ! allowed || blocked ) {
349
+ if ( blocked ) {
350
+ globalLogger . debug (
351
+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the static filter.` ,
352
+ ) ;
353
+ } else if ( ! allowed ) {
354
+ globalLogger . debug (
355
+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is not allowed by the static filter.` ,
356
+ ) ;
357
+ }
358
+ continue ;
382
359
}
383
- continue ; // skip this tool
384
360
}
385
361
}
386
362
}
363
+ filteredTools . push ( tool ) ;
387
364
}
388
- mcpTools . push ( tool ) ;
365
+ mcpTools = filteredTools ;
389
366
}
367
+
390
368
span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
391
369
const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
392
370
mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -400,21 +378,70 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
400
378
) ;
401
379
}
402
380
381
+ /**
382
+ * Options for fetching MCP tools.
383
+ */
384
+ export type GetAllMcpToolsOptions < TContext > = {
385
+ mcpServers : MCPServer [ ] ;
386
+ convertSchemasToStrict ?: boolean ;
387
+ runContext ?: RunContext < TContext > ;
388
+ agent ?: Agent < TContext , any > ;
389
+ } ;
390
+
403
391
/**
404
392
* Returns all MCP tools from the provided servers, using the function tool conversion.
393
+ * If runContext and agent are provided, callable tool filters will be applied.
405
394
*/
406
395
export async function getAllMcpTools < TContext = UnknownContext > (
407
396
mcpServers : MCPServer [ ] ,
408
- runContext : RunContext < TContext > ,
409
- agent : Agent < TContext , any > ,
397
+ ) : Promise < Tool < TContext > [ ] > ;
398
+ export async function getAllMcpTools < TContext = UnknownContext > (
399
+ opts : GetAllMcpToolsOptions < TContext > ,
400
+ ) : Promise < Tool < TContext > [ ] > ;
401
+ export async function getAllMcpTools < TContext = UnknownContext > (
402
+ mcpServersOrOpts : MCPServer [ ] | GetAllMcpToolsOptions < TContext > ,
403
+ runContext ?: RunContext < TContext > ,
404
+ agent ?: Agent < TContext , any > ,
410
405
convertSchemasToStrict = false ,
411
406
) : Promise < Tool < TContext > [ ] > {
412
- return getAllMcpFunctionTools (
407
+ const opts = Array . isArray ( mcpServersOrOpts )
408
+ ? {
409
+ mcpServers : mcpServersOrOpts ,
410
+ runContext,
411
+ agent,
412
+ convertSchemasToStrict,
413
+ }
414
+ : mcpServersOrOpts ;
415
+
416
+ const {
413
417
mcpServers,
414
- runContext ,
415
- agent ,
416
- convertSchemasToStrict ,
417
- ) ;
418
+ convertSchemasToStrict : convertSchemasToStrictFromOpts = false ,
419
+ runContext : runContextFromOpts ,
420
+ agent : agentFromOpts ,
421
+ } = opts ;
422
+ const allTools : Tool < TContext > [ ] = [ ] ;
423
+ const toolNames = new Set < string > ( ) ;
424
+
425
+ for ( const server of mcpServers ) {
426
+ const serverTools = await getFunctionToolsFromServer ( {
427
+ server,
428
+ convertSchemasToStrict : convertSchemasToStrictFromOpts ,
429
+ runContext : runContextFromOpts ,
430
+ agent : agentFromOpts ,
431
+ } ) ;
432
+ const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
433
+ const intersection = [ ...serverToolNames ] . filter ( ( n ) => toolNames . has ( n ) ) ;
434
+ if ( intersection . length > 0 ) {
435
+ throw new UserError (
436
+ `Duplicate tool names found across MCP servers: ${ intersection . join ( ', ' ) } ` ,
437
+ ) ;
438
+ }
439
+ for ( const t of serverTools ) {
440
+ toolNames . add ( t . name ) ;
441
+ allTools . push ( t ) ;
442
+ }
443
+ }
444
+ return allTools ;
418
445
}
419
446
420
447
/**
0 commit comments