Skip to main content
Glama
yufeizhou666

log-analyzer-mcp

by yufeizhou666

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

TableJSON Schema
NameRequiredDescriptionDefault
keywordsNoKeywords to search for in log files
regexNoRegular expression pattern to match
logPathNoPath to log file or directory/var/log
limitNoMaximum number of results

Implementation Reference

  • 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' }]
      };
    }
  • 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;
    }
  • 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 || {});
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description must disclose behavioral traits. It states the tool returns matching log entries with timestamps, which is helpful, but omits details like case sensitivity, regex+keyword interaction, and pagination. Basic coverage but incomplete.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences with no redundancy. First sentence states action and method, second states output. Front-loaded, every word adds value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 4 parameters and no output schema, the description covers core functionality but lacks details on parameter interplay (e.g., can keywords and regex be used together?) and default behavior of logPath and limit. Adequate but not fully complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% with descriptions for all 4 parameters. The tool description adds minimal extra meaning beyond restating search mechanisms and output; baseline 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool searches log files using keywords or regex patterns, and returns matching entries with timestamps. This verb+resource specificity distinguishes it from siblings like count_by_level and explain_error.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance on when to use this tool versus alternatives. The description does not mention when to prefer search_logs over count_by_level, explain_error, or query_by_timerange, leaving the agent without comparative context.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/yufeizhou666/my_mcp'

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