Skip to main content
Glama

Search and Replace

replace_text
Destructive

Find and replace literal text or RE2 regex across one file or files under a directory, with glob filters, dry-run previews, whole-word matching, and optional diff output.

Instructions

Find and replace every occurrence of literal text or an RE2 regex, like sed -i, in one file or all files under a directory, optionally filtered by glob. The pattern runs over the whole file: it can span lines, and ^ and $ anchor to file start and end. edit replaces one unique exact match.

Input Schema

NameRequiredDescriptionDefault
pathNoFile to rewrite, or directory to rewrite under. Omitting it targets the ENTIRE first allowed root — scope it deliberately, and pair a wide scope with dryRun=true first
dryRunNoPreview replacements without writing to disk (default: false = apply changes)
isRegexNoTreat searchPattern as a RE2 regex (default: literal text match)
patternNoGlob to restrict replacements to specific file types (e.g. **/*.ts); default: all text files
maxDepthNoMax directory depth to scan; 0 = base directory only, omit for unlimited
maxFilesNoMaximum number of files to process
wholeWordNoMatch whole words only (word boundary anchoring)
maxMatchesNoMaximum total match count across all files before stopping (default: 100)
maxResultsNoDeprecated alias of maxMatches; removed in the next release
returnDiffNoInclude a unified diff of all changes in the response
replacementYesReplacement text. Use capture group references ($1, $2, etc.) when isRegex=true. Use an empty string to delete all matches.
caseSensitiveNoEnable case-sensitive matching (default: case-insensitive)
includeHiddenNoInclude hidden items (starting with .)
searchPatternYesExact literal text or RE2 regex pattern to search for. When isRegex=true, uses RE2 syntax (no lookahead, lookbehind, or backreferences are supported). Cannot be empty or whitespace-only.
includeIgnoredNoInclude ignored items (node_modules, .git, etc).

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed4 schema fields changedv2.12.1
    • addedInput schema / properties / maxMatches
      Added value: +{
      +  "description": "Maximum total match count across all files before stopping (default: 100)",
      +  "maximum": 10000,
      +  "minimum": 1,
      +  "type": "integer"
      +}
    • removedInput schema / properties / maxResults / default
      Removed value: -100
    • addedInput schema / properties / maxResults / deprecated
      Added value: +true
    • changedInput schema / properties / maxResults / description
      Previous value: -"Maximum total match count across all files before stopping"New value: +"Deprecated alias of maxMatches; removed in the next release"
  2. Changed1 schema field changedv2.8.1
    • removedInput schema / properties / searchPattern / examples
      Removed value: -[
      -  "TODO",
      -  "function\\s+(\\w+)",
      -  "import.*from"
      -]
  3. Changed1 schema field changedv2.5.0
    • changedInput schema / properties / pattern / examples
      Previous value: -[
      -  "**/*.ts",
      -  "src/**/*.js",
      -  "*.{ts,tsx}"
      -]New value: +[
      +  "**/*.ts",
      +  "src/**/*.js",
      +  "**/*.{ts,tsx}"
      +]
  4. Changed4 schema fields changedv2.4.1
    • addedInput schema / $schema
      Added value: +"https://json-schema.org/draft/2020-12/schema"
    • addedInput schema / properties / pattern / examples
      Added value: +[
      +  "**/*.ts",
      +  "src/**/*.js",
      +  "*.{ts,tsx}"
      +]
    • addedInput schema / properties / replacement / examples
      Added value: +[
      +  "$1_renamed",
      +  "",
      +  "TODO: fix"
      +]
    • addedInput schema / properties / searchPattern / examples
      Added value: +[
      +  "TODO",
      +  "function\\s+(\\w+)",
      +  "import.*from"
      +]
  5. Changed1 schema field changedv2.1.5
    • changedOutput schema / (root)
      Previous value: -{
      -  "additionalProperties": false,
      -  "properties": {
      -    "diff": {
      -      "description": "Unified diff of all changes (present when returnDiff=true or dryRun=true)",
      -      "type": "string"
      -    },
      -    "diffTruncated": {
      -      "description": "True when the diff was cut due to the size limit",
      -      "type": "boolean"
      -    },
      -    "filesScanned": {
      -      "description": "Total number of files examined",
      -      "minimum": 0,
      -      "type": "integer"
      -    },
      -    "results": {
      -      "description": "Per-file results: modified files, then any that could not be processed",
      -      "items": {
      -        "additionalProperties": false,
      -        "properties": {
      -          "error": {
      -            "additionalProperties": false,
      -            "description": "Error details; present on failure",
      -            "properties": {
      -              "code": {
      -                "type": "string"
      -              },
      -              "message": {
      -                "type": "string"
      -              },
      -              "path": {
      -                "type": "string"
      -              },
      -              "suggestion": {
      -                "type": "string"
      -              }
      -            },
      -            "required": [
      -              "code",
      -              "message"
      -            ],
      -            "type": "object"
      -          },
      -          "path": {
      -            "description": "File path relative to the search root",
      -            "type": "string"
      -          },
      -          "value": {
      -            "additionalProperties": false,
      -            "description": "Replacement outcome; present on success",
      -            "properties": {
      -              "matches": {
      -                "description": "Replacements applied in this file",
      -                "minimum": 0,
      -                "type": "integer"
      -              }
      -            },
      -            "required": [
      -              "matches"
      -            ],
      -            "type": "object"
      -          }
      -        },
      -        "required": [
      -          "path"
      -        ],
      -        "type": "object"
      -      },
      -      "type": "array"
      -    },
      -    "resultsTruncated": {
      -      "description": "True when the results list holds fewer entries than summary.total: the changed-file or failed-file cap was hit. Trust summary over results.length.",
      -      "type": "boolean"
      -    },
      -    "stoppedReason": {
      -      "description": "Why enumeration stopped early: maxResults = match cap reached, maxFiles = file cap reached, timeout = time limit hit or cancelled. Absent when every matching file was enumerated. Marks the sweep incomplete, not the writes; files already dispatched still complete.",
      -      "enum": [
      -        "maxResults",
      -        "maxFiles",
      -        "timeout"
      -      ],
      -      "type": "string"
      -    },
      -    "summary": {
      -      "additionalProperties": false,
      -      "properties": {
      -        "failed": {
      -          "minimum": 0,
      -          "type": "integer"
      -        },
      -        "succeeded": {
      -          "minimum": 0,
      -          "type": "integer"
      -        },
      -        "total": {
      -          "minimum": 0,
      -          "type": "integer"
      -        }
      -      },
      -      "required": [
      -        "total",
      -        "succeeded",
      -        "failed"
      -      ],
      -      "type": "object"
      -    },
      -    "totalMatches": {
      -      "description": "Total number of replacements made across all files",
      -      "minimum": 0,
      -      "type": "integer"
      -    }
      -  },
      -  "required": [
      -    "results",
      -    "summary",
      -    "totalMatches",
      -    "filesScanned"
      -  ],
      -  "type": "object"
      -}New value: +null
  6. Addedv2.0.0

