Skip to main content
Glama
andyl25

Google Cloud MCP Server

by andyl25

query-logs

Query Google Cloud Logging entries using filters to retrieve specific log data for analysis and troubleshooting.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filterYesThe filter to apply to logs
limitNoMaximum number of log entries to return

Implementation Reference

  • The main execution handler for the 'query-logs' tool. Queries Google Cloud Logging using the filter and limit parameters, formats the log entries, and returns them in markdown format or handles errors gracefully.
        async ({ filter, limit }, _extra) => {
          try {
            const projectId = await getProjectId();
            const logging = getLoggingClient();
            
            const [entries] = await logging.getEntries({
              pageSize: limit,
              filter
            });
    
            if (!entries || entries.length === 0) {
              return {
                content: [{
                  type: 'text',
                  text: `No log entries found matching filter: ${filter}`
                }]
              };
            }
    
            const formattedLogs = entries
              .map((entry) => {
                try {
                  return formatLogEntry(entry as unknown as LogEntry);
                } catch (err: unknown) {
                  const errorMessage = err instanceof Error ? err.message : 'Unknown error';
                  return `## Error Formatting Log Entry\n\nAn error occurred while formatting a log entry: ${errorMessage}`;
                }
              })
              .join('\n\n');
    
            return {
              content: [{
                type: 'text',
                text: `# Log Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nEntries: ${entries.length}\n\n${formattedLogs}`
              }]
            };
          } catch (error: unknown) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            
            // Return a user-friendly error message instead of throwing
            return {
              content: [{
                type: 'text',
                text: `# Error Querying Logs
    
    An error occurred while querying logs: ${errorMessage}
    
    Please check your filter syntax and try again. For filter syntax help, see: https://cloud.google.com/logging/docs/view/logging-query-language`
              }],
              isError: true
            };
          }
        }
  • Input schema using Zod for validation: filter (required string) and limit (optional number, 1-1000, default 50).
    {
      filter: z.string().describe('The filter to apply to logs'),
      limit: z.number().min(1).max(1000).default(50).describe('Maximum number of log entries to return')
    },
  • Registration of the 'query-logs' tool on the MCP server instance within the registerLoggingTools function.
      server.tool(
        'query-logs',
        {
          filter: z.string().describe('The filter to apply to logs'),
          limit: z.number().min(1).max(1000).default(50).describe('Maximum number of log entries to return')
        },
        async ({ filter, limit }, _extra) => {
          try {
            const projectId = await getProjectId();
            const logging = getLoggingClient();
            
            const [entries] = await logging.getEntries({
              pageSize: limit,
              filter
            });
    
            if (!entries || entries.length === 0) {
              return {
                content: [{
                  type: 'text',
                  text: `No log entries found matching filter: ${filter}`
                }]
              };
            }
    
            const formattedLogs = entries
              .map((entry) => {
                try {
                  return formatLogEntry(entry as unknown as LogEntry);
                } catch (err: unknown) {
                  const errorMessage = err instanceof Error ? err.message : 'Unknown error';
                  return `## Error Formatting Log Entry\n\nAn error occurred while formatting a log entry: ${errorMessage}`;
                }
              })
              .join('\n\n');
    
            return {
              content: [{
                type: 'text',
                text: `# Log Query Results\n\nProject: ${projectId}\nFilter: ${filter}\nEntries: ${entries.length}\n\n${formattedLogs}`
              }]
            };
          } catch (error: unknown) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            
            // Return a user-friendly error message instead of throwing
            return {
              content: [{
                type: 'text',
                text: `# Error Querying Logs
    
    An error occurred while querying logs: ${errorMessage}
    
    Please check your filter syntax and try again. For filter syntax help, see: https://cloud.google.com/logging/docs/view/logging-query-language`
              }],
              isError: true
            };
          }
        }
      );
  • Supporting helper function that formats individual Google Cloud LogEntry objects into human-readable markdown strings, used in the handler to process query results.
    export function formatLogEntry(entry: LogEntry): string {
      // Safely format the timestamp
      let timestamp: string;
      try {
        // Check if timestamp exists and is valid
        if (!entry.timestamp) {
          timestamp = 'No timestamp';
        } else {
          const date = new Date(entry.timestamp);
          timestamp = !isNaN(date.getTime()) ? date.toISOString() : String(entry.timestamp);
        }
      } catch (error) {
        timestamp = String(entry.timestamp || 'Invalid timestamp');
      }
      
      const severity = entry.severity || 'DEFAULT';
      const resourceType = entry.resource?.type || 'unknown';
      const resourceLabels = entry.resource?.labels 
        ? Object.entries(entry.resource.labels)
            .map(([k, v]) => `${k}=${v}`)
            .join(', ')
        : '';
      
      const resource = resourceLabels 
        ? `${resourceType}(${resourceLabels})` 
        : resourceType;
      
      // Format the payload with better error handling
      let payload: string;
      try {
        if (entry.textPayload !== undefined && entry.textPayload !== null) {
          payload = String(entry.textPayload);
        } else if (entry.jsonPayload) {
          payload = JSON.stringify(entry.jsonPayload, null, 2);
        } else if (entry.protoPayload) {
          payload = JSON.stringify(entry.protoPayload, null, 2);
        } else if (entry.data) {
          payload = JSON.stringify(entry.data, null, 2);
        } else {
          payload = '[No payload]';
        }
      } catch (error: unknown) {
        const errorMessage = error instanceof Error ? error.message : 'Unknown error';
        payload = `[Error formatting payload: ${errorMessage}]`;
      }
    
      // Format labels if they exist
      let labelsStr = '';
      if (entry.labels && Object.keys(entry.labels).length > 0) {
        try {
          labelsStr = Object.entries(entry.labels)
            .map(([k, v]) => `${k}=${v}`)
            .join(', ');
        } catch (error: unknown) {
          const errorMessage = error instanceof Error ? error.message : 'Unknown error';
          labelsStr = `[Error formatting labels: ${errorMessage}]`;
        }
      }
    
      // Format operation if it exists
      let operationStr = '';
      if (entry.operation) {
        try {
          const op = entry.operation as Record<string, any>;
          operationStr = [
            op.id ? `id=${op.id}` : '',
            op.producer ? `producer=${op.producer}` : '',
            op.first ? 'first=true' : '',
            op.last ? 'last=true' : ''
          ].filter(Boolean).join(', ');
        } catch (error: unknown) {
          const errorMessage = error instanceof Error ? error.message : 'Unknown error';
          operationStr = `[Error formatting operation: ${errorMessage}]`;
        }
      }
    
      // Create a more detailed and markdown-friendly format
      return `## ${timestamp} | ${severity} | ${resource}
    ` +
        (entry.logName ? `**Log:** ${entry.logName}\n` : '') +
        (entry.insertId ? `**ID:** ${entry.insertId}\n` : '') +
        (labelsStr ? `**Labels:** ${labelsStr}\n` : '') +
        (operationStr ? `**Operation:** ${operationStr}\n` : '') +
        `\n\`\`\`\n${payload}\n\`\`\``;
    }
  • Utility function that initializes and returns the Google Cloud Logging client, used in the tool handler.
    export function getLoggingClient(): Logging {
      return new Logging({
        projectId: process.env.GOOGLE_CLOUD_PROJECT
      });
    }
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