|
| 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