fastmcp 4.30.1 → 4.30.3

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 (39) 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-C2Z93xC5.d.cts → OAuthProvider-BuBMMpsX.d.cts} +18 -2
  6. package/dist/{OAuthProvider-C2Z93xC5.d.ts → OAuthProvider-BuBMMpsX.d.ts} +18 -2
  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-CZGQ2WTQ.cjs → chunk-F7XD3S4L.cjs} +54 -7
  12. package/dist/chunk-F7XD3S4L.cjs.map +1 -0
  13. package/dist/{chunk-4JEDH2ON.cjs → chunk-M3SNOHIM.cjs} +156 -85
  14. package/dist/chunk-M3SNOHIM.cjs.map +1 -0
  15. package/dist/{chunk-FBDPX4H2.cjs → chunk-NYV5TK43.cjs} +125 -2
  16. package/dist/chunk-NYV5TK43.cjs.map +1 -0
  17. package/dist/{chunk-XPTUOWUT.js → chunk-P4ZFER5I.js} +50 -3
  18. package/dist/chunk-P4ZFER5I.js.map +1 -0
  19. package/dist/{chunk-5KA6SOWT.js → chunk-UEEX3RD5.js} +109 -38
  20. package/dist/chunk-UEEX3RD5.js.map +1 -0
  21. package/dist/{chunk-JCKJRUCE.js → chunk-YEXAT4PO.js} +125 -2
  22. package/dist/chunk-YEXAT4PO.js.map +1 -0
  23. package/dist/edge/index.cjs +4 -4
  24. package/dist/edge/index.js +1 -1
  25. package/dist/examples/custom-routes.cjs +4 -4
  26. package/dist/examples/custom-routes.js +3 -3
  27. package/dist/openapi/index.cjs +15 -12
  28. package/dist/openapi/index.cjs.map +1 -1
  29. package/dist/openapi/index.d.cts +4 -3
  30. package/dist/openapi/index.d.ts +4 -3
  31. package/dist/openapi/index.js +9 -6
  32. package/dist/openapi/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/dist/chunk-4JEDH2ON.cjs.map +0 -1
  35. package/dist/chunk-5KA6SOWT.js.map +0 -1
  36. package/dist/chunk-CZGQ2WTQ.cjs.map +0 -1
  37. package/dist/chunk-FBDPX4H2.cjs.map +0 -1
  38. package/dist/chunk-JCKJRUCE.js.map +0 -1
  39. package/dist/chunk-XPTUOWUT.js.map +0 -1
@@ -1,11 +1,12 @@
1
1
  import {
2
2
  cancelResponseBody
3
- } from "./chunk-XPTUOWUT.js";
3
+ } from "./chunk-P4ZFER5I.js";
4
4
  import {
5
5
  objectRootSchema,
6
6
  strictInputSchema,
7
- zodInputSchema
8
- } from "./chunk-JCKJRUCE.js";
7
+ zodInputSchema,
8
+ zodOutputSchema
9
+ } from "./chunk-YEXAT4PO.js";
9
10
 
10
11
  // src/FastMCP.ts
11
12
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
@@ -1029,7 +1030,7 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1029
1030
  streamableHttp ? { timeout: 1e3 } : void 0
1030
1031
  );
1031
1032
  this.#roots = roots?.roots || [];
1032
- if (streamableHttp) {
1033
+ if (streamableHttp || this.#connectionState === "ready") {
1033
1034
  this.emit("rootsChanged", { roots: this.#roots });
1034
1035
  }
1035
1036
  return;
@@ -1101,6 +1102,28 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1101
1102
  );
1102
1103
  });
1103
1104
  }
1105
+ /**
1106
+ * A gateway can start a stdio server long before any client connects and
1107
+ * forward the first initialize only then, after #setUpSession stopped
1108
+ * waiting for it. Finish the setup when that client initializes.
1109
+ */
1110
+ #setUpLateClient(streamableHttp) {
1111
+ const finish = () => {
1112
+ const capabilities = this.#server.getClientCapabilities();
1113
+ if (!capabilities || this.#clientCapabilities || this.#connectionState === "closed") {
1114
+ return;
1115
+ }
1116
+ this.#clientCapabilities = capabilities;
1117
+ void this.#loadRoots(streamableHttp);
1118
+ this.#startPing();
1119
+ };
1120
+ const oninitialized = this.#server.oninitialized;
1121
+ this.#server.oninitialized = () => {
1122
+ oninitialized?.();
1123
+ finish();
1124
+ };
1125
+ finish();
1126
+ }
1104
1127
  async #setUpSession(streamableHttp) {
