@@ -14,6 +14,9 @@ import {
1414 JsonObjectSchemaStrict ,
1515 UnknownContext ,
1616} from './types' ;
17+ import type { ToolFilterCallable , ToolFilterStatic } 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' ;
@@ -30,7 +33,10 @@ export interface MCPServer {
3033 connect ( ) : Promise < void > ;
3134 readonly name : string ;
3235 close ( ) : Promise < void > ;
33- listTools ( ) : Promise < MCPTool [ ] > ;
36+ listTools (
37+ runContext ?: RunContext < any > ,
38+ agent ?: Agent < any , any > ,
39+ ) : Promise < MCPTool [ ] > ;
3440 callTool (
3541 toolName : string ,
3642 args : Record < string , unknown > | null ,
@@ -41,18 +47,23 @@ export interface MCPServer {
4147export abstract class BaseMCPServerStdio implements MCPServer {
4248 public cacheToolsList : boolean ;
4349 protected _cachedTools : any [ ] | undefined = undefined ;
50+ protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
4451
4552 protected logger : Logger ;
4653 constructor ( options : MCPServerStdioOptions ) {
4754 this . logger =
4855 options . logger ?? getLogger ( DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME ) ;
4956 this . cacheToolsList = options . cacheToolsList ?? false ;
57+ this . toolFilter = options . toolFilter ;
5058 }
5159
5260 abstract get name ( ) : string ;
5361 abstract connect ( ) : Promise < void > ;
5462 abstract close ( ) : Promise < void > ;
55- abstract listTools ( ) : Promise < any [ ] > ;
63+ abstract listTools (
64+ runContext ?: RunContext < any > ,
65+ agent ?: Agent < any , any > ,
66+ ) : Promise < any [ ] > ;
5667 abstract callTool (
5768 _toolName : string ,
5869 _args : Record < string , unknown > | null ,
@@ -74,19 +85,24 @@ export abstract class BaseMCPServerStdio implements MCPServer {
7485export abstract class BaseMCPServerStreamableHttp implements MCPServer {
7586 public cacheToolsList : boolean ;
7687 protected _cachedTools : any [ ] | undefined = undefined ;
88+ protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
7789
7890 protected logger : Logger ;
7991 constructor ( options : MCPServerStreamableHttpOptions ) {
8092 this . logger =
8193 options . logger ??
8294 getLogger ( DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME ) ;
8395 this . cacheToolsList = options . cacheToolsList ?? false ;
96+ this . toolFilter = options . toolFilter ;
8497 }
8598
8699 abstract get name ( ) : string ;
87100 abstract connect ( ) : Promise < void > ;
88101 abstract close ( ) : Promise < void > ;
89- abstract listTools ( ) : Promise < any [ ] > ;
102+ abstract listTools (
103+ runContext ?: RunContext < any > ,
104+ agent ?: Agent < any , any > ,
105+ ) : Promise < any [ ] > ;
90106 abstract callTool (
91107 _toolName : string ,
92108 _args : Record < string , unknown > | null ,
@@ -141,11 +157,14 @@ export class MCPServerStdio extends BaseMCPServerStdio {
141157 close ( ) : Promise < void > {
142158 return this . underlying . close ( ) ;
143159 }
144- async listTools ( ) : Promise < MCPTool [ ] > {
160+ async listTools (
161+ runContext ?: RunContext < any > ,
162+ agent ?: Agent < any , any > ,
163+ ) : Promise < MCPTool [ ] > {
145164 if ( this . cacheToolsList && this . _cachedTools ) {
146165 return this . _cachedTools ;
147166 }
148- const tools = await this . underlying . listTools ( ) ;
167+ const tools = await this . underlying . listTools ( runContext , agent ) ;
149168 if ( this . cacheToolsList ) {
150169 this . _cachedTools = tools ;
151170 }
@@ -177,11 +196,14 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
177196 close ( ) : Promise < void > {
178197 return this . underlying . close ( ) ;
179198 }
180- async listTools ( ) : Promise < MCPTool [ ] > {
199+ async listTools (
200+ runContext ?: RunContext < any > ,
201+ agent ?: Agent < any , any > ,
202+ ) : Promise < MCPTool [ ] > {
181203 if ( this . cacheToolsList && this . _cachedTools ) {
182204 return this . _cachedTools ;
183205 }
184- const tools = await this . underlying . listTools ( ) ;
206+ const tools = await this . underlying . listTools ( runContext , agent ) ;
185207 if ( this . cacheToolsList ) {
186208 this . _cachedTools = tools ;
187209 }
@@ -205,13 +227,17 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
205227export async function getAllMcpFunctionTools < TContext = UnknownContext > (
206228 mcpServers : MCPServer [ ] ,
207229 convertSchemasToStrict = false ,
230+ runContext ?: RunContext < TContext > ,
231+ agent ?: Agent < TContext , any > ,
208232) : Promise < Tool < TContext > [ ] > {
209233 const allTools : Tool < TContext > [ ] = [ ] ;
210234 const toolNames = new Set < string > ( ) ;
211235 for ( const server of mcpServers ) {
212236 const serverTools = await getFunctionToolsFromServer (
213237 server ,
214238 convertSchemasToStrict ,
239+ runContext ,
240+ agent ,
215241 ) ;
216242 const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
217243 const intersection = [ ...serverToolNames ] . filter ( ( n ) => toolNames . has ( n ) ) ;
@@ -243,6 +269,8 @@ export async function invalidateServerToolsCache(serverName: string) {
243269async function getFunctionToolsFromServer < TContext = UnknownContext > (
244270 server : MCPServer ,
245271 convertSchemasToStrict : boolean ,
272+ runContext ?: RunContext < TContext > ,
273+ agent ?: Agent < TContext , any > ,
246274) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
247275 if ( server . cacheToolsList && _cachedTools [ server . name ] ) {
248276 return _cachedTools [ server . name ] . map ( ( t ) =>
@@ -251,7 +279,7 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
251279 }
252280 return withMCPListToolsSpan (
253281 async ( span ) => {
254- const mcpTools = await server . listTools ( ) ;
282+ const mcpTools = await server . listTools ( runContext , agent ) ;
255283 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
256284 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
257285 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -271,8 +299,15 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
271299export async function getAllMcpTools < TContext = UnknownContext > (
272300 mcpServers : MCPServer [ ] ,
273301 convertSchemasToStrict = false ,
302+ runContext ?: RunContext < TContext > ,
303+ agent ?: Agent < TContext , any > ,
274304) : Promise < Tool < TContext > [ ] > {
275- return getAllMcpFunctionTools ( mcpServers , convertSchemasToStrict ) ;
305+ return getAllMcpFunctionTools (
306+ mcpServers ,
307+ convertSchemasToStrict ,
308+ runContext ,
309+ agent ,
310+ ) ;
276311}
277312
278313/**
@@ -363,6 +398,7 @@ export interface BaseMCPServerStdioOptions {
363398 encoding ?: string ;
364399 encodingErrorHandler ?: 'strict' | 'ignore' | 'replace' ;
365400 logger ?: Logger ;
401+ toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
366402}
367403export interface DefaultMCPServerStdioOptions
368404 extends BaseMCPServerStdioOptions {
@@ -383,6 +419,7 @@ export interface MCPServerStreamableHttpOptions {
383419 clientSessionTimeoutSeconds ?: number ;
384420 name ?: string ;
385421 logger ?: Logger ;
422+ toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
386423
387424 // ----------------------------------------------------
388425 // OAuth
0 commit comments