@ahmedshaikh/apply-patch-mcp
v0.1.0
Published
Apply a unified diff / patch to files as an MCP server — tolerant of line-offset drift (matches by context), supports modify/create/delete across files, with a dry-run. So agents can apply PR diffs and generated patches without shell gymnastics.
Maintainers
Readme
apply-patch
Apply a unified diff / patch to files — for an agent that has diff text (from a
PR, git format-patch, or generated by an LLM) and needs to land it, without
shell gymnastics. Tolerant of line-offset drift: it matches each hunk by its
surrounding context, not just the stated line number.
apply_patch({ diff: "<unified diff>", dry_run: true }) // validate first
→ { ok: true, files: [ { path: "src/api.ts", action: "modified", hunks: 2 } ] }Why
Edit is for hand-written edits; there was no clean way to apply an external
diff. This closes that — and completes the edit-safety story: checkpoint →
apply_patch → verify (and restore if verify fails).
The tool
apply_patch(diff, root?, dry_run?) — applies a unified diff. Handles
modify / create (--- /dev/null) / delete (+++ /dev/null) across multiple
files. Returns per-file { path, ok, action, hunks, error? }.
- Context matching — if the file has drifted from the diff's line numbers, it searches outward from the expected position for the matching context block.
- Per-file safety — a file whose hunk doesn't match is reported as failed and left unchanged (no half-applied file).
dry_run: true— validate every hunk without writing.
Setup
npm install
npm testclaude mcp add apply-patch -e APPLY_PATCH_ROOT=/path/to/project -- node /abs/path/apply-patch/dist/server.js
# CLI: agent-patch change.diff | git diff | agent-patch --dry-runHonest limits
- Standard unified-diff format (
---/+++/@@); git-extended metadata lines are ignored. Renames-as-metadata aren't specially handled (delete+create works). - Created files don't get a guaranteed trailing newline (the
\ No newlinemarker is ignored). - Context matching is exact-line within the search window — it won't apply against
already-modified content (use
verifyafter,checkpointbefore).
