Skip to main content
Glama

Search and Replace

replace_text
Destructive

Replace every occurrence of literal text or an RE2 regex in one file or a directory tree, with dry-run previews and optional diffs for bulk edits.

Instructions

Replace every match of literal text or an RE2 regex in one file or across a directory tree. Use when the same change must land at every occurrence. Do not use it for one unique snippet; use edit instead, patch for a unified diff, or search_text to only find matches. A live run rewrites each file as the scan reaches it; one failed file does not stop the others. Reaching maxMatches (default 100), maxFiles, or the deadline stops the run; files already rewritten stay changed. Returns changed paths, failures, skip counts, stoppedReason, and an undo id (live runs, when undo is enabled) for undo, valid 30 minutes. isError is set when every file failed or the request is invalid (bad regex, path outside roots).

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "searchPattern": "<searchPattern>",
      "replacement": "<replacement>"
    },
    "name": "replace_text"
  }
}

Input Schema

NameRequiredDescriptionDefault
pathNoFile or directory to rewrite. A named file ignores pattern, hidden, and gitignore filters, and fails if binary or too large. Omitted, it targets the ENTIRE single root (required with several roots); try a wide scope with dryRun=true first
dryRunNoPreview without writing. Always returns the diff, never an undo id.
excludeNoGlobs for entries to leave out, with everything under a matching folder. Without / a pattern matches a name at any depth (vendor, *.test.ts); with / it matches the path relative to path.
isRegexNoTreat searchPattern as an RE2 regex and expand $1, $<name>, and $& in replacement (default: literal text match)
patternNoGlob limiting which files under a directory path are rewritten (e.g. **/*.ts). Without /, it matches file names at any depth. Ignored when path names a file
maxDepthNoMax directory depth. 0 scans only the base directory. Omit for no limit.
maxFilesNoStops after this many files scanned, counting files with no match; reports stoppedReason=maxFiles (default: no limit)
wholeWordNoMatch whole words only
maxMatchesNoMaximum matches replaced across all files (default: 100). Setting it makes the scan sequential and exact.
returnDiffNoInclude the unified diff in a live run. Cut at 20 KB.
replacementYesReplacement text. An empty string deletes matches.
caseSensitiveNoMatch case exactly. Unlike search_text, defaults to true.
includeHiddenNoInclude hidden items (starting with .)
searchPatternYesLiteral text or RE2 regex to find; can span lines. With isRegex=true, ^ and $ match per line (\A and \z for the whole file); no lookaround or backreferences. All-CRLF files match as LF: write \n.
includeIgnoredNoInclude ignored items (node_modules, .git, etc).

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed1 schema field changedv2.22.0
    • addedInput schema / properties / exclude
      Added value: +{
      +  "description": "Globs for entries to leave out, with everything under a matching folder. Without / a pattern matches a name at any depth (vendor, *.test.ts); with / it matches the path relative to path.",
      +  "items": {
      +    "description": "Relative glob pattern under the search root (e.g. \"**/*.ts\", \"src/**/*.js\").",
      +    "examples": [
      +      "**/*.ts",
      +      "src/**/*.js",
      +      "**/*.{ts,tsx}"
      +    ],
      +    "maxLength": 1000,
      +    "minLength": 1,
      +    "type": "string"
      +  },
      +  "maxItems": 20,
      +  "minItems": 1,
      +  "type": "array"
      +}
  2. Changed12 schema fields changedv2.20.0
    • changedInput schema / properties / caseSensitive / description
      Previous value: -"Match case exactly (default: true). Pass false to ignore case. search_text ignores case by default, so matches it reports can include case variants this call does not replace."New value: +"Match case exactly. Unlike search_text, defaults to true."
    • changedInput schema / properties / dryRun / description
      Previous value: -"Preview replacements without writing to disk (default: false = apply changes)"New value: +"Preview without writing. Always returns the diff, never an undo id."
    • changedInput schema / properties / isRegex / description
      Previous value: -"Treat searchPattern as a RE2 regex (default: literal text match)"New value: +"Treat searchPattern as an RE2 regex and expand $1, $<name>, and $& in replacement (default: literal text match)"
    • changedInput schema / properties / maxDepth / description
      Previous value: -"Max directory depth to scan; 0 = base directory only, omit for unlimited"New value: +"Max directory depth. 0 scans only the base directory. Omit for no limit."
    • changedInput schema / properties / maxFiles / description
      Previous value: -"Maximum number of files to process"New value: +"Stops after this many files scanned, counting files with no match; reports stoppedReason=maxFiles (default: no limit)"
    • changedInput schema / properties / maxMatches / description
      Previous value: -"Maximum total match count across all files before stopping (default: 100)"New value: +"Maximum matches replaced across all files (default: 100). Setting it makes the scan sequential and exact."
    • changedInput schema / properties / path / description
      Previous value: -"File to rewrite, or directory to rewrite under. Omitting it targets the ENTIRE root when there is only one; scope it deliberately, and pair a wide scope with dryRun=true first"New value: +"File or directory to rewrite. A named file ignores pattern, hidden, and gitignore filters, and fails if binary or too large. Omitted, it targets the ENTIRE single root (required with several roots); try a wide scope with dryRun=true first"
    • changedInput schema / properties / pattern / description
      Previous value: -"Glob to restrict replacements to specific file types (e.g. **/*.ts); default: all text files"New value: +"Glob limiting which files under a directory path are rewritten (e.g. **/*.ts). Without /, it matches file names at any depth. Ignored when path names a file"
    • changedInput schema / properties / replacement / description
      Previous value: -"Replacement text. Use capture group references ($1, $2, etc.) when isRegex=true. Use an empty string to delete all matches."New value: +"Replacement text. An empty string deletes matches."
    • changedInput schema / properties / returnDiff / description
      Previous value: -"Include a unified diff of all changes in the response"New value: +"Include the unified diff in a live run. Cut at 20 KB."
    • changedInput schema / properties / searchPattern / description
      Previous value: -"Exact 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."New value: +"Literal text or RE2 regex to find; can span lines. With isRegex=true, ^ and $ match per line (\\A and \\z for the whole file); no lookaround or backreferences. All-CRLF files match as LF: write \\n."
    • changedInput schema / properties / wholeWord / description
      Previous value: -"Match whole words only (word boundary anchoring)"New value: +"Match whole words only"
  3. Changed1 schema field changedv2.18.2
    • changedInput schema / properties / caseSensitive / description
      Previous value: -"Match case exactly (default: true). Pass false to ignore case."New value: +"Match case exactly (default: true). Pass false to ignore case. search_text ignores case by default, so matches it reports can include case variants this call does not replace."
  4. Changed4 schema fields changedv2.17.0
    • changedInput schema / properties / caseSensitive / default
      Previous value: -falseNew value: +true
    • changedInput schema / properties / caseSensitive / description
      Previous value: -"Enable case-sensitive matching (default: case-insensitive)"New value: +"Match case exactly (default: true). Pass false to ignore case."
    • removedInput schema / properties / maxResults
      Removed value: -{
      -  "deprecated": true,
      -  "description": "Deprecated alias of maxMatches; removed in the next release",
      -  "maximum": 10000,
      -  "minimum": 1,
      -  "type": "integer"
      -}
    • changedInput schema / properties / path / description
      Previous value: -"File 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"New value: +"File to rewrite, or directory to rewrite under. Omitting it targets the ENTIRE root when there is only one; scope it deliberately, and pair a wide scope with dryRun=true first"
  5. 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"
  6. Changed1 schema field changedv2.8.1
    • removedInput schema / properties / searchPattern / examples
      Removed value: -[
      -  "TODO",
      -  "function\\s+(\\w+)",
      -  "import.*from"
      -]
  7. 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}"
      +]
  8. 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"
      +]
  9. 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
  10. Addedv2.0.0

