Skip to content

Commit c0bc020

Browse files
authored
fix(opencode): disconnect dynamically added mcp servers (anomalyco#29452)
1 parent 0e669bb commit c0bc020

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

packages/opencode/src/mcp/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ interface AuthResult {
234234
// --- Effect Service ---
235235

236236
interface State {
237+
config: Record<string, ConfigMCP.Info>
237238
status: Record<string, Status>
238239
clients: Record<string, MCPClient>
239240
defs: Record<string, MCPToolDef[]>
@@ -525,6 +526,7 @@ export const layer = Layer.effect(
525526
const bridge = yield* EffectBridge.make()
526527
const config = cfg.mcp ?? {}
527528
const s: State = {
529+
config: {},
528530
status: {},
529531
clients: {},
530532
defs: {},
@@ -619,6 +621,10 @@ export const layer = Layer.effect(
619621
result[key] = s.status[key] ?? { status: "disabled" }
620622
}
621623

624+
for (const key of Object.keys(s.config)) {
625+
result[key] = s.status[key] ?? { status: "disabled" }
626+
}
627+
622628
return result
623629
})
624630

@@ -642,8 +648,9 @@ export const layer = Layer.effect(
642648
})
643649

644650
const add = Effect.fn("MCP.add")(function* (name: string, mcp: ConfigMCP.Info) {
645-
yield* createAndStore(name, mcp)
646651
const s = yield* InstanceState.get(state)
652+
s.config[name] = mcp
653+
yield* createAndStore(name, mcp)
647654
return { status: s.status }
648655
})
649656

@@ -677,7 +684,7 @@ export const layer = Layer.effect(
677684
([clientName, client]) =>
678685
Effect.gen(function* () {
679686
const mcpConfig = config[clientName]
680-
const entry = mcpConfig && isMcpConfigured(mcpConfig) ? mcpConfig : undefined
687+
const entry = mcpConfig && isMcpConfigured(mcpConfig) ? mcpConfig : s.config[clientName]
681688

682689
const listed = s.defs[clientName]
683690
if (!listed) {
@@ -756,6 +763,9 @@ export const layer = Layer.effect(
756763
})
757764

758765
const getMcpConfig = Effect.fnUntraced(function* (mcpName: string) {
766+
const s = yield* InstanceState.get(state)
767+
if (s.config[mcpName]) return s.config[mcpName]
768+
759769
const cfg = yield* cfgSvc.get()
760770
const mcpConfig = cfg.mcp?.[mcpName]
761771
if (!mcpConfig || !isMcpConfigured(mcpConfig)) return undefined

packages/opencode/test/server/httpapi-mcp.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ describe("mcp HttpApi", () => {
109109
expect(added.status).toBe(200)
110110
expect(yield* json(added)).toMatchObject({ added: { status: "disabled" } })
111111

112+
const addedDisconnected = yield* request(handler, "/mcp/added/disconnect", tmp.directory, { method: "POST" })
113+
expect(addedDisconnected.status).toBe(200)
114+
expect(yield* json(addedDisconnected)).toBe(true)
115+
112116
const connected = yield* request(handler, "/mcp/demo/connect", tmp.directory, { method: "POST" })
113117
expect(connected.status).toBe(200)
114118
expect(yield* json(connected)).toBe(true)

0 commit comments

Comments
 (0)