Skip to main content
Glama

create_instance

Create a new Compute Engine instance on Google Cloud Platform by specifying project ID, zone, instance name, machine type, source image, disk size, network, and optional external IP.

Instructions

    Create a new Compute Engine instance.
    
    Args:
        project_id: The ID of the GCP project
        zone: The zone to create the instance in (e.g., "us-central1-a")
        instance_name: The name for the new instance
        machine_type: The machine type (e.g., "e2-medium")
        source_image: The source image for the boot disk (e.g., "projects/debian-cloud/global/images/family/debian-11")
        boot_disk_size_gb: The size of the boot disk in GB (default: 10)
        network: The network to connect to (default: "default")
        subnet: The subnetwork to connect to (optional)
        external_ip: Whether to allocate an external IP (default: True)
    
    Returns:
        Status message indicating whether the instance was created successfully
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
boot_disk_size_gbNo
external_ipNo
instance_nameYes
machine_typeYes
networkNodefault
project_idYes
source_imageYes
subnetNo
zoneYes

Implementation Reference

  • The @mcp.tool()-decorated function that implements the core logic for creating a GCP Compute Engine instance using the Google Cloud compute_v1 client. It configures disk, network, service account, and calls insert() to create the VM.
        @mcp.tool()
        def create_instance(project_id: str, zone: str, instance_name: str, machine_type: str, 
                          source_image: str, boot_disk_size_gb: int = 10, 
                          network: str = "default", subnet: str = "", 
                          external_ip: bool = True) -> str:
            """
            Create a new Compute Engine instance.
            
            Args:
                project_id: The ID of the GCP project
                zone: The zone to create the instance in (e.g., "us-central1-a")
                instance_name: The name for the new instance
                machine_type: The machine type (e.g., "e2-medium")
                source_image: The source image for the boot disk (e.g., "projects/debian-cloud/global/images/family/debian-11")
                boot_disk_size_gb: The size of the boot disk in GB (default: 10)
                network: The network to connect to (default: "default")
                subnet: The subnetwork to connect to (optional)
                external_ip: Whether to allocate an external IP (default: True)
            
            Returns:
                Status message indicating whether the instance was created successfully
            """
            try:
                from google.cloud import compute_v1
                
                # Initialize the clients
                instances_client = compute_v1.InstancesClient()
                
                # Format the machine type
                machine_type_url = f"projects/{project_id}/zones/{zone}/machineTypes/{machine_type}"
                
                # Create the disk configuration
                boot_disk = compute_v1.AttachedDisk()
                boot_disk.boot = True
                initialize_params = compute_v1.AttachedDiskInitializeParams()
                initialize_params.source_image = source_image
                initialize_params.disk_size_gb = boot_disk_size_gb
                boot_disk.initialize_params = initialize_params
                boot_disk.auto_delete = True
                
                # Create the network configuration
                network_interface = compute_v1.NetworkInterface()
                if network.startswith("projects/"):
                    network_interface.network = network
                else:
                    network_interface.network = f"projects/{project_id}/global/networks/{network}"
                
                if subnet:
                    if subnet.startswith("projects/"):
                        network_interface.subnetwork = subnet
                    else:
                        network_interface.subnetwork = f"projects/{project_id}/regions/{zone.rsplit('-', 1)[0]}/subnetworks/{subnet}"
                
                if external_ip:
                    access_config = compute_v1.AccessConfig()
                    access_config.name = "External NAT"
                    access_config.type_ = "ONE_TO_ONE_NAT"
                    access_config.network_tier = "PREMIUM"
                    network_interface.access_configs = [access_config]
                
                # Create the instance
                instance = compute_v1.Instance()
                instance.name = instance_name
                instance.machine_type = machine_type_url
                instance.disks = [boot_disk]
                instance.network_interfaces = [network_interface]
                
                # Create a default service account for the instance
                service_account = compute_v1.ServiceAccount()
                service_account.email = "default"
                service_account.scopes = ["https://www.googleapis.com/auth/cloud-platform"]
                instance.service_accounts = [service_account]
                
                # Create the instance
                operation = instances_client.insert(
                    project=project_id,
                    zone=zone,
                    instance_resource=instance
                )
                
                # Wait for the create operation to complete
                operation_client = compute_v1.ZoneOperationsClient()
                
                # This is a synchronous call that will wait until the operation is complete
                while operation.status != compute_v1.Operation.Status.DONE:
                    operation = operation_client.get(project=project_id, zone=zone, operation=operation.name.split('/')[-1])
                    import time
                    time.sleep(1)
                
                if operation.error:
                    return f"Error creating instance {instance_name}: {operation.error.errors[0].message}"
                
                # Get the created instance to return its details
                created_instance = instances_client.get(project=project_id, zone=zone, instance=instance_name)
                
                # Get the instance IP addresses
                internal_ip = "None"
                external_ip = "None"
                
                if created_instance.network_interfaces:
                    internal_ip = created_instance.network_interfaces[0].network_i_p or "None"
                    if created_instance.network_interfaces[0].access_configs:
                        external_ip = created_instance.network_interfaces[0].access_configs[0].nat_i_p or "None"
                
                return f"""
    Instance {instance_name} created successfully in zone {zone}.
    
    Details:
    - Machine Type: {machine_type}
    - Internal IP: {internal_ip}
    - External IP: {external_ip}
    - Status: {created_instance.status}
    """
            except Exception as e:
                return f"Error creating instance: {str(e)}"
  • The register_tools function for the compute module defines and registers the create_instance tool (and others) via @mcp.tool() decorators inside it.
    def register_tools(mcp):
        """Register all compute tools with the MCP server."""
  • Top-level registration in the MCP server that invokes the compute module's register_tools(mcp), thereby registering the create_instance tool.
    compute_tools.register_tools(mcp)

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/henihaddad/gcp-mcp'

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