[v2] createMcpHandler legacy fallback misses an abort that happens before it subscribes
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Bắt đầu tại packages/server/src/server/createMcpHandler.ts, ở createLegacyStatelessFallback quanh các dòng được trích dẫn 342–360, và so sánh cách xử lý hủy với PerRequestHTTPServerTransport.handleMessage. Issue mô tả lỗi và một cách khắc phục có thể áp dụng; hoàn thành nghĩa là một yêu cầu bị hủy trước khi đăng ký sẽ đóng transport và hủy ctx.mcpReq.signal, giống như một yêu cầu bị hủy sau đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- TypeScript
- Star
- 13.7k
- Fork
- 2.4k
- Merge trung bình
- 2 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 26
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/typescript-sdk
-
[v2] requireBearerAuth: drop error="invalid_token" when no credentials were sent and resource_metadata is set (RFC 6750 §3.1)Có thể đã có người làm @Anandumv đã nhận 2 ngày trước. Đang mởv2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/typescript-sdk#3124 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
The `maxToolInputElements` refusal lacks the `Input validation error:` prefix that the schema refusal carriesCó thể đã có người làm @barry166 đã nhận 6 ngày trước. Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
modelcontextprotocol/typescript-sdk#3111 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
server: registeredResource.update and registeredResourceTemplate.update desync title and metadata.title when updating metadata aloneCó thể đã có người làm @SameRainbows đã nhận 1 ngày trước. Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
modelcontextprotocol/typescript-sdk#3103 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[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)Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởv1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
modelcontextprotocol/typescript-sdk#3090 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[v2] e2e `protocol:timeout:max-total` is flaky over SSE: fake timers advance before the request reaches the serverCó thể đã có người làm @Singmin đã nhận 15 ngày trước. Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/typescript-sdk#3045 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/typescript-sdk
Issue tương tự
-
Remove unused dependenciesCó thể đã có người làm @alastair-smith đã nhận hôm nay. Đang mởchore other
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
alastair-smith/card-games#384 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
code-yeongyu/senpi#5571 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nexu-io/open-design#9262 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
friction
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
kentcdodds/kody#4753 ·
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
lingdojo/kana-dojo#33508 · 1 bình luận · 5 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày