fastmcp 4.27.0 → 4.28.1

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.
Files changed (43) hide show
  1. package/dist/FastMCP.cjs +4 -4
  2. package/dist/FastMCP.d.cts +2 -2
  3. package/dist/FastMCP.d.ts +2 -2
  4. package/dist/FastMCP.js +3 -3
  5. package/dist/{OAuthProvider-CUfrAKTR.d.cts → OAuthProvider-DvIxa9N1.d.cts} +8 -5
  6. package/dist/{OAuthProvider-CUfrAKTR.d.ts → OAuthProvider-DvIxa9N1.d.ts} +8 -5
  7. package/dist/auth/index.cjs +2 -2
  8. package/dist/auth/index.d.cts +2 -2
  9. package/dist/auth/index.d.ts +2 -2
  10. package/dist/auth/index.js +1 -1
  11. package/dist/{chunk-SQ5JIDOQ.cjs → chunk-2ABBQFJB.cjs} +28 -13
  12. package/dist/chunk-2ABBQFJB.cjs.map +1 -0
  13. package/dist/chunk-2NZFMD6I.cjs +120 -0
  14. package/dist/chunk-2NZFMD6I.cjs.map +1 -0
  15. package/dist/{chunk-PKJVR5H3.cjs → chunk-3FM67CFK.cjs} +58 -49
  16. package/dist/chunk-3FM67CFK.cjs.map +1 -0
  17. package/dist/chunk-AM3HVK4Z.js +120 -0
  18. package/dist/chunk-AM3HVK4Z.js.map +1 -0
  19. package/dist/{chunk-TCXNF2SR.js → chunk-NRATQX66.js} +22 -7
  20. package/dist/chunk-NRATQX66.js.map +1 -0
  21. package/dist/{chunk-6L7ORLPC.js → chunk-PWSYODM5.js} +49 -40
  22. package/dist/chunk-PWSYODM5.js.map +1 -0
  23. package/dist/edge/index.cjs +114 -65
  24. package/dist/edge/index.cjs.map +1 -1
  25. package/dist/edge/index.js +90 -41
  26. package/dist/edge/index.js.map +1 -1
  27. package/dist/examples/custom-routes.cjs +4 -4
  28. package/dist/examples/custom-routes.js +3 -3
  29. package/dist/openapi/index.cjs +26 -15
  30. package/dist/openapi/index.cjs.map +1 -1
  31. package/dist/openapi/index.d.cts +4 -4
  32. package/dist/openapi/index.d.ts +4 -4
  33. package/dist/openapi/index.js +20 -9
  34. package/dist/openapi/index.js.map +1 -1
  35. package/package.json +1 -1
  36. package/dist/chunk-6L7ORLPC.js.map +0 -1
  37. package/dist/chunk-6TDYSPLC.cjs +0 -44
  38. package/dist/chunk-6TDYSPLC.cjs.map +0 -1
  39. package/dist/chunk-OKKFL2M4.js +0 -44
  40. package/dist/chunk-OKKFL2M4.js.map +0 -1
  41. package/dist/chunk-PKJVR5H3.cjs.map +0 -1
  42. package/dist/chunk-SQ5JIDOQ.cjs.map +0 -1
  43. package/dist/chunk-TCXNF2SR.js.map +0 -1
@@ -1,13 +1,14 @@
1
1
  import {
2
2
  strictInputSchema,
3
3
  zodInputSchema
4
- } from "../chunk-OKKFL2M4.js";
4
+ } from "../chunk-AM3HVK4Z.js";
5
5
 
6
6
  // src/edge/index.ts
7
7
  import {
8
8
  ErrorCode as ErrorCode2,
9
9
  isInitializeRequest as isInitializeRequest2,
10
10
  LATEST_PROTOCOL_VERSION,
11
+ McpError,
11
12
  SUPPORTED_PROTOCOL_VERSIONS as SUPPORTED_PROTOCOL_VERSIONS2
12
13
  } from "@modelcontextprotocol/sdk/types.js";
13
14
  import { Hono } from "hono";
@@ -32,6 +33,39 @@ var unsupportedProtocolVersion = (request) => {
32
33
  }
33
34
  return `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`;
34
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
+ };
35
69
  var MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024;
36
70
  var readBodyWithLimit = async (request) => {
37
71
  if (!request.body) {
@@ -81,7 +115,7 @@ var WebStreamableHTTPServerTransport = class {
81
115
  * send during that window has nothing to test against; persist rather than
82
116
  * drop, since over-storing for the length of a replay beats losing an event.
83
117
  */
84
- _replaysInFlight = 0;
118
+ _replaysInFlight = /* @__PURE__ */ new Set();
85
119
  _requestToStreamMapping = /* @__PURE__ */ new Map();
86
120
  _sessionClosing = false;
87
121
  /**
@@ -179,7 +213,7 @@ var WebStreamableHTTPServerTransport = class {
179
213
  }
180
214
  return;
181
215
  }
182
- const writer = this._streamMapping.get(streamId);
216
+ const writer = this._streamMapping.get(streamId)?.writer;
183
217
  const store = this.shouldPersist(streamId, writer) ? this._eventStore : void 0;
184
218
  if (writer || store) {
185
219
  try {
@@ -199,21 +233,18 @@ var WebStreamableHTTPServerTransport = class {
199
233
  error instanceof Error ? error : new Error(String(error))
200
234
  );
201
235
  }
202
- if (requestId !== void 0 && isResponse) {
236
+ if (requestId !== void 0 && isResponse && this._requestToStreamMapping.get(requestId) === streamId) {
203
237
  this._requestToStreamMapping.delete(requestId);
204
238
  const streamInUse = Array.from(
205
239
  this._requestToStreamMapping.values()
206
240
  ).includes(streamId);
207
- if (!streamInUse) {
208
- this._streamMapping.delete(streamId);
209
- if (writer) {
210
- try {
211
- await writer.close();
212
- } catch (error) {
213
- this.onerror?.(
214
- error instanceof Error ? error : new Error(String(error))
215
- );
216
- }
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
+ );
217
248
  }
218
249
  }
219
250
  }
@@ -347,10 +378,9 @@ var WebStreamableHTTPServerTransport = class {
347
378
  "Conflict: SSE stream already exists for this session"
348
379
  );
349
380
  }
350
- const { readable, writable } = new TransformStream();
351
- const writer = writable.getWriter();
352
- this.trackStream(this._standaloneSseStreamId, writer);
353
- return new Response(readable, {
381
+ const stream = createSSEStream();
382
+ this.trackStream(this._standaloneSseStreamId, stream);
383
+ return new Response(stream.readable, {
354
384
  headers: {
355
385
  ...this.getResponseHeaders(),
356
386
  "Cache-Control": "no-cache, no-transform",
@@ -509,9 +539,9 @@ var WebStreamableHTTPServerTransport = class {
509
539
  });
510
540
  });
511
541
  } else {
512
- const { readable, writable } = new TransformStream();
513
- this.trackStream(streamId, writable.getWriter());
514
- sseReadable = readable;
542
+ const stream = createSSEStream();
543
+ this.trackStream(streamId, stream);
544
+ sseReadable = stream.readable;
515
545
  }
516
546
  }
517
547
  for (const message of messages) {
@@ -571,8 +601,12 @@ var WebStreamableHTTPServerTransport = class {
571
601
  "Resumability not supported"
572
602
  );
573
603
  }
604
+ const generation = this._streamGeneration;
574
605
  if (this._eventStore.getStreamIdForEventId) {
575
606
  const existingStreamId = await this._eventStore.getStreamIdForEventId(lastEventId);
607
+ if (this._streamGeneration !== generation) {
608
+ return this.createErrorResponse(404, -32001, "Session not found");
609
+ }
576
610
  if (existingStreamId !== void 0 && this._streamMapping.has(existingStreamId)) {
577
611
  return this.createErrorResponse(
578
612
  409,
@@ -582,10 +616,9 @@ var WebStreamableHTTPServerTransport = class {
582
616
  }
583
617
  }
584
618
  const eventStore = this._eventStore;
585
- const generation = this._streamGeneration;
586
- const { readable, writable } = new TransformStream();
587
- const writer = writable.getWriter();
588
- this._replaysInFlight += 1;
619
+ const stream = createSSEStream();
620
+ const { readable, writer } = stream;
621
+ this._replaysInFlight.add(stream);
589
622
  void (async () => {
590
623
  try {
591
624
  const streamId = await eventStore.replayEventsAfter(lastEventId, {
@@ -594,7 +627,6 @@ var WebStreamableHTTPServerTransport = class {
594
627
  }
595
628
  });
596
629
  if (this._streamGeneration !== generation) {
597
- await this.closeQuietly(writer);
598
630
  return;
599
631
  }
600
632
  if (this._streamMapping.has(streamId)) {
@@ -606,14 +638,14 @@ var WebStreamableHTTPServerTransport = class {
606
638
  await this.closeQuietly(writer);
607
639
  return;
608
640
  }
609
- this.trackStream(streamId, writer);
641
+ this.trackStream(streamId, stream);
610
642
  } catch (error) {
611
643
  this.onerror?.(
612
644
  error instanceof Error ? error : new Error(String(error))
613
645
  );
614
646
  await this.closeQuietly(writer);
615
647
  } finally {
616
- this._replaysInFlight -= 1;
648
+ this._replaysInFlight.delete(stream);
617
649
  }
618
650
  })().catch(() => {
619
651
  });
@@ -660,11 +692,11 @@ var WebStreamableHTTPServerTransport = class {
660
692
  if (streamInUse) {
661
693
  return;
662
694
  }
663
- const writer = this._streamMapping.get(streamId);
695
+ const stream = this._streamMapping.get(streamId);
664
696
  this._streamMapping.delete(streamId);
665
- if (writer) {
697
+ if (stream) {
666
698
  try {
667
- await writer.close();
699
+ await stream.terminate();
668
700
  } catch (error) {
669
701
  this.onerror?.(
670
702
  error instanceof Error ? error : new Error(String(error))
@@ -737,7 +769,7 @@ var WebStreamableHTTPServerTransport = class {
737
769
  return this._standaloneStreamOpened || // A live writer means the stream is open now, which is stronger evidence
738
770
  // than the latch. Belt and braces: it keeps a writer reaching this point
739
771
  // without `trackStream` from producing an SSE event with no `id:`.
740
- writer !== void 0 || this._replaysInFlight > 0;
772
+ writer !== void 0 || this._replaysInFlight.size > 0;
741
773
  }
742
774
  /**
743
775
  * Close and forget every stream of the current session and release whatever
@@ -747,24 +779,25 @@ var WebStreamableHTTPServerTransport = class {
747
779
  */
748
780
  async teardownStreams() {
749
781
  this._streamGeneration += 1;
750
- const writers = [...this._streamMapping.values()];
782
+ const streams = [...this._streamMapping.values(), ...this._replaysInFlight];
783
+ this._replaysInFlight.clear();
751
784
  this._streamMapping.clear();
752
785
  this._standaloneStreamOpened = false;
753
786
  this.releasePendingRequests();
754
- for (const writer of writers) {
787
+ for (const stream of streams) {
755
788
  try {
756
- await writer.close();
789
+ await stream.terminate();
757
790
  } catch {
758
791
  }
759
792
  }
760
793
  }
761
- trackStream(streamId, writer) {
794
+ trackStream(streamId, stream) {
762
795
  if (streamId === this._standaloneSseStreamId) {
763
796
  this._standaloneStreamOpened = true;
764
797
  }
765
- this._streamMapping.set(streamId, writer);
798
+ this._streamMapping.set(streamId, stream);
766
799
  const removeStreamIfCurrent = () => {
767
- if (this._streamMapping.get(streamId) !== writer) {
800
+ if (this._streamMapping.get(streamId) !== stream) {
768
801
  return;
769
802
  }
770
803
  this._streamMapping.delete(streamId);
@@ -777,7 +810,10 @@ var WebStreamableHTTPServerTransport = class {
777
810
  }
778
811
  }
779
812
  };
780
- void writer.closed.then(removeStreamIfCurrent, removeStreamIfCurrent);
813
+ void stream.writer.closed.then(
814
+ removeStreamIfCurrent,
815
+ removeStreamIfCurrent
816
+ );
781
817
  }
782
818
  /**
783
819
  * Write an SSE event to the stream
@@ -1102,6 +1138,13 @@ var EdgeFastMCP = class {
1102
1138
  async #handlePromptsGet(id, params) {
1103
1139
  const promptName = params?.name;
1104
1140
  const promptArgs = params?.arguments;
1141
+ if (promptArgs !== void 0 && (promptArgs === null || typeof promptArgs !== "object" || Array.isArray(promptArgs) || Object.values(promptArgs).some((value) => typeof value !== "string"))) {
1142
+ return this.#rpcError(
1143
+ id,
1144
+ ErrorCode2.InvalidParams,
1145
+ "Prompt arguments must be an object with string values"
1146
+ );
1147
+ }
1105
1148
  const prompt = this.#prompts.find((p) => p.name === promptName);
1106
1149
  if (!prompt) {
1107
1150
  return this.#rpcError(
@@ -1128,6 +1171,9 @@ var EdgeFastMCP = class {
1128
1171
  result: { messages }
1129
1172
  };
1130
1173
  } catch (error) {
1174
+ if (error instanceof McpError) {
1175
+ return this.#rpcError(id, error.code, error.message, error.data);
1176
+ }
1131
1177
  return this.#rpcError(
1132
1178
  id,
1133
1179
  ErrorCode2.InternalError,
@@ -1194,6 +1240,9 @@ var EdgeFastMCP = class {
1194
1240
  result: { contents: [content] }
1195
1241
  };
1196
1242
  } catch (error) {
1243
+ if (error instanceof McpError) {
1244
+ return this.#rpcError(id, error.code, error.message, error.data);
1245
+ }
1197
1246
  return this.#rpcError(
1198
1247
  id,
1199
1248
  ErrorCode2.InternalError,
@@ -1300,9 +1349,9 @@ var EdgeFastMCP = class {
1300
1349
  /**
1301
1350
  * Create an RPC error message
1302
1351
  */
1303
- #rpcError(id, code, message) {
1352
+ #rpcError(id, code, message, data) {
1304
1353
  return {
1305
- error: { code, message },
1354
+ error: { code, ...data !== void 0 && { data }, message },
1306
1355
  id,
1307
1356
  jsonrpc: "2.0"
1308
1357
  };