list_gke_clusters
List all Google Kubernetes Engine (GKE) clusters within a specified GCP project, optionally filtered by region. Simplify cluster management and visibility for better resource tracking in GCP environments.
Instructions
List Google Kubernetes Engine (GKE) clusters in a GCP project.
Args:
project_id: The ID of the GCP project to list GKE clusters for
region: Optional region to filter clusters (e.g., "us-central1")
Returns:
List of GKE clusters in the specified GCP project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | ||
| region | No |
Implementation Reference
- The handler function for the 'list_gke_clusters' tool. It uses the Google Cloud Container API to list GKE clusters in the specified project, optionally filtered by region. Handles both regional and zonal clusters, formatting output with details like version, node count, and status.@mcp.tool() def list_gke_clusters(project_id: str, region: str = "") -> str: """ List Google Kubernetes Engine (GKE) clusters in a GCP project. Args: project_id: The ID of the GCP project to list GKE clusters for region: Optional region to filter clusters (e.g., "us-central1") Returns: List of GKE clusters in the specified GCP project """ try: from google.cloud import container_v1 # Initialize the GKE client client = container_v1.ClusterManagerClient() clusters_list = [] if region: # List clusters in the specified region parent = f"projects/{project_id}/locations/{region}" response = client.list_clusters(parent=parent) for cluster in response.clusters: version = cluster.current_master_version node_count = sum(pool.initial_node_count for pool in cluster.node_pools) status = "Running" if cluster.status == container_v1.Cluster.Status.RUNNING else cluster.status.name clusters_list.append(f"- {cluster.name} (Region: {region}, Version: {version}, Nodes: {node_count}, Status: {status})") else: # List clusters in all regions from google.cloud import compute_v1 # Get all regions regions_client = compute_v1.RegionsClient() regions_request = compute_v1.ListRegionsRequest(project=project_id) regions = regions_client.list(request=regions_request) for region_item in regions: region_name = region_item.name parent = f"projects/{project_id}/locations/{region_name}" try: response = client.list_clusters(parent=parent) for cluster in response.clusters: version = cluster.current_master_version node_count = sum(pool.initial_node_count for pool in cluster.node_pools) status = "Running" if cluster.status == container_v1.Cluster.Status.RUNNING else cluster.status.name clusters_list.append(f"- {cluster.name} (Region: {region_name}, Version: {version}, Nodes: {node_count}, Status: {status})") except Exception: # Skip regions where we can't list clusters continue # Also check zonal clusters zones_client = compute_v1.ZonesClient() zones_request = compute_v1.ListZonesRequest(project=project_id) zones = zones_client.list(request=zones_request) for zone_item in zones: zone_name = zone_item.name parent = f"projects/{project_id}/locations/{zone_name}" try: response = client.list_clusters(parent=parent) for cluster in response.clusters: version = cluster.current_master_version node_count = sum(pool.initial_node_count for pool in cluster.node_pools) status = "Running" if cluster.status == container_v1.Cluster.Status.RUNNING else cluster.status.name clusters_list.append(f"- {cluster.name} (Zone: {zone_name}, Version: {version}, Nodes: {node_count}, Status: {status})") except Exception: # Skip zones where we can't list clusters continue if not clusters_list: region_msg = f" in region {region}" if region else "" return f"No GKE clusters found{region_msg} for project {project_id}." clusters_str = "\n".join(clusters_list) region_msg = f" in region {region}" if region else "" return f""" Google Kubernetes Engine (GKE) Clusters{region_msg} in GCP Project {project_id}: {clusters_str} """ except Exception as e: return f"Error listing GKE clusters: {str(e)}"
- src/gcp_mcp/server.py:51-51 (registration)Registration of the Kubernetes tools module in the main MCP server, which includes the 'list_gke_clusters' tool via the call to kubernetes_tools.register_tools(mcp). This is invoked during server initialization.kubernetes_tools.register_tools(mcp)
- src/gcp_mcp/gcp_modules/kubernetes/tools.py:6-8 (registration)The register_tools function in the Kubernetes module that defines and registers the 'list_gke_clusters' tool using the @mcp.tool() decorator.def register_tools(mcp): """Register all kubernetes tools with the MCP server."""