Skip to content

Commit 5c8a76a

Browse files
author
Hernán Arce
committed
fix(mcp): detect transport close and update status to failed
Detect when MCP client transport closes unexpectedly (server crash, network drop, process exit) and immediately mark the connection status as 'failed' instead of leaving it as 'connected'. This fixes the issue where the TUI/API would continue to show MCP servers as online (green) even though they had stopped responding. Fixes anomalyco#23997
1 parent f033d2d commit 5c8a76a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,15 @@ export const layer = Layer.effect(
472472
Effect.catch(() => Effect.succeed([] as number[])),
473473
)
474474

475+
function dropConnection(s: State, name: string, client: MCPClient, reason: string) {
476+
if (s.clients[name] !== client || s.status[name]?.status !== "connected") return
477+
log.warn("mcp server disconnected, marking as failed", { server: name, reason })
478+
s.status[name] = { status: "failed", error: reason }
479+
delete s.clients[name]
480+
delete s.defs[name]
481+
void client.close()
482+
}
483+
475484
function watch(s: State, name: string, client: MCPClient, bridge: EffectBridge.Shape, timeout?: number) {
476485
client.setNotificationHandler(ToolListChangedNotificationSchema, async () => {
477486
log.info("tools list changed notification received", { server: name })
@@ -484,6 +493,10 @@ export const layer = Layer.effect(
484493
s.defs[name] = listed
485494
await bridge.promise(bus.publish(ToolsChanged, { server: name }).pipe(Effect.ignore))
486495
})
496+
497+
client.onclose = () => {
498+
bridge.fork(Effect.sync(() => dropConnection(s, name, client, "MCP transport closed unexpectedly")))
499+
}
487500
}
488501

489502
const state = yield* InstanceState.make<State>(

0 commit comments

Comments
 (0)