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
This commit is contained in:
Gabriel Horner
2025-10-17 13:18:01 -04:00
parent 0f7532e3c3
commit 9aaf5db25c
11 changed files with 31 additions and 14 deletions

View File

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

View File

@@ -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
deps/cli/README.md vendored
View File

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

View File

@@ -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")))

View File

@@ -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)}

View File

@@ -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 %))]

View File

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

View File

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

View File

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

View File

@@ -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"}})

View File

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