list_vpc_networks
Retrieve a list of Virtual Private Cloud (VPC) networks within a specified Google Cloud Platform (GCP) project to manage and analyze your network configurations.
Instructions
List Virtual Private Cloud (VPC) networks in a GCP project.
Args:
project_id: The ID of the GCP project to list VPC networks for
Returns:
List of VPC networks in the specified GCP project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes |
Implementation Reference
- The handler function for the 'list_vpc_networks' tool. It lists all VPC networks in the specified GCP project using the Google Cloud Compute API, formats the output with details like subnet mode, creation time, and subnets, and handles errors.def list_vpc_networks(project_id: str) -> str: """ List Virtual Private Cloud (VPC) networks in a GCP project. Args: project_id: The ID of the GCP project to list VPC networks for Returns: List of VPC networks in the specified GCP project """ try: from google.cloud import compute_v1 # Initialize the Compute Engine client for networks client = compute_v1.NetworksClient() # List networks request = compute_v1.ListNetworksRequest(project=project_id) networks = client.list(request=request) # Format the response networks_list = [] for network in networks: subnet_mode = "Auto" if network.auto_create_subnetworks else "Custom" creation_time = network.creation_timestamp if network.creation_timestamp else "Unknown" # Get subnet information if available subnets = [] if not network.auto_create_subnetworks and network.subnetworks: for subnet_url in network.subnetworks: subnet_name = subnet_url.split('/')[-1] subnet_region = subnet_url.split('/')[-3] subnets.append(f" - {subnet_name} (Region: {subnet_region})") network_info = f"- {network.name} (Mode: {subnet_mode}, Created: {creation_time})" if subnets: network_info += "\n Subnets:\n" + "\n".join(subnets) networks_list.append(network_info) if not networks_list: return f"No VPC networks found in project {project_id}." networks_str = "\n".join(networks_list) return f""" VPC Networks in GCP Project {project_id}: {networks_str} """ except Exception as e: return f"Error listing VPC networks: {str(e)}"
- src/gcp_mcp/gcp_modules/networking/tools.py:6-9 (registration)The registration of the 'list_vpc_networks' tool occurs within the register_tools function using the @mcp.tool() decorator.def register_tools(mcp): """Register all networking tools with the MCP server.""" @mcp.tool()
- The docstring provides the schema description for args (project_id: str) and returns (str with formatted list). The type hint project_id: str also defines input schema.""" List Virtual Private Cloud (VPC) networks in a GCP project. Args: project_id: The ID of the GCP project to list VPC networks for Returns: List of VPC networks in the specified GCP project """