Skip to main content
Glama
andyl25

Google Cloud MCP Server

by andyl25

query-metrics

Query Google Cloud Monitoring metrics using filters and time ranges to analyze performance data from your cloud infrastructure.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filterYesThe filter to apply to metrics
startTimeYesStart time in ISO format or relative time (e.g., "1h", "2d")
endTimeNoEnd time in ISO format (defaults to now)
alignmentPeriodNoAlignment period (e.g., "60s", "300s")

Implementation Reference

  • Executes the query-metrics tool: queries GCP Monitoring API with filter and time range, handles alignment period, formats time series data as markdown table.
      async ({ filter, startTime, endTime, alignmentPeriod }, context) => {
        try {
          const projectId = await getProjectId();
          const client = getMonitoringClient();
          
          const start = parseRelativeTime(startTime);
          const end = endTime ? parseRelativeTime(endTime) : new Date();
          
          // Build request
          const request: any = {
            name: `projects/${projectId}`,
            filter,
            interval: {
              startTime: {
                seconds: Math.floor(start.getTime() / 1000),
                nanos: 0
              },
              endTime: {
                seconds: Math.floor(end.getTime() / 1000),
                nanos: 0
              }
            }
          };
          
          // Add alignment if specified
          if (alignmentPeriod) {
            // Parse alignment period (e.g., "60s" -> 60 seconds)
            const match = alignmentPeriod.match(/^(\d+)([smhd])$/);
            if (!match) {
              throw new GcpMcpError(
                'Invalid alignment period format. Use format like "60s", "5m", "1h".',
                'INVALID_ARGUMENT',
                400
              );
            }
            
            const value = parseInt(match[1]);
            const unit = match[2];
            let seconds = value;
            
            switch (unit) {
              case 'm': // minutes
                seconds = value * 60;
                break;
              case 'h': // hours
                seconds = value * 60 * 60;
                break;
              case 'd': // days
                seconds = value * 60 * 60 * 24;
                break;
            }
            
            request.aggregation = {
              alignmentPeriod: {
                seconds: seconds
              },
              perSeriesAligner: 'ALIGN_MEAN'
            };
          }
          
          const [timeSeries] = await client.listTimeSeries(request);
          
          if (!timeSeries || timeSeries.length === 0) {
            return {
              content: [{
                type: 'text',
                text: `# Metric Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\n\nNo metrics found matching the filter.`
              }]
            };
          }
          
          const formattedData = formatTimeSeriesData(timeSeries as unknown as TimeSeriesData[]);
          
          return {
            content: [{
              type: 'text',
              text: `# Metric Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\n${alignmentPeriod ? `\nAlignment: ${alignmentPeriod}` : ''}\n\n${formattedData}`
            }]
          };
        } catch (error: any) {
          throw new GcpMcpError(
            `Failed to query metrics: ${error.message}`,
            error.code || 'UNKNOWN',
            error.statusCode || 500
          );
        }
      }
    );
  • Zod schema defining inputs for query-metrics tool: filter (required), startTime (required), endTime (optional), alignmentPeriod (optional).
    {
      filter: z.string().describe('The filter to apply to metrics'),
      startTime: z.string().describe('Start time in ISO format or relative time (e.g., "1h", "2d")'),
      endTime: z.string().optional().describe('End time in ISO format (defaults to now)'),
      alignmentPeriod: z.string().optional().describe('Alignment period (e.g., "60s", "300s")')
    },
  • Registers the 'query-metrics' tool with the MCP server inside registerMonitoringTools function.
    server.tool(
      'query-metrics',
      {
        filter: z.string().describe('The filter to apply to metrics'),
        startTime: z.string().describe('Start time in ISO format or relative time (e.g., "1h", "2d")'),
        endTime: z.string().optional().describe('End time in ISO format (defaults to now)'),
        alignmentPeriod: z.string().optional().describe('Alignment period (e.g., "60s", "300s")')
      },
      async ({ filter, startTime, endTime, alignmentPeriod }, context) => {
        try {
          const projectId = await getProjectId();
          const client = getMonitoringClient();
          
          const start = parseRelativeTime(startTime);
          const end = endTime ? parseRelativeTime(endTime) : new Date();
          
          // Build request
          const request: any = {
            name: `projects/${projectId}`,
            filter,
            interval: {
              startTime: {
                seconds: Math.floor(start.getTime() / 1000),
                nanos: 0
              },
              endTime: {
                seconds: Math.floor(end.getTime() / 1000),
                nanos: 0
              }
            }
          };
          
          // Add alignment if specified
          if (alignmentPeriod) {
            // Parse alignment period (e.g., "60s" -> 60 seconds)
            const match = alignmentPeriod.match(/^(\d+)([smhd])$/);
            if (!match) {
              throw new GcpMcpError(
                'Invalid alignment period format. Use format like "60s", "5m", "1h".',
                'INVALID_ARGUMENT',
                400
              );
            }
            
            const value = parseInt(match[1]);
            const unit = match[2];
            let seconds = value;
            
            switch (unit) {
              case 'm': // minutes
                seconds = value * 60;
                break;
              case 'h': // hours
                seconds = value * 60 * 60;
                break;
              case 'd': // days
                seconds = value * 60 * 60 * 24;
                break;
            }
            
            request.aggregation = {
              alignmentPeriod: {
                seconds: seconds
              },
              perSeriesAligner: 'ALIGN_MEAN'
            };
          }
          
          const [timeSeries] = await client.listTimeSeries(request);
          
          if (!timeSeries || timeSeries.length === 0) {
            return {
              content: [{
                type: 'text',
                text: `# Metric Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\n\nNo metrics found matching the filter.`
              }]
            };
          }
          
          const formattedData = formatTimeSeriesData(timeSeries as unknown as TimeSeriesData[]);
          
          return {
            content: [{
              type: 'text',
              text: `# Metric Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\n${alignmentPeriod ? `\nAlignment: ${alignmentPeriod}` : ''}\n\n${formattedData}`
            }]
          };
        } catch (error: any) {
          throw new GcpMcpError(
            `Failed to query metrics: ${error.message}`,
            error.code || 'UNKNOWN',
            error.statusCode || 500
          );
        }
      }
    );
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/andyl25/googlecloud-mcp'

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