Skip to content

Commit d7ab6fd

Browse files
committed
refactor(core): prefix tool filter names with MCP
1 parent 4e5c53d commit d7ab6fd

File tree

6 files changed

+32
-26
lines changed

6 files changed

+32
-26
lines changed

docs/src/content/docs/guides/mcp.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,21 @@ Only enable this if you're confident the tool list won't change. To invalidate t
100100
### Tool filtering
101101

102102
You can restrict which tools are exposed from each server. Pass either a static filter
103-
using `createStaticToolFilter` or a custom function:
103+
using `createMCPToolStaticFilter` or a custom function:
104104

105105
```ts
106106
const server = new MCPServerStdio({
107107
fullCommand: 'my-server',
108-
toolFilter: createStaticToolFilter({ allowed: ['safe_tool'], blocked: ['danger_tool'] }),
108+
toolFilter: createMCPToolStaticFilter({
109+
allowed: ['safe_tool'],
110+
blocked: ['danger_tool'],
111+
}),
109112
});
110113

111114
const dynamicServer = new MCPServerStreamableHttp({
112115
url: 'http://localhost:3000',
113-
toolFilter: ({ runContext }, tool) => runContext.context.allowAll || tool.name !== 'admin',
116+
toolFilter: ({ runContext }, tool) =>
117+
runContext.context.allowAll || tool.name !== 'admin',
114118
});
115119
```
116120

