Skip to main content
Glama
mcollina

MCP Ripgrep Server

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)) {
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions 'using ripgrep' which implies a command-line search tool behavior, but doesn't describe output format, error handling, performance characteristics, or whether this is a read-only operation. For a tool with 6 parameters and no annotations, this is inadequate.

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?

The description is extremely concise - just 6 words that directly state the tool's purpose. Every word earns its place with zero waste. It's appropriately sized for a simple search/count tool.

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

Completeness2/5

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

Given 6 parameters, no annotations, and no output schema, the description is insufficiently complete. It doesn't explain what the output looks like (just counts? formatted results?), doesn't mention error conditions, and provides no context about the ripgrep implementation details that might affect usage.

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 description coverage is 100%, so all parameters are documented in the schema. The description adds no additional parameter information beyond what's in the schema. The baseline score of 3 is appropriate when the schema does all the parameter documentation work.

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

Purpose4/5

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

The description clearly states the action ('count matches') and the tool used ('using ripgrep'), which is a specific verb+resource combination. However, it doesn't distinguish this tool from its sibling 'search' tool, which likely performs similar search functionality but with different output. The purpose is clear but lacks sibling differentiation.

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?

The description provides no guidance on when to use this tool versus alternatives like the 'search' or 'advanced-search' sibling tools. There's no mention of prerequisites, typical use cases, or exclusions. The agent must infer usage from the tool name alone.

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

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