You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update README to reflect changes from unified diff format to block format, enhance installation instructions, and clarify usage examples for patch_file tool.
Copy file name to clipboardExpand all lines: README.md
+81-75Lines changed: 81 additions & 75 deletions
Original file line number
Diff line number
Diff line change
@@ -1,50 +1,29 @@
1
1
# Patch File MCP
2
2
3
-
An MCP Server to patch existing files using unified diff format. This allows AI agents (like Claude) to make precise changes to files in your projects.
3
+
An MCP Server to patch existing files using block format. This allows AI agents (like Claude) to make precise changes to files in your projects.
4
4
5
5
## Overview
6
6
7
-
Patch File MCP provides a simple way to modify files by applying patches in unified diff format. The key benefits include:
7
+
Patch File MCP provides a simple way to modify files by applying patches in block format. The key benefits include:
8
8
9
9
- Makes targeted changes to specific parts of files without rewriting the entire content
10
-
- Supports multiple patches to the same file
11
-
-Safer than complete file rewrites since it only affects the specified sections
10
+
- Supports multiple patches to the same file in a single request
11
+
-Ensures safety through exact text matching and uniqueness verification
12
12
- Better alternative to the `edit_block` tool from `desktop-commander` for most file editing tasks
@@ -79,74 +77,82 @@ If you've installed the package with pip:
79
77
}
80
78
```
81
79
82
-
### Configuring Claude Desktop
80
+
> **Note:** Replace `/Users/your-username` with the actual path to your own projects and code directories.
83
81
84
-
1. Install Claude Desktop from the [official website](https://claude.ai/desktop)
85
-
2. Open Claude Desktop
86
-
3. From the menu, select Settings → Developer → Edit Config
87
-
4. Add the MCP configuration above to your existing config (modify paths as needed)
88
-
5. Save and restart Claude Desktop
82
+
## Arguments
89
83
90
-
## Tools
84
+
The `--allowed-dir` argument is used to specify the directories that the server has access to. You can use it multiple times to allow access to multiple directories. All directories inside the allowed directories are also allowed.
85
+
It is optional. If not provided, the server will only have access to the home directory of the user running the server.
91
86
92
-
Patch File MCP provides one main tool:
87
+
## Usage
93
88
94
-
### patch_file
89
+
The MCP server is started by the client (e.g., Claude Desktop) based on the configuration you provide. You don't need to start the server manually.
95
90
96
-
Updates a file by applying a unified diff/patch to it.
91
+
### Tools
97
92
98
-
```
99
-
patch_file(file_path: str, patch_content: str)
100
-
```
93
+
Patch File MCP provides one main tool:
101
94
102
-
**Parameters:**
103
-
-`file_path`: Path to the file to be patched
104
-
-`patch_content`: Unified diff/patch content to apply to the file
95
+
#### patch_file
105
96
106
-
**Notes:**
107
-
- The file must exist and be within an allowed directory
108
-
- The patch must be in valid unified diff format
109
-
- If the patch fails to apply, an error is raised
97
+
Update the file by applying a patch/edit to it using block format.
110
98
111
-
## Example Workflow
99
+
```python
100
+
patch_file(file_path: str, patch_content: str)
101
+
```
102
+
-**file_path**: Path to the file to be patched.
103
+
-**patch_content**: Content to search and replace in the file using block format with SEARCH/REPLACE markers. Multiple blocks are supported.
112
104
113
-
1. Begin a conversation with Claude about modifying a file in your project
114
-
2. Claude generates a unified diff/patch that makes the desired changes
115
-
3. Claude uses `patch_file` to apply these changes to your file
116
-
4. If the patch fails, Claude might suggest using `write_file` from another MCP as an alternative
105
+
The patch content must have the following format:
117
106
118
-
## Creating Unified Diffs
107
+
```
108
+
<<<<<<< SEARCH
109
+
Text to find in the file
110
+
=======
111
+
Text to replace it with
112
+
>>>>>>> REPLACE
113
+
```
119
114
120
-
A unified diff typically looks like:
115
+
You can include multiple search-replace blocks in a single request:
121
116
122
117
```
123
-
--- oldfile
124
-
+++ newfile
125
-
@@ -start,count +start,count @@
126
-
context line
127
-
-removed line
128
-
+added line
129
-
context line
118
+
<<<<<<< SEARCH
119
+
First text to find
120
+
=======
121
+
First replacement
122
+
>>>>>>> REPLACE
123
+
<<<<<<< SEARCH
124
+
Second text to find
125
+
=======
126
+
Second replacement
127
+
>>>>>>> REPLACE
130
128
```
131
129
132
-
Claude can generate these diffs automatically when suggesting file changes.
130
+
This tool verifies that each search text appears exactly once in the file to ensure the correct section is modified. If a search text appears multiple times or isn't found, it will report an error.
133
131
134
-
## Recent Changes
132
+
## Example Workflow
133
+
134
+
1. Begin a conversation with Claude about modifying a file in your project
135
+
2. Claude generates a block format patch that makes the desired changes
136
+
3. Claude uses `patch_file` to apply these changes to your file
137
+
4. If the patch fails, Claude provides detailed error information to help you fix the issue
135
138
136
-
### 2025-04-23 Bugfixes
137
-
- Fixed an issue where the patch operation would fail with "Not a directory" error when trying to apply patches.
138
-
- Updated the patching logic to use the parent directory as the root for patch application, rather than the file itself.
139
139
140
140
## Security Considerations
141
141
142
142
- All file operations are restricted to allowed directories
143
-
- The tool only modifies specified sections of files
143
+
- Search texts must appear exactly once in the file to ensure correct targeting
144
+
- Detailed error reporting helps identify and fix issues
144
145
- Each patch operation is validated before being applied
145
146
147
+
## Advantages over similar tools
148
+
149
+
-**Multiple blocks in one operation**: Can apply several changes in a single call
150
+
-**Safety checks**: Ensures the correct sections are modified through exact matching
151
+
-**Detailed errors**: Provides clear feedback when patches can't be applied
0 commit comments