Skip to main content
Glama
krzko

Google Cloud MCP Server

by krzko

gcp-logging-query-time-range

Query Google Cloud Logs within specific time ranges using relative times or ISO timestamps to retrieve and filter log entries for analysis.

Instructions

Query Google Cloud Logs within a specific time range. Supports relative times (1h, 2d) and ISO timestamps.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
startTimeYesStart time in ISO format or relative time (e.g., "1h", "2d")
endTimeNoEnd time in ISO format (defaults to now)
filterNoAdditional filter criteria
limitNoMaximum number of log entries to return

Implementation Reference

  • The main handler function for the 'gcp-logging-query-time-range' tool. It parses the start and end times, constructs a timestamp filter, queries the GCP Logging API, formats the log entries, and returns the results or handles errors gracefully.
    async ({ startTime, endTime, filter, limit }) => { try { const projectId = await getProjectId(); const logging = getLoggingClient(); const start = parseRelativeTime(startTime); const end = endTime ? parseRelativeTime(endTime) : new Date(); // Build filter string let filterStr = `timestamp >= "${start.toISOString()}" AND timestamp <= "${end.toISOString()}"`; if (filter) { filterStr = `${filterStr} AND ${filter}`; } const [entries] = await logging.getEntries({ pageSize: limit, filter: filterStr, }); if (!entries || entries.length === 0) { return { content: [ { type: "text", text: `No log entries found in the specified time range with filter: ${filterStr}`, }, ], }; } 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 Time Range Results\n\nProject: ${projectId}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\nFilter: ${filter || "None"}\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 time range format and try again. Valid formats include: - ISO date strings (e.g., "2025-03-01T00:00:00Z") - Relative time expressions: "1h" (1 hour ago), "2d" (2 days ago), "1w" (1 week ago), etc.`, }, ], isError: true, }; } },
  • Zod schema for the tool's input parameters: startTime (required), endTime (optional), filter (optional), and limit (with defaults).
    inputSchema: { 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)"), filter: z.string().optional().describe("Additional filter criteria"), limit: z .number() .min(1) .max(1000) .default(50) .describe("Maximum number of log entries to return"), },
  • Registration of the 'gcp-logging-query-time-range' tool with the MCP server inside the registerLoggingTools function.
    "gcp-logging-query-time-range", { title: "Query Logs by Time Range", description: "Query Google Cloud Logs within a specific time range. Supports relative times (1h, 2d) and ISO timestamps.", inputSchema: { 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)"), filter: z.string().optional().describe("Additional filter criteria"), limit: z .number() .min(1) .max(1000) .default(50) .describe("Maximum number of log entries to return"), }, }, async ({ startTime, endTime, filter, limit }) => { try { const projectId = await getProjectId(); const logging = getLoggingClient(); const start = parseRelativeTime(startTime); const end = endTime ? parseRelativeTime(endTime) : new Date(); // Build filter string let filterStr = `timestamp >= "${start.toISOString()}" AND timestamp <= "${end.toISOString()}"`; if (filter) { filterStr = `${filterStr} AND ${filter}`; } const [entries] = await logging.getEntries({ pageSize: limit, filter: filterStr, }); if (!entries || entries.length === 0) { return { content: [ { type: "text", text: `No log entries found in the specified time range with filter: ${filterStr}`, }, ], }; } 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 Time Range Results\n\nProject: ${projectId}\nTime Range: ${start.toISOString()} to ${end.toISOString()}\nFilter: ${filter || "None"}\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 time range format and try again. Valid formats include: - ISO date strings (e.g., "2025-03-01T00:00:00Z") - Relative time expressions: "1h" (1 hour ago), "2d" (2 days ago), "1w" (1 week ago), etc.`, }, ], isError: true, }; } }, );

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/krzko/google-cloud-mcp'

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