Skip to main content
Glama

add_iam_policy_binding

Grant IAM roles to specific users or service accounts in a GCP project by adding policy bindings. Specify project ID, role, and member to assign access permissions securely.

Instructions

    Add an IAM policy binding to a GCP project.
    
    Args:
        project_id: The ID of the GCP project
        role: The role to grant (e.g., "roles/compute.admin")
        member: The member to grant the role to (e.g., "user:email@example.com", "serviceAccount:name@project.iam.gserviceaccount.com")
    
    Returns:
        Result of the policy binding operation
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
memberYes
project_idYes
roleYes

Implementation Reference

  • The handler function that implements the 'add_iam_policy_binding' tool logic. It uses Google Cloud APIs to modify the IAM policy of a project by adding a new binding for a member to a specific role, with checks to avoid duplicates.
        def add_iam_policy_binding(project_id: str, role: str, member: str) -> str:
            """
            Add an IAM policy binding to a GCP project.
            
            Args:
                project_id: The ID of the GCP project
                role: The role to grant (e.g., "roles/compute.admin")
                member: The member to grant the role to (e.g., "user:email@example.com", "serviceAccount:name@project.iam.gserviceaccount.com")
            
            Returns:
                Result of the policy binding operation
            """
            try:
                from google.cloud import resourcemanager_v3
                from google.iam.v1 import iam_policy_pb2, policy_pb2
                
                # Initialize the Resource Manager client
                client = resourcemanager_v3.ProjectsClient()
                
                # Get the current IAM policy
                get_request = iam_policy_pb2.GetIamPolicyRequest(
                    resource=f"projects/{project_id}"
                )
                policy = client.get_iam_policy(request=get_request)
                
                # Check if the binding already exists
                binding_exists = False
                for binding in policy.bindings:
                    if binding.role == role and member in binding.members:
                        binding_exists = True
                        break
                
                if binding_exists:
                    return f"IAM policy binding already exists: {member} already has role {role} in project {project_id}."
                
                # Add the new binding
                binding = policy_pb2.Binding()
                binding.role = role
                binding.members.append(member)
                policy.bindings.append(binding)
                
                # Set the updated IAM policy
                set_request = iam_policy_pb2.SetIamPolicyRequest(
                    resource=f"projects/{project_id}",
                    policy=policy
                )
                updated_policy = client.set_iam_policy(request=set_request)
                
                return f"""
    IAM policy binding added successfully:
    - Project: {project_id}
    - Role: {role}
    - Member: {member}
    """
            except Exception as e:
                return f"Error adding IAM policy binding: {str(e)}"
  • Type hints and docstring provide the input schema (project_id, role, member) and output description for the MCP tool.
    def add_iam_policy_binding(project_id: str, role: str, member: str) -> str:
        """
        Add an IAM policy binding to a GCP project.
        
        Args:
            project_id: The ID of the GCP project
            role: The role to grant (e.g., "roles/compute.admin")
            member: The member to grant the role to (e.g., "user:email@example.com", "serviceAccount:name@project.iam.gserviceaccount.com")
        
        Returns:
            Result of the policy binding operation
        """
  • The @mcp.tool() decorator on the handler function registers it as an MCP tool named 'add_iam_policy_binding' (derived from function name).
    @mcp.tool()
  • Within the module registration function, calls register_tools from iam_tools module, which defines and registers the 'add_iam_policy_binding' tool.
    iam_tools.register_tools(mcp)
  • Imports the IAM tools module containing the register_tools function and the add_iam_policy_binding tool implementation.
    from .gcp_modules.iam import tools as iam_tools
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It states this is an 'Add' operation (implying mutation) but doesn't address critical aspects like required permissions, whether this overwrites existing bindings, rate limits, or what constitutes a successful operation. The return value description ('Result of the policy binding operation') is too vague.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized with a clear purpose statement followed by parameter documentation. The Args/Returns structure is helpful, though the return description could be more specific. No wasted sentences, but the formatting with extra whitespace slightly reduces efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with no annotations and no output schema, the description is insufficient. It doesn't explain what happens on success/failure, permission requirements, or how this interacts with existing policy bindings. The return value description is vague, leaving the agent uncertain about what to expect.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description compensates well by providing clear explanations for all three parameters, including format examples for 'role' and 'member'. This adds substantial value beyond the bare schema, though it could benefit from mentioning constraints like valid role formats or member types.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Add an IAM policy binding') and target resource ('to a GCP project'), providing a specific verb+resource combination. However, it doesn't differentiate from sibling tools like 'check_iam_permissions' or 'get_role_permissions', which prevents a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives, prerequisites, or exclusions. While the context suggests this is for IAM policy management, there's no explicit comparison with related tools like 'list_roles' or 'create_service_account'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/henihaddad/gcp-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server