create_snapshot
Generate a snapshot of a Compute Engine disk in Google Cloud Platform. Provide project ID, zone, disk name, and snapshot name to save disk data for backup or replication.
Instructions
Create a snapshot of a Compute Engine disk.
Args:
project_id: The ID of the GCP project
zone: The zone where the disk is located (e.g., "us-central1-a")
disk_name: The name of the disk to snapshot
snapshot_name: The name for the new snapshot
description: Optional description for the snapshot
Returns:
Status message indicating whether the snapshot was created successfully
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | No | ||
| disk_name | Yes | ||
| project_id | Yes | ||
| snapshot_name | Yes | ||
| zone | Yes |
Implementation Reference
- The core handler function for the 'create_snapshot' MCP tool. It uses the Google Cloud Compute API to create a snapshot from a specified disk, waits for completion, and returns success or error message.@mcp.tool() def create_snapshot(project_id: str, zone: str, disk_name: str, snapshot_name: str, description: str = "") -> str: """ Create a snapshot of a Compute Engine disk. Args: project_id: The ID of the GCP project zone: The zone where the disk is located (e.g., "us-central1-a") disk_name: The name of the disk to snapshot snapshot_name: The name for the new snapshot description: Optional description for the snapshot Returns: Status message indicating whether the snapshot was created successfully """ try: from google.cloud import compute_v1 # Initialize the Disks client disks_client = compute_v1.DisksClient() # Create the snapshot request snapshot = compute_v1.Snapshot() snapshot.name = snapshot_name if description: snapshot.description = description # Create the snapshot operation = disks_client.create_snapshot( project=project_id, zone=zone, disk=disk_name, snapshot_resource=snapshot ) # Wait for the 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 snapshot {snapshot_name}: {operation.error.errors[0].message}" return f"Snapshot {snapshot_name} of disk {disk_name} in zone {zone} created successfully." except Exception as e: return f"Error creating snapshot: {str(e)}"
- src/gcp_mcp/server.py:39-39 (registration)Registers the compute tools module, which includes the create_snapshot tool, by calling its register_tools function with the MCP server instance.compute_tools.register_tools(mcp)
- src/gcp_mcp/gcp_modules/compute/tools.py:6-6 (registration)The function that defines and registers all compute tools, including create_snapshot via decorators.def register_tools(mcp):