get-billing-budget
Retrieve billing budgets for Google Cloud Platform projects to monitor and control cloud spending. Use this tool to access budget information and manage cost allocation across GCP services.
Instructions
Get billing budgets for the current project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectId | No | Project ID to get budgets for (defaults to selected project) |
Implementation Reference
- index.ts:157-170 (registration)Registration of the 'get-billing-budget' tool in the ListTools response, including its name, description, and input schema.{ name: "get-billing-budget", description: "Get billing budgets for the current project", inputSchema: { type: "object", properties: { projectId: { type: "string", description: "Project ID to get budgets for (defaults to selected project)", }, }, required: [], }, },
- index.ts:237-239 (schema)Zod schema for validating input arguments to the 'get-billing-budget' tool.const GetBillingBudgetSchema = z.object({ projectId: z.string().optional(), });
- index.ts:496-562 (handler)Handler function that executes the 'get-billing-budget' tool logic, fetching project billing info and listing budgets using CloudBillingClient and BudgetServiceClient.} else if (name === "get-billing-budget") { const { projectId } = GetBillingBudgetSchema.parse(args); const targetProject = projectId || selectedProject; if (!targetProject) { return createTextResponse("No project selected. Please select a project first."); } try { const billingClient = new CloudBillingClient(); const [billingInfo] = await billingClient.getProjectBillingInfo({ name: `projects/${targetProject}` }); if (!billingInfo.billingEnabled) { return createTextResponse("Billing is not enabled for this project."); } const billingAccount = billingInfo.billingAccountName; if (!billingAccount) { return createTextResponse("No billing account associated with this project."); } // Use the BudgetServiceClient to list budgets const budgetClient = new BudgetServiceClient(); const [budgets] = await budgetClient.listBudgets({ parent: billingAccount }); interface Budget { name: string | null; displayName: string | null; amount: { units: string | null; currencyCode: string | null; }; thresholdRules: Array<{ thresholdPercent: number | null; spendBasis: string | null; }>; } const formattedBudgets = budgets.map((budget: any) => ({ name: budget.name ?? null, displayName: budget.displayName ?? null, amount: budget.amount ? { units: budget.amount.units ?? null, currencyCode: budget.amount.currencyCode ?? null } : null, thresholdRules: budget.thresholdRules?.map((rule: any) => ({ thresholdPercent: rule.thresholdPercent ?? null, spendBasis: rule.spendBasis ?? null })) ?? [] })); return createTextResponse(JSON.stringify({ projectId: targetProject, billingAccount: billingAccount, budgets: formattedBudgets }, null, 2)); } catch (error: any) { console.error('Error getting billing budgets:', error); if (error.code === 7) { return createTextResponse("Error: Cloud Billing API or Cloud Billing Budgets API is not enabled. Please enable it in the Google Cloud Console."); } return createTextResponse(`Error getting billing budgets: ${error.message}`); }