Skip to main content
Glama
rainhan99

Cloud Manage MCP Server

by rainhan99

list_aws_instances

Retrieve and display all AWS EC2 instances to monitor cloud infrastructure and manage resources effectively.

Instructions

列出所有AWS EC2实例

Returns:
    Dict: AWS实例列表

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Core handler function in AWSProvider class that lists all EC2 instances using boto3's describe_instances API, formats them, and returns a dictionary with instance summaries.
    def list_instances(self) -> Dict:
        """
        列出所有EC2实例
        
        Returns:
            Dict: 实例列表或错误信息
        """
        if not self.available:
            return {
                'error': f'AWS服务不可用: {getattr(self, "error", "未知错误")}',
                'provider': 'aws'
            }
        
        try:
            response = self.ec2.describe_instances()
            
            instances = []
            for reservation in response['Reservations']:
                for instance in reservation['Instances']:
                    instance_info = self._format_instance_summary(instance)
                    instances.append(instance_info)
            
            return {
                'provider': 'aws',
                'region': self.region,
                'total_instances': len(instances),
                'instances': instances
            }
            
        except ClientError as e:
            return {
                'error': f'AWS API调用失败: {str(e)}',
                'provider': 'aws'
            }
        except Exception as e:
            return {
                'error': f'列出EC2实例时发生错误: {str(e)}',
                'provider': 'aws'
            }
  • main.py:368-377 (registration)
    Registers the MCP tool 'list_aws_instances' with @mcp.tool() decorator. This wrapper function delegates to the aws_provider.list_instances() method.
    @mcp.tool()
    def list_aws_instances() -> Dict:
        """
        列出所有AWS EC2实例
        
        Returns:
            Dict: AWS实例列表
        """
        return aws_provider.list_instances()
  • Helper method used by list_instances to format each instance into a summary dictionary, extracting key fields like ID, name, state, IPs, etc.
    def _format_instance_summary(self, instance: Dict) -> Dict:
        """格式化实例摘要信息"""
        # 获取名称标签
        name = '未命名'
        for tag in instance.get('Tags', []):
            if tag['Key'] == 'Name':
                name = tag['Value']
                break
        
        return {
            'instance_id': instance.get('InstanceId'),
            'name': name,
            'instance_type': instance.get('InstanceType'),
            'state': instance.get('State', {}).get('Name'),
            'public_ip': instance.get('PublicIpAddress'),
            'private_ip': instance.get('PrivateIpAddress'),
            'availability_zone': instance.get('Placement', {}).get('AvailabilityZone'),
            'launch_time': instance.get('LaunchTime').isoformat() if instance.get('LaunchTime') else None
        }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rainhan99/cloud_manage_mcp_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server