index.ts•3.52 kB
#!/usr/bin/env node
/**
* ByteBot MCP Server Entry Point
*
* Loads configuration from environment variables and starts the MCP server
*/
import * as dotenv from 'dotenv';
import { ByteBotMCPServer } from './server.js';
import { EnvironmentConfig } from './types/mcp.js';
// Load environment variables
dotenv.config();
/**
* Load configuration from environment variables
*/
function loadConfig(): EnvironmentConfig {
return {
// ByteBot endpoints
agentUrl: process.env.BYTEBOT_AGENT_URL || 'http://localhost:9991',
desktopUrl: process.env.BYTEBOT_DESKTOP_URL || 'http://localhost:9990',
wsUrl: process.env.BYTEBOT_WS_URL || 'ws://localhost:9991',
// Server configuration
serverName: process.env.MCP_SERVER_NAME || 'bytebot-mcp',
enableWebSocket: process.env.ENABLE_WEBSOCKET === 'true',
// HTTP client configuration
requestTimeout: parseInt(process.env.REQUEST_TIMEOUT || '30000', 10),
desktopActionTimeout: parseInt(process.env.DESKTOP_ACTION_TIMEOUT || '10000', 10),
maxRetries: parseInt(process.env.MAX_RETRIES || '3', 10),
retryDelay: parseInt(process.env.RETRY_DELAY || '1000', 10),
// Monitoring configuration
taskPollInterval: parseInt(process.env.TASK_POLL_INTERVAL || '2000', 10),
taskMonitorTimeout: parseInt(process.env.TASK_MONITOR_TIMEOUT || '300000', 10),
// File configuration
maxFileSize: parseInt(process.env.MAX_FILE_SIZE || '10485760', 10), // 10MB
// Logging
logLevel: (process.env.LOG_LEVEL as any) || 'info',
};
}
/**
* Main entry point
*/
async function main() {
console.log('========================================');
console.log('ByteBot MCP Server');
console.log('========================================');
console.log('');
try {
// Load configuration
const config = loadConfig();
// Display configuration
console.log('[ByteBot MCP] Configuration:');
console.log(` Server Name: ${config.serverName}`);
console.log(` Agent API: ${config.agentUrl}`);
console.log(` Desktop API: ${config.desktopUrl}`);
console.log(` WebSocket: ${config.enableWebSocket ? 'Enabled' : 'Disabled'}`);
if (config.enableWebSocket) {
console.log(` WebSocket URL: ${config.wsUrl}`);
}
console.log(` Request Timeout: ${config.requestTimeout}ms`);
console.log(` Max Retries: ${config.maxRetries}`);
console.log('');
// Create and start server
const server = new ByteBotMCPServer(config);
// Handle graceful shutdown
const shutdown = async (signal: string) => {
console.log('');
console.log(`[ByteBot MCP] Received ${signal}, shutting down gracefully...`);
try {
await server.stop();
process.exit(0);
} catch (error) {
console.error('[ByteBot MCP] Error during shutdown:', error);
process.exit(1);
}
};
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
// Start server
await server.start();
// Display stats
const stats = server.getStats();
console.log('[ByteBot MCP] Server Statistics:');
console.log(` Tools Registered: ${stats.toolsRegistered}`);
console.log(` WebSocket Connected: ${stats.webSocketConnected}`);
console.log('');
} catch (error) {
console.error('[ByteBot MCP] Fatal error:', error);
process.exit(1);
}
}
// Run main function
main().catch((error) => {
console.error('[ByteBot MCP] Unhandled error:', error);
process.exit(1);
});