list_objects
Retrieve a list of objects from a specified Cloud Storage bucket in GCP, filtering by prefix and limiting results. Identify and manage stored data efficiently with this tool.
Instructions
List objects in a Cloud Storage bucket.
Args:
project_id: The ID of the GCP project
bucket_name: The name of the bucket to list objects from
prefix: Optional prefix to filter objects by
limit: Maximum number of objects to list (default: 100)
Returns:
List of objects in the specified Cloud Storage bucket
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bucket_name | Yes | ||
| limit | No | ||
| prefix | No | ||
| project_id | Yes |
Implementation Reference
- The main handler function for the 'list_objects' tool. It lists objects (blobs) in a GCP Cloud Storage bucket using the google.cloud.storage client, with optional prefix and limit. Formats and returns a string list of objects.@mcp.tool() def list_objects(project_id: str, bucket_name: str, prefix: Optional[str] = None, limit: int = 100) -> str: """ List objects in a Cloud Storage bucket. Args: project_id: The ID of the GCP project bucket_name: The name of the bucket to list objects from prefix: Optional prefix to filter objects by limit: Maximum number of objects to list (default: 100) Returns: List of objects in the specified Cloud Storage bucket """ try: from google.cloud import storage # Initialize the Storage client client = storage.Client(project=project_id) # Get the bucket bucket = client.get_bucket(bucket_name) # List blobs blobs = bucket.list_blobs(prefix=prefix, max_results=limit) # Format the response objects_list = [] for blob in blobs: size_mb = blob.size / (1024 * 1024) updated = blob.updated.strftime("%Y-%m-%d %H:%M:%S UTC") if blob.updated else "Unknown" objects_list.append(f"- {blob.name} (Size: {size_mb:.2f} MB, Updated: {updated}, Content-Type: {blob.content_type})") if not objects_list: return f"No objects found in bucket {bucket_name}{' with prefix ' + prefix if prefix else ''}." objects_str = "\n".join(objects_list) return f""" Objects in Cloud Storage Bucket {bucket_name}{' with prefix ' + prefix if prefix else ''}: {objects_str} """ except Exception as e: return f"Error listing objects: {str(e)}"
- src/gcp_mcp/server.py:42-42 (registration)Invocation of the storage tools registration function, which registers the list_objects tool (among others) with the MCP server instance.storage_tools.register_tools(mcp)
- src/gcp_mcp/server.py:9-9 (registration)Import of the storage tools module, providing the register_tools function used to register the list_objects tool.from .gcp_modules.storage import tools as storage_tools