MCP Commands - FileTools

<< 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

MCP.ApplyDiffToString - Apply Diff to String

PreviousTopNext


MiniRobot Language (MRL) - MCP FileTools Commands

 

MCP.ApplyDiffToString

Apply Diff to String

 

Purpose

 

The MCP.ApplyDiffToString command applies a unified diff (patch) to a string, producing the modified result. This command enables programmatic text transformations using standard diff format, which is particularly useful for automated code modifications, content updates, and batch text processing operations.

 

Syntax

 

MCP.ApplyDiffToString|$$ORIGINAL|$$DIFF|$$RESULT_VAR

 

Parameters

 

$$ORIGINAL - The original text content to which the diff will be applied.

 

$$DIFF - The unified diff content (patch) to apply. Must be in standard unified diff format.

 

$$RESULT_VAR - The variable that will receive the modified text after applying the diff.

 

Return Value

 

The command returns the modified text with all diff patches applied. If the diff cannot be applied cleanly (e.g., due to context mismatches), the command returns an error with details about the failure.

 

Unified Diff Format

 

The diff parameter must follow unified diff format:

• Lines starting with " " (space) are context lines

• Lines starting with "-" are removed

• Lines starting with "+" are added

• @@ markers indicate hunk headers with line numbers

 

Examples

 

' Example 1: Apply a simple diff to modify text

VAR.OriginalText = "Hello World"

VAR.Diff = "--- original\n+++ modified\n@@ -1 +1 @@\n-Hello World\n+Hello Universe"

MCP.ApplyDiffToString|$$OriginalText|$$Diff|$$Result

PRT.Result: $$Result ' Output: "Hello Universe"

 

' Example 2: Multi-line diff with context

VAR.Code = "function greet() {" + $CRLF + " console.log('Hello');" + $CRLF + "}"

VAR.Patch = "--- a\n+++ b\n@@ -1,3 +1,3 @@\n function greet() {\n- console.log('Hello');\n+ console.log('Hello World');\n }"

MCP.ApplyDiffToString|$$Code|$$Patch|$$NewCode

 

' Example 3: Apply diff from memory

MCP.GetContent|memory:myPatch|$$PatchContent

MCP.GetContent|file:C:\temp\source.txt|$$Source

MCP.ApplyDiffToString|$$Source|$$PatchContent|$$Modified

MCP.SetContent|file:C:\temp\source.txt|$$Modified

 

Remarks

 

The MCP.ApplyDiffToString command uses a fuzzy matching algorithm that can handle minor context mismatches, but for best results ensure the diff was generated from the exact original content. The command supports both Unix (LF) and Windows (CRLF) line endings and will preserve the line ending style of the original text.

 

For complex multi-file patches, use MCP.DiffEditFile or MCP.DiffEditMemory which handle file paths embedded in the diff.

 

Error Conditions

 

The command will fail with an error if:

• The original text parameter is missing

• The diff parameter is missing or malformed

• The diff context does not match the original text

• The return variable parameter is missing

 

See also:

 

MCP.DiffEditFile - Diff Edit File

MCP.DiffEditMemory - Diff Edit Memory

MCP.DiffEditClipboard - Diff Edit Clipboard

FileTools Overview