Browse Source

enhance(cli): graph args+options can be local files or dirs

This affects all cli commands that take graph args.
Also tweaked mcp-server host+port defaults to match the app
Gabriel Horner 4 ngày trước cách đây
mục cha
commit
9aaf5db25c

+ 1 - 0
.clj-kondo/config.edn

@@ -168,6 +168,7 @@
              logseq.cli.common.export.common cli-export-common
              logseq.cli.common.export.text cli-export-text
              logseq.cli.common.file common-file
+             logseq.cli.common.mcp.server cli-common-mcp-server
              logseq.cli.common.mcp.tools cli-common-mcp-tools
              logseq.cli.text-util cli-text-util
              logseq.common.config common-config

+ 1 - 0
deps/cli/.clj-kondo/config.edn

@@ -12,6 +12,7 @@
              clojure.string string
              datascript.core d
              logseq.cli.commands.graph cli-graph
+             logseq.cli.common.mcp.server cli-common-mcp-server
              logseq.cli.common.mcp.tools cli-common-mcp-tools
              logseq.cli.common.graph cli-common-graph
              logseq.cli.common.export.text cli-export-text

+ 7 - 0
deps/cli/README.md

@@ -125,6 +125,13 @@ Exported 16 properties, 16 classes and 36 pages
 # Append text to current page
 $ logseq append add this text -a my-token
 Success!
+
+# Start mcp-server against a local desktop graph
+$ logseq mcp-server -g yep
+MCP Streamable HTTP Server started on 127.0.0.1:12315
+# Start mcp-server against a local graph file
+$ logseq mcp-server -g ~/Downloads/logseq_db_yep_1751032977.sqlite
+MCP Streamable HTTP Server started on 127.0.0.1:12315
 ```
 
 ## API

+ 1 - 1
deps/cli/src/logseq/cli/commands/export.cljs

@@ -74,7 +74,7 @@
         (println "Exported" (count exported-files) "pages to" file-name)))))
 
 (defn export [{{:keys [graph] :as opts} :opts}]
-  (if (fs/existsSync (cli-util/get-graph-dir graph))
+  (if (fs/existsSync (cli-util/get-graph-path graph))
     (let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))]
       (export-repo-as-markdown! (str common-config/db-version-prefix graph) @conn opts))
     (cli-util/error "Graph" (pr-str graph) "does not exist")))

+ 1 - 1
deps/cli/src/logseq/cli/commands/export_edn.cljs

@@ -8,7 +8,7 @@
             [logseq.cli.util :as cli-util]))
 
 (defn export [{{:keys [graph] :as options} :opts}]
-  (if (fs/existsSync (cli-util/get-graph-dir graph))
+  (if (fs/existsSync (cli-util/get-graph-path graph))
     (let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
           export-map (sqlite-export/build-export @conn
                                                  (cond-> {:export-type (:export-type options)}

+ 1 - 1
deps/cli/src/logseq/cli/commands/graph.cljs

@@ -19,7 +19,7 @@
 (defn show-graph
   [{{:keys [graphs]} :opts}]
   (doseq [graph graphs]
-    (let [graph-dir (cli-util/get-graph-dir graph)]
+    (let [graph-dir (cli-util/get-graph-path graph)]
       (if (fs/existsSync graph-dir)
         (let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
               kv-value #(:kv/value (d/entity @conn %))]

+ 1 - 1
deps/cli/src/logseq/cli/commands/mcp_server.cljs

@@ -86,7 +86,7 @@
     (cli-common-mcp-server/create-mcp-api-server (partial call-api api-server-token))))
 
 (defn start [{{:keys [debug-tool graph stdio api-server-token] :as opts} :opts :as m}]
-  (when (and graph (not (fs/existsSync (cli-util/get-graph-dir graph))))
+  (when (and graph (not (fs/existsSync (cli-util/get-graph-path graph))))
     (cli-util/error "Graph" (pr-str graph) "does not exist"))
   (if debug-tool
     (if graph

+ 1 - 1
deps/cli/src/logseq/cli/commands/query.cljs

@@ -74,7 +74,7 @@
   [{{:keys [graph args graphs properties-readable title-query]} :opts}]
   (let [graphs' (into [graph] graphs)]
     (doseq [graph' graphs']
-      (if (fs/existsSync (cli-util/get-graph-dir graph'))
+      (if (fs/existsSync (cli-util/get-graph-path graph'))
         (let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
               query* (when (string? (first args)) (common-util/safe-read-string {:log-error? false} (first args)))
               results (cond

+ 1 - 1
deps/cli/src/logseq/cli/commands/search.cljs

@@ -51,7 +51,7 @@
       (p/catch cli-util/command-catch-handler)))
 
 (defn- local-search [search-term {{:keys [graph raw limit]} :opts}]
-  (if (fs/existsSync (cli-util/get-graph-dir graph))
+  (if (fs/existsSync (cli-util/get-graph-path graph))
     (let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
           nodes (->> (d/datoms @conn :aevt :block/title)
                      (filter (fn [datom]

+ 3 - 3
deps/cli/src/logseq/cli/spec.cljs

@@ -58,11 +58,11 @@
    :stdio {:alias :s
            :desc "Run the MCP server via stdio transport"}
    :port {:alias :p
-          :default 3000
+          :default 12315
           :coerce :long
           :desc "Port for streamable HTTP server"}
-   :host {:default "localhost"
-          :desc "Host for streamable HHP server"}
+   :host {:default "127.0.0.1"
+          :desc "Host for streamable HTTP server"}
    :debug-tool {:alias :t
                 :coerce :keyword
                 :desc "Debug mcp tool with direct invocation"}})

+ 13 - 5
deps/cli/src/logseq/cli/util.cljs

@@ -1,20 +1,28 @@
 (ns ^:node-only logseq.cli.util
   "CLI only util fns"
   (:require ["path" :as node-path]
+            ["fs" :as fs]
             [clojure.string :as string]
             [logseq.cli.common.graph :as cli-common-graph]
             [logseq.db.common.sqlite :as common-sqlite]
             [nbb.error]
             [promesa.core :as p]))
 
-(defn get-graph-dir
-  [graph]
-  (node-path/join (cli-common-graph/get-db-graphs-dir) (common-sqlite/sanitize-db-name graph)))
-
 (defn ->open-db-args
   "Creates args for sqlite-cli/open-db! given a graph. Similar to sqlite-cli/->open-db-args"
   [graph]
-  [(cli-common-graph/get-db-graphs-dir) (common-sqlite/sanitize-db-name graph)])
+  (cond
+    (and (fs/existsSync graph) (.isFile (fs/statSync graph)))
+    [graph]
+    (string/includes? graph "/")
+    ((juxt node-path/dirname node-path/basename) graph)
+    :else
+    [(cli-common-graph/get-db-graphs-dir) (common-sqlite/sanitize-db-name graph)]))
+
+(defn get-graph-path
+  "If graph is a file, return its path. Otherwise returns the graph's dir"
+  [graph]
+  (apply node-path/join (->open-db-args graph)))
 
 (defn api-fetch [token method args]
   (js/fetch "http://127.0.0.1:12315/api"