|
| 1 | +# Why MCP File Edit Patches Fail - Common Issues and Solutions |
| 2 | + |
| 3 | +## 1. Context Already Modified |
| 4 | +**Issue**: The context you're looking for has already been changed by a previous edit. |
| 5 | +**Solution**: Check the current file content first, or use a more unique context. |
| 6 | + |
| 7 | +## 2. Context Not Found |
| 8 | +**Issue**: The exact lines specified in context don't exist as continuous lines. |
| 9 | +**Common Causes**: |
| 10 | +- Extra or missing whitespace |
| 11 | +- Different line endings |
| 12 | +- Content has moved |
| 13 | +- Partial match only |
| 14 | + |
| 15 | +**Solutions**: |
| 16 | +- Use `read_file` to check exact content first |
| 17 | +- Use more specific, unique context |
| 18 | +- Use line-based patches if you know the exact line numbers |
| 19 | + |
| 20 | +## 3. Markdown Formatting Issues |
| 21 | +**Issue**: Incorrect number of backticks in code blocks (e.g., 4 instead of 3) |
| 22 | +**Solution**: Ensure markdown syntax is correct: ` ``` ` not ` ```` ` |
| 23 | + |
| 24 | +## 4. Multiple Matches |
| 25 | +**Issue**: Context appears multiple times in the file |
| 26 | +**Solution**: |
| 27 | +- Use more lines of context for uniqueness |
| 28 | +- Specify `occurrence` parameter (1-based) |
| 29 | +- Use pattern-based patches with `occurrence` |
| 30 | + |
| 31 | +## 5. Special Characters |
| 32 | +**Issue**: Regex special characters in find/replace patterns |
| 33 | +**Solution**: |
| 34 | +- Escape special characters: `(`, `)`, `[`, `]`, etc. |
| 35 | +- Use `regex: false` for literal matching (if supported) |
| 36 | + |
| 37 | +## Debugging Tips |
| 38 | + |
| 39 | +1. **Always check current content first**: |
| 40 | + ```python |
| 41 | + content = read_file("myfile.txt") |
| 42 | + # Verify your context exists |
| 43 | + ``` |
| 44 | + |
| 45 | +2. **Use specific context**: |
| 46 | + ```python |
| 47 | + # Bad - too generic |
| 48 | + {"context": ["", "## Tools"]} |
| 49 | + |
| 50 | + # Good - more specific |
| 51 | + {"context": ["specific line", "", "## Tools"]} |
| 52 | + ``` |
| 53 | + |
| 54 | +3. **Use line numbers when possible**: |
| 55 | + ```python |
| 56 | + # If you know the exact line |
| 57 | + {"line": 42, "content": "new content"} |
| 58 | + ``` |
| 59 | + |
| 60 | +4. **Test with dry_run**: |
| 61 | + ```python |
| 62 | + patch_file("file.txt", patches=[...], dry_run=True) |
| 63 | + ``` |
| 64 | + |
| 65 | +## Enhanced Error Messages |
| 66 | + |
| 67 | +The improved patch function now provides: |
| 68 | +- Exact search details |
| 69 | +- Partial match locations |
| 70 | +- Specific mismatch information |
| 71 | +- Suggestions for fixes |
| 72 | + |
| 73 | +## Example: Robust Patching |
| 74 | + |
| 75 | +```python |
| 76 | +# First, check what's there |
| 77 | +content = read_file("config.py") |
| 78 | + |
| 79 | +# Find unique context |
| 80 | +search_results = search_files("specific_string", "config.py") |
| 81 | + |
| 82 | +# Use robust context |
| 83 | +patch_file("config.py", patches=[{ |
| 84 | + "context": [ |
| 85 | + "# Unique comment", |
| 86 | + "def my_function():", |
| 87 | + " return old_value" |
| 88 | + ], |
| 89 | + "replace": [ |
| 90 | + "# Unique comment", |
| 91 | + "def my_function():", |
| 92 | + " return new_value" |
| 93 | + ] |
| 94 | +}]) |
| 95 | +``` |
0 commit comments