Skip to content

Commit 4dce2d5

Browse files
committed
revert: restore MCPServer interface and listTools signature
Reverts prior tool filtering interface changes and updates corresponding tests.
1 parent 713f8e0 commit 4dce2d5

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>;
@@ -51,7 +46,7 @@ export interface MCPServer {
5146
export abstract class BaseMCPServerStdio implements MCPServer {
5247
public cacheToolsList: boolean;
5348
protected _cachedTools: any[] | undefined = undefined;
54-
public toolFilter?: ToolFilterCallable | ToolFilterStatic;
49+
protected toolFilter?: ToolFilterCallable | ToolFilterStatic;
5550

5651
protected logger: Logger;
5752
constructor(options: MCPServerStdioOptions) {
@@ -88,7 +83,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
8883
export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8984
public cacheToolsList: boolean;
9085
protected _cachedTools: any[] | undefined = undefined;
91-
public toolFilter?: ToolFilterCallable | ToolFilterStatic;
86+
protected toolFilter?: ToolFilterCallable | ToolFilterStatic;
9287

9388
protected logger: Logger;
9489
constructor(options: MCPServerStreamableHttpOptions) {
@@ -160,13 +155,13 @@ export class MCPServerStdio extends BaseMCPServerStdio {
160155
return this.underlying.close();
161156
}
162157
async listTools(
163-
_runContext?: RunContext<any>,
164-
_agent?: Agent<any, any>,
158+
runContext?: RunContext<any>,
159+
agent?: Agent<any, any>,
165160
): Promise<MCPTool[]> {
166161
if (this.cacheToolsList && this._cachedTools) {
167162
return this._cachedTools;
168163
}
169-
const tools = await this.underlying.listTools();
164+
const tools = await this.underlying.listTools(runContext, agent);
170165
if (this.cacheToolsList) {
171166
this._cachedTools = tools;
172167
}
@@ -196,13 +191,13 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
196191
return this.underlying.close();
197192
}
198193
async listTools(
199-
_runContext?: RunContext<any>,
200-
_agent?: Agent<any, any>,
194+
runContext?: RunContext<any>,
195+
agent?: Agent<any, any>,
201196
): Promise<MCPTool[]> {
202197
if (this.cacheToolsList && this._cachedTools) {
203198
return this._cachedTools;
204199
}
205-
const tools = await this.underlying.listTools();
200+
const tools = await this.underlying.listTools(runContext, agent);
206201
if (this.cacheToolsList) {
207202
this._cachedTools = tools;
208203
}
@@ -273,16 +268,7 @@ async function getFunctionToolsFromServer<TContext = UnknownContext>(
273268
}
274269
return withMCPListToolsSpan(
275270
async (span) => {
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-
}
271+
const mcpTools = await server.listTools(runContext, agent);
286272
span.spanData.result = mcpTools.map((t) => t.name);
287273
const tools: FunctionTool<TContext, any, string>[] = mcpTools.map((t) =>
288274
mcpToFunctionTool(t, server, convertSchemasToStrict),
@@ -385,41 +371,6 @@ function ensureStrictJsonSchema(
385371
return out;
386372
}
387373

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-
423374
/**
424375
* Abstract base class for MCP servers that use a ClientSession for communication.
425376
* 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)