query-logs
Query Google Cloud Logging entries using filters to retrieve specific log data for analysis and troubleshooting.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | Yes | The filter to apply to logs | |
| limit | No | Maximum number of log entries to return |
Implementation Reference
- src/services/logging/tools.ts:24-76 (handler)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 }; } }
- src/services/logging/tools.ts:20-23 (schema)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') },
- src/services/logging/tools.ts:18-77 (registration)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 }; } } );
- src/services/logging/types.ts:40-124 (helper)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\`\`\``; }
- src/services/logging/types.ts:28-32 (helper)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 }); }