fastmcp 4.28.0 → 4.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,39 @@ var unsupportedProtocolVersion = (request) => {
33
33
  }
34
34
  return `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`;
35
35
  };
36
+ var createSSEStream = () => {
37
+ const { readable, writable } = new TransformStream();
38
+ const reader = readable.getReader();
39
+ const writer = writable.getWriter();
40
+ let cancelled = false;
41
+ let forwarding;
42
+ const body = new ReadableStream(
43
+ {
44
+ cancel(reason) {
45
+ cancelled = true;
46
+ return reader.cancel(reason);
47
+ },
48
+ pull(controller) {
49
+ forwarding = reader.read().then(({ done, value }) => {
50
+ if (!done && !cancelled) controller.enqueue(value);
51
+ });
52
+ return forwarding;
53
+ },
54
+ start(controller) {
55
+ void reader.closed.then(async () => {
56
+ await forwarding;
57
+ if (!cancelled) controller.close();
58
+ }).catch((error) => controller.error(error));
59
+ }
60
+ },
61
+ { highWaterMark: 0 }
62
+ );
63
+ return {
64
+ readable: body,
65
+ terminate: () => reader.cancel(new Error("SSE stream ended")),
66
+ writer
67
+ };
68
+ };
36
69
  var MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024;
37
70
  var readBodyWithLimit = async (request) => {
38
71
  if (!request.body) {
@@ -82,7 +115,7 @@ var WebStreamableHTTPServerTransport = class {
82
115
  * send during that window has nothing to test against; persist rather than
83
116
  * drop, since over-storing for the length of a replay beats losing an event.
84
117
  */
85
- _replaysInFlight = 0;
118
+ _replaysInFlight = /* @__PURE__ */ new Set();
86
119
  _requestToStreamMapping = /* @__PURE__ */ new Map();
87
120
  _sessionClosing = false;
88
121
  /**
@@ -180,7 +213,7 @@ var WebStreamableHTTPServerTransport = class {
180
213
  }
181
214
  return;
182
215
  }
183
- const writer = this._streamMapping.get(streamId);
216
+ const writer = this._streamMapping.get(streamId)?.writer;
184
217
  const store = this.shouldPersist(streamId, writer) ? this._eventStore : void 0;
185
218
  if (writer || store) {
186
219
  try {
@@ -200,21 +233,18 @@ var WebStreamableHTTPServerTransport = class {
200
233
  error instanceof Error ? error : new Error(String(error))
201
234
  );
202
235
  }
203
- if (requestId !== void 0 && isResponse) {
236
+ if (requestId !== void 0 && isResponse && this._requestToStreamMapping.get(requestId) === streamId) {
204
237
  this._requestToStreamMapping.delete(requestId);
205
238
  const streamInUse = Array.from(
206
239
  this._requestToStreamMapping.values()
207
240
  ).includes(streamId);
208
- if (!streamInUse) {
209
- this._streamMapping.delete(streamId);
210
- if (writer) {
211
- try {
212
- await writer.close();
213
- } catch (error) {
214
- this.onerror?.(
215
- error instanceof Error ? error : new Error(String(error))
216
- );
217
- }
241
+ if (writer && !streamInUse) {
242
+ try {
243
+ await writer.close();
244
+ } catch (error) {
245
+ this.onerror?.(
246
+ error instanceof Error ? error : new Error(String(error))
247
+ );
218
248
  }
219
249
  }
220
250
  }
@@ -348,10 +378,9 @@ var WebStreamableHTTPServerTransport = class {
348
378
  "Conflict: SSE stream already exists for this session"
349
379
  );
350
380
  }
351
- const { readable, writable } = new TransformStream();
352
- const writer = writable.getWriter();
353
- this.trackStream(this._standaloneSseStreamId, writer);
354
- return new Response(readable, {
381
+ const stream = createSSEStream();
382
+ this.trackStream(this._standaloneSseStreamId, stream);
383
+ return new Response(stream.readable, {
355
384
  headers: {
356
385
  ...this.getResponseHeaders(),
357
386
  "Cache-Control": "no-cache, no-transform",
@@ -510,9 +539,9 @@ var WebStreamableHTTPServerTransport = class {
510
539
  });
511
540
  });
512
541
  } else {
513
- const { readable, writable } = new TransformStream();
514
- this.trackStream(streamId, writable.getWriter());
515
- sseReadable = readable;
542
+ const stream = createSSEStream();
543
+ this.trackStream(streamId, stream);
544
+ sseReadable = stream.readable;
516
545
  }
517
546
  }
518
547
  for (const message of messages) {
@@ -572,8 +601,12 @@ var WebStreamableHTTPServerTransport = class {
572
601
  "Resumability not supported"
573
602
  );
574
603
  }
604
+ const generation = this._streamGeneration;
575
605
  if (this._eventStore.getStreamIdForEventId) {
576
606
  const existingStreamId = await this._eventStore.getStreamIdForEventId(lastEventId);
607
+ if (this._streamGeneration !== generation) {
608
+ return this.createErrorResponse(404, -32001, "Session not found");
609
+ }
577
610
  if (existingStreamId !== void 0 && this._streamMapping.has(existingStreamId)) {
578
611
  return this.createErrorResponse(
579
612
  409,
@@ -583,10 +616,9 @@ var WebStreamableHTTPServerTransport = class {
583
616
  }
584
617
  }
585
618
  const eventStore = this._eventStore;
586
- const generation = this._streamGeneration;
587
- const { readable, writable } = new TransformStream();
588
- const writer = writable.getWriter();
589
- this._replaysInFlight += 1;
619
+ const stream = createSSEStream();
620
+ const { readable, writer } = stream;
621
+ this._replaysInFlight.add(stream);
590
622
  void (async () => {
591
623
  try {
592
624
  const streamId = await eventStore.replayEventsAfter(lastEventId, {
@@ -595,7 +627,6 @@ var WebStreamableHTTPServerTransport = class {
595
627
  }
596
628
  });
597
629
  if (this._streamGeneration !== generation) {
598
- await this.closeQuietly(writer);
599
630
  return;
600
631
  }
601
632
  if (this._streamMapping.has(streamId)) {
@@ -607,14 +638,14 @@ var WebStreamableHTTPServerTransport = class {
607
638
  await this.closeQuietly(writer);
608
639
  return;
609
640
  }
610
- this.trackStream(streamId, writer);
641
+ this.trackStream(streamId, stream);
611
642
  } catch (error) {
612
643
  this.onerror?.(
613
644
  error instanceof Error ? error : new Error(String(error))
614
645
  );
615
646
  await this.closeQuietly(writer);
616
647
  } finally {
617
- this._replaysInFlight -= 1;
648
+ this._replaysInFlight.delete(stream);
618
649
  }
619
650
  })().catch(() => {
620
651
  });
@@ -661,11 +692,11 @@ var WebStreamableHTTPServerTransport = class {
661
692
  if (streamInUse) {
662
693
  return;
663
694
  }
664
- const writer = this._streamMapping.get(streamId);
695
+ const stream = this._streamMapping.get(streamId);
665
696
  this._streamMapping.delete(streamId);
666
- if (writer) {
697
+ if (stream) {
667
698
  try {
668
- await writer.close();
699
+ await stream.terminate();
669
700
  } catch (error) {
670
701
  this.onerror?.(
671
702
  error instanceof Error ? error : new Error(String(error))
@@ -738,7 +769,7 @@ var WebStreamableHTTPServerTransport = class {
738
769
  return this._standaloneStreamOpened || // A live writer means the stream is open now, which is stronger evidence
739
770
  // than the latch. Belt and braces: it keeps a writer reaching this point
740
771
  // without `trackStream` from producing an SSE event with no `id:`.
741
- writer !== void 0 || this._replaysInFlight > 0;
772
+ writer !== void 0 || this._replaysInFlight.size > 0;
742
773
  }
743
774
  /**
744
775
  * Close and forget every stream of the current session and release whatever
@@ -748,24 +779,25 @@ var WebStreamableHTTPServerTransport = class {
748
779
  */
749
780
  async teardownStreams() {
750
781
  this._streamGeneration += 1;
751
- const writers = [...this._streamMapping.values()];
782
+ const streams = [...this._streamMapping.values(), ...this._replaysInFlight];
783
+ this._replaysInFlight.clear();
752
784
  this._streamMapping.clear();
753
785
  this._standaloneStreamOpened = false;
754
786
  this.releasePendingRequests();
755
- for (const writer of writers) {
787
+ for (const stream of streams) {
756
788
  try {
757
- await writer.close();
789
+ await stream.terminate();
758
790
  } catch {
759
791
  }
760
792
  }
761
793
  }
762
- trackStream(streamId, writer) {
794
+ trackStream(streamId, stream) {
763
795
  if (streamId === this._standaloneSseStreamId) {
764
796
  this._standaloneStreamOpened = true;
765
797
  }
766
- this._streamMapping.set(streamId, writer);
798
+ this._streamMapping.set(streamId, stream);
767
799
  const removeStreamIfCurrent = () => {
768
- if (this._streamMapping.get(streamId) !== writer) {
800
+ if (this._streamMapping.get(streamId) !== stream) {
769
801
  return;
770
802
  }
771
803
  this._streamMapping.delete(streamId);
@@ -778,7 +810,10 @@ var WebStreamableHTTPServerTransport = class {
778
810
  }
779
811
  }
780
812
  };
781
- void writer.closed.then(removeStreamIfCurrent, removeStreamIfCurrent);
813
+ void stream.writer.closed.then(
814
+ removeStreamIfCurrent,
815
+ removeStreamIfCurrent
816
+ );
782
817
  }
783
818
  /**
784
819
  * Write an SSE event to the stream