Skip to content

Commit 180dcb7

Browse files
committed
Add MCP support for Anthropic
1 parent e8c10ce commit 180dcb7

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Fix ollama servers discovery
66
- Fix `.eca/config.json` read from workspace root
7-
- Add support for MCP servers (openai only for now)
7+
- Add support for MCP servers
88

99
## 0.0.2
1010

docs/models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| model | MCP | thinking/reasioning |
66
|-----------|-----|---------------------|
77
| OpenAI || X |
8-
| Anthropic | X | X |
8+
| Anthropic | | X |
99
| Ollama | X | X |
1010

1111
## Models

src/eca/llm_api.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
:tools tools
5656
:api-key (:anthropic-api-key config)}
5757
{:on-message-received on-message-received-wrapper
58-
:on-error on-error})
58+
:on-error on-error
59+
:on-tool-called on-tool-called})
5960

6061
(string/starts-with? model config/ollama-model-prefix)
6162
(llm-providers.ollama/completion!

src/eca/llm_providers/anthropic.clj

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,51 @@
4848
api-key past-messages tools]
4949
:or {max-tokens 1024
5050
temperature 1.0}}
51-
{:keys [on-message-received on-error]}]
52-
(let [body {:model model
53-
:messages (conj past-messages {:role "user" :content user-prompt})
51+
{:keys [on-message-received on-error on-tool-called]}]
52+
(let [messages (conj past-messages {:role "user" :content user-prompt})
53+
body {:model model
54+
:messages messages
5455
:max_tokens max-tokens
5556
:temperature temperature
5657
;; TODO support :thinking
5758
:stream true
5859
:tools (->tools tools)
5960
:system context}
61+
content-block* (atom nil)
6062
on-response-fn (fn handle-response [event data]
6163
(case event
6264
"content_block_delta" (case (-> data :delta :type)
6365
"text_delta" (on-message-received {:message (-> data :delta :text)})
66+
"input_json_delta" (swap! content-block* update-in [(:index data) :input-json] str (-> data :delta :partial_json))
6467
(logger/warn "Unkown response delta type" (-> data :delta :type)))
65-
"message_stop" (on-message-received {:finish-reason (:type data)})
68+
"content_block_start" (case (-> data :content_block :type)
69+
"tool_use" (swap! content-block* assoc (:index data) (:content_block data))
70+
71+
nil)
72+
"message_delta" (case (-> data :delta :stop_reason)
73+
"tool_use" (doseq [content-block (vals @content-block*)]
74+
(when (= "tool_use" (:type content-block))
75+
(let [function-name (:name content-block)
76+
function-args (:input-json content-block)
77+
response (on-tool-called {:name function-name
78+
:arguments (json/parse-string function-args)})
79+
messages (concat messages
80+
[{:role "assistant"
81+
:content [(dissoc content-block :input-json)]}]
82+
(mapv
83+
(fn [{:keys [_type content]}]
84+
{:role "user"
85+
:content [{:type "tool_result"
86+
:tool_use_id (:id content-block)
87+
:content content}]})
88+
(:contents response)))]
89+
(base-request!
90+
{:body (assoc body :messages messages)
91+
:api-key api-key
92+
:on-error on-error
93+
:on-response handle-response}))))
94+
"end_turn" (on-message-received {:finish-reason (-> data :delta :stop_reason)})
95+
nil)
6696
nil))]
6797
(base-request!
6898
{:body body

0 commit comments

Comments
 (0)