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: ${_typesjs.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 = 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
- __init4() {this._replaysInFlight = 0}
118
+ __init4() {this._replaysInFlight = /* @__PURE__ */ new Set()}
86
119
  __init5() {this._requestToStreamMapping = /* @__PURE__ */ new Map()}
87
120
  __init6() {this._sessionClosing = false}
88
121
  /**
@@ -180,7 +213,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
180
213
  }
181
214
  return;
182
215
  }
183
- const writer = this._streamMapping.get(streamId);
216
+ const writer = _optionalChain([this, 'access', _4 => _4._streamMapping, 'access', _5 => _5.get, 'call', _6 => _6(streamId), 'optionalAccess', _7 => _7.writer]);
184
217
  const store = this.shouldPersist(streamId, writer) ? this._eventStore : void 0;
185
218
  if (writer || store) {
186
219
  try {
@@ -196,25 +229,22 @@ var WebStreamableHTTPServerTransport = (_class = class {
196
229
  }
197
230
  }
198
231
  } catch (error) {
199
- _optionalChain([this, 'access', _4 => _4.onerror, 'optionalCall', _5 => _5(
232
+ _optionalChain([this, 'access', _8 => _8.onerror, 'optionalCall', _9 => _9(
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
- _optionalChain([this, 'access', _6 => _6.onerror, 'optionalCall', _7 => _7(
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
+ _optionalChain([this, 'access', _10 => _10.onerror, 'optionalCall', _11 => _11(
246
+ error instanceof Error ? error : new Error(String(error))
247
+ )]);
218
248
  }
219
249
  }
220
250
  }
@@ -299,7 +329,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
299
329
  this.sessionId = void 0;
300
330
  try {
301
331
  await this.teardownStreams();
302
- await _optionalChain([this, 'access', _8 => _8._onsessionclosed, 'optionalCall', _9 => _9(closedSessionId)]);
332
+ await _optionalChain([this, 'access', _12 => _12._onsessionclosed, 'optionalCall', _13 => _13(closedSessionId)]);
303
333
  } finally {
304
334
  this._sessionClosing = false;
305
335
  }
@@ -313,7 +343,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
313
343
  */
314
344
  async handleGetRequest(request) {
315
345
  const acceptHeader = request.headers.get("accept");
316
- if (!_optionalChain([acceptHeader, 'optionalAccess', _10 => _10.includes, 'call', _11 => _11("text/event-stream")])) {
346
+ if (!_optionalChain([acceptHeader, 'optionalAccess', _14 => _14.includes, 'call', _15 => _15("text/event-stream")])) {
317
347
  return this.createErrorResponse(
318
348
  406,
319
349
  -32e3,
@@ -348,10 +378,9 @@ var WebStreamableHTTPServerTransport = (_class = 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",
@@ -366,7 +395,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
366
395
  */
367
396
  async handlePostRequest(request, parsedBody) {
368
397
  const acceptHeader = request.headers.get("accept");
369
- if (!_optionalChain([acceptHeader, 'optionalAccess', _12 => _12.includes, 'call', _13 => _13("application/json")]) && !_optionalChain([acceptHeader, 'optionalAccess', _14 => _14.includes, 'call', _15 => _15("text/event-stream")])) {
398
+ if (!_optionalChain([acceptHeader, 'optionalAccess', _16 => _16.includes, 'call', _17 => _17("application/json")]) && !_optionalChain([acceptHeader, 'optionalAccess', _18 => _18.includes, 'call', _19 => _19("text/event-stream")])) {
370
399
  return this.createErrorResponse(
371
400
  406,
372
401
  -32e3,
@@ -374,7 +403,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
374
403
  );
375
404
  }
376
405
  const contentType = request.headers.get("content-type");
377
- if (!_optionalChain([contentType, 'optionalAccess', _16 => _16.includes, 'call', _17 => _17("application/json")])) {
406
+ if (!_optionalChain([contentType, 'optionalAccess', _20 => _20.includes, 'call', _21 => _21("application/json")])) {
378
407
  return this.createErrorResponse(
379
408
  415,
380
409
  -32e3,
@@ -473,7 +502,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
473
502
  const sessionGeneration = ++this._sessionGeneration;
474
503
  this.sessionId = generatedSessionId;
475
504
  try {
476
- await _optionalChain([this, 'access', _18 => _18._onsessioninitialized, 'optionalCall', _19 => _19(generatedSessionId)]);
505
+ await _optionalChain([this, 'access', _22 => _22._onsessioninitialized, 'optionalCall', _23 => _23(generatedSessionId)]);
477
506
  if (this._sessionGeneration !== sessionGeneration || this.activeSessionId !== generatedSessionId) {
478
507
  throw new Error("Session initialization was superseded");
479
508
  }
@@ -493,7 +522,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
493
522
  return this.createErrorResponse(400, -32e3, unsupported);
494
523
  }
495
524
  const requestIds = messages.filter((msg) => _typesjs.isJSONRPCRequest.call(void 0, msg)).map((msg) => msg.id);
496
- const useJsonResponse = this._enableJsonResponse && Boolean(_optionalChain([acceptHeader, 'optionalAccess', _20 => _20.includes, 'call', _21 => _21("application/json")]));
525
+ const useJsonResponse = this._enableJsonResponse && Boolean(_optionalChain([acceptHeader, 'optionalAccess', _24 => _24.includes, 'call', _25 => _25("application/json")]));
497
526
  let jsonResponses;
498
527
  let sseReadable;
499
528
  if (requestIds.length > 0) {
@@ -510,9 +539,9 @@ var WebStreamableHTTPServerTransport = (_class = 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) {
@@ -523,7 +552,7 @@ var WebStreamableHTTPServerTransport = (_class = class {
523
552
  }
524
553
  const requestInfo = { headers: Object.fromEntries(request.headers) };
525
554
  for (const message of messages) {
526
- _optionalChain([this, 'access', _22 => _22.onmessage, 'optionalCall', _23 => _23(message, {
555
+ _optionalChain([this, 'access', _26 => _26.onmessage, 'optionalCall', _27 => _27(message, {
527
556
  authInfo: void 0,
528
557
  requestInfo
529
558
  })]);
@@ -572,8 +601,12 @@ var WebStreamableHTTPServerTransport = (_class = 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 = 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,11 +627,10 @@ var WebStreamableHTTPServerTransport = (_class = 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)) {
602
- _optionalChain([this, 'access', _24 => _24.onerror, 'optionalCall', _25 => _25(
633
+ _optionalChain([this, 'access', _28 => _28.onerror, 'optionalCall', _29 => _29(
603
634
  new Error(
604
635
  `Conflict: SSE stream already exists for replayed stream ${streamId}`
605
636
  )
@@ -607,14 +638,14 @@ var WebStreamableHTTPServerTransport = (_class = 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
- _optionalChain([this, 'access', _26 => _26.onerror, 'optionalCall', _27 => _27(
643
+ _optionalChain([this, 'access', _30 => _30.onerror, 'optionalCall', _31 => _31(
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,13 +692,13 @@ var WebStreamableHTTPServerTransport = (_class = 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
- _optionalChain([this, 'access', _28 => _28.onerror, 'optionalCall', _29 => _29(
701
+ _optionalChain([this, 'access', _32 => _32.onerror, 'optionalCall', _33 => _33(
671
702
  error instanceof Error ? error : new Error(String(error))
672
703
  )]);
673
704
  }
@@ -738,7 +769,7 @@ var WebStreamableHTTPServerTransport = (_class = 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 = 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 (e3) {
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 = 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
@@ -933,7 +968,7 @@ var EdgeFastMCP = class {
933
968
  * Handle initialize request
934
969
  */
935
970
  #handleInitialize(id, params) {
936
- const requestedVersion = _optionalChain([params, 'optionalAccess', _30 => _30.protocolVersion]);
971
+ const requestedVersion = _optionalChain([params, 'optionalAccess', _34 => _34.protocolVersion]);
937
972
  const protocolVersion = typeof requestedVersion === "string" && _typesjs.SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : _typesjs.LATEST_PROTOCOL_VERSION;
938
973
  return {
939
974
  id,
@@ -957,7 +992,7 @@ var EdgeFastMCP = class {
957
992
  */
958
993
  async #handleMcpRequest(request) {
959
994
  const acceptHeader = request.headers.get("accept");
960
- if (!_optionalChain([acceptHeader, 'optionalAccess', _31 => _31.includes, 'call', _32 => _32("application/json")]) && !_optionalChain([acceptHeader, 'optionalAccess', _33 => _33.includes, 'call', _34 => _34("text/event-stream")])) {
995
+ if (!_optionalChain([acceptHeader, 'optionalAccess', _35 => _35.includes, 'call', _36 => _36("application/json")]) && !_optionalChain([acceptHeader, 'optionalAccess', _37 => _37.includes, 'call', _38 => _38("text/event-stream")])) {
961
996
  return this.#errorResponse(
962
997
  406,
963
998
  -32e3,
@@ -965,7 +1000,7 @@ var EdgeFastMCP = class {
965
1000
  );
966
1001
  }
967
1002
  const contentType = request.headers.get("content-type");
968
- if (!_optionalChain([contentType, 'optionalAccess', _35 => _35.includes, 'call', _36 => _36("application/json")])) {
1003
+ if (!_optionalChain([contentType, 'optionalAccess', _39 => _39.includes, 'call', _40 => _40("application/json")])) {
969
1004
  return this.#errorResponse(
970
1005
  415,
971
1006
  -32e3,
@@ -1023,7 +1058,7 @@ var EdgeFastMCP = class {
1023
1058
  */
1024
1059
  async #handleMcpSseRequest(request) {
1025
1060
  const acceptHeader = request.headers.get("accept");
1026
- if (!_optionalChain([acceptHeader, 'optionalAccess', _37 => _37.includes, 'call', _38 => _38("text/event-stream")])) {
1061
+ if (!_optionalChain([acceptHeader, 'optionalAccess', _41 => _41.includes, 'call', _42 => _42("text/event-stream")])) {
1027
1062
  return this.#errorResponse(
1028
1063
  406,
1029
1064
  -32e3,
@@ -1101,8 +1136,8 @@ var EdgeFastMCP = class {
1101
1136
  * Handle prompts/get request
1102
1137
  */
1103
1138
  async #handlePromptsGet(id, params) {
1104
- const promptName = _optionalChain([params, 'optionalAccess', _39 => _39.name]);
1105
- const promptArgs = _optionalChain([params, 'optionalAccess', _40 => _40.arguments]);
1139
+ const promptName = _optionalChain([params, 'optionalAccess', _43 => _43.name]);
1140
+ const promptArgs = _optionalChain([params, 'optionalAccess', _44 => _44.arguments]);
1106
1141
  if (promptArgs !== void 0 && (promptArgs === null || typeof promptArgs !== "object" || Array.isArray(promptArgs) || Object.values(promptArgs).some((value) => typeof value !== "string"))) {
1107
1142
  return this.#rpcError(
1108
1143
  id,
@@ -1183,7 +1218,7 @@ var EdgeFastMCP = class {
1183
1218
  * Handle resources/read request
1184
1219
  */
1185
1220
  async #handleResourcesRead(id, params) {
1186
- const uri = _optionalChain([params, 'optionalAccess', _41 => _41.uri]);
1221
+ const uri = _optionalChain([params, 'optionalAccess', _45 => _45.uri]);
1187
1222
  const resource = this.#resources.find((r) => r.uri === uri);
1188
1223
  if (!resource) {
1189
1224
  return this.#rpcError(
@@ -1219,8 +1254,8 @@ var EdgeFastMCP = class {
1219
1254
  * Handle tools/call request
1220
1255
  */
1221
1256
  async #handleToolsCall(id, params) {
1222
- const toolName = _optionalChain([params, 'optionalAccess', _42 => _42.name]);
1223
- const toolArgs = _optionalChain([params, 'optionalAccess', _43 => _43.arguments]);
1257
+ const toolName = _optionalChain([params, 'optionalAccess', _46 => _46.name]);
1258
+ const toolArgs = _optionalChain([params, 'optionalAccess', _47 => _47.arguments]);
1224
1259
  const tool = this.#tools.find((t) => t.name === toolName);
1225
1260
  if (!tool) {
1226
1261
  return this.#rpcError(
@@ -1243,11 +1278,11 @@ var EdgeFastMCP = class {
1243
1278
  }
1244
1279
  if (parsed.issues) {
1245
1280
  const friendlyErrors = parsed.issues.map((issue) => {
1246
- const path = _optionalChain([issue, 'access', _44 => _44.path, 'optionalAccess', _45 => _45.map, 'call', _46 => _46(
1281
+ const path = _optionalChain([issue, 'access', _48 => _48.path, 'optionalAccess', _49 => _49.map, 'call', _50 => _50(
1247
1282
  (segment) => String(
1248
1283
  typeof segment === "object" && segment !== null ? segment.key : segment
1249
1284
  )
1250
- ), 'access', _47 => _47.join, 'call', _48 => _48(".")]) || "root";
1285
+ ), 'access', _51 => _51.join, 'call', _52 => _52(".")]) || "root";
1251
1286
  return `${path}: ${issue.message}`;
1252
1287
  }).join(", ");
1253
1288
  return {