gcp-billing-list-services
Retrieve Google Cloud services for billing analysis to monitor costs. Use pagination to manage large datasets and identify service usage patterns.
Instructions
List all available Google Cloud services for billing and cost analysis
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pageSize | No | Maximum number of services to return (1-200) | |
| pageToken | No | Token for pagination to get next page of results |
Implementation Reference
- src/services/billing/tools.ts:410-478 (handler)The main handler function for the 'gcp-billing-list-services' tool. It uses the CloudCatalogClient to list Google Cloud services, formats them into a markdown table, handles pagination, and returns the response in MCP format.async ({ pageSize, pageToken }) => { try { const catalogClient = getCatalogClient(); logger.debug( `Listing Google Cloud services with pageSize: ${pageSize}`, ); const request: any = { pageSize, }; if (pageToken) { request.pageToken = pageToken; } const [services, nextPageToken] = await catalogClient.listServices(request); if (!services || services.length === 0) { return { content: [ { type: "text", text: "No Google Cloud services found.", }, ], }; } let response = `# Google Cloud Services\n\n`; response += `Found ${services.length} service(s):\n\n`; response += "| Service ID | Display Name | Business Entity |\n"; response += "|------------|--------------|----------------|\n"; for (const service of services) { const cloudService: CloudService = { name: service.name || "", serviceId: service.serviceId || "Unknown", displayName: service.displayName || "Unknown", businessEntityName: service.businessEntityName || "Unknown", }; response += `| ${cloudService.serviceId} | ${cloudService.displayName} | ${cloudService.businessEntityName} |\n`; } if (nextPageToken) { response += `\n**Next Page Token:** ${nextPageToken}\n`; response += `Use this token with the same tool to get the next page of results.\n`; } return { content: [ { type: "text", text: response, }, ], }; } catch (error: any) { logger.error(`Error listing services: ${error.message}`); throw new GcpMcpError( `Failed to list services: ${error.message}`, error.code || "UNKNOWN", error.status || 500, ); } },
- The tool schema defining title, description, and Zod input schema for pagination parameters (pageSize and pageToken).{ title: "List Google Cloud Services", description: "List all available Google Cloud services for billing and cost analysis", inputSchema: { pageSize: z .number() .min(1) .max(200) .default(50) .describe("Maximum number of services to return (1-200)"), pageToken: z .string() .optional() .describe("Token for pagination to get next page of results"), },
- src/services/billing/tools.ts:392-479 (registration)Registers the 'gcp-billing-list-services' tool on the MCP server within the registerBillingTools function."gcp-billing-list-services", { title: "List Google Cloud Services", description: "List all available Google Cloud services for billing and cost analysis", inputSchema: { pageSize: z .number() .min(1) .max(200) .default(50) .describe("Maximum number of services to return (1-200)"), pageToken: z .string() .optional() .describe("Token for pagination to get next page of results"), }, }, async ({ pageSize, pageToken }) => { try { const catalogClient = getCatalogClient(); logger.debug( `Listing Google Cloud services with pageSize: ${pageSize}`, ); const request: any = { pageSize, }; if (pageToken) { request.pageToken = pageToken; } const [services, nextPageToken] = await catalogClient.listServices(request); if (!services || services.length === 0) { return { content: [ { type: "text", text: "No Google Cloud services found.", }, ], }; } let response = `# Google Cloud Services\n\n`; response += `Found ${services.length} service(s):\n\n`; response += "| Service ID | Display Name | Business Entity |\n"; response += "|------------|--------------|----------------|\n"; for (const service of services) { const cloudService: CloudService = { name: service.name || "", serviceId: service.serviceId || "Unknown", displayName: service.displayName || "Unknown", businessEntityName: service.businessEntityName || "Unknown", }; response += `| ${cloudService.serviceId} | ${cloudService.displayName} | ${cloudService.businessEntityName} |\n`; } if (nextPageToken) { response += `\n**Next Page Token:** ${nextPageToken}\n`; response += `Use this token with the same tool to get the next page of results.\n`; } return { content: [ { type: "text", text: response, }, ], }; } catch (error: any) { logger.error(`Error listing services: ${error.message}`); throw new GcpMcpError( `Failed to list services: ${error.message}`, error.code || "UNKNOWN", error.status || 500, ); } }, );
- Helper function to initialize and return the Google Cloud Catalog client used by the tool handler to list services.export function getCatalogClient(): CloudCatalogClient { return new CloudCatalogClient({ projectId: process.env.GOOGLE_CLOUD_PROJECT, }); }
- src/services/billing/types.ts:30-35 (schema)TypeScript interface defining the structure of a Google Cloud service object, used in the handler to type the formatted service data.export interface CloudService { name: string; serviceId: string; displayName: string; businessEntityName: string; }