describe_bills
Retrieve detailed billing summaries for Alibaba Cloud RDS database instances or billing items within a specified period. Input billing cycles and optional instance ID or billing item flag to generate accurate consumption reports.
Instructions
Query the consumption summary of all product instances or billing items for a user within a specific billing period.
Args:
billing_cycles: bill cycle YYYY-MM, e.g. 2020-03
db_instance_id: DB instance id (e.g., "rm-xxx")
is_billing_item: Whether to pull data according to the billing item dimension.
Returns:
str: billing information.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| billing_cycles | Yes | ||
| db_instance_id | No | ||
| is_billing_item | No |
Input Schema (JSON Schema)
{
"properties": {
"billing_cycles": {
"items": {
"type": "string"
},
"title": "Billing Cycles",
"type": "array"
},
"db_instance_id": {
"default": null,
"title": "Db Instance Id",
"type": "string"
},
"is_billing_item": {
"default": false,
"title": "Is Billing Item",
"type": "boolean"
}
},
"required": [
"billing_cycles"
],
"title": "describe_billsArguments",
"type": "object"
}
Implementation Reference
- The main handler function implementing the 'describe_bills' tool. It queries Alibaba Cloud billing data for RDS instances using the BSS OpenAPI client (DescribeInstanceBill), filters by instance ID if provided, handles pagination with next_token, and formats results as CSV per billing cycle.@mcp.tool(annotations=READ_ONLY_TOOL) async def describe_bills( billing_cycles: list[str], db_instance_id: str = None, is_billing_item: bool = False ) -> dict[str, Any]: """ Query the consumption summary of all product instances or billing items for a user within a specific billing period. Args: billing_cycles: bill cycle YYYY-MM, e.g. 2020-03 db_instance_id: DB instance id (e.g., "rm-xxx") is_billing_item: Whether to pull data according to the billing item dimension. Returns: str: billing information. """ try: client = get_bill_client("cn-hangzhou") res = {} for billing_cycle in billing_cycles: has_next_token = True next_token = None items = [] while has_next_token: describe_instance_bill_request = bss_open_api_20171214_models.DescribeInstanceBillRequest( billing_cycle=billing_cycle, product_code='rds', is_billing_item=is_billing_item, next_token=next_token ) if db_instance_id: describe_instance_bill_request.db_instance_id = db_instance_id response = client.describe_instance_bill(describe_instance_bill_request) if not response.body.data: break next_token = response.body.data.next_token has_next_token = next_token is not None and next_token.strip() != "" items.extend(response.body.data.items) item_filters = [] for item in items: if db_instance_id is None or db_instance_id in item.instance_id.split(";"): item_filters.append( { "Item": item.item, "AfterDiscountAmount": item.after_discount_amount, "InstanceID": item.instance_id, "BillingDate": item.billing_date, "InvoiceDiscount": item.invoice_discount, "SubscriptionType": item.subscription_type, "PretaxGrossAmount": item.pretax_gross_amount, "Currency": item.currency, "CommodityCode": item.commodity_code, "CostUnit": item.cost_unit, "NickName": item.nick_name, "PretaxAmount": item.pretax_amount, "BillingItem": item.billing_item, "BillingItemPriceUnit": item.list_price_unit, "BillingItemUsage": item.usage, } ) res[billing_cycle] = json_array_to_csv(item_filters) return res except Exception as e: raise e
- Helper function to create and return the Alibaba Cloud BSS OpenAPI client instance used by describe_bills for querying bills.def get_bill_client(region_id: str): ak, sk, sts = get_aksk() config = Config( access_key_id=ak, access_key_secret=sk, security_token=sts, region_id=region_id, protocol="https", connect_timeout=10 * 1000, read_timeout=300 * 1000 ) client = BssOpenApi20171214Client(config) return client