Skip to main content
Glama
Search Code MCP

Search Code MCP

by joutvhu

Replace / Refactor Code

replace
Destructive

Refactor code using AST, regex, or literal rewrites, with a dry-run unified diff preview by default before writing changes.

Instructions

Refactor or replace code using structural AST rewrites ('ast', DEFAULT) or regex/literal matching ('regex', 'text'). Runs in dryRun=true by default to return a unified diff preview without modifying files; inspect the diff first, then re-run with dryRun=false to write changes. Capped at maxReplacements (default 50) matches per call. Note: 'regex' mode uses ripgrep for discovery and JavaScript RegExp for replacement.

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": {
      "query": "<query>",
      "replacement": "<replacement>"
    },
    "name": "replace"
  }
}

Input Schema

NameRequiredDescriptionDefault
pathNoFile or directory to modify (relative or absolute; default: '.'). Narrow to a subdirectory for large refactors..
typeNoReplacement engine (NOTE: defaults to 'ast', unlike 'search' and 'count' which default to 'regex'). Use 'ast' for structural rewrites, 'regex' for JS regex replacement, or 'text' for literal string replacement.ast
queryYesPattern to match: ast-grep pattern with $VAR/$$$ARGS ('ast'), JavaScript RegExp ('regex'), or exact literal string ('text').
dryRunNoSafety preview mode. Default true: returns unified diff without modifying files. Set false only after inspecting the diff.
languageNo[ast only] Target language (e.g. 'typescript', 'python'). Auto-detected when 'path' is a single file; recommended when 'path' is a directory.
noIgnoreNoIf true, also replace in files ignored by .gitignore. Default: false (respects .gitignore).
multilineNo[regex only] Allow pattern to match across multiple lines. Automatically enabled when query contains \n or multiline character classes.
filePatternNoGlob pattern(s) to include, comma-separated, e.g. '*.ts' or 'src/**/*.tsx'.
replacementYesReplacement template: reuse captured meta-variables like $VAR/$$$ARGS ('ast'), $1/$& capture groups ('regex'), or exact literal text ('text').
caseSensitiveNo[regex/text only] Force case-sensitive (true) or case-insensitive (false). Default: true.
includeHiddenNoIf true, include hidden dotfiles and dot-directories (excludes .git unless noIgnore is true). Default: false.
excludePatternNoComma-separated glob patterns to exclude, e.g. '**/dist/**,**/*.test.ts'.
maxReplacementsNoSafety guardrail: maximum number of individual match occurrences to replace per call (default: 50).

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed17 schema fields changedv1.1.0
    • addedInput schema / properties / caseSensitive
      Added value: +{
      +  "description": "[regex/text only] Force case-sensitive (true) or case-insensitive (false). Default: true.",
      +  "type": "boolean"
      +}
    • changedInput schema / properties / dryRun / description
      Previous value: -"If true (default), outputs unified diff preview without modifying files on disk"New value: +"Safety preview mode. Default true: returns unified diff without modifying files. Set false only after inspecting the diff."
    • changedInput schema / properties / excludePattern / description
      Previous value: -"Glob pattern to exclude"New value: +"Comma-separated glob patterns to exclude, e.g. '**/dist/**,**/*.test.ts'."
    • changedInput schema / properties / filePattern / description
      Previous value: -"Glob pattern to filter files"New value: +"Glob pattern(s) to include, comma-separated, e.g. '*.ts' or 'src/**/*.tsx'."
    • removedInput schema / properties / fileType
      Removed value: -{
      -  "description": "File type filter",
      -  "type": "string"
      -}
    • addedInput schema / properties / includeHidden
      Added value: +{
      +  "default": false,
      +  "description": "If true, include hidden dotfiles and dot-directories (excludes .git unless noIgnore is true). Default: false.",
      +  "type": "boolean"
      +}
    • changedInput schema / properties / language / description
      Previous value: -"Programming language for AST mode (auto-detected if omitted)"New value: +"[ast only] Target language (e.g. 'typescript', 'python'). Auto-detected when 'path' is a single file; recommended when 'path' is a directory."
    • changedInput schema / properties / maxReplacements / description
      Previous value: -"Safety guardrail: maximum number of matches to replace (default: 50)"New value: +"Safety guardrail: maximum number of individual match occurrences to replace per call (default: 50)."
    • addedInput schema / properties / maxReplacements / maximum
      Added value: +9007199254740991
    • addedInput schema / properties / maxReplacements / minimum
      Added value: +1
    • changedInput schema / properties / maxReplacements / type
      Previous value: -"number"New value: +"integer"
    • addedInput schema / properties / multiline
      Added value: +{
      +  "description": "[regex only] Allow pattern to match across multiple lines. Automatically enabled when query contains \\n or multiline character classes.",
      +  "type": "boolean"
      +}
    • changedInput schema / properties / noIgnore / description
      Previous value: -"Whether to search inside gitignored files"New value: +"If true, also replace in files ignored by .gitignore. Default: false (respects .gitignore)."
    • changedInput schema / properties / path / description
      Previous value: -"Directory or file to perform replacement in. Accepts relative or absolute paths (default: '.')"New value: +"File or directory to modify (relative or absolute; default: '.'). Narrow to a subdirectory for large refactors."
    • changedInput schema / properties / query / description
      Previous value: -"Target search pattern to replace (AST pattern or regex)"New value: +"Pattern to match: ast-grep pattern with $VAR/$$$ARGS ('ast'), JavaScript RegExp ('regex'), or exact literal string ('text')."
    • changedInput schema / properties / replacement / description
      Previous value: -"Replacement string or AST rewrite pattern"New value: +"Replacement template: reuse captured meta-variables like $VAR/$$$ARGS ('ast'), $1/$& capture groups ('regex'), or exact literal text ('text')."
    • changedInput schema / properties / type / description
      Previous value: -"Replacement engine: 'ast' (default structural rewrite), 'regex', 'text'"New value: +"Replacement engine (NOTE: defaults to 'ast', unlike 'search' and 'count' which default to 'regex'). Use 'ast' for structural rewrites, 'regex' for JS regex replacement, or 'text' for literal string replacement."
  2. Changed1 schema field changedv1.0.2
    • changedInput schema / properties / path / description
      Previous value: -"Directory or file to perform replacement in (default: '.')"New value: +"Directory or file to perform replacement in. Accepts relative or absolute paths (default: '.')"
  3. First observedv1.0.0

