search_logs
Search log files using keywords or regex patterns to find matching entries with timestamps.
Instructions
Search log files by keywords or regex patterns. Returns matching log entries with timestamps.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keywords | No | Keywords to search for in log files | |
| regex | No | Regular expression pattern to match | |
| logPath | No | Path to log file or directory | /var/log |
| limit | No | Maximum number of results |
Implementation Reference
- src/tools/search_logs.ts:61-88 (handler)Main handler function for search_logs tool. Extracts keywords/regex/logPath/limit from input, finds log files, searches them sequentially, and returns formatted results.
export async function searchLogs(input: ToolInput): Promise<{ content: Array<{ type: string; text: string }> }> { const { keywords, regex, logPath = '/var/log', limit = 100 } = input as SearchLogsInput; if (!keywords?.length && !regex) { return { content: [{ type: 'text', text: 'Error: Provide keywords or regex' }] }; } const files = findLogFiles(logPath); if (files.length === 0) { return { content: [{ type: 'text', text: `No .log files found in ${logPath}` }] }; } const allResults: LogEntry[] = []; for (const file of files) { const results = await searchFile(file, keywords, regex, limit - allResults.length); allResults.push(...results); if (allResults.length >= limit) break; } const text = allResults .slice(0, limit) .map(e => `[${e.timestamp.toISOString()}] [${e.level}] ${e.message}`) .join('\n'); return { content: [{ type: 'text', text: text || 'No matches found' }] }; } - src/tools/search_logs.ts:31-59 (handler)Helper function that reads a single log file line-by-line, applies keyword/regex matching, and collects matching LogEntry objects up to the limit.
async function searchFile( filePath: string, keywords: string[] | undefined, regex: string | undefined, limit: number ): Promise<LogEntry[]> { const results: LogEntry[] = []; const stream = fs.createReadStream(filePath); const rl = readline.createInterface({ input: stream }); for await (const line of rl) { let matched = false; if (regex) { matched = matchesRegex(line, regex); } else if (keywords && keywords.length > 0) { matched = matchesKeywords(line, keywords); } if (matched) { const entry = parseLogLine(line); if (entry) { results.push(entry); if (results.length >= limit) break; } } } return results; } - src/tools/search_logs.ts:7-12 (schema)Input schema for search_logs tool: keywords (string[]), regex (string), logPath (string, default /var/log), limit (number, default 100).
interface SearchLogsInput extends ToolInput { keywords?: string[]; regex?: string; logPath?: string; limit?: number; } - src/index.ts:14-42 (registration)Tool registration in the TOOLS array with name 'search_logs', description, and JSON Schema input validation.
const TOOLS = [ { name: 'search_logs', description: 'Search log files by keywords or regex patterns. Returns matching log entries with timestamps.', inputSchema: { type: 'object', properties: { keywords: { type: 'array', items: { type: 'string' }, description: 'Keywords to search for in log files' }, regex: { type: 'string', description: 'Regular expression pattern to match' }, logPath: { type: 'string', default: '/var/log', description: 'Path to log file or directory' }, limit: { type: 'number', default: 100, description: 'Maximum number of results' } } } }, - src/index.ts:148-149 (registration)Dispatch handler that calls searchLogs() when the tool name 'search_logs' is invoked via the MCP CallToolRequestSchema handler.
case 'search_logs': return await searchLogs(args || {});