Skip to main content
Glama

count-matches

Count pattern matches in files using ripgrep to track occurrences across directories or specific files.

Instructions

Count matches in files using ripgrep

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYesThe search pattern (regex by default)
pathYesDirectory or file(s) to search.
caseSensitiveNoUse case sensitive search (default: auto)
filePatternNoFilter by file type or glob
countLinesNoCount matching lines instead of total matches
useColorsNoUse colors in output (default: false)

Implementation Reference

  • Main handler logic for the 'count-matches' tool. Parses input arguments, builds a ripgrep command with appropriate flags for counting matches ( -c for lines or --count-matches for total), executes it via the exec function, processes output, and returns the count.
    case "count-matches": { const pattern = String(args.pattern || ""); const path = String(args.path); const caseSensitive = typeof args.caseSensitive === 'boolean' ? args.caseSensitive : undefined; const filePattern = args.filePattern ? String(args.filePattern) : undefined; const countLines = typeof args.countLines === 'boolean' ? args.countLines : true; const useColors = typeof args.useColors === 'boolean' ? args.useColors : false; if (!pattern) { return { isError: true, content: [{ type: "text", text: "Error: Pattern is required" }] }; } // Build the rg command with flags let command = "rg"; // Add case sensitivity flag if specified if (caseSensitive === true) { command += " -s"; // Case sensitive } else if (caseSensitive === false) { command += " -i"; // Case insensitive } // Add file pattern if specified if (filePattern) { command += ` -g ${escapeShellArg(filePattern)}`; } // Add count flag if (countLines) { command += " -c"; // Count lines } else { command += " --count-matches"; // Count total matches } // Add color setting command += useColors ? " --color always" : " --color never"; // Add pattern and path command += ` ${escapeShellArg(pattern)} ${escapeShellArg(path)}`; console.error(`Executing: ${command}`); const { stdout, stderr } = await exec(command); // If there's anything in stderr, log it for debugging if (stderr) { console.error(`ripgrep stderr: ${stderr}`); } return { content: [ { type: "text", text: processOutput(stdout, useColors) || "No matches found" } ] }; }
  • src/index.ts:139-154 (registration)
    Tool registration in the ListTools response, defining name, description, and input schema for 'count-matches'.
    { name: "count-matches", description: "Count matches in files using ripgrep", inputSchema: { type: "object", properties: { pattern: { type: "string", description: "The search pattern (regex by default)" }, path: { type: "string", description: "Directory or file(s) to search." }, caseSensitive: { type: "boolean", description: "Use case sensitive search (default: auto)" }, filePattern: { type: "string", description: "Filter by file type or glob" }, countLines: { type: "boolean", description: "Count matching lines instead of total matches" }, useColors: { type: "boolean", description: "Use colors in output (default: false)" } }, required: ["pattern", "path"] } },
  • Input schema definition for the 'count-matches' tool, specifying parameters like pattern, path, caseSensitive, etc.
    inputSchema: { type: "object", properties: { pattern: { type: "string", description: "The search pattern (regex by default)" }, path: { type: "string", description: "Directory or file(s) to search." }, caseSensitive: { type: "boolean", description: "Use case sensitive search (default: auto)" }, filePattern: { type: "string", description: "Filter by file type or glob" }, countLines: { type: "boolean", description: "Count matching lines instead of total matches" }, useColors: { type: "boolean", description: "Use colors in output (default: false)" } }, required: ["pattern", "path"] }
  • Flag addition for counting total matches (non-line based) in ripgrep command.
    command += " --count-matches"; // Count total matches
  • src/index.ts:185-185 (registration)
    Whitelist check for handling 'count-matches' tool in CallToolRequestSchema handler.
    if (!["search", "advanced-search", "count-matches", "list-files", "list-file-types"].includes(toolName)) {

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/mcollina/mcp-ripgrep'

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