@@ -14,7 +14,11 @@ import {
1414 JsonObjectSchemaStrict ,
1515 UnknownContext ,
1616} from './types' ;
17- import type { ToolFilterCallable , ToolFilterStatic } from './mcpUtil' ;
17+ import type {
18+ ToolFilterCallable ,
19+ ToolFilterStatic ,
20+ ToolFilterContext ,
21+ } from './mcpUtil' ;
1822import type { RunContext } from './runContext' ;
1923import type { Agent } from './agent' ;
2024
@@ -30,6 +34,7 @@ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
3034 */
3135export interface MCPServer {
3236 cacheToolsList : boolean ;
37+ toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
3338 connect ( ) : Promise < void > ;
3439 readonly name : string ;
3540 close ( ) : Promise < void > ;
@@ -46,7 +51,7 @@ export interface MCPServer {
4651export abstract class BaseMCPServerStdio implements MCPServer {
4752 public cacheToolsList : boolean ;
4853 protected _cachedTools : any [ ] | undefined = undefined ;
49- protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
54+ public toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
5055
5156 protected logger : Logger ;
5257 constructor ( options : MCPServerStdioOptions ) {
@@ -83,7 +88,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
8388export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8489 public cacheToolsList : boolean ;
8590 protected _cachedTools : any [ ] | undefined = undefined ;
86- protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
91+ public toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
8792
8893 protected logger : Logger ;
8994 constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -155,13 +160,13 @@ export class MCPServerStdio extends BaseMCPServerStdio {
155160 return this . underlying . close ( ) ;
156161 }
157162 async listTools (
158- runContext ?: RunContext < any > ,
159- agent ?: Agent < any , any > ,
163+ _runContext ?: RunContext < any > ,
164+ _agent ?: Agent < any , any > ,
160165 ) : Promise < MCPTool [ ] > {
161166 if ( this . cacheToolsList && this . _cachedTools ) {
162167 return this . _cachedTools ;
163168 }
164- const tools = await this . underlying . listTools ( runContext , agent ) ;
169+ const tools = await this . underlying . listTools ( ) ;
165170 if ( this . cacheToolsList ) {
166171 this . _cachedTools = tools ;
167172 }
@@ -191,13 +196,13 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
191196 return this . underlying . close ( ) ;
192197 }
193198 async listTools (
194- runContext ?: RunContext < any > ,
195- agent ?: Agent < any , any > ,
199+ _runContext ?: RunContext < any > ,
200+ _agent ?: Agent < any , any > ,
196201 ) : Promise < MCPTool [ ] > {
197202 if ( this . cacheToolsList && this . _cachedTools ) {
198203 return this . _cachedTools ;
199204 }
200- const tools = await this . underlying . listTools ( runContext , agent ) ;
205+ const tools = await this . underlying . listTools ( ) ;
201206 if ( this . cacheToolsList ) {
202207 this . _cachedTools = tools ;
203208 }
@@ -268,7 +273,16 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
268273 }
269274 return withMCPListToolsSpan (
270275 async ( span ) => {
271- const mcpTools = await server . listTools ( runContext , agent ) ;
276+ let mcpTools = await server . listTools ( runContext , agent ) ;
277+ if ( server . toolFilter ) {
278+ mcpTools = await filterMcpTools (
279+ mcpTools ,
280+ server . toolFilter as ToolFilterCallable < TContext > | ToolFilterStatic ,
281+ runContext ,
282+ agent ,
283+ server . name ,
284+ ) ;
285+ }
272286 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
273287 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
274288 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -371,6 +385,41 @@ function ensureStrictJsonSchema(
371385 return out ;
372386}
373387
388+ async function filterMcpTools < TContext = UnknownContext > (
389+ tools : MCPTool [ ] ,
390+ filter : ToolFilterCallable < TContext > | ToolFilterStatic ,
391+ runContext : RunContext < TContext > | undefined ,
392+ agent : Agent < TContext , any > | undefined ,
393+ serverName : string ,
394+ ) : Promise < MCPTool [ ] > {
395+ if ( typeof filter === 'function' ) {
396+ if ( ! runContext || ! agent ) {
397+ return tools ;
398+ }
399+ const ctx = {
400+ runContext,
401+ agent,
402+ serverName,
403+ } as ToolFilterContext < TContext > ;
404+ const result : MCPTool [ ] = [ ] ;
405+ for ( const tool of tools ) {
406+ if ( await filter ( ctx , tool ) ) {
407+ result . push ( tool ) ;
408+ }
409+ }
410+ return result ;
411+ }
412+ return tools . filter ( ( t ) => {
413+ if ( filter . allowedToolNames && ! filter . allowedToolNames . includes ( t . name ) ) {
414+ return false ;
415+ }
416+ if ( filter . blockedToolNames && filter . blockedToolNames . includes ( t . name ) ) {
417+ return false ;
418+ }
419+ return true ;
420+ } ) ;
421+ }
422+
374423/**
375424 * Abstract base class for MCP servers that use a ClientSession for communication.
376425 * Handles session management, tool listing, tool calling, and cleanup.
0 commit comments