Skip to main content
Glama

get_cost_by_service

Analyze Oracle Cloud Infrastructure spending by retrieving cost breakdowns per service for a specified tenancy and time period.

Instructions

Get cost breakdown by service for a tenancy.

Args:
    tenant_id: OCID of the tenancy
    time_usage_started: Start time in ISO format (YYYY-MM-DD)
    time_usage_ended: End time in ISO format (YYYY-MM-DD)

Returns:
    List of costs grouped by service with total cost per service

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tenant_idYes
time_usage_startedYes
time_usage_endedYes

Implementation Reference

  • MCP tool registration and wrapper handler function that provides the tool interface, handles context, logging, errors, and delegates to the core helper using OCI usage_api client.
    @mcp.tool(name="get_cost_by_service")
    @mcp_tool_wrapper(
        start_msg="Getting cost breakdown by service...",
        error_prefix="Error getting cost by service"
    )
    async def mcp_get_cost_by_service(ctx: Context, tenant_id: str, time_usage_started: str,
                                      time_usage_ended: str) -> List[Dict[str, Any]]:
        """
        Get cost breakdown by service for a tenancy.
    
        Args:
            tenant_id: OCID of the tenancy
            time_usage_started: Start time in ISO format (YYYY-MM-DD)
            time_usage_ended: End time in ISO format (YYYY-MM-DD)
    
        Returns:
            List of costs grouped by service with total cost per service
        """
        return get_cost_by_service(oci_clients["usage_api"], tenant_id, time_usage_started, time_usage_ended)
  • Core helper function that executes the OCI Usage API call to request summarized usages grouped by service and aggregates the total cost per service.
    def get_cost_by_service(usage_api_client: oci.usage_api.UsageapiClient,
                           tenant_id: str,
                           time_usage_started: str,
                           time_usage_ended: str) -> List[Dict[str, Any]]:
        """
        Get cost breakdown by service.
    
        Args:
            usage_api_client: OCI UsageApi client
            tenant_id: OCID of the tenancy
            time_usage_started: Start time in ISO format (YYYY-MM-DD)
            time_usage_ended: End time in ISO format (YYYY-MM-DD)
    
        Returns:
            List of costs grouped by service
        """
        try:
            request_summarized_usages_details = oci.usage_api.models.RequestSummarizedUsagesDetails(
                tenant_id=tenant_id,
                time_usage_started=time_usage_started,
                time_usage_ended=time_usage_ended,
                granularity="DAILY",
                group_by=["service"]
            )
    
            usage_response = oci.pagination.list_call_get_all_results(
                usage_api_client.request_summarized_usages,
                request_summarized_usages_details=request_summarized_usages_details
            )
    
            # Aggregate by service
            service_costs = {}
            for item in usage_response.data.items:
                service = item.service
                if service not in service_costs:
                    service_costs[service] = {
                        "service": service,
                        "total_cost": 0.0,
                        "currency": item.currency,
                        "unit": item.unit,
                    }
                service_costs[service]["total_cost"] += float(item.computed_amount) if item.computed_amount else 0.0
    
            result = list(service_costs.values())
            logger.info(f"Retrieved cost breakdown for {len(result)} services")
            return result
    
        except Exception as e:
            logger.exception(f"Error getting cost by service: {e}")
            raise

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/jopsis/mcp-server-oci'

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