get_metrics
Retrieve performance metrics for cloud resources across AWS, Azure, and GCP to monitor system health and analyze operational data over specified time periods.
Instructions
Get metrics for a cloud resource
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| provider | Yes | Cloud provider | |
| resourceId | Yes | Resource ID | |
| metricName | Yes | Metric name (e.g., CPUUtilization, NetworkIn) | |
| startTime | Yes | Start time (ISO 8601) | |
| endTime | Yes | End time (ISO 8601) | |
| period | No | Period in seconds |
Implementation Reference
- src/tools/monitoring.ts:128-194 (handler)Core handler function that retrieves metrics from AWS CloudWatch for the specified resource, metric, and time period, returning sorted Metric objects.async function getAWSMetrics( resourceId: string, metricName: string, startTime: string, endTime: string, period: number ): Promise<Metric[]> { try { const credentials = await credentialManager.getCredentials('aws'); if (!credentials) { throw new Error('AWS credentials not found'); } const client = new CloudWatchClient({ region: credentials.region || 'us-east-1', credentials: credentials.accessKeyId && credentials.secretAccessKey ? { accessKeyId: credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, } : undefined, }); // Determine namespace based on resource type let namespace = 'AWS/EC2'; if (resourceId.includes('lambda')) { namespace = 'AWS/Lambda'; } else if (resourceId.includes('rds')) { namespace = 'AWS/RDS'; } const command = new GetMetricStatisticsCommand({ Namespace: namespace, MetricName: metricName, Dimensions: [ { Name: namespace === 'AWS/EC2' ? 'InstanceId' : 'FunctionName', Value: resourceId, }, ], StartTime: new Date(startTime), EndTime: new Date(endTime), Period: period, Statistics: ['Average', 'Maximum', 'Minimum'], }); const response = await client.send(command); const metrics: Metric[] = []; if (response.Datapoints) { for (const datapoint of response.Datapoints) { if (datapoint.Timestamp && datapoint.Average !== undefined) { metrics.push({ name: metricName, value: datapoint.Average, unit: datapoint.Unit || 'Count', timestamp: datapoint.Timestamp, }); } } } return metrics.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()); } catch (error) { throw new Error(`Failed to get AWS metrics: ${error instanceof Error ? error.message : String(error)}`); } }
- src/tools/monitoring.ts:10-41 (schema)Input schema defining the parameters for the get_metrics tool, including provider, resource ID, metric name, time range, and optional period.inputSchema: { type: 'object', properties: { provider: { type: 'string', enum: ['aws', 'azure', 'gcp'], description: 'Cloud provider', }, resourceId: { type: 'string', description: 'Resource ID', }, metricName: { type: 'string', description: 'Metric name (e.g., CPUUtilization, NetworkIn)', }, startTime: { type: 'string', description: 'Start time (ISO 8601)', }, endTime: { type: 'string', description: 'End time (ISO 8601)', }, period: { type: 'number', description: 'Period in seconds', default: 3600, }, }, required: ['provider', 'resourceId', 'metricName', 'startTime', 'endTime'], },
- src/tools/monitoring.ts:7-42 (registration)Tool registration object for 'get_metrics' included in the monitoringTools array.{ name: 'get_metrics', description: 'Get metrics for a cloud resource', inputSchema: { type: 'object', properties: { provider: { type: 'string', enum: ['aws', 'azure', 'gcp'], description: 'Cloud provider', }, resourceId: { type: 'string', description: 'Resource ID', }, metricName: { type: 'string', description: 'Metric name (e.g., CPUUtilization, NetworkIn)', }, startTime: { type: 'string', description: 'Start time (ISO 8601)', }, endTime: { type: 'string', description: 'End time (ISO 8601)', }, period: { type: 'number', description: 'Period in seconds', default: 3600, }, }, required: ['provider', 'resourceId', 'metricName', 'startTime', 'endTime'], }, },
- src/tools/monitoring.ts:93-104 (handler)Dispatch handler within handleMonitoringTool that processes get_metrics requests and calls the AWS-specific implementation.case 'get_metrics': { const resourceId = params.resourceId as string; const metricName = params.metricName as string; const startTime = params.startTime as string; const endTime = params.endTime as string; const period = (params.period as number) || 3600; if (provider === 'aws') { return await getAWSMetrics(resourceId, metricName, startTime, endTime, period); } return { message: `Metrics not yet implemented for ${provider}` }; }
- src/server.ts:74-76 (registration)MCP server request handler routes tool calls matching monitoringTools (including get_metrics) to the appropriate handler.} else if (monitoringTools.some((t) => t.name === name)) { result = await handleMonitoringTool(name, args || {}); } else if (securityTools.some((t) => t.name === name)) {