[v2] createMcpHandler legacy fallback misses an abort that happens before it subscribes
維護者通常 1 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 82/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- typescript
研究方向
從 packages/server/src/server/createMcpHandler.ts 中引用的第 342–360 行附近的 createLegacyStatelessFallback 開始,並將其對中止的處理與 PerRequestHTTPServerTransport.handleMessage 進行比較。Issue 描述了故障和一種可能的修正方法;完成的標準是,在訂閱前中止的請求會拆除 transport 並中止 ctx.mcpReq.signal,與在訂閱後中止的請求一樣。
由索引模型根據 Issue 內容生成。
描述
What happened?
In the legacy stateless fallback of createMcpHandler, a request whose client aborts before the handler subscribes to request.signal is never seen as aborted. The tool runs with a ctx.mcpReq.signal that never fires, and the transport and server built for that request are only torn down when the tool finishes.
createLegacyStatelessFallback awaits factory() and product.connect(transport), and only then calls request.signal?.addEventListener('abort', teardown, { once: true }). An abort event that already fired is not delivered to a listener added later, and nothing checks request.signal.aborted after subscribing:
We hit it on a server whose tool waits up to 45 s for a state change and stops early on ctx.mcpReq.signal. A client that dropped the request while auth and the factory were still running left that wait running to its end. On a loaded machine that window was a few hundred ms. Our workaround is to also listen on ctx.http.req.signal in the tool.
What did you expect?
A request aborted before the handler subscribed is handled like one aborted after: teardown runs, the transport closes and ctx.mcpReq.signal aborts. The modern path already does this, since PerRequestHTTPServerTransport.handleMessage checks signal.aborted before dispatching.
A possible fix is to check right after subscribing:
request.signal?.addEventListener('abort', teardown, { once: true });
if (request.signal?.aborted) teardown();
Code to reproduce
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
const handler = createMcpHandler(() => {
const server = new McpServer({ name: 'repro', version: '0.0.0' });
server.registerTool('wait', { description: 'Waits up to 2 s for the request to be aborted' }, async (ctx) => {
const started = Date.now();
const aborted = await new Promise((resolve) => {
if (ctx.mcpReq.signal.aborted) return resolve(true);
ctx.mcpReq.signal.addEventListener('abort', () => resolve(true), { once: true });
setTimeout(() => resolve(false), 2_000);
});
console.log(`tool saw the abort: ${aborted}, after ${Date.now() - started} ms`);
return { content: [{ type: 'text', text: 'done' }] };
});
return server;
});
async function call(signal) {
const request = new Request('http://localhost/mcp', {
method: 'POST',
headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'wait', arguments: {} } }),
signal,
});
// Held for the whole exchange, as a server framework holds it.
await handler.fetch(request).then((res) => res.text(), () => undefined);
return request;
}
console.log('client aborts 1 s into the call:');
const late = new AbortController();
setTimeout(() => late.abort(), 1_000);
await call(late.signal);
console.log('client aborted before the handler subscribed:');
await call(AbortSignal.abort());
Output with Node 22.11:
client aborts 1 s into the call:
tool saw the abort: true, after 971 ms
client aborted before the handler subscribed:
tool saw the abort: false, after 2001 ms
The script keeps the Request alive until the response is read. Without that, undici can garbage-collect the original request and its signal stops reaching the clone that classifyEntryRequest forwards, so even the first case never sees the abort. That is unrelated to this issue.
SDK version
@modelcontextprotocol/server 2.3.1. The same code is on main at 39ae8e6 (2.4.0 was not tested).
Area
Server
- 主要語言
- TypeScript
- 星號
- 13.7k
- 分支
- 2.4k
- 平均合併
- 1 天 22 小時
- 30 天內合併 PR
- 18
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/typescript-sdk 的其他 Issue
-
[v2] requireBearerAuth: drop error="invalid_token" when no credentials were sent and resource_metadata is set (RFC 6750 §3.1)可能已有人在做 @Anandumv 於 5 天前認領。 未關閉v2
難度 2/5 1-3 小時 新手友好度 82/100
modelcontextprotocol/typescript-sdk#3124 · 2 則留言 ·
維護者通常 1 天內回覆
-
The `maxToolInputElements` refusal lacks the `Input validation error:` prefix that the schema refusal carries可能已有人在做 @barry166 於 9 天前認領。 未關閉v1 v2
難度 2/5 1-3 小時 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3111 · 3 則留言 ·
維護者通常 1 天內回覆
-
server: registeredResource.update and registeredResourceTemplate.update desync title and metadata.title when updating metadata alone可能已有人在做 @SameRainbows 於 4 天前認領。 未關閉v1 v2
難度 2/5 1-3 小時 新手友好度 76/100
modelcontextprotocol/typescript-sdk#3103 · 6 則留言 ·
維護者通常 1 天內回覆
-
[v1.x] Protocol.request keeps its abort listener after the request resolved, so a later abort sends notifications/cancelled for an answered request (1.30.0)可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉v1
難度 2/5 1-3 小時 新手友好度 76/100
modelcontextprotocol/typescript-sdk#3090 · 2 則留言 ·
維護者通常 1 天內回覆
-
[v2] e2e `protocol:timeout:max-total` is flaky over SSE: fake timers advance before the request reaches the server可能已有人在做 @Singmin 於 18 天前認領。 未關閉v1 v2
難度 2/5 1-3 小時 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3045 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/typescript-sdk 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 2 天內回覆
-
reproduced
難度 2/5 1-3 小時 新手友好度 74/100
anomalyco/opencode#62052 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
strapi/design-system#2084 ·
維護者通常 3 天內回覆
-
難度 1/5 1 小時以內 新手友好度 88/100
維護者通常 1 天內回覆