logs-time-range
Retrieve Google Cloud logs within specified time ranges using ISO timestamps or relative formats, with optional filtering and result limits for targeted log analysis.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"startTime": "<startTime>"
},
"name": "logs-time-range"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| startTime | Yes | Start time in ISO format or relative time (e.g., "1h", "2d") | |
| endTime | No | End time in ISO format (defaults to now) | |
| filter | No | Additional filter criteria | |
| limit | No | Maximum number of log entries to return |
Implementation Reference
- src/services/logging/tools.ts:88-151 (handler)The async handler function that implements the core logic of the 'logs-time-range' tool: parses time inputs, builds logging filter, queries GCP Logging API, formats entries, handles errors, and returns markdown-formatted results.
async ({ startTime, endTime, filter, limit }, _extra) => { 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 }; } } - src/services/logging/tools.ts:82-86 (schema)Zod input schema for 'logs-time-range' tool defining parameters: startTime (required string), endTime (optional string), filter (optional string), limit (number with defaults and constraints).
{ 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') - src/services/logging/tools.ts:80-152 (registration)The server.tool() call that registers the 'logs-time-range' tool on the MCP server, specifying the name, input schema, and inline handler function.
server.tool( 'logs-time-range', { 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 }, _extra) => { 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 }; } } );