list_compartments
Retrieve all Oracle Cloud Infrastructure compartments accessible to your user account for resource management and organization.
Instructions
List all compartments accessible to the user.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- mcp_server_oci/mcp_server.py:425-427 (handler)MCP tool handler function that retrieves the identity client from global oci_clients and calls the helper function to list compartments."""List all compartments accessible to the user.""" return list_compartments(oci_clients["identity"])
- mcp_server_oci/mcp_server.py:419-419 (registration)Registration of the 'list_compartments' tool using the @mcp.tool decorator.@mcp_tool_wrapper(
- Core helper function that implements the logic to list all compartments (root and nested) using the OCI Identity SDK, formatting them into a standardized dictionary structure.def list_compartments(identity_client: oci.identity.IdentityClient) -> List[Dict[str, Any]]: """ List all compartments accessible to the user. Args: identity_client: OCI Identity client Returns: List of compartments with their details """ try: # Get tenant ID (root compartment) tenant_id = identity_client.get_user(identity_client.base_client.config.get("user")).data.compartment_id # Get all compartments (including nested ones) compartments = [] # Get root compartment first try: root_compartment = identity_client.get_compartment(tenant_id).data compartments.append({ "id": root_compartment.id, "name": root_compartment.name, "description": root_compartment.description, "lifecycle_state": root_compartment.lifecycle_state, "is_accessible": True, "time_created": str(root_compartment.time_created), "is_root": True, }) except Exception as e: logger.warning(f"Could not get root compartment: {e}") # Get all compartments in the tenancy list_compartments_response = oci.pagination.list_call_get_all_results( identity_client.list_compartments, tenant_id, compartment_id_in_subtree=True, lifecycle_state="ACTIVE", ) # Format the compartments for compartment in list_compartments_response.data: compartments.append({ "id": compartment.id, "name": compartment.name, "description": compartment.description, "parent_compartment_id": compartment.compartment_id, "lifecycle_state": compartment.lifecycle_state, "is_accessible": compartment.lifecycle_state == "ACTIVE", "time_created": str(compartment.time_created), "is_root": False, }) logger.info(f"Found {len(compartments)} compartments") return compartments except Exception as e: logger.exception(f"Error listing compartments: {e}") raise