fix(core): tell the model a dropped MCP server reconnects on the next call

This commit is contained in:
Aiden Cline 2026-11-10 00:34:45 +00:00 • committed by Aiden Cline
parent f30c3d5b3c
commit 3a33b3e9eb
2 changed files with 16 additions and 6 deletions

View file

@ -100,7 +100,11 @@ export class ToolCallError extends Schema.TaggedError<ToolCallError>()("MCP.Tool
message: Schema.String,
}) {}
const unavailable = (server: ServerName, status: Status) => {
const unavailable = (server: ServerName, entry: ServerEntry) => {
const status = entry.status
// The model cannot reconnect a server from /mcps; one that dropped on its own reconnects on the next call.
if (entry.disconnected)
return `MCP server "${server}" lost its connection: ${entry.disconnected.reason}. It reconnects on the next call; try again shortly.`
switch (status.status) {
case "failed":
return `MCP server "${server}" is not connected: ${status.error}. Reconnect it from /mcps.`
@ -130,7 +134,7 @@ type ServerEntry = {
retained?: ReadonlyArray<McpClient.Tool>
// Set while the connection is down after dropping on its own. The next request that needs the server reconnects,
// unless a reconnect failed and its backoff has not passed yet. An explicit lifecycle operation clears it.
disconnected?: { failures: number; retryAt: number }
disconnected?: { failures: number; retryAt: number; reason: string }
}
// A client-hosted MCP server attached over a connection the client opened. Internal: never listed,
@ -696,7 +700,7 @@ export const layer = (options?: Options) =>
const drop = Effect.fnUntraced(function* (name: ServerName, entry: ServerEntry, reason: string) {
// A shared connection has already emptied its catalogs, so fall back to the last list read from it.
entry.retained = entry.connection.tools ?? entry.tagged?.source
entry.disconnected = { failures: 0, retryAt: 0 }
entry.disconnected = { failures: 0, retryAt: 0, reason }
yield* entry.connection.stop
entry.status = { status: "failed", error: disconnectedError(reason) }
yield* Effect.logWarning("mcp connection dropped", { server: name, reason })
@ -857,6 +861,7 @@ export const layer = (options?: Options) =>
const disconnected = entry.disconnected
if (disconnected && failure.status === "failed") {
disconnected.failures += 1
disconnected.reason = failure.error
disconnected.retryAt =
(yield* Clock.currentTimeMillis) +
Math.min(
@ -1271,7 +1276,7 @@ export const layer = (options?: Options) =>
return yield* new ToolCallError({
server,
tool: input.name,
message: unavailable(server, target.entry.status),
message: unavailable(server, target.entry),
})
return yield* target.entry.connection.recovering(target.client, call).pipe(failed("server"))
}),

View file

@ -4419,8 +4419,13 @@ describe("MCP reconnect", () => {
expect((yield* server(service))?.status).toBe("failed")
expect(yield* service.tools()).toEqual(tools)
// Within the 1 s backoff a call fails with the same error and nothing is spawned.
expect((yield* echo(service).pipe(Effect.flip)).message).toContain("missing API key")
// Within the 1 s backoff a call fails with the same error and nothing is spawned. The model is told the
// server reconnects on its own, not to reconnect it from /mcps.
const message = (yield* echo(service).pipe(Effect.flip)).message
expect(message).toContain("lost its connection: ")
expect(message).toContain("missing API key")
expect(message).toContain("It reconnects on the next call; try again shortly.")
expect(message).not.toContain("/mcps")
yield* TestClock.adjust("999 millis")
yield* echo(service).pipe(Effect.flip)
expect(fake.state.spawns).toBe(2)