Skip to content

Commit 4161253

Browse files
committed
feat(examples): add MCP tool-filter example
1 parent 39f74dd commit 4161253

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

examples/mcp/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ Run the example from the repository root:
1212
```bash
1313
pnpm -F mcp start:stdio
1414
```
15+
16+
`tool-filter-example.ts` shows how to expose only a subset of server tools:
17+
18+
```bash
19+
pnpm -F mcp start:tool-filter
20+
```

examples/mcp/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"start:streamable-http": "tsx streamable-http-example.ts",
1313
"start:hosted-mcp-on-approval": "tsx hosted-mcp-on-approval.ts",
1414
"start:hosted-mcp-human-in-the-loop": "tsx hosted-mcp-human-in-the-loop.ts",
15-
"start:hosted-mcp-simple": "tsx hosted-mcp-simple.ts"
15+
"start:hosted-mcp-simple": "tsx hosted-mcp-simple.ts",
16+
"start:tool-filter": "tsx tool-filter-example.ts"
1617
}
1718
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
Agent,
3+
run,
4+
MCPServerStdio,
5+
createStaticToolFilter,
6+
withTrace,
7+
} from '@openai/agents';
8+
import * as path from 'node:path';
9+
10+
async function main() {
11+
const samplesDir = path.join(__dirname, 'sample_files');
12+
const mcpServer = new MCPServerStdio({
13+
name: 'Filesystem Server with filter',
14+
fullCommand: `npx -y @modelcontextprotocol/server-filesystem ${samplesDir}`,
15+
toolFilter: createStaticToolFilter(
16+
['read_file', 'list_directory'],
17+
['write_file'],
18+
),
19+
});
20+
21+
await mcpServer.connect();
22+
23+
try {
24+
await withTrace('MCP Tool Filter Example', async () => {
25+
const agent = new Agent({
26+
name: 'MCP Assistant',
27+
instructions:
28+
'Use the filesystem tools to answer questions. The write_file tool is blocked via toolFilter.',
29+
mcpServers: [mcpServer],
30+
});
31+
32+
console.log('Listing sample files:');
33+
let result = await run(agent, 'List the files in the current directory.');
34+
console.log(result.finalOutput);
35+
36+
console.log('\nAttempting to write a file (should be blocked):');
37+
result = await run(
38+
agent,
39+
'Create a file named test.txt with the text "hello"',
40+
);
41+
console.log(result.finalOutput);
42+
});
43+
} finally {
44+
await mcpServer.close();
45+
}
46+
}
47+
48+
main().catch((err) => {
49+
console.error(err);
50+
process.exit(1);
51+
});

0 commit comments

Comments
 (0)