fix(cli): mcp-server api mode can fail confusingly

Describe the required setup and fail fast with CLI instead
of lazily in the llm client
This commit is contained in:
Gabriel Horner
2025-11-20 11:48:26 -05:00
parent c488335f9d
commit 23937b59fc
2 changed files with 9 additions and 6 deletions

View File

@@ -103,7 +103,7 @@
:args->opts [:args] :require [:args] :coerce {:args []}
:spec cli-spec/append}
{:cmds ["mcp-server"] :desc "Run a MCP server"
:description "Run a MCP server against a local graph if --graph is given or against the current in-app graph. By default the MCP server runs as a HTTP Streamable server. Use --stdio to run it as a stdio server."
:description "Run a MCP server against a local graph if --graph is given or against the current in-app graph. For the in-app graph, the API server must be on in the app. By default the MCP server runs as a HTTP Streamable server. Use --stdio to run it as a stdio server."
:fn (lazy-load-fn 'logseq.cli.commands.mcp-server/start)
:spec cli-spec/mcp-server}
{:cmds ["validate"] :desc "Validate DB graph"

View File

@@ -76,14 +76,17 @@
#js {:error (str "Server status " (.-status resp)
"\nAPI Response: " (pr-str body))}))))
(defn- create-mcp-server [{{:keys [api-server-token]} :opts} graph]
(if graph
(defn- create-mcp-server [{{:keys [api-server-token] :as opts} :opts} graph]
(if (cli-util/api-command? opts)
;; Make an intial /api call to ensure the API server is on
(-> (p/let [_resp (call-api api-server-token "logseq.app.search" ["foo"])]
(cli-common-mcp-server/create-mcp-api-server (partial call-api api-server-token)))
(p/catch cli-util/command-catch-handler))
(let [mcp-server (cli-common-mcp-server/create-mcp-server)
conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))]
(doseq [[k v] local-tools]
(.registerTool mcp-server (name k) (:config v) (partial (:fn v) conn)))
mcp-server)
(cli-common-mcp-server/create-mcp-api-server (partial call-api api-server-token))))
mcp-server)))
(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-path graph))))
@@ -101,7 +104,7 @@
(clj->js (dissoc opts :debug-tool)))]
(js/console.log resp))
(cli-util/error "Tool" (pr-str debug-tool) "not found")))
(let [mcp-server (create-mcp-server m graph)]
(p/let [mcp-server (create-mcp-server m graph)]
(if stdio
(nbb/await (.connect mcp-server (StdioServerTransport.)))
(start-http-server mcp-server (select-keys opts [:port :host]))))))