reduce duplication with mcp-res
This commit is contained in:
parent
42f853a91f
commit
95152a12ee
1 changed files with 16 additions and 26 deletions
|
|
@ -32,6 +32,7 @@
|
||||||
:description "Array of tags for categorizing the snippet"}}
|
:description "Array of tags for categorizing the snippet"}}
|
||||||
:required ["title" "slug" "markdown" "tags"]}}])
|
:required ["title" "slug" "markdown" "tags"]}}])
|
||||||
|
|
||||||
|
;; interact with backend api
|
||||||
(defn fetch-snippets []
|
(defn fetch-snippets []
|
||||||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=2000"))]
|
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=2000"))]
|
||||||
(json/parse-string (:body res) true)))
|
(json/parse-string (:body res) true)))
|
||||||
|
|
@ -48,39 +49,28 @@
|
||||||
:body (pr-str {:title title :slug slug :markdown markdown :tags tags})}))
|
:body (pr-str {:title title :slug slug :markdown markdown :tags tags})}))
|
||||||
|
|
||||||
;; Tool implementations
|
;; Tool implementations
|
||||||
(defn list-snippets-impl []
|
(defn mcp-res [fn args]
|
||||||
(try
|
(try
|
||||||
(let [snippets (fetch-snippets)]
|
(let [fn-res (apply fn args)]
|
||||||
{:content [{:type "text"
|
{:content [{:type "text"
|
||||||
:text (json/encode {:success true
|
:text (json/encode (assoc fn-res :success true))}]})
|
||||||
:snippets (map #(select-keys % [:id :title :slug :tags]) snippets)})}]})
|
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
{:content [{:type "text"
|
{:content [{:type "text"
|
||||||
:text (json/encode {:success false
|
:text (json/encode {:success false
|
||||||
:error (.getMessage e)})}]})))
|
: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]
|
(defn get-snippet-impl [id]
|
||||||
(try
|
(let [snippet (fetch-snippet id)]
|
||||||
(let [snippet (fetch-snippet id)]
|
{:snippet snippet}))
|
||||||
{:content [{:type "text"
|
|
||||||
:text (json/encode {:success true
|
|
||||||
:snippet snippet})}]})
|
|
||||||
(catch Exception e
|
|
||||||
{:content [{:type "text"
|
|
||||||
:text (json/encode {:success false
|
|
||||||
:error (.getMessage e)})}]})))
|
|
||||||
|
|
||||||
(defn create-snippet-impl [{:keys [title slug markdown tags]}]
|
(defn create-snippet-impl [{:keys [title slug markdown tags]}]
|
||||||
(try
|
(let [response (create-snippet {:title title :slug slug :markdown markdown :tags tags})]
|
||||||
(let [response (create-snippet {:title title :slug slug :markdown markdown :tags tags})]
|
{:message "Snippet created successfully"
|
||||||
{:content [{:type "text"
|
:status (:status response)}))
|
||||||
: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)})}]})))
|
|
||||||
|
|
||||||
;; Handle tools/list request
|
;; Handle tools/list request
|
||||||
(defn handle-tools-list [id]
|
(defn handle-tools-list [id]
|
||||||
|
|
@ -93,9 +83,9 @@
|
||||||
{:jsonrpc "2.0"
|
{:jsonrpc "2.0"
|
||||||
:id id
|
:id id
|
||||||
:result (case tool-name
|
:result (case tool-name
|
||||||
"list_snippets" (list-snippets-impl)
|
"list_snippets" (mcp-res list-snippets-impl [])
|
||||||
"get_snippet" (get-snippet-impl (:id tool-input))
|
"get_snippet" (mcp-res get-snippet-impl [(:id tool-input)])
|
||||||
"create_snippet" (create-snippet-impl tool-input)
|
"create_snippet" (mcp-res create-snippet-impl [tool-input])
|
||||||
{:error (str "Unknown tool: " tool-name)})})
|
{:error (str "Unknown tool: " tool-name)})})
|
||||||
|
|
||||||
;; Handle initialize request
|
;; Handle initialize request
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue