Replace / Refactor Code
replaceRefactor 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
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | File or directory to modify (relative or absolute; default: '.'). Narrow to a subdirectory for large refactors. | . |
| type | No | 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. | ast |
| query | Yes | Pattern to match: ast-grep pattern with $VAR/$$$ARGS ('ast'), JavaScript RegExp ('regex'), or exact literal string ('text'). | |
| dryRun | No | Safety preview mode. Default true: returns unified diff without modifying files. Set false only after inspecting the diff. | |
| language | No | [ast only] Target language (e.g. 'typescript', 'python'). Auto-detected when 'path' is a single file; recommended when 'path' is a directory. | |
| noIgnore | No | If true, also replace in files ignored by .gitignore. Default: false (respects .gitignore). | |
| multiline | No | [regex only] Allow pattern to match across multiple lines. Automatically enabled when query contains \n or multiline character classes. | |
| filePattern | No | Glob pattern(s) to include, comma-separated, e.g. '*.ts' or 'src/**/*.tsx'. | |
| replacement | Yes | Replacement template: reuse captured meta-variables like $VAR/$$$ARGS ('ast'), $1/$& capture groups ('regex'), or exact literal text ('text'). | |
| caseSensitive | No | [regex/text only] Force case-sensitive (true) or case-insensitive (false). Default: true. | |
| includeHidden | No | If true, include hidden dotfiles and dot-directories (excludes .git unless noIgnore is true). Default: false. | |
| excludePattern | No | Comma-separated glob patterns to exclude, e.g. '**/dist/**,**/*.test.ts'. | |
| maxReplacements | No | Safety guardrail: maximum number of individual match occurrences to replace per call (default: 50). |