Skip to content

Commit 82bb619

Browse files
authored
fix(mcp): stop holding subscriptions/listen streams open until Vercel kills the function (#17)
Clients on MCP 2026-07-28 open a subscriptions/listen stream when the server advertises tools.listChanged. The SDK keeps that SSE body open with keepalives until the client disconnects, so on Vercel every such invocation ran to the 300 s maxDuration and was logged as "Task timed out" (1,426 times since the first deploy). Tool calls completed but the connector kept failing around them. - Do not advertise tools.listChanged: the tool list is static and a stateless function has no event bus that could deliver a change anyway. The SDK then acknowledges a listen request with an empty filter and closes the stream. - Bound every event-stream response at MAX_STREAM_MS (default 250 s) so a held stream ends cleanly below the platform limit. - Log one JSON line per request (method, protocol, status, duration), never the URL, so the Vercel logs show what is being asked. - Add a node:test suite covering the token check, both protocol eras, the listen stream, and the stream bound. Refs modelcontextprotocol/typescript-sdk#2873
1 parent 43b985d commit 82bb619

7 files changed

Lines changed: 228 additions & 3 deletions

File tree

‎mcp/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Two project settings matter:
116116
| `HOME_AGENT_API_KEY` | the same key the home agent runs with (`FABRIC_API_KEY` / `--api-key`) |
117117
| `MCP_PATH_TOKEN` | a long random secret, e.g. `openssl rand -hex 24` — becomes part of the connector URL |
118118
| `PATTERNS_BASE_URL` | optional; default `https://raw.githubusercontent.com/FRAQTIV/Fabric/main` |
119+
| `MAX_STREAM_MS` | optional; default `250000`. Hard bound on any event-stream response; keep it below the function's maxDuration (300 s) |
119120

120121
The MCP endpoint is `https://<project>.vercel.app/mcp/<MCP_PATH_TOKEN>` — for this deployment
121122
(Vercel project `fabric`): `https://fabric-theta-murex.vercel.app/mcp/<MCP_PATH_TOKEN>`. Use the
@@ -165,6 +166,7 @@ npm run mock-agent # terminal 1: fake home a
165166
HOME_AGENT_URL=http://localhost:8080 MCP_PATH_TOKEN=dev npm run dev # terminal 2
166167
MCP_PATH_TOKEN=dev npm run smoke # terminal 3: JSON-RPC round-trip
167168
npm run typecheck
169+
npm test # node:test suite (protocol, listen streams, stream bound)
168170
```
169171

170172
`vercel dev` from the repository root runs the service through the real `vercel.json` routing instead.
@@ -182,3 +184,8 @@ Point `HOME_AGENT_URL` at a real tunnel (and set `HOME_AGENT_API_KEY`) to test r
182184
- **"No captions were returned"** — the video has no subtitles in that language; try `language: "es"` etc.
183185
yt-dlp itself occasionally needs an update (`yt-dlp -U`) when YouTube changes things.
184186
- **Slow first call** — the transcript tool waits up to `TRANSCRIPT_TIMEOUT_MS` (100 s); long videos take 10–30 s.
187+
- **Vercel logs `Task timed out after 300 seconds` on every other request** — a client speaking MCP
188+
2026-07-28 opened a `subscriptions/listen` stream, which the SDK holds open until the platform kills the
189+
function (modelcontextprotocol/typescript-sdk#2873). The server no longer advertises `tools.listChanged`,
190+
so the SDK closes those streams at once, and every event stream is cut at `MAX_STREAM_MS` as a backstop.
191+
Each request now logs one JSON line (`{"mcp":"response","method":…,"status":…,"ms":…}`) without the URL.

‎mcp/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"dev": "tsx src/dev.ts",
99
"mock-agent": "tsx src/mock-agent.ts",
1010
"typecheck": "tsc --noEmit",
11-
"smoke": "bash scripts/smoke.sh"
11+
"smoke": "bash scripts/smoke.sh",
12+
"test": "tsx --test test/*.test.ts"
1213
},
1314
"dependencies": {
1415
"@modelcontextprotocol/server": "^2.3.1",

‎mcp/server.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
33
import { config as settings } from "./src/config.js";
44
import { createHandler } from "./src/mcp.js";
55
import { isNodeRequest, sendWebResponse, toWebRequest } from "./src/node-adapter.js";
6+
import { boundStream } from "./src/stream-bound.js";
67

78
const mcp = createHandler();
89

@@ -21,11 +22,46 @@ function suppliedToken(url: URL): string {
2122
return m ? decodeURIComponent(m[1]) : "";
2223
}
2324

25+
// The JSON-RPC method(s) in a request body, for the log line. The URL is never logged:
26+
// it carries the access token.
27+
async function methodsOf(request: Request): Promise<string> {
28+
if (request.method !== "POST" || !(request.headers.get("content-type") ?? "").includes("json")) {
29+
return "-";
30+
}
31+
try {
32+
const body = (await request.clone().json()) as unknown;
33+
const messages = Array.isArray(body) ? body : [body];
34+
return messages
35+
.map((m) =>
36+
m && typeof m === "object" && "method" in m ? String((m as { method: unknown }).method) : "response",
37+
)
38+
.join(",");
39+
} catch {
40+
return "?";
41+
}
42+
}
43+
2444
export async function handle(request: Request): Promise<Response> {
2545
if (settings.pathToken && !tokenMatches(suppliedToken(new URL(request.url)))) {
2646
return new Response("Not found", { status: 404 });
2747
}
28-
return mcp(request);
48+
const started = Date.now();
49+
const method = await methodsOf(request);
50+
const proto = request.headers.get("mcp-protocol-version") ?? "legacy";
51+
const log = (event: string, extra: Record<string, unknown> = {}) =>
52+
console.log(JSON.stringify({ mcp: event, method, proto, ms: Date.now() - started, ...extra }));
53+
54+
const response = await mcp(request);
55+
log("response", { status: response.status });
56+
57+
// An event stream has to end before the Vercel maxDuration kills the invocation.
58+
if (response.body && (response.headers.get("content-type") ?? "").includes("text/event-stream")) {
59+
const body = boundStream(response.body, settings.maxStreamMs, () =>
60+
log("stream-timeout", { maxStreamMs: settings.maxStreamMs }),
61+
);
62+
return new Response(body, { status: response.status, headers: response.headers });
63+
}
64+
return response;
2965
}
3066

3167
// Vercel's Node runtime treats this file as the service entrypoint. The `{ fetch }` export is

‎mcp/src/config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ export const config = {
88
pathToken: process.env.MCP_PATH_TOKEN ?? "",
99
transcriptTimeoutMs: Number(process.env.TRANSCRIPT_TIMEOUT_MS ?? 100_000),
1010
patternCacheTtlMs: Number(process.env.PATTERN_CACHE_TTL_MS ?? 10 * 60_000),
11+
// Hard bound on any event-stream response. Keep it below the Vercel function
12+
// maxDuration (300 s by default) so a held-open stream ends cleanly instead of
13+
// the invocation being killed and logged as "Task timed out".
14+
maxStreamMs: Number(process.env.MAX_STREAM_MS ?? 250_000),
1115
};

‎mcp/src/mcp.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ export function createHandler() {
218218
},
219219
);
220220
},
221-
{ serverInfo: { name: "fabric-mcp", version: VERSION } },
221+
{
222+
serverInfo: { name: "fabric-mcp", version: VERSION },
223+
// The tool list never changes at runtime and, on Vercel, every request runs in its own
224+
// invocation with its own in-memory event bus, so a list_changed notification could never
225+
// be delivered anyway. Advertising listChanged makes 2026-07-28 clients open a
226+
// subscriptions/listen stream that the SDK holds open until the platform kills the
227+
// function (modelcontextprotocol/typescript-sdk#2873). With it off, the SDK acknowledges
228+
// the listen request with an empty filter and closes the stream at once.
229+
capabilities: { tools: { listChanged: false } },
230+
},
222231
);
223232
}

‎mcp/src/stream-bound.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Wraps a response body so that it ends no later than `maxMs` after it starts.
3+
*
4+
* Vercel kills an invocation at its maximum duration (300 s by default) and logs
5+
* "Task timed out". The MCP SDK keeps some SSE bodies open for as long as the client
6+
* stays connected, so without a bound every such request runs to the kill. The
7+
* wrapper passes chunks through untouched and, if the source is still open when the
8+
* timer fires, closes the body cleanly and cancels the source.
9+
*/
10+
export function boundStream(
11+
source: ReadableStream<Uint8Array>,
12+
maxMs: number,
13+
onTimeout?: () => void,
14+
): ReadableStream<Uint8Array> {
15+
const reader = source.getReader();
16+
let finished = false;
17+
let timer: ReturnType<typeof setTimeout> | undefined;
18+
19+
return new ReadableStream<Uint8Array>({
20+
start(controller) {
21+
const finish = (end: () => void) => {
22+
if (finished) return;
23+
finished = true;
24+
clearTimeout(timer);
25+
end();
26+
};
27+
28+
timer = setTimeout(() => {
29+
if (finished) return;
30+
onTimeout?.();
31+
finish(() => controller.close());
32+
reader.cancel(new Error(`response stream exceeded ${maxMs}ms`)).catch(() => {});
33+
}, maxMs);
34+
35+
void (async () => {
36+
try {
37+
for (;;) {
38+
const { value, done } = await reader.read();
39+
if (finished) return;
40+
if (done) break;
41+
controller.enqueue(value);
42+
}
43+
finish(() => controller.close());
44+
} catch (err) {
45+
finish(() => controller.error(err));
46+
}
47+
})();
48+
},
49+
cancel(reason) {
50+
finished = true;
51+
clearTimeout(timer);
52+
return reader.cancel(reason);
53+
},
54+
});
55+
}

‎mcp/test/server.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
process.env.MCP_PATH_TOKEN = "test-token";
5+
process.env.HOME_AGENT_URL = "http://127.0.0.1:9";
6+
process.env.MAX_STREAM_MS = "250";
7+
8+
const { handle } = await import("../server.js");
9+
const { boundStream } = await import("../src/stream-bound.js");
10+
11+
const ENDPOINT = "http://localhost/mcp/test-token";
12+
const META = {
13+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
14+
"io.modelcontextprotocol/clientCapabilities": {},
15+
};
16+
17+
function post(body: unknown, headers: Record<string, string> = {}): Promise<Response> {
18+
return handle(
19+
new Request(ENDPOINT, {
20+
method: "POST",
21+
headers: {
22+
"content-type": "application/json",
23+
accept: "application/json, text/event-stream",
24+
...headers,
25+
},
26+
body: JSON.stringify(body),
27+
}),
28+
);
29+
}
30+
31+
function modern(method: string, params: Record<string, unknown> = {}, id = 1) {
32+
return post(
33+
{ jsonrpc: "2.0", id, method, params: { ...params, _meta: META } },
34+
{ "mcp-protocol-version": "2026-07-28", "mcp-method": method },
35+
);
36+
}
37+
38+
/** Reads the whole body, or fails if the stream is still open after `ms`. */
39+
async function readWithin(res: Response, ms: number): Promise<string> {
40+
let timer: NodeJS.Timeout;
41+
const timeout = new Promise<never>((_, reject) => {
42+
timer = setTimeout(() => reject(new Error(`response body still open after ${ms}ms`)), ms);
43+
});
44+
try {
45+
return await Promise.race([res.text(), timeout]);
46+
} finally {
47+
clearTimeout(timer!);
48+
}
49+
}
50+
51+
test("a wrong path token is answered 404", async () => {
52+
const res = await handle(new Request("http://localhost/mcp/nope", { method: "POST", body: "{}" }));
53+
assert.equal(res.status, 404);
54+
});
55+
56+
test("legacy (2025) tools/list answers and closes the stream", async () => {
57+
const res = await post({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} });
58+
assert.equal(res.status, 200);
59+
const body = await readWithin(res, 2000);
60+
assert.match(body, /"name":"get_transcript"/);
61+
});
62+
63+
test("server/discover does not advertise tools.listChanged (nothing can deliver it on Vercel)", async () => {
64+
const res = await modern("server/discover");
65+
assert.equal(res.status, 200);
66+
const json = JSON.parse(await readWithin(res, 2000)) as {
67+
result: { capabilities: { tools?: { listChanged?: boolean } } };
68+
};
69+
assert.ok(json.result.capabilities.tools, "tools capability is still advertised");
70+
assert.notEqual(json.result.capabilities.tools.listChanged, true);
71+
});
72+
73+
test("subscriptions/listen is acknowledged and closed at once instead of being held open", async () => {
74+
const started = Date.now();
75+
const res = await modern("subscriptions/listen", { notifications: { toolsListChanged: true } }, 2);
76+
assert.equal(res.status, 200);
77+
const body = await readWithin(res, 2000);
78+
assert.match(body, /notifications\/subscriptions\/acknowledged/);
79+
assert.match(body, /"resultType":"complete"/);
80+
assert.ok(Date.now() - started < 2000);
81+
});
82+
83+
test("modern tools/list still works", async () => {
84+
const res = await modern("tools/list", {}, 3);
85+
assert.equal(res.status, 200);
86+
assert.match(await readWithin(res, 2000), /"name":"extract_wisdom"/);
87+
});
88+
89+
test("boundStream ends a stream that never closes on its own", async () => {
90+
const never = new ReadableStream<Uint8Array>({
91+
start(controller) {
92+
controller.enqueue(new TextEncoder().encode(": hello\n\n"));
93+
// and then nothing, forever
94+
},
95+
});
96+
const started = Date.now();
97+
const text = await new Response(boundStream(never, 200)).text();
98+
assert.match(text, /hello/);
99+
assert.ok(Date.now() - started < 1500, `took ${Date.now() - started}ms`);
100+
});
101+
102+
test("boundStream passes a finishing stream through untouched", async () => {
103+
const src = new Response("a\nb\n").body!;
104+
assert.equal(await new Response(boundStream(src, 5000)).text(), "a\nb\n");
105+
});
106+
107+
test("an event stream through handle() is cut at MAX_STREAM_MS even if the SDK holds it", async () => {
108+
// MAX_STREAM_MS=250 for this process. tools/list closes well before that; this checks the
109+
// wrapper does not break a normal, short stream.
110+
const res = await post({ jsonrpc: "2.0", id: 9, method: "ping" });
111+
assert.equal(res.status, 200);
112+
assert.match(await readWithin(res, 2000), /"result":\{\}/);
113+
});

0 commit comments

Comments
 (0)