UNPKG

browser-x-mcp

Version:

AI-Powered Browser Automation with Advanced Form Testing - A Model Context Provider (MCP) server that enables intelligent browser automation with form testing, element extraction, and comprehensive logging

95 lines (75 loc) • 2.84 kB
#!/usr/bin/env node /** * Browser[X]MCP Daemon Server * Long-running MCP server with persistent browser instance */ import BrowserXMCPServer from './index.js'; class BrowserXMCPDaemon { constructor() { this.server = new BrowserXMCPServer(); this.isRunning = false; } async start() { if (this.isRunning) { console.error('šŸ”„ Daemon already running'); return; } try { console.error('šŸš€ Starting Browser[X]MCP Daemon...'); // Start the MCP server await this.server.run(); this.isRunning = true; console.error('āœ… Daemon started successfully!'); console.error('🌐 Browser instance will persist between MCP calls'); // Keep process alive this.setupSignalHandlers(); } catch (error) { console.error('āŒ Failed to start daemon:', error.message); process.exit(1); } } setupSignalHandlers() { // Graceful shutdown on SIGINT (Ctrl+C) process.on('SIGINT', async () => { console.error('\nšŸ›‘ Received SIGINT, shutting down gracefully...'); await this.shutdown(); }); // Graceful shutdown on SIGTERM process.on('SIGTERM', async () => { console.error('\nšŸ›‘ Received SIGTERM, shutting down gracefully...'); await this.shutdown(); }); // Handle uncaught exceptions process.on('uncaughtException', (error) => { console.error('āŒ Uncaught Exception:', error); this.shutdown().then(() => process.exit(1)); }); // Handle unhandled promise rejections process.on('unhandledRejection', (reason, promise) => { console.error('āŒ Unhandled Rejection at:', promise, 'reason:', reason); }); } async shutdown() { if (!this.isRunning) return; try { console.error('šŸ”„ Shutting down Browser[X]MCP Daemon...'); // Close browser if running if (this.server.browserInstance?.browser) { await this.server.browserInstance.browser.close(); console.error('🌐 Browser closed'); } this.isRunning = false; console.error('āœ… Daemon shut down successfully'); } catch (error) { console.error('āŒ Error during shutdown:', error.message); } finally { process.exit(0); } } } // Start daemon if run directly if (import.meta.url === `file://${process.argv[1]}`) { const daemon = new BrowserXMCPDaemon(); daemon.start().catch(console.error); } export default BrowserXMCPDaemon;