list_assets
Retrieve assets in a GCP project via Cloud Asset Inventory API, with options to filter by specific types and control paginated results for efficient asset management.
Instructions
List assets in a GCP project using Cloud Asset Inventory API.
Args:
project_id: The ID of the GCP project to list assets for
asset_types: Optional list of asset types to filter by (e.g., ["compute.googleapis.com/Instance"])
page_size: Number of assets to return per page (default: 50, max: 1000)
Returns:
List of assets in the specified GCP project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| asset_types | No | ||
| page_size | No | ||
| project_id | Yes |
Implementation Reference
- The handler function for the 'list_assets' tool. It uses the Google Cloud AssetServiceClient to list assets in a GCP project, supports filtering by asset types and pagination.@mcp.tool() def list_assets(project_id: str, asset_types: Optional[List[str]] = None, page_size: int = 50) -> str: """ List assets in a GCP project using Cloud Asset Inventory API. Args: project_id: The ID of the GCP project to list assets for asset_types: Optional list of asset types to filter by (e.g., ["compute.googleapis.com/Instance"]) page_size: Number of assets to return per page (default: 50, max: 1000) Returns: List of assets in the specified GCP project """ try: try: from google.cloud import asset_v1 except ImportError: return "Error: The Google Cloud Asset Inventory library is not installed. Please install it with 'pip install google-cloud-asset'." # Initialize the Asset client client = asset_v1.AssetServiceClient() # Format the parent resource parent = f"projects/{project_id}" # Create the request request = asset_v1.ListAssetsRequest( parent=parent, content_type=asset_v1.ContentType.RESOURCE, page_size=min(page_size, 1000) # API limit is 1000 ) # Add asset types filter if provided if asset_types: request.asset_types = asset_types # List assets response = client.list_assets(request=request) # Format the response assets_list = [] for asset in response: asset_type = asset.asset_type name = asset.name display_name = asset.display_name if hasattr(asset, 'display_name') and asset.display_name else name.split('/')[-1] # Extract location if available location = "global" if hasattr(asset.resource, 'location') and asset.resource.location: location = asset.resource.location assets_list.append(f"- {display_name} ({asset_type})\n Location: {location}\n Name: {name}") if not assets_list: filter_msg = f" with types {asset_types}" if asset_types else "" return f"No assets found{filter_msg} in project {project_id}." # Add pagination info if there's a next page token pagination_info = "" if hasattr(response, 'next_page_token') and response.next_page_token: pagination_info = "\n\nMore assets are available. Refine your search or increase page_size to see more." return f"Assets in GCP Project {project_id}:\n\n" + "\n\n".join(assets_list) + pagination_info except Exception as e: return f"Error listing assets: {str(e)}"
- src/gcp_mcp/server.py:32-33 (registration)Registration of resource management tools, which includes the 'list_assets' tool, by calling register_tools on the imported module.# Register resource management tools resource_tools.register_tools(mcp)
- src/gcp_mcp/gcp_modules/resource_management/tools.py:6-7 (registration)The register_tools function in the resource_management module that defines and registers the 'list_assets' tool using @mcp.tool() decorator.def register_tools(mcp): """Register all resource management tools with the MCP server."""
- Function signature and docstring defining the input schema (parameters) and output type for the 'list_assets' tool.def list_assets(project_id: str, asset_types: Optional[List[str]] = None, page_size: int = 50) -> str: """ List assets in a GCP project using Cloud Asset Inventory API. Args: project_id: The ID of the GCP project to list assets for asset_types: Optional list of asset types to filter by (e.g., ["compute.googleapis.com/Instance"]) page_size: Number of assets to return per page (default: 50, max: 1000) Returns: List of assets in the specified GCP project """