@@ -5,7 +5,13 @@ import {
55 MCPServerStreamableHttp as UnderlyingMCPServerStreamableHttp ,
66 MCPServerSSE as UnderlyingMCPServerSSE ,
77} from '@openai/agents-core/_shims' ;
8- import { getCurrentSpan , withMCPListToolsSpan } from './tracing' ;
8+ import {
9+ getCurrentSpan ,
10+ getCurrentTrace ,
11+ withMCPListToolsSpan ,
12+ type MCPListToolsSpanData ,
13+ type Span ,
14+ } from './tracing' ;
915import { logger as globalLogger , getLogger , Logger } from './logger' ;
1016import debug from 'debug' ;
1117import { z } from 'zod' ;
@@ -314,68 +320,78 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>({
314320 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
315321 ) ;
316322 }
317- return withMCPListToolsSpan (
318- async ( span ) => {
319- const fetchedMcpTools = await server . listTools ( ) ;
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 ;
323+
324+ const listToolsForServer = async (
325+ span ?: Span < MCPListToolsSpanData > ,
326+ ) : Promise < FunctionTool < TContext , any , unknown > [ ] > => {
327+ const fetchedMcpTools = await server . listTools ( ) ;
328+ let mcpTools : MCPTool [ ] = fetchedMcpTools ;
329+
330+ if ( runContext && agent ) {
331+ const context = { runContext , agent , serverName : server . name } ;
332+ const filteredTools : MCPTool [ ] = [ ] ;
333+ for ( const tool of fetchedMcpTools ) {
334+ const filter = server . toolFilter ;
335+ if ( filter ) {
336+ if ( typeof filter === 'function' ) {
337+ const filtered = await filter ( context , tool ) ;
338+ if ( ! filtered ) {
339+ globalLogger . debug (
340+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the callable filter.` ,
341+ ) ;
342+ continue ;
343+ }
344+ } else {
345+ const allowedToolNames = filter . allowedToolNames ?? [ ] ;
346+ const blockedToolNames = filter . blockedToolNames ?? [ ] ;
347+ if ( allowedToolNames . length > 0 || blockedToolNames . length > 0 ) {
348+ const allowed =
349+ allowedToolNames . length > 0
350+ ? allowedToolNames . includes ( tool . name )
351+ : true ;
352+ const blocked =
353+ blockedToolNames . length > 0
354+ ? blockedToolNames . includes ( tool . name )
355+ : false ;
356+ if ( ! allowed || blocked ) {
357+ if ( blocked ) {
358+ globalLogger . debug (
359+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the static filter.` ,
360+ ) ;
361+ } else if ( ! allowed ) {
362+ globalLogger . debug (
363+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is not allowed by the static filter.` ,
364+ ) ;
359365 }
366+ continue ;
360367 }
361368 }
362369 }
363- filteredTools . push ( tool ) ;
364370 }
365- mcpTools = filteredTools ;
371+ filteredTools . push ( tool ) ;
366372 }
373+ mcpTools = filteredTools ;
374+ }
367375
376+ if ( span ) {
368377 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
369- const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
370- mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
371- ) ;
372- if ( server . cacheToolsList ) {
373- _cachedTools [ server . name ] = mcpTools ;
374- }
375- return tools ;
376- } ,
377- { data : { server : server . name } } ,
378- ) ;
378+ }
379+ const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
380+ mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
381+ ) ;
382+ if ( server . cacheToolsList ) {
383+ _cachedTools [ server . name ] = mcpTools ;
384+ }
385+ return tools ;
386+ } ;
387+
388+ if ( ! getCurrentTrace ( ) ) {
389+ return listToolsForServer ( ) ;
390+ }
391+
392+ return withMCPListToolsSpan ( listToolsForServer , {
393+ data : { server : server . name } ,
394+ } ) ;
379395}
380396
381397/**
0 commit comments