@@ -14,6 +14,9 @@ import {
1414 JsonObjectSchemaStrict ,
1515 UnknownContext ,
1616} from './types' ;
17+ import type { MCPToolFilterCallable , MCPToolFilterStatic } from './mcpUtil' ;
18+ import type { RunContext } from './runContext' ;
19+ import type { Agent } from './agent' ;
1720
1821export const DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME =
1922 'openai-agents:stdio-mcp-client' ;
@@ -27,6 +30,7 @@ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
2730 */
2831export interface MCPServer {
2932 cacheToolsList : boolean ;
33+ toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
3034 connect ( ) : Promise < void > ;
3135 readonly name : string ;
3236 close ( ) : Promise < void > ;
@@ -41,12 +45,14 @@ export interface MCPServer {
4145export abstract class BaseMCPServerStdio implements MCPServer {
4246 public cacheToolsList : boolean ;
4347 protected _cachedTools : any [ ] | undefined = undefined ;
48+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
4449
4550 protected logger : Logger ;
4651 constructor ( options : MCPServerStdioOptions ) {
4752 this . logger =
4853 options . logger ?? getLogger ( DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME ) ;
4954 this . cacheToolsList = options . cacheToolsList ?? false ;
55+ this . toolFilter = options . toolFilter ;
5056 }
5157
5258 abstract get name ( ) : string ;
@@ -74,13 +80,15 @@ export abstract class BaseMCPServerStdio implements MCPServer {
7480export abstract class BaseMCPServerStreamableHttp implements MCPServer {
7581 public cacheToolsList : boolean ;
7682 protected _cachedTools : any [ ] | undefined = undefined ;
83+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
7784
7885 protected logger : Logger ;
7986 constructor ( options : MCPServerStreamableHttpOptions ) {
8087 this . logger =
8188 options . logger ??
8289 getLogger ( DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME ) ;
8390 this . cacheToolsList = options . cacheToolsList ?? false ;
91+ this . toolFilter = options . toolFilter ;
8492 }
8593
8694 abstract get name ( ) : string ;
@@ -204,13 +212,17 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
204212 */
205213export async function getAllMcpFunctionTools < TContext = UnknownContext > (
206214 mcpServers : MCPServer [ ] ,
215+ runContext : RunContext < TContext > ,
216+ agent : Agent < any , any > ,
207217 convertSchemasToStrict = false ,
208218) : Promise < Tool < TContext > [ ] > {
209219 const allTools : Tool < TContext > [ ] = [ ] ;
210220 const toolNames = new Set < string > ( ) ;
211221 for ( const server of mcpServers ) {
212222 const serverTools = await getFunctionToolsFromServer (
213223 server ,
224+ runContext ,
225+ agent ,
214226 convertSchemasToStrict ,
215227 ) ;
216228 const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
@@ -242,6 +254,8 @@ export async function invalidateServerToolsCache(serverName: string) {
242254 */
243255async function getFunctionToolsFromServer < TContext = UnknownContext > (
244256 server : MCPServer ,
257+ runContext : RunContext < TContext > ,
258+ agent : Agent < any , any > ,
245259 convertSchemasToStrict : boolean ,
246260) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
247261 if ( server . cacheToolsList && _cachedTools [ server . name ] ) {
@@ -251,7 +265,53 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
251265 }
252266 return withMCPListToolsSpan (
253267 async ( span ) => {
254- const mcpTools = await server . listTools ( ) ;
268+ const fetchedMcpTools = await server . listTools ( ) ;
269+ const mcpTools : MCPTool [ ] = [ ] ;
270+ const context = {
271+ runContext,
272+ agent,
273+ serverName : server . name ,
274+ } ;
275+ for ( const tool of fetchedMcpTools ) {
276+ const filter = server . toolFilter ;
277+ if ( filter ) {
278+ if ( filter && typeof filter === 'function' ) {
279+ const filtered = await filter ( context , tool ) ;
280+ if ( ! filtered ) {
281+ globalLogger . debug (
282+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the callable filter.` ,
283+ ) ;
284+ continue ; // skip this tool
285+ }
286+ } else {
287+ const allowedToolNames = filter . allowedToolNames ?? [ ] ;
288+ const blockedToolNames = filter . blockedToolNames ?? [ ] ;
289+ if ( allowedToolNames . length > 0 || blockedToolNames . length > 0 ) {
290+ const allowed =
291+ allowedToolNames . length > 0
292+ ? allowedToolNames . includes ( tool . name )
293+ : true ;
294+ const blocked =
295+ blockedToolNames . length > 0
296+ ? blockedToolNames . includes ( tool . name )
297+ : false ;
298+ if ( ! allowed || blocked ) {
299+ if ( blocked ) {
300+ globalLogger . debug (
301+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the static filter.` ,
302+ ) ;
303+ } else if ( ! allowed ) {
304+ globalLogger . debug (
305+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is not allowed by the static filter.` ,
306+ ) ;
307+ }
308+ continue ; // skip this tool
309+ }
310+ }
311+ }
312+ }
313+ mcpTools . push ( tool ) ;
314+ }
255315 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
256316 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
257317 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -270,9 +330,16 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
270330 */
271331export async function getAllMcpTools < TContext = UnknownContext > (
272332 mcpServers : MCPServer [ ] ,
333+ runContext : RunContext < TContext > ,
334+ agent : Agent < TContext , any > ,
273335 convertSchemasToStrict = false ,
274336) : Promise < Tool < TContext > [ ] > {
275- return getAllMcpFunctionTools ( mcpServers , convertSchemasToStrict ) ;
337+ return getAllMcpFunctionTools (
338+ mcpServers ,
339+ runContext ,
340+ agent ,
341+ convertSchemasToStrict ,
342+ ) ;
276343}
277344
278345/**
@@ -363,6 +430,7 @@ export interface BaseMCPServerStdioOptions {
363430 encoding ?: string ;
364431 encodingErrorHandler ?: 'strict' | 'ignore' | 'replace' ;
365432 logger ?: Logger ;
433+ toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
366434}
367435export interface DefaultMCPServerStdioOptions
368436 extends BaseMCPServerStdioOptions {
@@ -383,6 +451,7 @@ export interface MCPServerStreamableHttpOptions {
383451 clientSessionTimeoutSeconds ?: number ;
384452 name ?: string ;
385453 logger ?: Logger ;
454+ toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
386455
387456 // ----------------------------------------------------
388457 // OAuth
0 commit comments