gcp-billing-cost-recommendations
Analyze Google Cloud billing to identify cost-saving opportunities by generating tailored recommendations based on account, project, savings priority, and minimum savings threshold.
Instructions
Generate cost optimisation recommendations with potential savings for Google Cloud billing
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| billingAccountName | Yes | Billing account name (e.g., 'billingAccounts/123456-789ABC-DEF012') | |
| minSavingsAmount | No | Minimum savings amount to include in recommendations | |
| priority | No | Filter recommendations by priority level | all |
| projectId | No | Optional project ID to filter recommendations |
Implementation Reference
- src/services/billing/tools.ts:954-1054 (handler)The main handler function for the 'gcp-billing-cost-recommendations' tool. It generates mock cost optimization recommendations based on input parameters, filters them, formats the response, and returns markdown content with potential savings.async ({ billingAccountName, projectId, minSavingsAmount, priority }) => { try { // Use project hierarchy: provided -> state manager -> auth default const actualProjectId = projectId || stateManager.getCurrentProjectId() || (await getProjectId()); logger.debug( `Generating cost recommendations for billing account: ${billingAccountName}, project: ${actualProjectId || "all"}`, ); // Mock recommendations for demonstration const mockRecommendations: CostRecommendation[] = [ { type: "rightsizing", projectId: actualProjectId || "example-project-1", serviceId: "compute.googleapis.com", resourceName: "instance-group-1", description: "Compute Engine instances are underutilised and can be right-sized", potentialSavings: { amount: 450, currency: "USD", percentage: 30 }, effort: "medium", priority: "high", actionRequired: "Resize instances from n1-standard-4 to n1-standard-2", implementationSteps: [ "Stop the affected instances during maintenance window", "Change machine type from n1-standard-4 to n1-standard-2", "Restart instances and monitor performance", "Validate application performance meets requirements", ], }, { type: "idle_resources", projectId: actualProjectId || "example-project-2", serviceId: "storage.googleapis.com", resourceName: "unused-disk-volumes", description: "Several persistent disks are not attached to any instances", potentialSavings: { amount: 125, currency: "USD", percentage: 100 }, effort: "low", priority: "medium", actionRequired: "Delete unattached persistent disks", implementationSteps: [ "Verify disks are not needed for backups or future use", "Create snapshots if data retention is required", "Delete unattached persistent disks", "Set up alerts to monitor for future unattached disks", ], }, ]; // Filter by minimum savings amount const filteredRecommendations = mockRecommendations.filter( (rec) => rec.potentialSavings.amount >= minSavingsAmount, ); // Filter by priority if specified const finalRecommendations = priority === "all" ? filteredRecommendations : filteredRecommendations.filter( (rec) => rec.priority === priority, ); let response = `# Cost Optimisation Recommendations\n\n`; response += `**Billing Account:** ${billingAccountName}\n`; response += `**Minimum Savings:** ${formatCurrency(minSavingsAmount)}\n`; response += `**Priority Filter:** ${priority}\n\n`; response += `⚠️ **Note:** This is a demonstration with mock recommendations. `; response += `For actual recommendations, integrate with Google Cloud Recommender API.\n\n`; response += formatCostRecommendations(finalRecommendations); if (finalRecommendations.length > 0) { const totalSavings = finalRecommendations.reduce( (sum, rec) => sum + rec.potentialSavings.amount, 0, ); response += `\n**Total Potential Savings:** ${formatCurrency(totalSavings)}\n`; } return { content: [ { type: "text", text: response, }, ], }; } catch (error: any) { logger.error(`Error generating cost recommendations: ${error.message}`); throw new GcpMcpError( `Failed to generate cost recommendations: ${error.message}`, error.code || "UNKNOWN", error.status || 500, ); } },
- The input schema definition for the tool, using Zod schemas for validation of billingAccountName, optional projectId, minSavingsAmount, and priority parameters.{ title: "Generate Cost Recommendations", description: "Generate cost optimisation recommendations with potential savings for Google Cloud billing", inputSchema: { billingAccountName: z .string() .describe( "Billing account name (e.g., 'billingAccounts/123456-789ABC-DEF012')", ), projectId: z .string() .optional() .describe("Optional project ID to filter recommendations"), minSavingsAmount: z .number() .min(0) .default(10) .describe("Minimum savings amount to include in recommendations"), priority: z .enum(["low", "medium", "high", "all"]) .default("all") .describe("Filter recommendations by priority level"), }, },
- src/services/billing/tools.ts:928-1055 (registration)The server.registerTool call that registers the 'gcp-billing-cost-recommendations' tool with the MCP server, providing schema and handler."gcp-billing-cost-recommendations", { title: "Generate Cost Recommendations", description: "Generate cost optimisation recommendations with potential savings for Google Cloud billing", inputSchema: { billingAccountName: z .string() .describe( "Billing account name (e.g., 'billingAccounts/123456-789ABC-DEF012')", ), projectId: z .string() .optional() .describe("Optional project ID to filter recommendations"), minSavingsAmount: z .number() .min(0) .default(10) .describe("Minimum savings amount to include in recommendations"), priority: z .enum(["low", "medium", "high", "all"]) .default("all") .describe("Filter recommendations by priority level"), }, }, async ({ billingAccountName, projectId, minSavingsAmount, priority }) => { try { // Use project hierarchy: provided -> state manager -> auth default const actualProjectId = projectId || stateManager.getCurrentProjectId() || (await getProjectId()); logger.debug( `Generating cost recommendations for billing account: ${billingAccountName}, project: ${actualProjectId || "all"}`, ); // Mock recommendations for demonstration const mockRecommendations: CostRecommendation[] = [ { type: "rightsizing", projectId: actualProjectId || "example-project-1", serviceId: "compute.googleapis.com", resourceName: "instance-group-1", description: "Compute Engine instances are underutilised and can be right-sized", potentialSavings: { amount: 450, currency: "USD", percentage: 30 }, effort: "medium", priority: "high", actionRequired: "Resize instances from n1-standard-4 to n1-standard-2", implementationSteps: [ "Stop the affected instances during maintenance window", "Change machine type from n1-standard-4 to n1-standard-2", "Restart instances and monitor performance", "Validate application performance meets requirements", ], }, { type: "idle_resources", projectId: actualProjectId || "example-project-2", serviceId: "storage.googleapis.com", resourceName: "unused-disk-volumes", description: "Several persistent disks are not attached to any instances", potentialSavings: { amount: 125, currency: "USD", percentage: 100 }, effort: "low", priority: "medium", actionRequired: "Delete unattached persistent disks", implementationSteps: [ "Verify disks are not needed for backups or future use", "Create snapshots if data retention is required", "Delete unattached persistent disks", "Set up alerts to monitor for future unattached disks", ], }, ]; // Filter by minimum savings amount const filteredRecommendations = mockRecommendations.filter( (rec) => rec.potentialSavings.amount >= minSavingsAmount, ); // Filter by priority if specified const finalRecommendations = priority === "all" ? filteredRecommendations : filteredRecommendations.filter( (rec) => rec.priority === priority, ); let response = `# Cost Optimisation Recommendations\n\n`; response += `**Billing Account:** ${billingAccountName}\n`; response += `**Minimum Savings:** ${formatCurrency(minSavingsAmount)}\n`; response += `**Priority Filter:** ${priority}\n\n`; response += `⚠️ **Note:** This is a demonstration with mock recommendations. `; response += `For actual recommendations, integrate with Google Cloud Recommender API.\n\n`; response += formatCostRecommendations(finalRecommendations); if (finalRecommendations.length > 0) { const totalSavings = finalRecommendations.reduce( (sum, rec) => sum + rec.potentialSavings.amount, 0, ); response += `\n**Total Potential Savings:** ${formatCurrency(totalSavings)}\n`; } return { content: [ { type: "text", text: response, }, ], }; } catch (error: any) { logger.error(`Error generating cost recommendations: ${error.message}`); throw new GcpMcpError( `Failed to generate cost recommendations: ${error.message}`, error.code || "UNKNOWN", error.status || 500, ); } }, );