|
| 1 | +import 'dotenv/config'; |
| 2 | +import Sandbox from 'e2b'; |
| 3 | +import { Agent, run, MCPServerStreamableHttp } from '@openai/agents'; |
| 4 | + |
| 5 | +async function runBrowserbaseExample() { |
| 6 | + console.log('Creating E2B sandbox with Browserbase MCP server...'); |
| 7 | + |
| 8 | + // Create E2B sandbox with Browserbase MCP server |
| 9 | + const sandbox = await Sandbox.create({ |
| 10 | + mcp: { |
| 11 | + browserbase: { |
| 12 | + apiKey: process.env.BROWSERBASE_API_KEY!, |
| 13 | + geminiApiKey: process.env.GEMINI_API_KEY!, |
| 14 | + projectId: process.env.BROWSERBASE_PROJECT_ID!, |
| 15 | + }, |
| 16 | + }, |
| 17 | + timeoutMs: 600_000, // 10 minutes |
| 18 | + }); |
| 19 | + |
| 20 | + const mcpUrl = sandbox.getMcpUrl(); |
| 21 | + console.log(`Sandbox created with MCP URL: ${mcpUrl}`); |
| 22 | + |
| 23 | + // Set up MCP server connection |
| 24 | + const mcpServer = new MCPServerStreamableHttp({ |
| 25 | + url: mcpUrl, |
| 26 | + name: 'E2B Browserbase Gateway', |
| 27 | + requestInit: { |
| 28 | + headers: { |
| 29 | + 'Authorization': `Bearer ${await sandbox.getMcpToken()}` |
| 30 | + } |
| 31 | + }, |
| 32 | + toolFilter: async (_, tool) => { |
| 33 | + console.log(`Calling tool: ${tool.name}`); |
| 34 | + return true; |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + // Create OpenAI agent |
| 39 | + const agent = new Agent({ |
| 40 | + name: 'Web Automation Assistant', |
| 41 | + model: 'gpt-4o', // Using gpt-4o for compatibility |
| 42 | + mcpServers: [mcpServer], |
| 43 | + }); |
| 44 | + |
| 45 | + console.log('Connecting to MCP server...'); |
| 46 | + await mcpServer.connect(); |
| 47 | + console.log('Connected to MCP server successfully'); |
| 48 | + |
| 49 | + console.log('Starting web automation task...'); |
| 50 | + const taskPrompt = 'Make a screenshot of the e2b.dev landing page and tell me what it is about.'; |
| 51 | + |
| 52 | + const result = await run( |
| 53 | + agent, |
| 54 | + taskPrompt, |
| 55 | + { |
| 56 | + stream: true, |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + console.log('\nWeb Automation Results:'); |
| 61 | + result |
| 62 | + .toTextStream({ compatibleWithNodeStreams: true }) |
| 63 | + .pipe(process.stdout); |
| 64 | + |
| 65 | + await result.completed; |
| 66 | + console.log('\nWeb automation completed successfully!'); |
| 67 | + |
| 68 | + // Cleanup |
| 69 | + console.log('\nClosing MCP server connection...'); |
| 70 | + await mcpServer.close(); |
| 71 | + console.log('MCP server closed successfully'); |
| 72 | + |
| 73 | + console.log('Cleaning up sandbox...'); |
| 74 | + await sandbox.kill(); |
| 75 | + console.log('Sandbox closed successfully'); |
| 76 | +} |
| 77 | + |
| 78 | +// Run the browserbase example |
| 79 | +runBrowserbaseExample().catch((error) => { |
| 80 | + console.error('Failed to run browserbase example:', error); |
| 81 | + process.exit(1); |
| 82 | +}); |
0 commit comments