File Patch MCP Server
Version: 2.0.0 Status: Phase 5 Complete - Production Ready Coverage: 83% overall (244 tests passing)
Overview
A Model Context Protocol (MCP) server for applying unified diff patches to files with comprehensive security validation and error recovery workflows. This server provides 7 tools for patch management with automatic backup, rollback, and atomic operations.
Features
7 Powerful Tools: Complete patch lifecycle management
4 Error Recovery Patterns: Safe workflows with automatic rollback
Comprehensive Security: Symlink, binary file, disk space, and size validation
Atomic Operations: All-or-nothing multi-file patch application
Dry Run Support: Test patches without modification
Multi-file Support: Handle patches affecting multiple files
Automatic Backup & Restore: Safe experimentation with rollback
Quick Start
Installation
Basic Usage
Using Workflow Patterns
Available Tools
Core Patch Tools
apply_patch - Apply a patch to a file (supports dry_run)
validate_patch - Check if a patch can be applied (read-only)
revert_patch - Reverse a previously applied patch
generate_patch - Create a patch from two files
Analysis Tools
inspect_patch - Analyze patch content (supports multi-file patches)
Backup Tools
backup_file - Create a timestamped backup
restore_backup - Restore a file from backup
Error Recovery Patterns
The server provides 4 workflow patterns for safe patch operations:
Pattern 1: Try-Revert (Sequential Patches)
Apply multiple patches sequentially with automatic revert on failure.
Use cases: Multi-step refactoring, dependent patches
Pattern 2: Backup-Restore (Safe Experimentation)
Apply patch with automatic backup and restore on failure.
Use cases: Critical files, experimental changes, production updates
Pattern 3: Validate-All-Then-Apply (Atomic Batch)
Apply multiple patches atomically - all succeed or all rollback.
Use cases: Multi-file refactoring, coordinated changes, consistency requirements
Pattern 4: Progressive Validation
Step-by-step validation with detailed error reporting.
Use cases: Debugging, troubleshooting, learning
For detailed documentation, see WORKFLOWS.md.
Security Features
All file operations include comprehensive security checks:
✅ Symlink Detection: Symlinks rejected (security policy)
✅ Binary File Detection: Binary files not supported
✅ File Size Limits: 10MB maximum file size
✅ Disk Space Validation: 100MB minimum free space required
✅ Path Traversal Protection: Prevents directory escaping
✅ Permission Checks: Read/write permissions validated
✅ Atomic Operations: File replacements use atomic rename
Configuration Constants
Testing
Run All Tests
Test Statistics
Total Tests: 244 (all passing)
Overall Coverage: 83%
Unit Tests: 209 tests
Integration Tests: 35 tests
Workflow Tests: 21 tests
Example Workflow Tests: 14 tests
Coverage Breakdown
Module | Coverage |
models.py | 100% |
inspect.py | 99% |
validate.py | 92% |
revert.py | 91% |
utils.py | 88% |
apply.py | 87% |
server.py | 86% |
generate.py | 81% |
backup.py | 70% |
workflows.py | 70% |
Project Structure
Implementation Phases
Phase 1: Foundation ✅
Data models (Pydantic)
Security utilities
Test infrastructure
Phase 2: Core Tools ✅
apply_patch (with dry_run)
validate_patch
revert_patch
generate_patch
inspect_patch (multi-file support)
Phase 3: Backup Tools ✅
backup_file
restore_backup
Phase 4: MCP Server ✅
Server implementation
Tool registration
MCP protocol integration
Phase 5: Error Recovery Patterns ✅
Try-Revert pattern
Backup-Restore pattern
Atomic Batch pattern
Progressive Validation pattern
Comprehensive integration tests
Example workflow tests
API Correctness
All tools follow consistent API semantics:
validate_patch Return Values
Can apply (success):
Cannot apply (failure):
inspect_patch Return Values
Always returns array of files (multi-file support):
Error Types
The server provides 10 distinct error types:
Standard Errors:
file_not_found
- File doesn't existpermission_denied
- Cannot read/write fileinvalid_patch
- Patch format is malformedcontext_mismatch
- Patch context doesn't match file contentencoding_error
- File encoding issueio_error
- General I/O error
Security Errors:
symlink_error
- Target is a symlink (security policy)binary_file
- Target is a binary file (not supported)disk_space_error
- Insufficient disk spaceresource_limit
- File too large or operation timed out
Code Quality
MCP Server Usage
Run the MCP server:
Or use with Claude Desktop by adding to your MCP configuration.
Documentation
WORKFLOWS.md - Complete guide to error recovery patterns
project_design.md - Full design specification
AI_IMPLEMENTATION_GUIDE.md - Implementation details
Example Workflows
Safe Single Patch Application
Dry Run Test Before Apply
Multi-file Atomic Application
This project follows strict type checking, comprehensive testing, and security-first design principles.
License
This project is part of the File Patch MCP Server implementation. This project is part of the File Patch MCP Server implementation.
Last Updated: 2025-10-17 Phase: 5 of 5 (Complete - Production Ready) Tools Implemented: 7/7 Workflow Patterns: 4/4 Test Coverage: 83% (244 tests passing)
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
Enables applying unified diff patches to files with comprehensive security validation, automatic backup/rollback, and atomic multi-file operations. Provides 7 tools and 4 error recovery patterns for safe patch management workflows.