1105
1128
  if (!this.#stateless) {
1106
1129
  let attempt = 0;
@@ -1135,38 +1158,44 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1135
1158
  }
1136
1159
  }
1137
1160
  if (this.#clientCapabilities) {
1138
- const pingConfig = this.#getPingConfig();
1139
- if (pingConfig.enabled) {
1140
- this.#pingInterval = setInterval(async () => {
1141
- if (this.#pingInFlight) {
1142
- return;
1143
- }
1144
- this.#pingInFlight = true;
1145
- try {
1146
- await this.#server.ping();
1147
- } catch {
1148
- const logLevel = pingConfig.logLevel;
1149
- if (logLevel === "debug") {
1150
- this.#logger.debug("[FastMCP debug] server ping failed");
1151
- } else if (logLevel === "warning") {
1152
- this.#logger.warn(
1153
- "[FastMCP warning] server is not responding to ping"
1154
- );
1155
- } else if (logLevel === "error") {
1156
- this.#logger.error(
1157
- "[FastMCP error] server is not responding to ping"
1158
- );
1159
- } else {
1160
- this.#logger.info("[FastMCP info] server ping failed");
1161
- }
1162
- } finally {
1163
- this.#pingInFlight = false;
1164
- }
1165
- }, pingConfig.intervalMs);
1166
- }
1161
+ this.#startPing();
1167
1162
  }
1168
1163
  this.#connectionState = "ready";
1169
1164
  this.emit("ready");
1165
+ if (!this.#stateless && !this.#clientCapabilities) {
1166
+ this.#setUpLateClient(streamableHttp);
1167
+ }
1168
+ }
1169
+ #startPing() {
1170
+ const pingConfig = this.#getPingConfig();
1171
+ if (pingConfig.enabled) {
1172
+ this.#pingInterval = setInterval(async () => {
1173
+ if (this.#pingInFlight) {
1174
+ return;
1175
+ }
1176
+ this.#pingInFlight = true;
1177
+ try {
1178
+ await this.#server.ping();
1179
+ } catch {
1180
+ const logLevel = pingConfig.logLevel;
1181
+ if (logLevel === "debug") {
1182
+ this.#logger.debug("[FastMCP debug] server ping failed");
1183
+ } else if (logLevel === "warning") {
1184
+ this.#logger.warn(
1185
+ "[FastMCP warning] server is not responding to ping"
1186
+ );
1187
+ } else if (logLevel === "error") {
1188
+ this.#logger.error(
1189
+ "[FastMCP error] server is not responding to ping"
1190
+ );
1191
+ } else {
1192
+ this.#logger.info("[FastMCP info] server ping failed");
1193
+ }
1194
+ } finally {
1195
+ this.#pingInFlight = false;
1196
+ }
1197
+ }, pingConfig.intervalMs);
1198
+ }
1170
1199
  }
1171
1200
  /**
1172
1201
  * Periodically writes to the response stream of an in-flight tool call, so an
@@ -1679,7 +1708,7 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1679
1708
  if (tool.outputSchema) {
1680
1709
  try {
1681
1710
  outputSchema = objectRootSchema(
1682
- await toJsonSchema(tool.outputSchema)
1711
+ tool.outputSchema["~standard"].vendor === "zod" && "_zod" in tool.outputSchema ? zodOutputSchema(tool.outputSchema, "draft-7") : await toJsonSchema(tool.outputSchema)
1683
1712
  );
1684
1713
  } catch (error) {
1685
1714
  this.#logger.warn(
@@ -2264,6 +2293,12 @@ var FastMCP = class extends FastMCPEventEmitter {
2264
2293
  #resources = [];
2265
2294
  #resourcesTemplates = [];
2266
2295
  #serverState = "stopped" /* Stopped */;
2296
+ /**
2297
+ * What each session was built from, until it is listed in `#sessions`. Only
2298
+ * listed sessions hear about a registration, so one made while a session is
2299
+ * still connecting is passed on when it is listed.
2300
+ */
2301
+ #sessionRegistrations = /* @__PURE__ */ new WeakMap();
2267
2302
  #sessions = [];
