query-logs
Filter and retrieve log entries from Google Cloud services using natural language queries, with customizable limits for efficient data extraction.
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-77 (handler)Executes the query-logs tool: fetches project ID, queries Cloud Logging entries with given filter and limit, formats results using formatLogEntry, returns markdown text response or error.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)Zod input schema for query-logs tool: filter (required string), 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-78 (registration)Registers the 'query-logs' MCP tool on the server within registerLoggingTools function, specifying name, input schema, and handler.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/index.ts:132-132 (registration)Calls registerLoggingTools(server) in main server setup, which in turn registers the query-logs tool.registerLoggingTools(server);