TDQS

A4.4/5.0
Behavior4/5

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

Annotations already declare destructiveHint=true and idempotentHint=false, but the description adds substantial context beyond them: partial-failure semantics ('one failed file does not stop the others'), that already-rewritten files stay changed when a limit trips, the 30-minute undo window, and the isError conditions. It does not spell out permission/root requirements, which keeps it from a 5.

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

Conciseness4/5

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

Front-loaded with purpose and alternatives, then behavior, then return values — a logical order with no filler sentences. It is dense and long, but nearly every clause carries operational information.

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

Completeness5/5

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

For a 15-parameter destructive tool with no output schema, the description covers return shape (changed paths, failures, skip counts, stoppedReason, undo id), failure modes, and limit behavior, which is everything an agent needs to call it safely.

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%, so all 15 parameters are already documented in the schema. The description restates a few defaults (maxMatches 100) and the stopping behavior of maxMatches/maxFiles/deadline, but adds little syntax or format meaning beyond what the schema provides, so the baseline 3 applies.

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 and resource ('Replace every match of literal text or an RE2 regex in one file or across a directory tree') and immediately scopes it against siblings by naming `edit`, `patch`, and `search_text` with their distinct roles.

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

Usage Guidelines5/5

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

Gives an explicit when-to-use condition ('when the same change must land at every occurrence') plus an explicit when-not ('one unique snippet') and routes to three named alternatives. Nothing is left to inference.

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