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
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | The search pattern (regex by default) | |
| path | Yes | Directory or file(s) to search. | |
| caseSensitive | No | Use case sensitive search (default: auto) | |
| filePattern | No | Filter by file type or glob | |
| countLines | No | Count matching lines instead of total matches | |
| useColors | No | Use colors in output (default: false) |
Implementation Reference
- src/index.ts:380-439 (handler)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"] } },
- src/index.ts:142-153 (schema)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"] }
- src/index.ts:414-414 (helper)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)) {