Skip to content

Commit e0c2c14

Browse files
author
Bruce Hauman
committed
more nrepl-launcher config logic improvements
1 parent c62789e commit e0c2c14

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ For a quick start: **[Creating Your Own Custom MCP Server](doc/custom-mcp-server
933933
Using the -X invocation requires EDN values.
934934

935935
#### `:port`
936-
**Optional** - The nREPL server port to connect to. Required unless using `:start-nrepl-cmd` with `:parse-nrepl-port` or relying on an existing `.nrepl-port` file.
936+
**Optional** - The nREPL server port to connect to. **Required** unless using `:start-nrepl-cmd` with `:parse-nrepl-port`
937937

938938
`:port 7888`
939939

@@ -996,9 +996,6 @@ clojure -X:mcp :port 7888 :host '"0.0.0.0"' :project-dir '"/path/to/project"'
996996
# Using a custom config file
997997
clojure -X:mcp :port 7888 :config-file '"/path/to/custom-config.edn"'
998998

999-
# Using existing .nrepl-port file (no explicit port needed)
1000-
clojure -X:mcp
1001-
1002999
# Specifying Babashka environment
10031000
clojure -X:mcp :port 7888 :nrepl-env-type :bb
10041001
```

src/clojure_mcp/nrepl_launcher.clj

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,25 @@
189189

190190
(defn should-start-nrepl?
191191
"Determine if we should auto-start an nREPL server based on conditions:
192+
Providing a :port with :start-nrepl-cmd works
193+
Providing a :parse-nrepl-port with :start-nrepl-cmd works
194+
Providing a :port AND :parse-nrepl-port with an :start-nrepl-cmd does not work
192195
1. CLI: Both :start-nrepl-cmd AND :project-dir provided in args
193196
2. Config: .clojure-mcp/config.edn exists with :start-nrepl-cmd"
194197
[nrepl-args]
195-
(let [{:keys [start-nrepl-cmd project-dir port]} nrepl-args]
198+
(let [{:keys [start-nrepl-cmd project-dir parse-nrepl-port port]} nrepl-args]
196199
(cond
197200
;; Don't start if port provided but no start command (existing behavior)
198201
(and port (not start-nrepl-cmd))
199202
(do
200203
(log/debug "Port already provided without start command, skipping auto-start")
201204
false)
202205

206+
(and port start-nrepl-cmd parse-nrepl-port) ;; makes no sense to provide this on the command line
207+
(do
208+
(log/debug ":port and :parse-nrepl-port both provided with nrepl start command, skipping auto-start")
209+
false)
210+
203211
;; CLI condition: start-nrepl-cmd AND project-dir provided (regardless of port)
204212
(and start-nrepl-cmd project-dir)
205213
(do
@@ -209,17 +217,30 @@
209217
;; Config file condition
210218
:else
211219
(if-let [config (load-config-if-exists project-dir)]
212-
(if (:start-nrepl-cmd config)
220+
(cond
221+
;; To me providing a port along with parse-nrepl-port indicates that command line args are overriding the config.edn
222+
;; this way you can have startup commands in config.edn and yet
223+
;; still easily override them on the cli... This is overly complex of course.
224+
(and port (:start-nrepl-cmd config) (:parse-nrepl-port config))
225+
(do
226+
(log/debug ":port and :parse-nrepl-port both provided with nrepl start command, skipping auto-start")
227+
false)
228+
229+
(:start-nrepl-cmd config)
213230
(do
214231
(log/info "Auto-start condition met: config file with :start-nrepl-cmd")
215232
true)
216-
false)
233+
234+
:else false)
217235
false))))
218236

219-
(defn add-project-dir [nrepl-args]
220-
(if (and (:start-nrepl-cmd nrepl-args) (not (:project-dir nrepl-args)))
237+
(defn add-project-dir [{:keys [start-nrepl-cmd project-dir parse-nrepl-port port] :as nrepl-args}]
238+
(cond
239+
;; if a port is provided the intent is not clear here
240+
(and parse-nrepl-port start-nrepl-cmd port) nrepl-args
241+
(and start-nrepl-cmd (not project-dir))
221242
(assoc nrepl-args :project-dir (System/getProperty "user.dir"))
222-
nrepl-args))
243+
:else nrepl-args))
223244

224245
(defn maybe-start-nrepl-process
225246
"Main wrapper that conditionally starts an nREPL process.

test/clojure_mcp/nrepl_launcher_test.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@
5454
(testing "returns false for empty args"
5555
(is (not (launcher/should-start-nrepl? {}))))
5656

57-
(testing "allows auto-start when both start-nrepl-cmd and port provided"
58-
(is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
59-
:project-dir "/tmp/test"
60-
:port 7888})))
57+
(testing "does not auto-start when both start-nrepl-cmd and port and parse-nrepl-port provided"
58+
(is (not (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
59+
:project-dir "/tmp/test"
60+
:port 7888
61+
:parse-nrepl-port true}))))
6162

6263
(testing "works with vector format for start-nrepl-cmd"
6364
(is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
@@ -96,8 +97,8 @@
9697
(let [args {:host "localhost"}]
9798
(is (= args (launcher/maybe-start-nrepl-process args)))))
9899

99-
(testing "returns unchanged args when port already provided"
100-
(let [args {:port 7888 :start-nrepl-cmd ["lein" "repl"]}]
100+
(testing "returns unchanged args when port already provided with parse-nrepl-port"
101+
(let [args {:parse-nrepl-port true :port 7888 :start-nrepl-cmd ["lein" "repl"]}]
101102
(is (= args (launcher/maybe-start-nrepl-process args)))))
102103

103104
;; Note: Testing actual process startup would require integration tests

0 commit comments

Comments
 (0)