Skip to content

Commit 4c1adbd

Browse files
committed
refactor(core): prefix tool filter names with MCP
1 parent 47ae347 commit 4c1adbd

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
@@ -74,10 +74,10 @@ export {
7474
MCPServerStreamableHttp,
7575
} from './mcp';
7676
export {
77-
ToolFilterCallable,
78-
ToolFilterContext,
79-
ToolFilterStatic,
80-
createStaticToolFilter,
77+
MCPToolFilterCallable,
78+
MCPToolFilterContext,
79+
MCPToolFilterStatic,
80+
createMCPToolStaticFilter,
8181
} from './mcpUtil';
8282
export {
8383
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

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

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

8888
protected logger: Logger;
8989
constructor(options: MCPServerStreamableHttpOptions) {
@@ -386,7 +386,7 @@ export interface BaseMCPServerStdioOptions {
386386
encoding?: string;
387387
encodingErrorHandler?: 'strict' | 'ignore' | 'replace';
388388
logger?: Logger;
389-
toolFilter?: ToolFilterCallable | ToolFilterStatic;
389+
toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
390390
}
391391
export interface DefaultMCPServerStdioOptions
392392
extends BaseMCPServerStdioOptions {
@@ -407,7 +407,7 @@ export interface MCPServerStreamableHttpOptions {
407407
clientSessionTimeoutSeconds?: number;
408408
name?: string;
409409
logger?: Logger;
410-
toolFilter?: ToolFilterCallable | ToolFilterStatic;
410+
toolFilter?: MCPToolFilterCallable | MCPToolFilterStatic;
411411

412412
// ----------------------------------------------------
413413
// 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)