TDQS

A4.1/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, so the bar is lower; the description adds real value by disclosing the default dry-run safety preview, the maxReplacements=50 cap, and that 'regex' uses ripgrep discovery with JS RegExp replacement. It does not state what the response looks like on a partial or failed replacement, keeping 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?

Three front-loaded sentences: capability/modes first, then the dry-run workflow, then the cap. The ripgrep/RegExp note is mildly tangential but earns its place as an engine caveat.

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 13-parameter destructive tool with no output schema, the description covers mode selection, the safety workflow, and the replacement cap adequately. Gaps are minor: no mention of error behavior on invalid patterns or what the dry-run diff looks like.

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 13 parameters are already documented with defaults and enums. The description reinforces dryRun default and the maxReplacements cap but adds no syntax or format detail beyond the schema, 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 (refactor/replace) plus resource (code) and names the three engines ('ast', 'regex', 'text') with their semantics. An agent can tell this mutates code and which mode applies without opening the 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?

Gives a clear operating workflow: dryRun=true by default, inspect the diff, then re-run with dryRun=false. It also implicitly routes structural edits to 'ast' and literal edits to 'text'. It does not explicitly say when to prefer this over sibling 'search'/'count' or when not to use it, so it stops short of a 5.

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

Deploy Server

Other Tools