|
| 1 | +(ns eca.features.mcp |
| 2 | + (:require |
| 3 | + [cheshire.core :as json] |
| 4 | + [eca.logger :as logger]) |
| 5 | + (:import |
| 6 | + [com.fasterxml.jackson.databind ObjectMapper] |
| 7 | + [io.modelcontextprotocol.client McpClient McpSyncClient] |
| 8 | + [io.modelcontextprotocol.client.transport ServerParameters StdioClientTransport] |
| 9 | + [io.modelcontextprotocol.spec |
| 10 | + McpSchema$CallToolRequest |
| 11 | + McpSchema$ClientCapabilities |
| 12 | + McpSchema$Content |
| 13 | + McpSchema$Root |
| 14 | + McpSchema$TextContent |
| 15 | + McpSchema$Tool |
| 16 | + McpTransport] |
| 17 | + [java.time Duration] |
| 18 | + [java.util List Map])) |
| 19 | + |
| 20 | +(set! *warn-on-reflection* true) |
| 21 | + |
| 22 | +(def ^:private logger-tag "[MCP]") |
| 23 | + |
| 24 | +(defn ^:private ->transport ^McpTransport [{:keys [command args]}] |
| 25 | + (StdioClientTransport. |
| 26 | + (-> (ServerParameters/builder ^String command) |
| 27 | + (.args ^List args) |
| 28 | + (.build)))) |
| 29 | + |
| 30 | +(defn ^:private ->client ^McpSyncClient [transport config] |
| 31 | + (-> (McpClient/sync transport) |
| 32 | + (.requestTimeout (Duration/ofSeconds (:mcp-timeout-seconds config))) |
| 33 | + (.capabilities (-> (McpSchema$ClientCapabilities/builder) |
| 34 | + (.roots true) |
| 35 | + (.build))) |
| 36 | + (.build))) |
| 37 | + |
| 38 | +(defn initialize! [db* config] |
| 39 | + (doseq [[name server-config] (:mcp-servers config)] |
| 40 | + (try |
| 41 | + (when-not (get-in @db* [:mcp-clients name]) |
| 42 | + (let [transport (->transport server-config) |
| 43 | + client (->client transport config)] |
| 44 | + (swap! db* assoc-in [:mcp-clients name :client] client) |
| 45 | + (doseq [{:keys [name uri]} (:workspace-folders @db*)] |
| 46 | + (.addRoot client (McpSchema$Root. uri name))) |
| 47 | + (.initialize client))) |
| 48 | + (catch Exception e |
| 49 | + (logger/warn logger-tag (format "Could not initialize MCP server %s. Error: %s" name (.getMessage e))))))) |
| 50 | + |
| 51 | +(defn tools-cached? [db] |
| 52 | + (boolean (:mcp-tools db))) |
| 53 | + |
| 54 | +(defn cache-tools! [db*] |
| 55 | + (let [obj-mapper (ObjectMapper.)] |
| 56 | + (doseq [[name {:keys [client]}] (:mcp-clients @db*)] |
| 57 | + (doseq [^McpSchema$Tool tool-client (.tools (.listTools ^McpSyncClient client))] |
| 58 | + (let [tool {:name (.name tool-client) |
| 59 | + :mcp-name name |
| 60 | + :mcp-client client |
| 61 | + :description (.description tool-client) |
| 62 | + ;; We convert to json to then read so we have the clojrue map |
| 63 | + ;; TODO avoid this converting to clojure map directly |
| 64 | + :parameters (json/parse-string (.writeValueAsString obj-mapper (.inputSchema tool-client)) true)}] |
| 65 | + (swap! db* assoc-in [:mcp-tools (:name tool)] tool)))))) |
| 66 | + |
| 67 | +(defn list-tools [db] |
| 68 | + (vals (:mcp-tools db))) |
| 69 | + |
| 70 | +(defn call-tool! [^String name ^Map arguments db] |
| 71 | + (let [result (.callTool ^McpSyncClient (get-in db [:mcp-tools name :mcp-client]) |
| 72 | + (McpSchema$CallToolRequest. name arguments))] |
| 73 | + (if (.isError result) |
| 74 | + {:error (.content result)} |
| 75 | + {:contents (map (fn [content] |
| 76 | + (case (.type ^McpSchema$Content content) |
| 77 | + "text" {:type :text |
| 78 | + :content (.text ^McpSchema$TextContent content)} |
| 79 | + nil)) |
| 80 | + (.content result))}))) |
| 81 | + |
| 82 | +(defn shutdown! [db*] |
| 83 | + (doseq [[_name {:keys [_client]}] (:mcp-clients @db*)] |
| 84 | + ;; TODO NoClassDefFound being thrown for some reason |
| 85 | + #_(.closeGracefully ^McpSyncClient client)) |
| 86 | + (swap! db* assoc |
| 87 | + :mcp-clients {} |
| 88 | + :mcp-tools {})) |
0 commit comments