From 95152a12ee709548c5e7a030dc78332effa5cb05 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 8 Mar 2026 18:06:06 +0100 Subject: [PATCH] reduce duplication with mcp-res --- src/cli/mcp.clj | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/cli/mcp.clj b/src/cli/mcp.clj index 5b420fa..8b3a13e 100644 --- a/src/cli/mcp.clj +++ b/src/cli/mcp.clj @@ -32,6 +32,7 @@ :description "Array of tags for categorizing the snippet"}} :required ["title" "slug" "markdown" "tags"]}}]) +;; interact with backend api (defn fetch-snippets [] (let [res (http/get (str (:backend-host config) "/api/snippets?limit=2000"))] (json/parse-string (:body res) true))) @@ -48,39 +49,28 @@ :body (pr-str {:title title :slug slug :markdown markdown :tags tags})})) ;; Tool implementations -(defn list-snippets-impl [] +(defn mcp-res [fn args] (try - (let [snippets (fetch-snippets)] + (let [fn-res (apply fn args)] {:content [{:type "text" - :text (json/encode {:success true - :snippets (map #(select-keys % [:id :title :slug :tags]) snippets)})}]}) + :text (json/encode (assoc fn-res :success true))}]}) (catch Exception e {:content [{:type "text" :text (json/encode {:success false :error (.getMessage e)})}]}))) +(defn list-snippets-impl [] + (let [snippets (fetch-snippets)] + {:snippets (map #(select-keys % [:id :title :slug :tags]) snippets)})) + (defn get-snippet-impl [id] - (try - (let [snippet (fetch-snippet id)] - {:content [{:type "text" - :text (json/encode {:success true - :snippet snippet})}]}) - (catch Exception e - {:content [{:type "text" - :text (json/encode {:success false - :error (.getMessage e)})}]}))) + (let [snippet (fetch-snippet id)] + {:snippet snippet})) (defn create-snippet-impl [{:keys [title slug markdown tags]}] - (try - (let [response (create-snippet {:title title :slug slug :markdown markdown :tags tags})] - {:content [{:type "text" - :text (json/encode {:success true - :message "Snippet created successfully" - :status (:status response)})}]}) - (catch Exception e - {:content [{:type "text" - :text (json/encode {:success false - :error (.getMessage e)})}]}))) + (let [response (create-snippet {:title title :slug slug :markdown markdown :tags tags})] + {:message "Snippet created successfully" + :status (:status response)})) ;; Handle tools/list request (defn handle-tools-list [id] @@ -93,9 +83,9 @@ {:jsonrpc "2.0" :id id :result (case tool-name - "list_snippets" (list-snippets-impl) - "get_snippet" (get-snippet-impl (:id tool-input)) - "create_snippet" (create-snippet-impl tool-input) + "list_snippets" (mcp-res list-snippets-impl []) + "get_snippet" (mcp-res get-snippet-impl [(:id tool-input)]) + "create_snippet" (mcp-res create-snippet-impl [tool-input]) {:error (str "Unknown tool: " tool-name)})}) ;; Handle initialize request