|
| 1 | +import * as readline from 'readline/promises'; |
| 2 | +import { stdin, stdout } from 'node:process'; |
| 3 | +import { Agent, run, hostedMcpTool, RunToolApprovalItem } from '@openai/agents'; |
| 4 | + |
| 5 | +async function promptApproval(item: RunToolApprovalItem): Promise<boolean> { |
| 6 | + const rl = readline.createInterface({ input: stdin, output: stdout }); |
| 7 | + const name = item.rawItem.name; |
| 8 | + const params = JSON.parse(item.rawItem.providerData?.arguments || '{}'); |
| 9 | + const answer = await rl.question( |
| 10 | + `Approve running tool (mcp: ${name}, params: ${JSON.stringify(params)})? (y/n) `, |
| 11 | + ); |
| 12 | + rl.close(); |
| 13 | + return answer.toLowerCase().trim() === 'y'; |
| 14 | +} |
| 15 | + |
| 16 | +async function main(verbose: boolean, stream: boolean): Promise<void> { |
| 17 | + // 'always' | |
| 18 | + // 'never' | |
| 19 | + // { never?: { toolNames: string[] }; always?: { toolNames: string[] } } |
| 20 | + const requireApproval = { |
| 21 | + never: { toolNames: ['search_codex_code', 'fetch_codex_documentation'] }, |
| 22 | + always: { toolNames: ['fetch_generic_url_content'] }, |
| 23 | + }; |
| 24 | + const agent = new Agent({ |
| 25 | + name: 'MCP Assistant', |
| 26 | + instructions: 'You must always use the MCP tools to answer questions.', |
| 27 | + tools: [ |
| 28 | + hostedMcpTool({ |
| 29 | + serverLabel: 'gitmcp', |
| 30 | + serverUrl: 'https://gitmcp.io/openai/codex', |
| 31 | + requireApproval, |
| 32 | + // when you don't pass onApproval, the agent loop will handle the approval process |
| 33 | + }), |
| 34 | + ], |
| 35 | + }); |
| 36 | + |
| 37 | + const input = 'Which language is this repo written in?'; |
| 38 | + |
| 39 | + if (stream) { |
| 40 | + // Streaming |
| 41 | + const result = await run(agent, input, { stream: true, maxTurns: 100 }); |
| 42 | + for await (const event of result) { |
| 43 | + if (verbose) { |
| 44 | + console.log(JSON.stringify(event, null, 2)); |
| 45 | + } else { |
| 46 | + if ( |
| 47 | + event.type === 'raw_model_stream_event' && |
| 48 | + event.data.type === 'model' |
| 49 | + ) { |
| 50 | + console.log(event.data.event.type); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + console.log(`Done streaming; final result: ${result.finalOutput}`); |
| 55 | + } else { |
| 56 | + // Non-streaming |
| 57 | + let result = await run(agent, input, { maxTurns: 100 }); |
| 58 | + while (result.interruptions && result.interruptions.length) { |
| 59 | + for (const interruption of result.interruptions) { |
| 60 | + const approval = await promptApproval(interruption); |
| 61 | + if (approval) { |
| 62 | + result.state.approve(interruption); |
| 63 | + } else { |
| 64 | + result.state.reject(interruption); |
| 65 | + } |
| 66 | + } |
| 67 | + result = await run(agent, result.state, { maxTurns: 100 }); |
| 68 | + } |
| 69 | + console.log(result.finalOutput); |
| 70 | + |
| 71 | + if (verbose) { |
| 72 | + console.log('----------------------------------------------------------'); |
| 73 | + console.log(JSON.stringify(result.newItems, null, 2)); |
| 74 | + console.log('----------------------------------------------------------'); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const args = process.argv.slice(2); |
| 80 | +const verbose = args.includes('--verbose'); |
| 81 | +const stream = args.includes('--stream'); |
| 82 | + |
| 83 | +main(verbose, stream).catch((err) => { |
| 84 | + console.error(err); |
| 85 | + process.exit(1); |
| 86 | +}); |
0 commit comments