Skip to content

Commit 06cf8b9

Browse files
committed
revert: restore MCPServer interface and listTools signature
Reverts prior tool filtering interface changes and updates corresponding tests.
1 parent af7feeb commit 06cf8b9

File tree

2 files changed

+10
-103
lines changed

2 files changed

+10
-103
lines changed

packages/agents-core/src/mcp.ts

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2218
import type { RunContext } from './runContext';
2319
import type { Agent } from './agent';
2420

@@ -34,7 +30,6 @@ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
3430
*/
3531
export 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 {
5247
export 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 {
9085
export 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.

packages/agents-core/test/mcpToolFilter.test.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, it, expect } from 'vitest';
22
import { withTrace } from '../src/tracing';
33
import { NodeMCPServerStdio } from '../src/shims/mcp-server/node';
44
import { createStaticToolFilter } from '../src/mcpUtil';
5-
import { getAllMcpTools } from '../src/mcp';
65
import { Agent } from '../src/agent';
76
import { RunContext } from '../src/runContext';
87

@@ -199,47 +198,4 @@ describe('MCP tool filtering', () => {
199198
expect(result.map((t) => t.name)).toEqual(['x']);
200199
});
201200
});
202-
203-
it('applies filter in getAllMcpTools', async () => {
204-
await withTrace('test', async () => {
205-
const tools = [
206-
{
207-
name: 'allow',
208-
description: '',
209-
inputSchema: {
210-
type: 'object',
211-
properties: {},
212-
required: [],
213-
additionalProperties: false,
214-
},
215-
},
216-
{
217-
name: 'block',
218-
description: '',
219-
inputSchema: {
220-
type: 'object',
221-
properties: {},
222-
required: [],
223-
additionalProperties: false,
224-
},
225-
},
226-
];
227-
const server = new StubServer(
228-
'filter',
229-
tools,
230-
createStaticToolFilter({ allowed: ['allow'] }),
231-
);
232-
const agent = new Agent({
233-
name: 'agent',
234-
instructions: '',
235-
model: '',
236-
modelSettings: {},
237-
tools: [],
238-
mcpServers: [server],
239-
});
240-
const runContext = new RunContext();
241-
const result = await getAllMcpTools([server], false, runContext, agent);
242-
expect(result.map((t) => t.name)).toEqual(['allow']);
243-
});
244-
});
245201
});

0 commit comments

Comments
 (0)