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
JavaScript
/**
* 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;