import { ClientService } from './services/client-service'; import { AwsService } from './services/aws-service'; import { logger } from './utils/logger'; import { healthCheckManager } from './utils/health-check'; /** * Initialize the client and connect to the MCP server * @returns Connected client service */ async function initializeClient(): Promise { const startTime = Date.now(); logger.info('Initializing AWS Logs MCP test client...', { component: 'initializeClient' }); const clientService = new ClientService(); try { await clientService.connect(); const duration = Date.now() - startTime; logger.info('Successfully connected to MCP server', { component: 'initializeClient', duration, status: 'success' }); return clientService; } catch (error) { logger.error('Failed to connect to MCP server', { component: 'initializeClient', error: error instanceof Error ? { name: error.name, message: error.message, stack: error.stack } : error, status: 'error' }); throw error; // Re-throw to be handled by the main function } } /** * List available tools from the MCP server * @param clientService Connected client service */ async function listAvailableTools(clientService: ClientService): Promise { try { logger.info('Listing available tools...', { component: 'listAvailableTools' }); const toolsResponse = await clientService.listTools(); if (toolsResponse?.tools?.length > 0) { logger.info('Found tools available', { component: 'listAvailableTools', count: toolsResponse.tools.length, status: 'success' }); } else { logger.warn('No tools found or empty response', { component: 'listAvailableTools', status: 'warning' }); } } catch (error) { logger.error('Error listing tools', { component: 'listAvailableTools', error: error instanceof Error ? { name: error.name, message: error.message } : error, status: 'error' }); // Non-fatal error, continue execution } } /** * Test AWS connection through MCP server * @param clientService Connected client service */ async function testAwsConnectionThroughMcp(clientService: ClientService): Promise { try { logger.info('Testing AWS connection through MCP server...', { component: 'testAwsConnectionThroughMcp' }); const result = await clientService.callTool({ name: 'testAwsConnection', arguments: {} }); logger.info('AWS connection test completed', { component: 'testAwsConnectionThroughMcp', result, status: 'success' }); } catch (error) { logger.error('Error testing AWS connection through MCP server', { component: 'testAwsConnectionThroughMcp', error: error instanceof Error ? { name: error.name, message: error.message } : error, status: 'error' }); await testDirectAwsConnection(); } } /** * Test direct AWS connection (fallback) */ async function testDirectAwsConnection(): Promise { try { logger.info('Trying direct AWS connection as fallback...', { component: 'testDirectAwsConnection' }); const awsService = new AwsService(); const directResult = await awsService.testConnection(); logger.info('Direct AWS connection test completed', { component: 'testDirectAwsConnection', result: directResult, status: 'success' }); } catch (error) { logger.error('Direct AWS connection failed', { component: 'testDirectAwsConnection', error: error instanceof Error ? { name: error.name, message: error.message } : error, status: 'error' }); } } /** * Run a comprehensive health check */ async function runHealthCheck(): Promise { try { logger.info('Running system health check...', { component: 'healthCheck' }); const healthResult = await healthCheckManager.checkHealth(); logger.info('System health check complete', { component: 'healthCheck', overallStatus: healthResult.status, version: healthResult.version, uptime: `${healthResult.uptime} seconds` }); // Log individual component results healthResult.components.forEach(component => { logger.info(`Health check: ${component.name}`, { component: 'healthCheck', status: component.status, message: component.message, details: component.details }); }); } catch (error) { logger.error('Error running health check', { component: 'healthCheck', error: error instanceof Error ? { name: error.name, message: error.message } : error }); } } async function main(): Promise { const startTime = Date.now(); let clientService: ClientService | undefined; try { // Initialize and connect clientService = await initializeClient(); // Execute main operations await listAvailableTools(clientService); await testAwsConnectionThroughMcp(clientService); // Run health check - helpful to validate our full system await runHealthCheck(); const duration = Date.now() - startTime; logger.info('Test client execution completed successfully', { component: 'main', duration, status: 'success' }); } catch (error) { logger.error('Unhandled error in main', { component: 'main', error: error instanceof Error ? { name: error.name, message: error.message, stack: error.stack } : error, status: 'error' }); // Try to run a health check even if main execution failed // This can help diagnose the issue try { await runHealthCheck(); } catch (healthCheckError) { logger.error('Failed to run health check after main error', { component: 'main', error: healthCheckError instanceof Error ? { name: healthCheckError.name, message: healthCheckError.message } : healthCheckError }); } } finally { // Always try to disconnect the client if it was initialized if (clientService) { try { await clientService.disconnect(); logger.info('Client disconnected successfully', { component: 'main', status: 'success' }); } catch (disconnectError) { logger.error('Error during disconnect', { component: 'main', error: disconnectError instanceof Error ? { name: disconnectError.name, message: disconnectError.message } : disconnectError, status: 'error' }); } } } } // Call main function with top-level error handling main().catch(err => logger.error('Unhandled error at top level', { component: 'process', error: err instanceof Error ? { name: err.name, message: err.message, stack: err.stack } : err, status: 'fatal' }));