11diff --git a/dist/mcp/index.d.ts b/dist/mcp/index.d.ts
2- index c8fad44..439da01 100644
2+ index c8fad448e8797b89690a99d93490d1363851b225..439da01e789713d217778e8bdebfa1a9d52a71e2 100644
33--- a/dist/mcp/index.d.ts
44+++ b/dist/mcp/index.d.ts
55@@ -29,6 +29,7 @@ import {
@@ -19,7 +19,7 @@ index c8fad44..439da01 100644
1919 McpAgent,
2020 type McpAuthContext,
2121diff --git a/dist/mcp/index.js b/dist/mcp/index.js
22- index 1edcf0c..707f1cf 100644
22+ index 1edcf0c8c9e67aa211ae515e7672cdf79912101e..0ba4f5bff8e6a968aa18c609952e576cedf13d8f 100644
2323--- a/dist/mcp/index.js
2424+++ b/dist/mcp/index.js
2525@@ -28,13 +28,14 @@ import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/
@@ -178,7 +178,7 @@ index 1edcf0c..707f1cf 100644
178178 props: ctx.props,
179179 jurisdiction: options.jurisdiction
180180 });
181- @@ -306,27 +344,66 @@ const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
181+ @@ -306,27 +344,86 @@ const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
182182 if (!ws) {
183183 await writer.close();
184184 return new Response("Failed to establish WS to DO", { status: 500 });
@@ -187,7 +187,7 @@ index 1edcf0c..707f1cf 100644
187187- ws.addEventListener("message", (event) => {
188188+ }
189189+ ws.accept();
190- + const __closeSse = () => {
190+ + const __abortSse = () => {
191191+ __sseClosed = true;
192192+ try {
193193+ clearInterval(keepAlive);
@@ -197,22 +197,42 @@ index 1edcf0c..707f1cf 100644
197197+ } catch {}
198198+ writer.abort(new Error("SSE client not draining")).catch(() => {});
199199+ };
200+ + const __closeSseGracefully = () => {
201+ + __sseClosed = true;
202+ + try {
203+ + clearInterval(keepAlive);
204+ + } catch {}
205+ + try {
206+ + const finalFrame = encoder.encode(": max-age rotation, reconnect\n\n");
207+ + __writeChain = __writeChain.then(() => writer.write(finalFrame)).catch(() => {}).then(() => writer.close()).catch(() => {
208+ + writer.abort().catch(() => {});
209+ + });
210+ + } catch {
211+ + __writeChain = writer.close().catch(() => {
212+ + writer.abort().catch(() => {});
213+ + });
214+ + }
215+ + try {
216+ + ws.close(1000, "sse_max_age_rotation");
217+ + } catch {}
218+ + return __writeChain;
219+ + };
200220+ const __forwardSse = (frame) => {
201221+ if (__sseClosed) return __writeChain;
202222+ const ageMs = Date.now() - __openedAt;
203223+ if (ageMs > MAX_SSE_AGE_MS) {
204- + console.error (JSON.stringify({
224+ + console.log (JSON.stringify({
205225+ event: "sse_max_age_close",
206226+ sessionId,
207227+ variant: "streamable-get",
208228+ ageMs,
209229+ pendingBytes: __pendingBytes
210230+ }));
211- + __closeSse ();
231+ + __closeSseGracefully ();
212232+ return Promise.resolve();
213233+ }
214234+ if (__pendingBytes + frame.byteLength > MAX_PENDING_SSE_BYTES) {
215- + __closeSse ();
235+ + __abortSse ();
216236+ return Promise.resolve();
217237+ }
218238+ __pendingBytes += frame.byteLength;
@@ -260,7 +280,7 @@ index 1edcf0c..707f1cf 100644
260280 return new Response(readable, {
261281 headers: {
262282 "Cache-Control": "no-cache",
263- @@ -389,10 +466 ,16 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
283+ @@ -389,10 +486 ,16 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
264284 const url = new URL(request.url);
265285 if (request.method === "GET" && basePattern.test(url)) {
266286 const sessionId = url.searchParams.get("sessionId") || namespace.newUniqueId().toString();
@@ -281,7 +301,7 @@ index 1edcf0c..707f1cf 100644
281301 endpointUrl.pathname = encodeURI(`${basePath}/message`);
282302 endpointUrl.searchParams.set("sessionId", sessionId);
283303 const endpointMessage = `event: endpoint\ndata: ${endpointUrl.pathname + endpointUrl.search + endpointUrl.hash}\n\n`;
284- @@ -414,35 +497,74 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
304+ @@ -414,35 +517,94 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
285305 console.error("Failed to establish WebSocket connection");
286306 await writer.close();
287307 return new Response("Failed to establish WebSocket connection", { status: 500 });
@@ -290,7 +310,7 @@ index 1edcf0c..707f1cf 100644
290310- ws.addEventListener("message", (event) => {
291311+ }
292312+ ws.accept();
293- + const __closeSse = () => {
313+ + const __abortSse = () => {
294314+ __sseClosed = true;
295315+ try {
296316+ clearInterval(keepAlive);
@@ -300,22 +320,42 @@ index 1edcf0c..707f1cf 100644
300320+ } catch {}
301321+ writer.abort(new Error("SSE client not draining")).catch(() => {});
302322+ };
323+ + const __closeSseGracefully = () => {
324+ + __sseClosed = true;
325+ + try {
326+ + clearInterval(keepAlive);
327+ + } catch {}
328+ + try {
329+ + const finalFrame = encoder.encode(": max-age rotation, reconnect\n\n");
330+ + __writeChain = __writeChain.then(() => writer.write(finalFrame)).catch(() => {}).then(() => writer.close()).catch(() => {
331+ + writer.abort().catch(() => {});
332+ + });
333+ + } catch {
334+ + __writeChain = writer.close().catch(() => {
335+ + writer.abort().catch(() => {});
336+ + });
337+ + }
338+ + try {
339+ + ws.close(1000, "sse_max_age_rotation");
340+ + } catch {}
341+ + return __writeChain;
342+ + };
303343+ const __forwardSse = (frame) => {
304344+ if (__sseClosed) return __writeChain;
305345+ const ageMs = Date.now() - __openedAt;
306346+ if (ageMs > MAX_SSE_AGE_MS) {
307- + console.error (JSON.stringify({
347+ + console.log (JSON.stringify({
308348+ event: "sse_max_age_close",
309349+ sessionId,
310350+ variant: "legacy-sse",
311351+ ageMs,
312352+ pendingBytes: __pendingBytes
313353+ }));
314- + __closeSse ();
354+ + __closeSseGracefully ();
315355+ return Promise.resolve();
316356+ }
317357+ if (__pendingBytes + frame.byteLength > MAX_PENDING_SSE_BYTES) {
318- + __closeSse ();
358+ + __abortSse ();
319359+ return Promise.resolve();
320360+ }
321361+ __pendingBytes += frame.byteLength;
@@ -374,13 +414,11 @@ index 1edcf0c..707f1cf 100644
374414 console.error("Error closing SSE connection:", error);
375415 }
376416 }
377- @@ -1698,6 +1820 ,6 @@ var McpAgent = class McpAgent extends Agent {
417+ @@ -1698,6 +1860 ,6 @@ var McpAgent = class McpAgent extends Agent {
378418 };
379419 McpAgent.STREAM_REQS_KEY_PREFIX = "__mcp_stream_reqs__:";
380420 //#endregion
381421- export { DurableObjectEventStore, ElicitRequestSchema, MCP_SERVER_ID_MAX_LENGTH, McpAgent, RPCClientTransport, RPCServerTransport, RPC_DO_PREFIX, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext, normalizeServerId };
382422+ export { DurableObjectEventStore, ElicitRequestSchema, MAX_SSE_AGE_MS, MCP_SERVER_ID_MAX_LENGTH, McpAgent, RPCClientTransport, RPCServerTransport, RPC_DO_PREFIX, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext, normalizeServerId };
383423
384- - //# sourceMappingURL=index.js.map
385- \ No newline at end of file
386- + //# sourceMappingURL=index.js.map
424+ //# sourceMappingURL=index.js.map
0 commit comments