examples/mcp/tool-filter-example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
Agent,
33
run,
44
MCPServerStdio,
5-
createStaticToolFilter,
5+
createMCPToolStaticFilter,
66
withTrace,
77
} from '@openai/agents';
88
import * as path from 'node:path';
@@ -12,7 +12,7 @@ async function main() {
1212
const mcpServer = new MCPServerStdio({
1313
name: 'Filesystem Server with filter',
1414
fullCommand: `npx -y @modelcontextprotocol/server-filesystem ${samplesDir}`,
15-
toolFilter: createStaticToolFilter({
15+
toolFilter: createMCPToolStaticFilter({
1616
allowed: ['read_file', 'list_directory'],
1717
blocked: ['write_file'],
1818
}),

packages/agents-core/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export {
7575
MCPServerStreamableHttp,
7676
} from './mcp';
7777
export {
78-
ToolFilterCallable,
79-
ToolFilterContext,
80-
ToolFilterStatic,
81-
createStaticToolFilter,
78+
MCPToolFilterCallable,
79+
MCPToolFilterContext,
80+
MCPToolFilterStatic,
81+
createMCPToolStaticFilter,
8282
} from './mcpUtil';
8383
export {
8484
Model,

packages/agents-core/src/mcp.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
JsonObjectSchemaStrict,
1515
UnknownContext,
1616
} from './types';
17-
import type { ToolFilterCallable, ToolFilterStatic } from './mcpUtil';
17+
import type { MCPToolFilterCallable, MCPToolFilterStatic } from './mcpUtil';
1818
import type { RunContext } from './runContext';
1919
import type { Agent } from './agent';
2020

@@ -47,7 +47,7 @@ export interface MCPServer {
4747
export abstract class BaseMCPServerStdio implements MCPServer {
4848
public cacheToolsList: boolean;
4949
protected _cachedTools: any[] | undefined = undefined;
50-
protected toolFilter?: ToolFilterCallable | ToolFilterStatic;
50+
protected toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
5151

5252
protected logger: Logger;
5353
constructor(options: MCPServerStdioOptions) {
@@ -85,7 +85,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
8585
export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8686
public cacheToolsList: boolean;
8787
protected _cachedTools: any[] | undefined = undefined;
88-
protected toolFilter?: ToolFilterCallable | ToolFilterStatic;
88+
protected toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
8989

9090
protected logger: Logger;
9191
constructor(options: MCPServerStreamableHttpOptions) {
@@ -398,7 +398,7 @@ export interface BaseMCPServerStdioOptions {
398398
encoding?: string;
399399
encodingErrorHandler?: 'strict' | 'ignore' | 'replace';
400400
logger?: Logger;
401-
toolFilter?: ToolFilterCallable | ToolFilterStatic;
401+
toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
402402
}
403403
export interface DefaultMCPServerStdioOptions
404404
extends BaseMCPServerStdioOptions {
@@ -419,7 +419,7 @@ export interface MCPServerStreamableHttpOptions {
419419
clientSessionTimeoutSeconds?: number;
420420
name?: string;
421421
logger?: Logger;
422-
toolFilter?: ToolFilterCallable | ToolFilterStatic;
422+
toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
423423

424424
// ----------------------------------------------------
425425
// OAuth

packages/agents-core/src/mcpUtil.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { MCPTool } from './mcp';
44
import type { UnknownContext } from './types';
55

66
/** Context information available to tool filter functions. */
7-
export interface ToolFilterContext<TContext = UnknownContext> {
7+
export interface MCPToolFilterContext<TContext = UnknownContext> {
88
/** The current run context. */
99
runContext: RunContext<TContext>;
1010
/** The agent requesting the tools. */
@@ -14,28 +14,28 @@ export interface ToolFilterContext<TContext = UnknownContext> {
1414
}
1515

1616
/** A function that determines whether a tool should be available. */
17-
export type ToolFilterCallable<TContext = UnknownContext> = (
18-
context: ToolFilterContext<TContext>,
17+
export type MCPToolFilterCallable<TContext = UnknownContext> = (
18+
context: MCPToolFilterContext<TContext>,
1919
tool: MCPTool,
2020
) => boolean | Promise<boolean>;
2121

2222
/** Static tool filter configuration using allow and block lists. */
23-
export interface ToolFilterStatic {
23+
export interface MCPToolFilterStatic {
2424
/** Optional list of tool names to allow. */
2525
allowedToolNames?: string[];
2626
/** Optional list of tool names to block. */
2727
blockedToolNames?: string[];
2828
}
2929

3030
/** Convenience helper to create a static tool filter. */
31-
export function createStaticToolFilter(options?: {
31+
export function createMCPToolStaticFilter(options?: {
3232
allowed?: string[];
3333
blocked?: string[];
34-
}): ToolFilterStatic | undefined {
34+
}): MCPToolFilterStatic | undefined {
3535
if (!options?.allowed && !options?.blocked) {
3636
return undefined;
3737
}
38-
const filter: ToolFilterStatic = {};
38+
const filter: MCPToolFilterStatic = {};
3939
if (options?.allowed) {
4040
filter.allowedToolNames = options.allowed;
4141
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22
import { withTrace } from '../src/tracing';
33
import { NodeMCPServerStdio } from '../src/shims/mcp-server/node';
4-
import { createStaticToolFilter } from '../src/mcpUtil';
4+
import { createMCPToolStaticFilter } from '../src/mcpUtil';
55
import { Agent } from '../src/agent';
66
import { RunContext } from '../src/runContext';
77

@@ -49,7 +49,7 @@ describe('MCP tool filtering', () => {
4949
const server = new StubServer(
5050
's',
5151
tools,
52-
createStaticToolFilter({ allowed: ['a'], blocked: ['b'] }),
52+
createMCPToolStaticFilter({ allowed: ['a'], blocked: ['b'] }),
5353
);
5454
const agent = new Agent({
5555
name: 'agent',
@@ -144,7 +144,7 @@ describe('MCP tool filtering', () => {
144144
const serverA = new StubServer(
145145
'A',
146146
toolsA,
147-
createStaticToolFilter({ allowed: ['a1'] }),
147+
createMCPToolStaticFilter({ allowed: ['a1'] }),
148148
);
149149
const serverB = new StubServer('B', toolsB);
150150
const agent = new Agent({
@@ -180,7 +180,7 @@ describe('MCP tool filtering', () => {
180180
const server = new StubServer(
181181
'cache',
182182
tools,
183-
createStaticToolFilter({ allowed: ['x'] }),
183+
createMCPToolStaticFilter({ allowed: ['x'] }),
184184
);
185185
const agent = new Agent({
186186
name: 'agent',
@@ -193,7 +193,9 @@ describe('MCP tool filtering', () => {
193193
const runContext = new RunContext();
194194
let result = await server.listTools(runContext, agent);
195195
expect(result.map((t) => t.name)).toEqual(['x']);
196-
(server as any).toolFilter = createStaticToolFilter({ allowed: ['y'] });
196+
(server as any).toolFilter = createMCPToolStaticFilter({
197+
allowed: ['y'],
198+
});
197199
result = await server.listTools(runContext, agent);
198200
expect(result.map((t) => t.name)).toEqual(['x']);
199201
});

0 commit comments

Comments
 (0)