import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { z } from 'zod'; import { getLogger } from '../utils/logging.js'; import { testAwsConnectionTool, cloudWatchLogGroupsTool, cloudWatchLogsTool, cloudTrailEventsTool } from '../tools/index.js'; const logger = getLogger(); /** * Create and configure the MCP server with all tools * @param name Server name * @param version Server version * @param description Server description * @param instructions Server instructions * @returns Configured MCP server */ export function createMcpServer({ name, version, description, instructions }: { name: string; version: string; description: string; instructions: string; }): McpServer { logger.info(`Creating MCP server: ${name} v${version}`); // Create the MCP server const server = new McpServer({ name, version, description, instructions }); // Register the Test AWS Connection tool server.tool( 'testAwsConnection', 'Test AWS connectivity', async () => { return await testAwsConnectionTool(); } ); // Register the CloudWatch Log Groups tool server.tool( 'cloudWatchLogGroups', 'List available CloudWatch Log Groups', { prefix: z.string().optional().describe('Filter log groups by prefix'), limit: z.number().optional().describe('Maximum number of log groups to return') }, async (params) => { return await cloudWatchLogGroupsTool(params); } ); // Register the CloudWatch Logs tool server.tool( 'cloudWatchLogs', 'Retrieve logs from CloudWatch', { logGroupName: z.string().describe('The name of the log group to query'), logStreamName: z.string().optional().describe('Optional specific log stream name'), filterPattern: z.string().optional().describe('Filter pattern to apply to logs'), startTime: z.string().optional().describe('Start time for log retrieval (ISO format or relative like -30m, -1h, -1d)'), endTime: z.string().optional().describe('End time for log retrieval (ISO format)'), limit: z.number().optional().describe('Maximum number of log events to return') }, async (params) => { return await cloudWatchLogsTool(params); } ); // Register the CloudTrail Events tool server.tool( 'cloudTrailEvents', 'Retrieve events from CloudTrail', { eventName: z.string().optional().describe('Filter events by name (e.g., "CreateFunction", "InvokeFunction")'), username: z.string().optional().describe('Filter events by AWS username'), resourceName: z.string().optional().describe('Filter events by resource name'), resourceType: z.string().optional().describe('Filter events by resource type'), startTime: z.string().optional().describe('Start time for event retrieval (ISO format or relative like -30m, -1h, -1d)'), endTime: z.string().optional().describe('End time for event retrieval (ISO format)'), limit: z.number().optional().describe('Maximum number of events to return') }, async (params) => { return await cloudTrailEventsTool(params); } ); logger.info('MCP server created and tools registered'); return server; }