TDQS

A4.2/5.0
Behavior4/5

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

Annotations already declare destructiveHint=true and readOnlyHint=false, so the safety profile is covered; the description adds real behavioral context the annotations cannot: in-place rewriting ("like sed -i") and the fact that the pattern spans lines and that ^/$ anchor to file start/end rather than line boundaries. It does not state irreversibility or that `undo` is the recovery path, which is a remaining gap for a destructive tool.

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?

Three sentences, front-loaded with the action and scope, then matching semantics, then the sibling differentiation. Every sentence adds information; nothing is boilerplate.

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

Completeness4/5

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

For a destructive, 15-parameter bulk rewrite tool with no output schema, the definition covers what it does, matching semantics, and the sibling alternative, while the schema handles per-parameter and return-shape detail (returnDiff). It would be complete at 5 if it flagged the irreversibility/dryRun-first practice in the description given how damaging a wide-scope run is.

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 15 parameters are already documented (including dryRun, maxMatches, casing, hidden/ignored files). The description adds only the isRegex literal-vs-regex distinction and the path/glob scoping, which the schema already carries. 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?

States a specific verb+resource (find and replace) with precise scope: literal or RE2 regex, one file or a directory tree, glob-filtered. It also distinguishes itself from the sibling `edit` ("edit replaces one unique exact match"), so an agent can choose without opening either schema.

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

Usage Guidelines4/5

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

The closing sentence routes multi-match work here versus single-unique-match work to `edit`, and "optionally filtered by glob" plus the sed analogy imply the bulk-edit use case. It stops short of explicit exclusions (e.g., prefer `search_text` first, or `diff` to verify) and never mentions dryRun in the description itself, so it is clear context rather than a full when/when-not map.

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