@@ -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 ,
@@ -47,7 +45,7 @@ export interface MCPServer {
4745export abstract class BaseMCPServerStdio implements MCPServer {
4846 public cacheToolsList : boolean ;
4947 protected _cachedTools : any [ ] | undefined = undefined ;
50- protected toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
48+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
5149
5250 protected logger : Logger ;
5351 constructor ( options : MCPServerStdioOptions ) {
@@ -60,10 +58,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
6058 abstract get name ( ) : string ;
6159 abstract connect ( ) : Promise < void > ;
6260 abstract close ( ) : Promise < void > ;
63- abstract listTools (
64- runContext ?: RunContext < any > ,
65- agent ?: Agent < any , any > ,
66- ) : Promise < any [ ] > ;
61+ abstract listTools ( ) : Promise < any [ ] > ;
6762 abstract callTool (
6863 _toolName : string ,
6964 _args : Record < string , unknown > | null ,
@@ -85,7 +80,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
8580export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8681 public cacheToolsList : boolean ;
8782 protected _cachedTools : any [ ] | undefined = undefined ;
88- protected toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
83+ public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
8984
9085 protected logger : Logger ;
9186 constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -99,10 +94,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
9994 abstract get name ( ) : string ;
10095 abstract connect ( ) : Promise < void > ;
10196 abstract close ( ) : Promise < void > ;
102- abstract listTools (
103- runContext ?: RunContext < any > ,
104- agent ?: Agent < any , any > ,
105- ) : Promise < any [ ] > ;
97+ abstract listTools ( ) : Promise < any [ ] > ;
10698 abstract callTool (
10799 _toolName : string ,
108100 _args : Record < string , unknown > | null ,
@@ -157,14 +149,11 @@ export class MCPServerStdio extends BaseMCPServerStdio {
157149 close ( ) : Promise < void > {
158150 return this . underlying . close ( ) ;
159151 }
160- async listTools (
161- runContext ?: RunContext < any > ,
162- agent ?: Agent < any , any > ,
163- ) : Promise < MCPTool [ ] > {
152+ async listTools ( ) : Promise < MCPTool [ ] > {
164153 if ( this . cacheToolsList && this . _cachedTools ) {
165154 return this . _cachedTools ;
166155 }
167- const tools = await this . underlying . listTools ( runContext , agent ) ;
156+ const tools = await this . underlying . listTools ( ) ;
168157 if ( this . cacheToolsList ) {
169158 this . _cachedTools = tools ;
170159 }
@@ -196,14 +185,11 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
196185 close ( ) : Promise < void > {
197186 return this . underlying . close ( ) ;
198187 }
199- async listTools (
200- runContext ?: RunContext < any > ,
201- agent ?: Agent < any , any > ,
202- ) : Promise < MCPTool [ ] > {
188+ async listTools ( ) : Promise < MCPTool [ ] > {
203189 if ( this . cacheToolsList && this . _cachedTools ) {
204190 return this . _cachedTools ;
205191 }
206- const tools = await this . underlying . listTools ( runContext , agent ) ;
192+ const tools = await this . underlying . listTools ( ) ;
207193 if ( this . cacheToolsList ) {
208194 this . _cachedTools = tools ;
209195 }
@@ -226,18 +212,18 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
226212 */
227213export async function getAllMcpFunctionTools < TContext = UnknownContext > (
228214 mcpServers : MCPServer [ ] ,
215+ runContext : RunContext < TContext > ,
216+ agent : Agent < any , any > ,
229217 convertSchemasToStrict = false ,
230- runContext ?: RunContext < TContext > ,
231- agent ?: Agent < TContext , any > ,
232218) : Promise < Tool < TContext > [ ] > {
233219 const allTools : Tool < TContext > [ ] = [ ] ;
234220 const toolNames = new Set < string > ( ) ;
235221 for ( const server of mcpServers ) {
236222 const serverTools = await getFunctionToolsFromServer (
237223 server ,
238- convertSchemasToStrict ,
239224 runContext ,
240225 agent ,
226+ convertSchemasToStrict ,
241227 ) ;
242228 const serverToolNames = new Set ( serverTools . map ( ( t ) => t . name ) ) ;
243229 const intersection = [ ...serverToolNames ] . filter ( ( n ) => toolNames . has ( n ) ) ;
@@ -268,9 +254,9 @@ export async function invalidateServerToolsCache(serverName: string) {
268254 */
269255async function getFunctionToolsFromServer < TContext = UnknownContext > (
270256 server : MCPServer ,
257+ runContext : RunContext < TContext > ,
258+ agent : Agent < any , any > ,
271259 convertSchemasToStrict : boolean ,
272- runContext ?: RunContext < TContext > ,
273- agent ?: Agent < TContext , any > ,
274260) : Promise < FunctionTool < TContext , any , unknown > [ ] > {
275261 if ( server . cacheToolsList && _cachedTools [ server . name ] ) {
276262 return _cachedTools [ server . name ] . map ( ( t ) =>
@@ -279,7 +265,53 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
279265 }
280266 return withMCPListToolsSpan (
281267 async ( span ) => {
282- const mcpTools = await server . listTools ( runContext , agent ) ;
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+ }
283315 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
284316 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
285317 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -298,15 +330,15 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
298330 */
299331export async function getAllMcpTools < TContext = UnknownContext > (
300332 mcpServers : MCPServer [ ] ,
333+ runContext : RunContext < TContext > ,
334+ agent : Agent < TContext , any > ,
301335 convertSchemasToStrict = false ,
302- runContext ?: RunContext < TContext > ,
303- agent ?: Agent < TContext , any > ,
304336) : Promise < Tool < TContext > [ ] > {
305337 return getAllMcpFunctionTools (
306338 mcpServers ,
307- convertSchemasToStrict ,
308339 runContext ,
309340 agent ,
341+ convertSchemasToStrict ,
310342 ) ;
311343}
312344
0 commit comments