import { ToolExecutionError, isOperationalError } from '../../utils/error-handling.js'; import { getLogger } from '../../utils/logging.js'; import { trackToolRequest } from '../../utils/metrics.js'; import { CloudWatchLogsService, createCloudWatchLogsService } from '../../services/aws/cloudwatch-logs.js'; import { McpToolResponse } from '../../types/tools.js'; const logger = getLogger(); /** * Tool to test AWS connection * @returns MCP tool response */ export async function testAwsConnectionTool(): Promise { logger.info('Testing AWS connection'); const startTime = Date.now(); let success = false; try { const service: CloudWatchLogsService = createCloudWatchLogsService(); const result = await service.testConnection(); logger.info(`AWS connection test successful: ${result}`); success = true; return { content: [{ type: 'text' as const, text: `✅ ${result}` }] }; } catch (error) { logger.error('AWS connection test failed', error); // Determine if this is an expected error const isOperational = isOperationalError(error); // If it's already a BaseError (like our AwsServiceError), just wrap it with tool context if (isOperational) { throw new ToolExecutionError( `Failed to connect to AWS: ${error instanceof Error ? error.message : String(error)}`, 'testAwsConnection', {}, error ); } else { // For unexpected errors, create a new ToolExecutionError throw new ToolExecutionError( `AWS connection test failed: ${error instanceof Error ? error.message : String(error)}`, 'testAwsConnection', {}, error ); } } finally { // Track metrics for this tool invocation const responseTime = Date.now() - startTime; trackToolRequest('testAwsConnection', responseTime, success); } }