reduce duplication with mcp-res

This commit is contained in:
Travis Shears 2026-03-08 18:06:06 +01:00
parent 42f853a91f
commit 95152a12ee
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469

View file

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