This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 22694d345dd4 CAMEL-26932: camel-jbang-mcp - camel_edit_file keeps a 
replace of only whitespace (#29925)
22694d345dd4 is described below

commit 22694d345dd486956a0b5cbfaa26846488ef32d6
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 09:59:04 2026 +0100

    CAMEL-26932: camel-jbang-mcp - camel_edit_file keeps a replace of only 
whitespace (#29925)
    
    Item 6 of [CAMEL-26932](https://issues.apache.org/jira/browse/CAMEL-26932).
    
    ## Problem
    
    `camel_edit_file` of the MCP server passes its arguments through 
`AuthoringTools.args()`, which leaves out blank strings, so a tool's default 
applies. A `replace` of only whitespace (`"\n"`, `"  "`, the indentation of a 
YAML line) was dropped, and the registry tool then replaced the found text with 
nothing: the text was deleted, and the answer said "edited". In YAML, 
whitespace is structure.
    
    ## Change
    
    `camel_edit_file` passes `replace` to the registry tool as it is, blank 
included, when the call gives it. The other arguments still go through 
`args()`. The registry tool already keeps a blank replace (`replace == null ? 
"" : replace`), and the `edits` list was not affected (`editsJson` keeps the 
values).
    
    `camel_write_file` with empty content is still refused. The JIRA mentions 
it too, but it refuses rather than destroying anything, so it stays as it is.
    
    ## Tests
    
    - `AuthoringToolsTest.aReplaceOfOnlyWhitespaceIsKept`: `TODO` in 
`greeting=hello TODO world` replaced by one space gives three spaces. Without 
the fix the test fails (the space is lost).
    - camel-jbang-mcp: 546 tests pass, built in a reactor with core.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/dsl/jbang/core/commands/mcp/AuthoringTools.java   | 10 ++++++++--
 .../dsl/jbang/core/commands/mcp/AuthoringToolsTest.java     | 13 +++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java
 
b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java
index 1e722486bfd6..5a59f7547d41 100644
--- 
a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java
+++ 
b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java
@@ -243,8 +243,14 @@ public class AuthoringTools {
                 }
             }
         }
-        return call("camel_edit_file", args("directory", directory, "file", 
file, "find", find, "replace", replace,
-                "edits", editsJson(edits), "camelVersion", camelVersion), 
null, clientRoot(roots));
+        Map<String, String> arguments = args("directory", directory, "file", 
file, "find", find,
+                "edits", editsJson(edits), "camelVersion", camelVersion);
+        if (replace != null) {
+            // as it is: args() leaves out a blank value, and a replace of 
only whitespace (a newline, the indentation
+            // of a YAML line) then deleted the text it was to replace 
(CAMEL-26932)
+            arguments.put("replace", replace);
+        }
+        return call("camel_edit_file", arguments, null, clientRoot(roots));
     }
 
     /**
diff --git 
a/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java
 
b/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java
index 4c0ee14dd2c8..2f2c8acae7c3 100644
--- 
a/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java
@@ -111,6 +111,19 @@ class AuthoringToolsTest {
                 
.isInstanceOf(ToolCallException.class).hasMessageContaining("directory is 
required");
     }
 
+    @Test
+    void aReplaceOfOnlyWhitespaceIsKept(@TempDir Path dir) throws Exception {
+        // a blank replace was left out of the call, and the text was deleted 
instead (CAMEL-26932)
+        Files.writeString(dir.resolve("application.properties"), 
"greeting=hello TODO world\n", StandardCharsets.UTF_8);
+
+        JsonObject edited = tools.camel_edit_file(null, dir.toString(), 
"application.properties", "TODO", " ", null,
+                null);
+
+        
assertThat(edited.getString("status")).as(edited.toJson()).isEqualTo("edited");
+        assertThat(Files.readString(dir.resolve("application.properties"), 
StandardCharsets.UTF_8))
+                .isEqualTo("greeting=hello   world\n");
+    }
+
     /** CAMEL-25501: several places of one file in one call, written once. */
     @Test
     void severalEditsOfOneFileAreWrittenOnce(@TempDir Path dir) throws 
Exception {

Reply via email to