@@ -30,13 +30,11 @@ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
3030 */
3131export interface MCPServer {
3232 cacheToolsList : boolean ;
33+ toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
3334 connect ( ) : Promise < void > ;
3435 readonly name : string ;
3536 close ( ) : Promise < void > ;
36- listTools (
37- runContext ?: RunContext < any > ,
38- agent ?: Agent < any , any > ,
39- ) : Promise < MCPTool [ ] > ;
37+ listTools ( ) : Promise < MCPTool [ ] > ;
4038 callTool (
4139 toolName : string ,
4240 args : Record < string , unknown > | null ,
@@ -46,7 +44,7 @@ export interface MCPServer {
4644export abstract class BaseMCPServerStdio implements MCPServer {
4745 public cacheToolsList : boolean ;
4846 protected _cachedTools : any [ ] | undefined = undefined ;
49- protected toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
47+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
5048
5149 protected logger : Logger ;
5250 constructor ( options : MCPServerStdioOptions ) {
@@ -59,10 +57,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
5957 abstract get name ( ) : string ;
6058 abstract connect ( ) : Promise < void > ;
6159 abstract close ( ) : Promise < void > ;
62- abstract listTools (
63- runContext ?: RunContext < any > ,
64- agent ?: Agent < any , any > ,
65- ) : Promise < any [ ] > ;
60+ abstract listTools ( ) : Promise < any [ ] > ;
6661 abstract callTool (
6762 _toolName : string ,
6863 _args : Record < string , unknown > | null ,
@@ -83,7 +78,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
8378export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8479 public cacheToolsList : boolean ;
8580 protected _cachedTools : any [ ] | undefined = undefined ;
86- protected toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
81+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
8782
8883 protected logger : Logger ;
8984 constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -97,10 +92,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
9792 abstract get name ( ) : string ;
9893 abstract connect ( ) : Promise < void > ;
9994 abstract close ( ) : Promise < void > ;
100- abstract listTools (
101- runContext ?: RunContext < any > ,
102- agent ?: Agent < any , any > ,
103- ) : Promise < any [ ] > ;
95+ abstract listTools ( ) : Promise < any [ ] > ;
10496 abstract callTool (
10597 _toolName : string ,
10698 _args : Record < string , unknown > | null ,
@@ -154,14 +146,11 @@ export class MCPServerStdio extends BaseMCPServerStdio {
154146 close ( ) : Promise < void > {
155147 return this . underlying . close ( ) ;
156148 }
157- async listTools (
158- runContext ?: RunContext < any > ,
159- agent ?: Agent < any , any > ,
160- ) : Promise < MCPTool [ ] > {
149+ async listTools ( ) : Promise < MCPTool [ ] > {
161150 if ( this . cacheToolsList && this . _cachedTools ) {
162151 return this . _cachedTools ;
163152 }
164- const tools = await this . underlying . listTools ( runContext , agent ) ;
153+ const tools = await this . underlying . listTools ( ) ;
165154 if ( this . cacheToolsList ) {
166155 this . _cachedTools = tools ;
167156 }
@@ -190,14 +179,11 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
190179 close ( ) : Promise < void > {
191180 return this . underlying . close ( ) ;
192181 }
193- async listTools (
194- runContext ?: RunContext < any > ,
195- agent ?: Agent < any , any > ,
196- ) : Promise < MCPTool [ ] > {
182+ async listTools ( ) : Promise < MCPTool [ ] > {
197183 if ( this . cacheToolsList && this . _cachedTools ) {
198184 return this . _cachedTools ;
199185 }
200- const tools = await this . underlying . listTools ( runContext , agent ) ;
186+ const tools = await this . underlying . listTools ( ) ;
201187 if ( this . cacheToolsList ) {
202188 this . _cachedTools = tools ;
203189 }
@@ -217,18 +203,18 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
217203 */
218204export async function getAllMcpFunctionTools < TContext = UnknownContext > (
219205 mcpServers : MCPServer [ ] ,
206+ runContext : RunContext < TContext > ,
207+ agent : Agent < any , any > ,
220208 convertSchemasToStrict = false ,
221- runContext ?: RunContext < TContext > ,
222- agent ?: Agent < TContext , any > ,
223209) : Promise < Tool < TContext > [ ] > {
224210 const allTools : Tool < TContext > [ ] = [ ] ;
225211 const toolNames = new Set < string > ( ) ;
226212 for ( const server of mcpServers ) {
227213 const serverTools = await getFunctionToolsFromServer (
228214 server ,
229- convertSchemasToStrict ,
230215 runContext ,
231216 agent ,
217+ convertSchemasToStrict ,
232218 ) ;
233219 const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
234220 const intersection = [ ...serverToolNames ] . filter ( ( n ) => toolNames . has ( n ) ) ;
@@ -259,16 +245,62 @@ export function invalidateServerToolsCache(serverName: string) {
259245 */
260246async function getFunctionToolsFromServer < TContext = UnknownContext > (
261247 server : MCPServer ,
248+ runContext : RunContext < TContext > ,
249+ agent : Agent < any , any > ,
262250 convertSchemasToStrict : boolean ,
263- runContext ?: RunContext < TContext > ,
264- agent ?: Agent < TContext , any > ,
265251) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
266252 if ( server . cacheToolsList && _cachedTools [ server . name ] ) {
267253 return _cachedTools [ server . name ] ;
268254 }
269255 return withMCPListToolsSpan (
270256 async ( span ) => {
271- const mcpTools = await server . listTools ( runContext , agent ) ;
257+ const fetchedMcpTools = await server . listTools ( ) ;
258+ const mcpTools : MCPTool [ ] = [ ] ;
259+ const context = {
260+ runContext,
261+ agent,
262+ serverName : server . name ,
263+ } ;
264+ for ( const tool of fetchedMcpTools ) {
265+ const filter = server . toolFilter ;
266+ if ( filter ) {
267+ if ( filter && typeof filter === 'function' ) {
268+ const filtered = await filter ( context , tool ) ;
269+ if ( ! filtered ) {
270+ globalLogger . debug (
271+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the callable filter.` ,
272+ ) ;
273+ continue ; // skip this tool
274+ }
275+ } else {
276+ const allowedToolNames = filter . allowedToolNames ?? [ ] ;
277+ const blockedToolNames = filter . blockedToolNames ?? [ ] ;
278+ if ( allowedToolNames . length > 0 || blockedToolNames . length > 0 ) {
279+ const allowed =
280+ allowedToolNames . length > 0
281+ ? allowedToolNames . includes ( tool . name )
282+ : true ;
283+ const blocked =
284+ blockedToolNames . length > 0
285+ ? blockedToolNames . includes ( tool . name )
286+ : false ;
287+ if ( ! allowed || blocked ) {
288+ if ( blocked ) {
289+ globalLogger . debug (
290+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is blocked by the static filter.` ,
291+ ) ;
292+ } else if ( ! allowed ) {
293+ globalLogger . debug (
294+ `MCP Tool (server: ${ server . name } , tool: ${ tool . name } ) is not allowed by the static filter.` ,
295+ ) ;
296+ }
297+ continue ; // skip this tool
298+ }
299+ }
300+ }
301+ }
302+ mcpTools . push ( tool ) ;
303+ }
272304 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
273305 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
274306 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -287,15 +319,15 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
287319 */
288320export async function getAllMcpTools < TContext = UnknownContext > (
289321 mcpServers : MCPServer [ ] ,
322+ runContext : RunContext < TContext > ,
323+ agent : Agent < TContext , any > ,
290324 convertSchemasToStrict = false ,
291- runContext ?: RunContext < TContext > ,
292- agent ?: Agent < TContext , any > ,
293325) : Promise < Tool < TContext > [ ] > {
294326 return getAllMcpFunctionTools (
295327 mcpServers ,
296- convertSchemasToStrict ,
297328 runContext ,
298329 agent ,
330+ convertSchemasToStrict ,
299331 ) ;
300332}
301333
0 commit comments