2268
2303
  #stdioSessions = /* @__PURE__ */ new Set();
2269
2304
  #tools = [];
@@ -2617,6 +2652,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2617
2652
  version: this.#options.version,
2618
2653
  websiteUrl: this.#options.websiteUrl
2619
2654
  });
2655
+ this.#sessionRegistrations.set(session, this.#registrations());
2620
2656
  let stdinClosed = false;
2621
2657
  const onStdinClose = () => {
2622
2658
  if (stdinClosed) return;
@@ -2751,7 +2787,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2751
2787
  });
2752
2788
  },
2753
2789
  onConnect: async (session) => {
2754
- this.#sessions.push(session);
2790
+ this.#addSession(session);
2755
2791
  this.#logger.info(`[FastMCP info] HTTP Stream session established`);
2756
2792
  this.emit("connect", {
2757
2793
  session
@@ -2799,6 +2835,31 @@ var FastMCP = class extends FastMCPEventEmitter {
2799
2835
  }
2800
2836
  this.#serverState = "stopped" /* Stopped */;
2801
2837
  }
2838
+ /**
2839
+ * Lists a connected session in `sessions`, first passing on whatever was
2840
+ * registered or removed since it was built.
2841
+ */
2842
+ #addSession(session) {
2843
+ this.#sessions.push(session);
2844
+ const built = this.#sessionRegistrations.get(session);
2845
+ const current = this.#registrations();
2846
+ this.#sessionRegistrations.delete(session);
2847
+ if (!built) {
2848
+ return;
2849
+ }
2850
+ if (built.tools !== current.tools) {
2851
+ session.toolsListChanged(current.tools);
2852
+ }
2853
+ if (built.resources !== current.resources) {
2854
+ session.resourcesListChanged(current.resources);
2855
+ }
2856
+ if (built.resourcesTemplates !== current.resourcesTemplates) {
2857
+ session.resourceTemplatesListChanged(current.resourcesTemplates);
2858
+ }
2859
+ if (built.prompts !== current.prompts) {
2860
+ session.promptsListChanged(current.prompts);
2861
+ }
2862
+ }
2802
2863
  /**
2803
2864
  * mcp-proxy's `beforeDispatch`: answers a `tools/call` the session's token
2804
2865
  * lacks scopes for with MCP's 403 scope challenge. It runs for every message
@@ -2834,7 +2895,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2834
2895
  if (closed) {
2835
2896
  return;
2836
2897
  }
2837
- this.#sessions.push(session);
2898
+ this.#addSession(session);
2838
2899
  session.once("error", () => {
2839
2900
  this.#removeSession(session);
2840
2901
  });
@@ -2900,7 +2961,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2900
2961
  throw this.#createUnauthorizedResponse(errorMessage);
2901
2962
  }
2902
2963
  const authRequired = this.#authenticate !== void 0;
2903
- return new FastMCPSession({
2964
+ const session = new FastMCPSession({
2904
2965
  auth,
2905
2966
  authRequired,
2906
2967
  enableJsonResponse,
@@ -2925,6 +2986,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2925
2986
  version: this.#options.version,
2926
2987
  websiteUrl: this.#options.websiteUrl
2927
2988
  });
2989
+ this.#sessionRegistrations.set(session, this.#registrations());
2990
+ return session;
2928
2991
  }
2929
2992
  /**
2930
2993
  * Builds a 401 Unauthorized HTTP Response for authentication failures.
@@ -3607,6 +3670,14 @@ var FastMCP = class extends FastMCPEventEmitter {
3607
3670
  session.promptsListChanged(prompts);
3608
3671
  }
3609
3672
  }
3673
+ #registrations() {
3674
+ return {
3675
+ prompts: this.#prompts,
3676
+ resources: this.#resources,
3677
+ resourcesTemplates: this.#resourcesTemplates,
3678
+ tools: this.#tools
3679
+ };
3680
+ }
3610
3681
  #removeSession(session) {
3611
3682
  const sessionIndex = this.#sessions.indexOf(session);
3612
3683
  if (sessionIndex !== -1) {
@@ -3699,4 +3770,4 @@ export {
3699
3770
  FastMCPSession,
3700
3771
  FastMCP
3701
3772
  };
3702
- //# sourceMappingURL=chunk-5KA6SOWT.js.map
3773
+ //# sourceMappingURL=chunk-UEEX3RD5.js.map