Skip to content

Commit 0a41ebe

Browse files
author
Bruce Hauman
committed
Add dry-run parameter to file_edit tool
- Add optional dry-run parameter that accepts 'diff' or 'new-source' - When dry-run is set, skip file save, timestamp update, and Emacs highlight - With 'diff' mode: returns diff of proposed changes - With 'new-source' mode: returns complete new source code - Parameter is not exposed in tool schema (internal use for clients) - Addresses issue #115
1 parent 928069b commit 0a41ebe

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/clojure_mcp/tools/file_edit/pipeline.clj

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
(s/def ::old-string string?)
2424
(s/def ::new-string string?)
2525
(s/def ::nrepl-client-atom (s/nilable #(instance? clojure.lang.Atom %)))
26+
27+
(s/def ::dry-run (s/nilable #{"diff" "new-source"}))
2628
;; Pipeline specific steps
2729

2830
;; Using check-file-modified from form-edit/pipeline instead
@@ -117,15 +119,16 @@
117119
- file-path: Path to the file to edit
118120
- old-string: String to replace
119121
- new-string: New string to insert
120-
- nrepl-client-atom: Atom containing the nREPL client (optional)
121-
- config: Optional tool configuration map
122+
- dry-run: Optional string, either \"diff\" or \"new-source\" to skip actual file write
123+
- config: Optional tool configuration map with :nrepl-client-atom
122124
123125
Returns:
124126
- A context map with the result of the operation"
125-
[file-path old-string new-string {:keys [nrepl-client-atom] :as config}]
127+
[file-path old-string new-string dry-run {:keys [nrepl-client-atom] :as config}]
126128
(let [initial-ctx {::form-pipeline/file-path file-path
127129
::old-string old-string
128130
::new-string new-string
131+
::dry-run dry-run
129132
::form-pipeline/nrepl-client-atom nrepl-client-atom
130133
::form-pipeline/config config}]
131134
;; Pipeline for existing file edit
@@ -147,9 +150,14 @@
147150
format-clojure-content ;; Format Clojure files automatically
148151
form-pipeline/determine-file-type ;; This will mark as "update"
149152
form-pipeline/generate-diff ;; Generate diff between old and new
150-
form-pipeline/save-file ;; Save the file
151-
form-pipeline/update-file-timestamp ;; Update the timestamp after save
152-
form-pipeline/highlight-form))) ;; Update the timestamp after save ;; Update the timestamp after save
153+
;; Skip file operations if dry-run is set
154+
(fn [ctx]
155+
(if (::dry-run ctx)
156+
ctx
157+
(-> ctx
158+
form-pipeline/save-file
159+
form-pipeline/update-file-timestamp
160+
form-pipeline/highlight-form)))))) ;; Update the timestamp after save ;; Update the timestamp after save
153161

154162
;; Format result for tool consumption
155163
(defn format-result
@@ -159,17 +167,25 @@
159167
- ctx: The final context map from the pipeline
160168
161169
Returns:
162-
- A map with :error, :message, and :diff keys, and potentially :repaired"
170+
- A map with :error, :message, and :diff or :new-source keys, and potentially :repaired"
163171
[ctx]
164172
(if (::form-pipeline/error ctx)
165173
{:error true
166174
:message (::form-pipeline/message ctx)}
167-
(cond-> {:error false
168-
:diff (::form-pipeline/diff ctx)
169-
:type (::form-pipeline/type ctx)}
170-
;; Include repaired flag if present
171-
(::form-pipeline/repaired ctx)
172-
(assoc :repaired true))))
175+
(let [dry-run (::dry-run ctx)]
176+
(cond-> {:error false
177+
:type (::form-pipeline/type ctx)}
178+
;; Include repaired flag if present
179+
(::form-pipeline/repaired ctx)
180+
(assoc :repaired true)
181+
182+
;; Return new-source if dry-run is "new-source"
183+
(= dry-run "new-source")
184+
(assoc :new-source (::form-pipeline/output-source ctx))
185+
186+
;; Otherwise return diff (default behavior and for "diff" dry-run)
187+
(not= dry-run "new-source")
188+
(assoc :diff (::form-pipeline/diff ctx))))))
173189

174190
(comment
175191
;; === Examples of using the file-edit pipeline directly ===

src/clojure_mcp/tools/file_edit/tool.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ To make a file edit, provide the file_path, old_string (the text to replace), an
8181
:new_string new_string))))
8282

8383
(defmethod tool-system/execute-tool :file-edit [{:keys [nrepl-client-atom] :as tool} inputs]
84-
(let [{:keys [file_path old_string new_string]} inputs
85-
result (pipeline/file-edit-pipeline file_path old_string new_string tool)]
84+
(let [{:keys [file_path old_string new_string dry-run]} inputs
85+
result (pipeline/file-edit-pipeline file_path old_string new_string dry-run tool)]
8686
(pipeline/format-result result)))
8787

88-
(defmethod tool-system/format-results :file-edit [_ {:keys [error message diff type repaired]}]
88+
(defmethod tool-system/format-results :file-edit [_ {:keys [error message diff new-source type repaired]}]
8989
(if error
9090
{:error true
9191
:result [message]}
9292
(cond-> {:error false
93-
:result [diff]
93+
:result [(or new-source diff)]
9494
:type type}
9595
;; Include repaired flag if present
9696
repaired (assoc :repaired true))))

0 commit comments

Comments
 (0)