@@ -14,11 +14,7 @@ import {
1414 JsonObjectSchemaStrict ,
1515 UnknownContext ,
1616} from './types' ;
17- import type {
18- ToolFilterCallable ,
19- ToolFilterStatic ,
20- ToolFilterContext ,
21- } from './mcpUtil' ;
17+ import type { ToolFilterCallable , ToolFilterStatic } from './mcpUtil' ;
2218import type { RunContext } from './runContext' ;
2319import type { Agent } from './agent' ;
2420
@@ -34,7 +30,6 @@ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
3430 */
3531export interface MCPServer {
3632 cacheToolsList : boolean ;
37- toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
3833 connect ( ) : Promise < void > ;
3934 readonly name : string ;
4035 close ( ) : Promise < void > ;
@@ -52,7 +47,7 @@ export interface MCPServer {
5247export abstract class BaseMCPServerStdio implements MCPServer {
5348 public cacheToolsList : boolean ;
5449 protected _cachedTools : any [ ] | undefined = undefined ;
55- public toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
50+ protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
5651
5752 protected logger : Logger ;
5853 constructor ( options : MCPServerStdioOptions ) {
@@ -90,7 +85,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
9085export abstract class BaseMCPServerStreamableHttp implements MCPServer {
9186 public cacheToolsList : boolean ;
9287 protected _cachedTools : any [ ] | undefined = undefined ;
93- public toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
88+ protected toolFilter ?: ToolFilterCallable | ToolFilterStatic ;
9489
9590 protected logger : Logger ;
9691 constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -163,13 +158,13 @@ export class MCPServerStdio extends BaseMCPServerStdio {
163158 return this . underlying . close ( ) ;
164159 }
165160 async listTools (
166- _runContext ?: RunContext < any > ,
167- _agent ?: Agent < any , any > ,
161+ runContext ?: RunContext < any > ,
162+ agent ?: Agent < any , any > ,
168163 ) : Promise < MCPTool [ ] > {
169164 if ( this . cacheToolsList && this . _cachedTools ) {
170165 return this . _cachedTools ;
171166 }
172- const tools = await this . underlying . listTools ( ) ;
167+ const tools = await this . underlying . listTools ( runContext , agent ) ;
173168 if ( this . cacheToolsList ) {
174169 this . _cachedTools = tools ;
175170 }
@@ -202,13 +197,13 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
202197 return this . underlying . close ( ) ;
203198 }
204199 async listTools (
205- _runContext ?: RunContext < any > ,
206- _agent ?: Agent < any , any > ,
200+ runContext ?: RunContext < any > ,
201+ agent ?: Agent < any , any > ,
207202 ) : Promise < MCPTool [ ] > {
208203 if ( this . cacheToolsList && this . _cachedTools ) {
209204 return this . _cachedTools ;
210205 }
211- const tools = await this . underlying . listTools ( ) ;
206+ const tools = await this . underlying . listTools ( runContext , agent ) ;
212207 if ( this . cacheToolsList ) {
213208 this . _cachedTools = tools ;
214209 }
@@ -284,16 +279,7 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
284279 }
285280 return withMCPListToolsSpan (
286281 async ( span ) => {
287- let mcpTools = await server . listTools ( runContext , agent ) ;
288- if ( server . toolFilter ) {
289- mcpTools = await filterMcpTools (
290- mcpTools ,
291- server . toolFilter as ToolFilterCallable < TContext > | ToolFilterStatic ,
292- runContext ,
293- agent ,
294- server . name ,
295- ) ;
296- }
282+ const mcpTools = await server . listTools ( runContext , agent ) ;
297283 span . spanData . result = mcpTools . map ( ( t ) => t . name ) ;
298284 const tools : FunctionTool < TContext , any , string > [ ] = mcpTools . map ( ( t ) =>
299285 mcpToFunctionTool ( t , server , convertSchemasToStrict ) ,
@@ -396,41 +382,6 @@ function ensureStrictJsonSchema(
396382 return out ;
397383}
398384
399- async function filterMcpTools < TContext = UnknownContext > (
400- tools : MCPTool [ ] ,
401- filter : ToolFilterCallable < TContext > | ToolFilterStatic ,
402- runContext : RunContext < TContext > | undefined ,
403- agent : Agent < TContext , any > | undefined ,
404- serverName : string ,
405- ) : Promise < MCPTool [ ] > {
406- if ( typeof filter === 'function' ) {
407- if ( ! runContext || ! agent ) {
408- return tools ;
409- }
410- const ctx = {
411- runContext,
412- agent,
413- serverName,
414- } as ToolFilterContext < TContext > ;
415- const result : MCPTool [ ] = [ ] ;
416- for ( const tool of tools ) {
417- if ( await filter ( ctx , tool ) ) {
418- result . push ( tool ) ;
419- }
420- }
421- return result ;
422- }
423- return tools . filter ( ( t ) => {
424- if ( filter . allowedToolNames && ! filter . allowedToolNames . includes ( t . name ) ) {
425- return false ;
426- }
427- if ( filter . blockedToolNames && filter . blockedToolNames . includes ( t . name ) ) {
428- return false ;
429- }
430- return true ;
431- } ) ;
432- }
433-
434385/**
435386 * Abstract base class for MCP servers that use a ClientSession for communication.
436387 * Handles session management, tool listing, tool calling, and cleanup.
0 commit comments