list_service_accounts
Retrieve a list of service accounts within a specified Google Cloud Platform (GCP) project using the project ID, simplifying IAM and resource management.
Instructions
List service accounts in a GCP project.
Args:
project_id: The ID of the GCP project
Returns:
List of service accounts in the project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes |
Implementation Reference
- Handler function implementing the list_service_accounts tool. It lists service accounts in a GCP project using the IAM client and returns a formatted string list. Registered inline with @mcp.tool() decorator.def list_service_accounts(project_id: str) -> str: """ List service accounts in a GCP project. Args: project_id: The ID of the GCP project Returns: List of service accounts in the project """ try: from google.cloud import iam_v1 # Initialize the IAM client client = iam_v1.IAMClient() # List service accounts request = iam_v1.ListServiceAccountsRequest( name=f"projects/{project_id}" ) service_accounts = client.list_service_accounts(request=request) accounts_list = [] for account in service_accounts: display_name = account.display_name or "No display name" accounts_list.append(f"- {account.email} ({display_name})") if not accounts_list: return f"No service accounts found in project {project_id}." accounts_str = "\n".join(accounts_list) return f""" Service Accounts in GCP Project {project_id}: {accounts_str} """ except Exception as e: return f"Error listing service accounts: {str(e)}"