Skip to content

Commit c898b86

Browse files
fix(mcp): disconnect drops a pending OAuth transport, like remove
A transport a connect left behind has no recorded flow, so finishing it after a disconnect was a new call that reconnected the disabled server. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0172qrhMa5TQgETASi5hxMqD
1 parent 488306e commit c898b86

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ export const layer = Layer.effect(
11111111
yield* closeClient(s, name)
11121112
// altimate_change start — the close suspends; a newer call may have committed since
11131113
if (!isCurrent(s, name, token)) return
1114+
// A pending OAuth transport goes too, and a browser flow waiting on its callback is
1115+
// cancelled: a transport a connect left behind has no recorded flow, so finishing it later
1116+
// would be a new call that reconnects the server this disconnect just disabled.
1117+
pendingOAuthTransports.delete(name)
1118+
pendingOAuthFlows.delete(name)
1119+
McpOAuthCallback.cancelPending(name)
11141120
// altimate_change end
11151121
delete s.clients[name]
11161122
s.status[name] = { status: "disabled" }

‎packages/opencode/test/mcp/oauth-auto-connect.test.ts‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,27 +407,52 @@ mcpTest.instance(
407407
// altimate_change start — finishing an OAuth flow continues the call that began it: a disconnect
408408
// or remove issued after startAuth is the later call, so finishAuth must not connect. (review)
409409
mcpTest.instance(
410-
"a disconnect after startAuth keeps finishAuth from exchanging the code or connecting",
410+
"a connect issued after startAuth wins: the late finishAuth exchanges nothing",
411411
() =>
412412
MCP.Service.use((mcp) =>
413413
Effect.gen(function* () {
414414
yield* mcp.add("test-oauth-late-finish", { type: "remote", url: "https://example.com/mcp" })
415415
const started = yield* mcp.startAuth("test-oauth-late-finish")
416416
expect(started.authorizationUrl).toBeTruthy()
417417

418-
yield* mcp.disconnect("test-oauth-late-finish")
418+
// A newer call that leaves the pending flow alone: the server connects without auth.
419419
simulateAuthFlow = false
420420
connectSucceedsImmediately = true
421-
const result = yield* mcp.finishAuth("test-oauth-late-finish", "code")
421+
yield* mcp.connect("test-oauth-late-finish")
422+
expect((yield* mcp.status())["test-oauth-late-finish"]?.status).toBe("connected")
422423

423-
expect(result.status).not.toBe("connected")
424+
const result = yield* mcp.finishAuth("test-oauth-late-finish", "code")
425+
expect(result.status).toBe("connected")
424426
expect(tokenExchanges).toBe(0)
425-
expect((yield* mcp.clients())["test-oauth-late-finish"]).toBeUndefined()
426427
}),
427428
),
428429
{ config: config("test-oauth-late-finish") },
429430
)
430431

432+
mcpTest.instance(
433+
"a disconnect drops a pending transport a connect left behind, so finishAuth has nothing to finish",
434+
() =>
435+
MCP.Service.use((mcp) =>
436+
Effect.gen(function* () {
437+
// The connect finds the server needs auth and keeps its transport, with no flow recorded.
438+
const added = yield* mcp.add("test-oauth-connect-left", { type: "remote", url: "https://example.com/mcp" })
439+
expect((added.status as Record<string, { status: string }>)["test-oauth-connect-left"]?.status).toBe("needs_auth")
440+
yield* mcp.disconnect("test-oauth-connect-left")
441+
442+
simulateAuthFlow = false
443+
connectSucceedsImmediately = true
444+
const error = yield* mcp.finishAuth("test-oauth-connect-left", "code").pipe(
445+
Effect.flip,
446+
Effect.catchDefect((defect) => Effect.succeed(defect)),
447+
)
448+
expect(String(error)).toContain("No pending OAuth flow")
449+
expect(tokenExchanges).toBe(0)
450+
expect((yield* mcp.clients())["test-oauth-connect-left"]).toBeUndefined()
451+
}),
452+
),
453+
{ config: config("test-oauth-connect-left") },
454+
)
455+
431456
mcpTest.instance(
432457
"a remove after startAuth drops the pending flow, so finishAuth has nothing to finish",
433458
() =>

0 commit comments

Comments
 (0)