|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > MCP. - MCP (Model-Context Protocol) server operations > File Tools - File and content operations > MCP Commands - FileTools |
MiniRobot Language (MRL) - MCP FileTools Commands
MCP.DiffEditFile
Diff Edit File
Purpose
The MCP.DiffEditFile command applies a unified diff (patch) directly to a file, modifying the file in-place. This command is useful for automated file modifications, applying patches from version control, and batch editing operations. The file path can be embedded in the diff itself or specified separately.
Syntax
MCP.DiffEditFile|$$FILE_PATH|$$DIFF
MCP.DiffEditFile|$$DIFF
Parameters
$$FILE_PATH - (Optional) The path to the file to patch. If omitted, the file path is extracted from the diff header.
$$DIFF - The unified diff content (patch) to apply. Must be in standard unified diff format.
Return Value
The command returns a success message on completion, including statistics about the changes made (files modified, insertions, deletions), or an error message if the diff could not be applied.
Examples
' Example 1: Apply diff with explicit file path
VAR.Patch = "--- a\n+++ b\n@@ -1,3 +1,3 @@\n line 1\n-old line 2\n+new line 2\n line 3"
MCP.DiffEditFile|C:\Project\file.txt|$$Patch
' Example 2: Apply diff with path embedded in patch
VAR.FullPatch = "--- C:\\Project\\config.ini\n+++ C:\\Project\\config.ini\n@@ -5,7 +5,7 @@\n [Settings]\n-debug=1\n+debug=0\n timeout=30"
MCP.DiffEditFile|$$FullPatch
' Example 3: Load and apply patch from file
MCP.GetContent|file:C:\Patches\fix.patch|$$PatchContent
MCP.DiffEditFile|C:\Source\main.bas|$$PatchContent
' Example 4: Batch patch multiple files
' Create a patch for settings update
VAR.SettingsPatch = "--- settings.conf\n+++ settings.conf\n@@ -10,5 +10,5 @@\n- version=1.0\n+ version=2.0\n auto_update=false\n+ notify=true"
MCP.DiffEditFile|C:\App\settings.conf|$$SettingsPatch
Remarks
The MCP.DiffEditFile command modifies files in-place. It is recommended to create backups of important files before applying patches. The command uses fuzzy matching algorithms that can handle minor context changes, but significant differences between the patch context and actual file content may cause failures.
The unified diff format requires proper context lines (lines starting with space) to match correctly. At least 3 lines of context are recommended for reliable patching. The command preserves the original file's line ending style (CRLF or LF).
When the file path is embedded in the diff (--- and +++ lines), the command extracts it automatically. If both an explicit path and embedded paths are provided, the explicit path takes precedence.
Error Conditions
The command will fail with an error if:
• The file does not exist
• The diff parameter is missing or malformed
• The diff context does not match the file content
• Permission is denied for file write access
• The file is locked by another process
See also:
• MCP.DiffEditMemory - Diff Edit Memory
• MCP.DiffEditClipboard - Diff Edit Clipboard
• MCP.ApplyDiffToString - Apply Diff to String