OpenCode: stopping one session disconnects the happier MCP server for other sessions in the same directory
Summary
Stopping one OpenCode session disconnects the shared happier MCP server for every other OpenCode session using the same directory on the same managed OpenCode server. The surviving session silently loses all Happier tools (happier_action_execute, happier_change_title, …), so delegation, run control and title updates stop working mid-session.
What happened (current behavior)
- Session A (OpenCode backend) was running in directory
D. - Session B (OpenCode backend) was started in the same directory
D. It registered thehappierMCP server successfully. - About 4 seconds later, session A was stopped from the app (daemon sent SIGTERM).
- Session A's runtime teardown called
POST /mcp/happier/disconnect?directory=Don the shared managed OpenCode server. GET /mcp?directory=Dthen returned{"happier":{"status":"disabled"}}. Session B's model reported thathappier_action_executewas not available; its tool list contained only built-in tools.
Manually calling POST /mcp/happier/connect?directory=D restored {"happier":{"status":"connected"}} and session B could use the Happier tools again.
Expected behavior
Stopping a session should not remove MCP tools from other live sessions. Registration and teardown should be scoped per session, or reference-counted per (server, directory, name), or teardown should skip mcpDisconnect when another live session still depends on that registration.
Reproduction steps
- Start an OpenCode session in a git repo directory.
- Start a second OpenCode session in the same directory (the same managed OpenCode server is reused).
- Stop the first session.
- In the second session, ask the agent to call
happier_action_execute(for exampleaction_spec_search), or checkGET /mcp?directory=<dir>on the managed server: status isdisabled.
Severity
medium
Frequency
always
Happier version
CLI 0.2.12; code path unchanged on dev @ 4a2dac94d
Platform
Oracle Linux 9 (aarch64), OpenCode 1.18.25 (managed opencode serve)
Deployment type
unsure
Additional context
Code path (dev @ 4a2dac94d), apps/cli/src/backends/opencode/server/runtime.ts:
- Registration:
c.mcpAdd({ name: serverName, config: { type: 'local', … } }), thenensuredMcpServerNames.add(serverName)(~L3846–3861). The name is the same (happier) for every session. - Teardown:
names.map((name) => c.mcpDisconnect({ name }))(~L4476–4483).mcpDisconnectposts/mcp/${name}/disconnectwith only{ directory }as scope (server/client.ts~L670).
OpenCode keys MCP clients by name within a directory instance, so the second session's registration is removed by the first session's teardown.
Possible fixes: per-session MCP server names (this changes tool names such as happier_action_execute, so allow-lists would need updating), a live-session reference count per (managed server, directory, name) before disconnecting, or re-running registration when a session detects its required MCP server is not connected before prompt admission.
Note: this install has an unrelated local modification to the OpenCode permission mapping; the MCP registration and teardown code is unmodified and matches dev.
Thanks, Tom — the precise two-session timeline made the teardown collision straightforward to verify.
We confirmed that OpenCode 1.18.25 owns MCP clients by working directory and server name, not by OpenCode or Happier session. Both sessions therefore mutate the same happier entry. When session A reset, Happier called OpenCode’s directory-global disconnect endpoint for that name, which disabled the entry most recently registered by session B.
The stop-induced failure is fixed on dev in https://github.com/happier-dev/happier/commit/3e3fc87e286531a8d74da5a0e904a99749775bdd. Session teardown no longer disconnects directory-owned MCP registrations, the now-unused disconnect client path was removed, and a regression test starts two runtimes in one directory and verifies that resetting the first leaves the shared registration connected. The test was proven RED before the fix and GREEN after it; the CLI typecheck also passes.
The evolved 0.3 OpenCode plugin already has registration-only lifecycle behavior and no teardown disconnect path, so no 0.3 source change was needed for this intent.
One limitation remains, so I am deliberately leaving this issue open: OpenCode 1.18.25 also replaces an existing same-name MCP client when the second session registers, and its MCP tool invocation does not carry the calling OpenCode session ID. Refcounting would only mask disconnects, while per-session MCP names would expose ambiguous cross-session tool aliases. We did not ship either workaround. This commit fixes the reported loss of B’s tools when A stops; fully independent simultaneous session-bound Happier tool routing needs an upstream-capable session identity or process isolation.
Could you retry the exact A/B stop sequence with a build containing the commit above and confirm whether B retains its Happier tools?
Posted on behalf of @leeroybrun.
Thanks for the two-session Stop reproduction. CLI 0.2.16 fixes stopping one session disconnecting another’s shared Happier MCP tools. OpenCode still does not identify which session calls a shared MCP client.
Please reply or reopen with versions if the disconnection persists or OpenCode adds that session information.
Posted on behalf of @leeroybrun.