Patch MCP Server
A Model Context Protocol (MCP) server that enables AI assistants to safely apply unified diff patches to files with comprehensive security validation.
Version: 2.0.0 | Status: Beta | Tools: 7 | Test Coverage: 84% (286 tests)
Why Patch MCP Server?
Enable your AI assistant to:
✅ Apply code changes using standard unified diff format
✅ Validate patches before applying them
✅ Create and restore backups automatically
✅ Revert changes safely if something goes wrong
✅ Apply multiple changes atomically via multi-hunk patches
✅ Test changes with dry-run mode before committing
All with built-in security (no symlinks, binary files, or directory traversal) and automatic rollback on failures.
Why Use Patch Tools Instead of Direct Editing?
For AI assistants and developers, apply_patch provides significant advantages over traditional Edit operations:
Feature | apply_patch | Edit Tool |
Format | Standard unified diff (like git diff) | Custom old/new strings |
Multiple changes | ✅ Multi-hunk (atomic) | ❌ Separate calls (no atomicity) |
Change visibility | ✅ Clear diff view | ❌ Hard to spot differences |
Token efficiency | ✅ ~50% less tokens | ❌ Full old+new strings required |
Testing | ✅ Dry-run mode available | ❌ No preview capability |
Atomicity | ✅ All changes succeed/fail together | ❌ Partial updates possible |
Reviewability | ✅ Standard format developers know | ❌ Custom format |
Real-World Example
Task: Update 3 config values in one file
Edit: 3 separate tool calls, no atomicity, hard to review
apply_patch: 1 call with 3 hunks, atomic operation, clear diff
Bottom line: For most file modifications, apply_patch is more efficient, safer, and clearer than Edit operations.
Quick Start
Installation
Configure with Claude Desktop
Add to your Claude Desktop MCP configuration (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
Restart Claude Desktop and the patch tools will be available.
Run Standalone
The server runs in stdio mode and communicates via the Model Context Protocol.
Available Tools
The server provides 7 tools for comprehensive patch management:
Core Patch Operations
apply_patch- Apply a unified diff patch to a fileSupports multi-hunk patches (apply multiple changes atomically)
Dry-run mode for testing without modification
Automatic validation before application
validate_patch- Check if a patch can be applied (read-only)Preview changes before applying
Detect context mismatches
See affected line ranges
revert_patch- Reverse a previously applied patchUndo changes safely
Works with multi-hunk patches
Requires exact original patch
generate_patch- Create a patch from two file versionsCompare original and modified files
Generate standard unified diff format
Configurable context lines
Analysis & Inspection
inspect_patch- Analyze patch content without filesSee what files are affected
Count hunks and line changes
Supports multi-file patches
Backup & Recovery
backup_file- Create timestamped backupsFormat:
filename.backup.YYYYMMDD_HHMMSSPreserves file metadata
Automatic disk space checks
restore_backup- Restore from backupsAuto-detect original location
Safety checks before overwriting
Force option available
Example: How an AI Assistant Uses This Server
Scenario 1: Simple Code Modification
AI Assistant's thought process:
"The user wants to change the timeout from 30 to 60 seconds in config.py. I'll use the patch server to do this safely."
AI uses tools:
Generate the patch:
Validate it can be applied:
Create backup before applying:
Apply the patch:
AI reports to user:
"I've updated the timeout from 30 to 60 seconds in config.py. A backup was created at config.py.backup.20250118_143052 in case you need to revert."
Scenario 2: Multi-file Refactoring with Dry Run
AI Assistant's thought process:
"The user wants to rename a function across 3 files. I should test this first with dry-run, then apply atomically."
AI uses tools:
Test each patch with dry-run:
Apply all patches:
AI reports to user:
"I've renamed the function across utils.py, handlers.py, and tests.py. All changes were validated first and applied atomically."
Scenario 3: Something Goes Wrong
AI Assistant's thought process:
"The patch failed to apply. I should revert to the backup."
AI uses tools:
AI reports to user:
"The patch couldn't be applied because the file content didn't match. I've restored the original file from backup. The file may have been modified since we discussed the change."
Security Features
All operations include comprehensive security checks:
🔒 Symlink Protection - Symlinks are rejected (security policy)
🔒 Binary File Detection - Binary files automatically detected and rejected
🔒 Size Limits - Maximum 10MB file size
🔒 Disk Space Validation - Ensures 100MB+ free space before operations
🔒 Path Traversal Protection - Prevents directory escaping
🔒 Permission Checks - Validates read/write permissions
🔒 Atomic Operations - File replacements use atomic rename
See SECURITY.md for detailed security information.
Multi-Hunk Patches
A powerful feature: apply multiple changes to different parts of a file atomically in a single patch:
All three changes are applied together or none are applied. If any hunk fails, the entire patch is rejected.
Documentation
Documentation
SECURITY.md - Security policy and best practices
WORKFLOWS.md - Error recovery workflow patterns
CONTRIBUTING.md - Contributing guidelines
CHANGELOG.md - Version history and changes
Error Types
The server provides 10 distinct error types for precise error handling:
Standard Errors:
file_not_found,permission_denied,invalid_patch,context_mismatch,encoding_error,io_error
Security Errors:
symlink_error,binary_file,disk_space_error,resource_limit
Testing & Quality
286 tests (all passing)
84% code coverage across all modules
Strict type checking with mypy
Code formatting with black
Linting with ruff
CI/CD via GitHub Actions (Linux, macOS, Windows)
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for:
Development setup
Testing guidelines
Code quality standards
Commit message conventions
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author: Scott Henning
Support
Issues: GitHub Issues
Discussions: GitHub Discussions
Security: See SECURITY.md for vulnerability reporting
Model Context Protocol
This server implements the Model Context Protocol (MCP), an open protocol that enables AI assistants to securely interact with local tools and data sources.
Learn more:
Last Updated: 2025-10-19 | Phase: 5 of 5 (Beta) | Tools: 7/7