Skip to content

Commit be42168

Browse files
committed
Fix MCP server incorrectly marked as running when HTTP transport returns 4xx (e.g. 401/403) during initialization
1 parent 4f239ad commit be42168

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

CHANGELOG.md

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

55
- Improve OAuth callback error page to show error code, description, and error URI from the authorization server response.
66
- Bump plumcp to 0.2.0-beta4, simplify MCP tool call error handling now that HTTP 400/404/500 are returned as JSON-RPC errors.
7+
- Fix MCP server incorrectly marked as running when HTTP transport returns 4xx (e.g. 401/403) during initialization.
78

89
## 0.115.0
910

src/eca/features/tools/mcp.clj

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,35 @@
8585
{:transport (phct/make-streamable-http-transport
8686
hc
8787
:on-other-response (fn [response]
88-
(let [status (long (:status response))]
88+
(let [status (long (:status response))
89+
read-body (fn []
90+
(try
91+
(let [body* (atom nil)]
92+
(when-let [on-msg (:on-msg response)]
93+
(on-msg (fn [text] (reset! body* text))))
94+
(when-let [body @body*]
95+
(subs body 0 (min (count body) 1000))))
96+
(catch Exception _ nil)))]
8997
(cond
9098
(= 202 status) nil
9199

92-
(= 404 status)
93-
(do (logger/info logger-tag (format "MCP server '%s' returned 404, session expired" server-name))
94-
(reset! needs-reinit?* true))
95-
96-
(<= 500 status)
97-
(do (logger/warn logger-tag (format "MCP server '%s' returned %d, needs re-initialization" server-name status))
98-
(reset! needs-reinit?* true))
100+
(or (<= 400 status 499)
101+
(<= 500 status))
102+
(let [body (read-body)
103+
;; 405 with body mentioning GET means server doesn't support
104+
;; SSE streaming, which is fine — it still works via POST.
105+
sse-unsupported? (and (= 405 status)
106+
body
107+
(string/includes? body "GET"))]
108+
(if sse-unsupported?
109+
(logger/info logger-tag
110+
(format "MCP server '%s' does not support SSE streaming, continuing with POST only"
111+
server-name))
112+
(do (logger/warn logger-tag
113+
(format "MCP server '%s' returned %d, re-initializing.%s"
114+
server-name status
115+
(if body (str " Response: " body) "")))
116+
(reset! needs-reinit?* true))))
99117

100118
:else
101119
(logger/warn logger-tag (format "Unexpected HTTP response from MCP server '%s': %d"
@@ -330,10 +348,16 @@
330348
(swap! db* assoc-in [:mcp-clients name :tools] (list-server-tools client))
331349
(swap! db* assoc-in [:mcp-clients name :prompts] (list-server-prompts client))
332350
(swap! db* assoc-in [:mcp-clients name :resources] (list-server-resources client))
333-
(swap! db* assoc-in [:mcp-clients name :status] :running)
334-
(on-server-updated (->server name server-config :running @db*))
335-
(logger/info logger-tag (format "Started MCP server %s" name))
336-
:ok)
351+
(if (and needs-reinit?* @needs-reinit?*)
352+
(do (try (pp/stop-client-transport! transport false) (catch Exception _))
353+
(logger/error logger-tag (format "MCP server '%s' transport error during initialization" name))
354+
(swap! db* assoc-in [:mcp-clients name :status] :failed)
355+
(on-server-updated (->server name server-config :failed @db*))
356+
:failed)
357+
(do (swap! db* assoc-in [:mcp-clients name :status] :running)
358+
(on-server-updated (->server name server-config :running @db*))
359+
(logger/info logger-tag (format "Started MCP server %s" name))
360+
:ok)))
337361
(catch Exception e
338362
(try (pp/stop-client-transport! transport false) (catch Exception _))
339363
(if (and (transient-transport-error? e) (< attempt max-init-retries))

0 commit comments

Comments
 (0)