Repository navigation
Description
Description
On a 2026-07-28 (modern) MCP connection, tools/list_changed, prompts/list_changed and resources/list_changed arrive on the subscriptions/listen stream that the SDK opens during connect. If that stream ends (server redeploy, proxy idle timeout, serverless response limit, network blip), opencode never opens it again. The server still shows connected, but its tool list is frozen until the user reconnects it by hand.
The SDK leaves this to the caller. Its listen() docs say "No automatic re-listen — call listen() again to re-establish."
opencode already does this for the MCP Events listen stream in packages/core/src/mcp/client.ts:945-972 (reopen with the reopen backoff, then relist). The list-changed stream has no equivalent:
client.ts:348-358andclient.ts:415-420register the list-changed handlers and let the SDK auto-open the stream.- Nothing watches
client.autoOpenedSubscription?.closed, soconnection.ts:157(onToolsChanged) never fires again after the stream ends. - If the auto-open fails during connect, the SDK only reports it to
onerror, and there is no retry either.
Fix idea: on a modern connection, watch autoOpenedSubscription.closed (or open the subscription ourselves) and reopen it with the same backoff as the events stream. After each reopen, run the tools/prompts/resources changed callbacks once, to pick up changes missed while the stream was down.
I can send a PR with a test if this approach is OK.
Plugins
No response
OpenCode version
dev adc958a7d7 (2.4.1)
Steps to reproduce
- Run a modern MCP server (
createMcpHandlerfrom@modelcontextprotocol/server,legacy: "reject") withtools: { listChanged: true }, and add it as a remote server. - After connect, end the
subscriptions/listenresponse stream right after thenotifications/subscriptions/acknowledgedevent. - Wait a few seconds, then add a tool on the server and call
notify.toolsChanged(). Mcp.tools()still returns only the old tools. The server saw onesubscriptions/listenrequest in total. With the stream left open, the new tool shows up as expected.
I have this as a bun test in packages/core/test (control case passes, cut-stream case fails).
Screenshot
No response
Operating System
Windows 11
Terminal
No response
Activity
opencodejr commentedon Nov 24, 2026
Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.
Thanks for the detailed report. I reproduced this with the released 2.4.1 and on the latest
dev(adc958a7d7).What I did: I ran
opencode servewith a remote MCP server that uses the modern protocol (createMcpHandler,legacy: "reject",tools: { listChanged: true }). That server ended thesubscriptions/listenresponse right afternotifications/subscriptions/acknowledged. About 10 seconds after connect, I added a tool and callednotify.toolsChanged().tools/listagain about a second after the change notification.subscriptions/listenand notools/listafter the change./api/mcpstill reported the server asconnected, so the tool list stayed stale.The only stand-in is the test MCP server that ends the stream, which is the situation your report starts from. No model was involved. The script below runs both cases (
sh repro.sh cutorsh repro.sh control).In the code, the MCP Events listen stream in
packages/core/src/mcp/client.tsis reopened with backoff and then relisted. I found nothing that does the same for the list-changed subscription, which fits what you described. A maintainer will look at the fix you proposed.Reproduction script (repro.sh)
Run it:
sh repro.shGuembri01 commentedon Nov 24, 2026
Happy to write this if you would rather not — your analysis is the map here, so the credit is yours either way.
The shape I would follow, from the code:
autoRefresh: falseand let the SDK open the stream (packages/core/src/mcp/client.ts:348-358,:416-420), so nothing survives that stream ending — which matches your read.client.listen(filter)whoseclosedpromise is awaited,Effect.retry(reopen)while the failure is not aProtocolError, and a relist queued after each reopen (:933-973). That is the pattern I would copy.One thing worth settling before the diff: in the installed
@modelcontextprotocol/client(2.4.0 inbun.lock), I could not findautoOpenedSubscriptionin the shipped types — the client package only has bundleddist, so it may be private or renamed. If it is not reachable, the fix has to open the subscription itself (as the Events path does) whenever the server negotiates the modern protocol, rather than watching something the SDK owns. Which way it lands changes the diff, not the behaviour.If you are already on it, say so and I will leave it alone.
Guembri01 commentedon Nov 24, 2026
Correction to my previous comment, since I checked wrongly:
autoOpenedSubscriptionis public —get autoOpenedSubscription(): McpSubscription | undefinedin@modelcontextprotocol/[email protected](dist/client-BTIWJvaF.d.mts:1006), returninghonoredFilterplusclosed: Promise<'local' | 'graceful' | 'remote'>. My grep only matched.d.tsand the package ships.d.mts, so I missed it. Your approach works as written.So the diff is the small one: hold the auto-opened subscription, wait on
closed, reopen withclient.listen(honoredFilter)under the existingreopenschedule (ProtocolErrornot retried), and fire the tools/prompts/resources callbacks once after each reopen so a change announced while the stream was down is not lost. Stopping only on'local'(our own close), not on'graceful'— a server shutdown should still be followed when it comes back.I am writing it now against your test shape (cut the
subscriptions/listenresponse after the ack, thennotify.toolsChanged()). If you already have the branch, say so and I will drop mine rather than race you.Guembri01 commentedon Nov 24, 2026
Written up as #66170, with your diagnosis credited in the PR body: the auto-opened subscription is followed, reopened with
honoredFilterunder the existingreopenbackoff,'local'stops the loop, and the callbacks fire once after each reopen so a change announced while the stream was down is not lost.The test is your shape: a raw 2026-07-28 server over
InMemoryTransportthat acks the auto-openedsubscriptions/listen, ends it the way a shutdown does (the empty listen result), then asserts the stream comes back with the same filter, that the reopen refreshes once, and that anotifications/tools/list_changedon the reopened stream reaches the consumer. Without the change it times out waiting for the second listen; with it, 3/3 runs pass, and the MCP suites are 139 pass / 0 fail.If you were already on a branch, say so and I will close mine — yours would be the one to land.
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it
shaojunying commentedon Nov 24, 2026
Thanks @Guembri01 for writing this up, and for the credit in #66170.
I had a fix with tests on my fork too, so I ran my test cases against #66170 at a2114d4. Spacing of reopens when the server ends every stream: passes.
MCP pool > fans tool list changes out to every Location: passes on my Windows machine.One case is still open. If the listen stream fails to open at connect, for example a 503 on the first
subscriptions/listen, the SDK only reports it toonerrorandautoOpenedSubscriptionstays undefined. #66170 starts the reopen loop only whenautoOpenedSubscriptionexists, so that connection never gets a stream and never sees a tool change.My branch handles it by watching the SDK's stream first, and opening its own when there is none or after it ends. It does not close the SDK's stream at connect, so a connection does not do an extra listen and relist.
Maintainers, your call on which to take. I'm also happy for @Guembri01 to fold the failed-open case into #66170.
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it
fix(mcp): reopen the list change stream when a server ends it