[v2] Auto-opened subscriptions/listen stream is never reopened after the server restarts or the stream drops, and the application is not notified
维护者通常 1 天内回复
@Li-john1021 已经在做这个了。
开始于 2026年10月7日。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 38/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- node.js, typescript
调研方向
Start with the client’s ClientOptions.listChanged, Client.listen() and autoOpenedSubscription behavior; the issue does not name implementation files or tests. Compare the documented one-shot behavior with the auto-opened subscription’s reconnect expectations, and use the supplied reproduction to verify what happens after the stream ends. Done means the stream ending is no longer silent: it is either reopened with a way to refresh missed lists, or clearly surfaced and documented for applications to handle.
由索引模型根据 Issue 内容生成。
描述
What happened?
With ClientOptions.listChanged configured, the client auto-opens a subscriptions/listen stream on connect (2026-07-28 revision). When that stream ends because the server restarted (or closed it, or the network dropped) the client never opens a new one, and the application is told nothing through the usual channels. tools/list_changed notifications from the restarted server are never delivered, onChanged never fires again, and the tool list the application holds stays stale. The connection itself still looks healthy: client.onclose / transport.onclose do not fire, and a plain listTools() call works against the new server.
The only signal is client.autoOpenedSubscription.closed (resolves 'graceful' or 'remote'), which an application has to know to attach to before the drop. Client.listen() documents "No automatic re-listen", but with the auto-opened subscription driven by the constructor option (documented as "durable across reconnects") there is no hint that the stream is one-shot per connection, and no event, callback or error hook when it ends.
Observed on @modelcontextprotocol/[email protected] + @modelcontextprotocol/[email protected] (latest on npm), Node 22, Streamable HTTP over 127.0.0.1. I did not test a stateful 2025-era server whose standby GET stream could be affected in the same way.
Output of the script below (graceful variant, handler.close() before the process exits):
[ 120ms] connected, protocol 2026-07-28
[ 120ms] autoOpenedSubscription present: true honored: {"toolsListChanged":true}
[ 624ms] --- server 1: trigger tools/list_changed
[ 951ms] onChanged: err= null tools= [ 'tool-gen-1' ]
[ 1625ms] --- stopping server 1
[ 1628ms] subscription.closed resolved: graceful
[ 3127ms] --- starting server 2 on the same port (tool list differs)
[ 9128ms] --- server 2: trigger tools/list_changed <- no onChanged follows
[12131ms] client.autoOpenedSubscription is same object: true
[12140ms] explicit listTools() on server 2 returns: [ 'tool-gen-2' ]
[12140ms] transport.onclose fired <- only from my explicit client.close()
With the server killed without handler.close() (ABRUPT=1), the result is the same, except that transport.onerror / client.onerror additionally log SSE stream disconnected: TypeError: terminated once and the subscription resolves 'remote'. Nothing re-listens in either case.
What did you expect?
Any of these would remove the silent stale state:
- With
ClientOptions.listChangedset, re-open the auto-opened subscription after a'remote'end (and probably after'graceful'when the connection is still usable), with the same backoff options the transport uses for SSE reconnection, and give the application a hook (for example aClientOptions.listChanged.onSubscriptionClosed/onResubscribedcallback) so it can refresh its cached lists, since notifications sent while the stream was down are lost. - Or, if staying one-shot is intended, surface the end of the auto-opened stream through an existing channel (
client.onerrorfor'remote', a documented event for'graceful') and state in thelistChangedoption docs that the application must watchautoOpenedSubscription.closedand calllisten()again, then re-fetch the lists.
Today an application has to attach to client.autoOpenedSubscription.closed right after connect(), call client.listen(filter) again with its own backoff, and re-run listTools() after each successful re-listen.
Code to reproduce
Save as repro.mjs (npm i @modelcontextprotocol/[email protected] @modelcontextprotocol/[email protected] zod, "type": "module"), run node repro.mjs (or ABRUPT=1 node repro.mjs). The small node:http-to-fetch bridge is only there because the example does not use the Node adapter package.
import http from 'node:http';
import net from 'node:net';
import { McpServer, createMcpHandler } from '@modelcontextprotocol/server';
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
import { z } from 'zod';
const t0 = Date.now();
const log = (...a) => console.log(`[${String(Date.now() - t0).padStart(5)}ms]`, ...a);
function freePort() {
return new Promise((res) => {
const s = net.createServer().listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => res(p)); });
});
}
function startServer(port, generation) {
const handler = createMcpHandler(() => {
const server = new McpServer({ name: 'repro', version: '1.0.0' }, { capabilities: { tools: { listChanged: true } } });
server.registerTool(`tool-gen-${generation}`, { description: 'x', inputSchema: z.object({}) }, async () => ({ content: [{ type: 'text', text: 'ok' }] }));
return server;
});
const httpServer = http.createServer(async (req, res) => {
const chunks = [];
for await (const c of req) chunks.push(c);
const body = chunks.length ? Buffer.concat(chunks) : undefined;
const ac = new AbortController();
res.on('close', () => { if (!res.writableFinished) ac.abort(); });
const webReq = new Request(`http://127.0.0.1:${port}${req.url}`, {
method: req.method, headers: Object.fromEntries(Object.entries(req.headers).map(([k, v]) => [k, String(v)])),
body: ['GET', 'HEAD'].includes(req.method) ? undefined : body, signal: ac.signal,
});
const webRes = await handler.fetch(webReq);
res.writeHead(webRes.status, Object.fromEntries(webRes.headers));
res.flushHeaders();
if (!webRes.body) return res.end();
const reader = webRes.body.getReader();
res.on('close', () => reader.cancel().catch(() => {}));
try { for (;;) { const { done, value } = await reader.read(); if (done) break; res.write(value); } } catch {} finally { res.end(); }
});
return new Promise((res) => httpServer.listen(port, '127.0.0.1', () => res({
handler,
stop: async () => { if (!process.env.ABRUPT) await handler.close(); httpServer.closeAllConnections(); await new Promise((r) => httpServer.close(r)); },
})));
}
const port = await freePort();
let server = await startServer(port, 1);
log('server 1 listening on', port);
const client = new Client({ name: 'repro-client', version: '1.0.0' }, {
versionNegotiation: { mode: 'auto' },
listChanged: { tools: { autoRefresh: true, onChanged: (err, tools) => log('onChanged: err=', err?.message ?? null, 'tools=', tools?.map((t) => t.name)) } },
});
client.onclose = () => log('client.onclose fired');
client.onerror = (e) => log('client.onerror:', e?.message ?? e);
const transport = new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${port}/mcp`));
transport.onclose = () => log('transport.onclose fired');
transport.onerror = (e) => log('transport.onerror:', e?.message ?? e);
await client.connect(transport);
log('connected, protocol', client.getNegotiatedProtocolVersion());
const sub = client.autoOpenedSubscription;
log('autoOpenedSubscription present:', sub !== undefined, 'honored:', JSON.stringify(sub?.honoredFilter));
sub?.closed.then((c) => log('subscription.closed resolved:', c));
await new Promise((r) => setTimeout(r, 500));
log('--- server 1: trigger tools/list_changed');
server.handler.notify.toolsChanged();
await new Promise((r) => setTimeout(r, 1000));
log('--- stopping server 1');
await server.stop();
await new Promise((r) => setTimeout(r, 1500));
log('--- starting server 2 on the same port (tool list differs)');
server = await startServer(port, 2);
await new Promise((r) => setTimeout(r, 6000));
log('--- server 2: trigger tools/list_changed');
server.handler.notify.toolsChanged();
await new Promise((r) => setTimeout(r, 3000));
log('client.autoOpenedSubscription is same object:', client.autoOpenedSubscription === sub);
const listed = await client.listTools();
log('explicit listTools() on server 2 returns:', listed.tools.map((t) => t.name), '(the app never got an onChanged for it if none logged above after server 2 started)');
await client.close();
await server.stop();
process.exit(0);
Related
Not the same as #2682 (standby GET stream reconnecting forever on idle close, where the transport does reconnect), #2873 (listen streams without lifetime bound on serverless hosts) or #2650 (listen stream held open with nothing honoured, closed). Here the stream ends once and is never reopened.
SDK version: @modelcontextprotocol/[email protected], @modelcontextprotocol/[email protected]
Area: Client
- 主要语言
- TypeScript
- 星标
- 13.8k
- 派生
- 2.5k
- 平均合并
- 6 天 23 小时
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/typescript-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 条评论 ·
维护者通常 1 天内回复
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`可能已有人在做 @mgoldsborough 于 6 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3249 ·
维护者通常 1 天内回复
-
v2
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 条评论 ·
维护者通常 1 天内回复
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` reply可能已有人在做 @Tidehyc 于 8 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 条评论 ·
维护者通常 1 天内回复
-
UriTemplate.match() throws a TypeError on an empty expression it accepts可能已有人在做 @yunusemrejs 于 12 天前认领。 未关闭v1 v2
难度 2/5 1-3 小时 新手友好度 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/typescript-sdk 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
Vessel9817/onion-site-template#512 ·
维护者通常 1 天内回复
-
nightly-tests
难度 2/5 1-3 小时 新手友好度 68/100
BlackBeltTechnology/pi-agent-dashboard#1256 ·
维护者通常 1 天内回复
-
approved
难度 2/5 1-3 小时 新手友好度 85/100
fritzprix/libr-agent#3266 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
better-auth/better-auth#13101 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复