How to refactor a function across an entire codebase without breaking it
When to use: You need to rename a function, change its signature, or inline a helper — and it's used in 30+ files across the repo.
Prerequisites
- Clean git working tree —
git statusshows nothing staged — so you cangit diffto review andgit restoreif needed - Filesystem root scoped to the repo — Launch with
npx -y @modelcontextprotocol/server-filesystem /abs/path/to/repo
Flow
-
Find every callsiteSearch the codebase for every usage of
getUserProfile(. Group matches by file and give me a count per file.✓ Copied→ File list with match counts, excluding tests vs source distinction -
Dry-run the edit on one fileShow me what the edit would look like in src/api/users.ts — a diff, not the full file. Don't write yet.✓ Copied→ Minimal diff patch, not a full-file rewrite
-
Apply to all files and reportApply the same transformation to every file from step 1. Use edit_file (line-level), not write_file (overwrite). Tell me any file where the pattern didn't match cleanly.✓ Copied→ Per-file success/skip log
Outcome: A focused, reviewable git diff you can run tests against — no surprise whole-file rewrites.
Pitfalls
- Claude uses
write_fileand silently drops half the file when the edit is complex — Explicitly requireedit_filefor in-place changes; only allowwrite_filefor files being created fresh - Match hits unrelated code (e.g.
getUserProfileAvatar) — Anchor the search:getUserProfile(with the trailing paren, or use a word-boundary regex