fastmcp 4.22.8 → 4.22.10

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.
@@ -668,10 +668,6 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
668
668
  }
669
669
  }
670
670
  async close() {
671
- this.#connectionState = "closed";
672
- if (this.#pingInterval) {
673
- clearInterval(this.#pingInterval);
674
- }
675
671
  this.#abortSession();
676
672
  try {
677
673
  await this.#server.close();
@@ -828,11 +824,16 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
828
824
  });
829
825
  }
830
826
  /**
831
- * Cancels the `signal` held by every tool still executing on this session.
832
- * Idempotent, so the close path and the transport's own close handler can
833
- * both call it.
827
+ * Marks the session closed, stops pinging the client and cancels the
828
+ * `signal` held by every tool still executing on this session. Idempotent,
829
+ * so the close path and the transport's own close handler can both call it.
834
830
  */
835
831
  #abortSession() {
832
+ this.#connectionState = "closed";
833
+ if (this.#pingInterval) {
834
+ clearInterval(this.#pingInterval);
835
+ this.#pingInterval = null;
836
+ }
836
837
  if (!this.#abortController.signal.aborted) {
837
838
  this.#abortController.abort(new SessionError("Session closed"));
838
839
  }
@@ -1013,6 +1014,9 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1013
1014
  void this.#loadRoots(true);
1014
1015
  } else {
1015
1016
  await this.#loadRoots(false);
1017
+ if (this.#abortController.signal.aborted) {
1018
+ return;
1019
+ }
1016
1020
  }
1017
1021
  if (this.#clientCapabilities) {
1018
1022
  const pingConfig = this.#getPingConfig();
@@ -1775,6 +1779,35 @@ function parseBasicAuthHeader(authHeader) {
1775
1779
  return null;
1776
1780
  }
1777
1781
  }
1782
+ function parseBooleanSetting(value, source) {
1783
+ if (!value) {
1784
+ return void 0;
1785
+ }
1786
+ switch (value.toLowerCase()) {
1787
+ case "false":
1788
+ return false;
1789
+ case "true":
1790
+ return true;
1791
+ default:
1792
+ throw new Error(`${source} must be "true" or "false", got "${value}"`);
1793
+ }
1794
+ }
1795
+ function parseTransportSetting(value, source) {
1796
+ if (!value) {
1797
+ return void 0;
1798
+ }
1799
+ switch (value.toLowerCase()) {
1800
+ case "http-stream":
1801
+ case "httpstream":
1802
+ return "httpStream";
1803
+ case "stdio":
1804
+ return "stdio";
1805
+ default:
1806
+ throw new Error(
1807
+ `${source} must be "stdio", "httpStream" or "http-stream", got "${value}"`
1808
+ );
1809
+ }
1810
+ }
1778
1811
  function protectedResourceMetadataUrl(resource) {
1779
1812
  let url;
1780
1813
  try {
@@ -1972,7 +2005,8 @@ var FastMCP = class extends FastMCPEventEmitter {
1972
2005
  *
1973
2006
  * The transport's lifecycle belongs to the caller: `stop()` does not close
1974
2007
  * transports passed here. Close the returned session (or the transport) when
1975
- * you are done with it.
2008
+ * you are done with it. If the client hangs up before the session is set up,
2009
+ * the returned session is already closed and is not listed in `sessions`.
1976
2010
  *
1977
2011
  * @param transport - An already-constructed MCP server transport.
1978
2012
  * @param auth - Session auth, equivalent to what `authenticate` would return.
@@ -1980,21 +2014,7 @@ var FastMCP = class extends FastMCPEventEmitter {
1980
2014
  */
1981
2015
  async connect(transport, auth) {
1982
2016
  const session = this.#createSession(auth);
1983
- await session.connect(transport);
1984
- this.#sessions.push(session);
1985
- session.once("error", () => {
1986
- this.#removeSession(session);
1987
- });
1988
- const originalOnClose = transport.onclose;
1989
- transport.onclose = () => {
1990
- this.#removeSession(session);
1991
- if (originalOnClose) {
1992
- originalOnClose();
1993
- }
1994
- };
1995
- this.emit("connect", {
1996
- session
1997
- });
2017
+ await this.#connectSession(session, transport);
1998
2018
  this.#serverState = "running" /* Running */;
1999
2019
  return session;
2000
2020
  }
@@ -2232,7 +2252,6 @@ var FastMCP = class extends FastMCPEventEmitter {
2232
2252
  version: this.#options.version,
2233
2253
  websiteUrl: this.#options.websiteUrl
2234
2254
  });
2235
- await session.connect(transport);
2236
2255
  let stdinClosed = false;
2237
2256
  const onStdinClose = () => {
2238
2257
  if (stdinClosed) return;
@@ -2244,29 +2263,9 @@ var FastMCP = class extends FastMCPEventEmitter {
2244
2263
  };
2245
2264
  process.stdin.on("close", onStdinClose);
2246
2265
  process.stdin.on("end", onStdinClose);
2247
- this.#sessions.push(session);
2248
- session.once("error", () => {
2249
- this.#removeSession(session);
2250
- });
2251
- if (transport.onclose) {
2252
- const originalOnClose = transport.onclose;
2253
- transport.onclose = () => {
2254
- process.stdin.off("close", onStdinClose);
2255
- process.stdin.off("end", onStdinClose);
2256
- this.#removeSession(session);
2257
- if (originalOnClose) {
2258
- originalOnClose();
2259
- }
2260
- };
2261
- } else {
2262
- transport.onclose = () => {
2263
- process.stdin.off("close", onStdinClose);
2264
- process.stdin.off("end", onStdinClose);
2265
- this.#removeSession(session);
2266
- };
2267
- }
2268
- this.emit("connect", {
2269
- session
2266
+ await this.#connectSession(session, transport, () => {
2267
+ process.stdin.off("close", onStdinClose);
2268
+ process.stdin.off("end", onStdinClose);
2270
2269
  });
2271
2270
  this.#serverState = "running" /* Running */;
2272
2271
  } else if (config.transportType === "httpStream") {
@@ -2388,6 +2387,32 @@ var FastMCP = class extends FastMCPEventEmitter {
2388
2387
  }
2389
2388
  this.#serverState = "stopped" /* Stopped */;
2390
2389
  }
2390
+ /**
2391
+ * Connects `session` to `transport` and lists it in `sessions` until the
2392
+ * transport closes. The close is watched from before the session connects,
2393
+ * so a client that hangs up during setup leaves no session behind.
2394
+ */
2395
+ async #connectSession(session, transport, onClose) {
2396
+ let closed = false;
2397
+ const originalOnClose = transport.onclose;
2398
+ transport.onclose = () => {
2399
+ closed = true;
2400
+ _optionalChain([onClose, 'optionalCall', _46 => _46()]);
2401
+ this.#removeSession(session);
2402
+ _optionalChain([originalOnClose, 'optionalCall', _47 => _47()]);
2403
+ };
2404
+ await session.connect(transport);
2405
+ if (closed) {
2406
+ return;
2407
+ }
2408
+ this.#sessions.push(session);
2409
+ session.once("error", () => {
2410
+ this.#removeSession(session);
2411
+ });
2412
+ this.emit("connect", {
2413
+ session
2414
+ });
2415
+ }
2391
2416
  /**
2392
2417
  * Creates a new FastMCPSession instance with the current configuration.
2393
2418
  * Used both for regular sessions and stateless requests.
@@ -2438,7 +2463,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2438
2463
  */
2439
2464
  #createUnauthorizedResponse(message) {
2440
2465
  const oauth = this.#options.oauth;
2441
- const resource = _optionalChain([oauth, 'optionalAccess', _46 => _46.enabled]) ? _optionalChain([oauth, 'access', _47 => _47.protectedResource, 'optionalAccess', _48 => _48.resource]) : void 0;
2466
+ const resource = _optionalChain([oauth, 'optionalAccess', _48 => _48.enabled]) ? _optionalChain([oauth, 'access', _49 => _49.protectedResource, 'optionalAccess', _50 => _50.resource]) : void 0;
2442
2467
  const wwwAuthenticateParts = [
2443
2468
  'error="invalid_token"',
2444
2469
  `error_description="${message.replace(/"/g, '\\"')}"`
@@ -2577,7 +2602,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2577
2602
  }
2578
2603
  }
2579
2604
  const oauthConfig = this.#options.oauth;
2580
- if (_optionalChain([oauthConfig, 'optionalAccess', _49 => _49.enabled]) && req.method === "GET") {
2605
+ if (_optionalChain([oauthConfig, 'optionalAccess', _51 => _51.enabled]) && req.method === "GET") {
2581
2606
  const url2 = new URL(req.url || "", `http://${host}`);
2582
2607
  const authorizationServerMetadataPath = joinPaths(
2583
2608
  "",
@@ -2611,8 +2636,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2611
2636
  }
2612
2637
  }
2613
2638
  }
2614
- const oauthProxy = _optionalChain([oauthConfig, 'optionalAccess', _50 => _50.proxy]);
2615
- if (oauthProxy && _optionalChain([oauthConfig, 'optionalAccess', _51 => _51.enabled])) {
2639
+ const oauthProxy = _optionalChain([oauthConfig, 'optionalAccess', _52 => _52.proxy]);
2640
+ if (oauthProxy && _optionalChain([oauthConfig, 'optionalAccess', _53 => _53.enabled])) {
2616
2641
  const url2 = new URL(req.url || "", `http://${host}`);
2617
2642
  const oauthPath = basePathRelativePath;
2618
2643
  try {
@@ -2665,7 +2690,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2665
2690
  const statusCode = error.statusCode || 400;
2666
2691
  res.writeHead(statusCode, OAUTH_CREDENTIAL_RESPONSE_HEADERS).end(
2667
2692
  JSON.stringify(
2668
- _optionalChain([error, 'access', _52 => _52.toJSON, 'optionalCall', _53 => _53()]) || {
2693
+ _optionalChain([error, 'access', _54 => _54.toJSON, 'optionalCall', _55 => _55()]) || {
2669
2694
  error: "invalid_request"
2670
2695
  }
2671
2696
  )
@@ -2692,7 +2717,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2692
2717
  } catch (error) {
2693
2718
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2694
2719
  JSON.stringify(
2695
- _optionalChain([error, 'access', _54 => _54.toJSON, 'optionalCall', _55 => _55()]) || {
2720
+ _optionalChain([error, 'access', _56 => _56.toJSON, 'optionalCall', _57 => _57()]) || {
2696
2721
  error: "invalid_request"
2697
2722
  }
2698
2723
  )
@@ -2714,7 +2739,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2714
2739
  } catch (error) {
2715
2740
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2716
2741
  JSON.stringify(
2717
- _optionalChain([error, 'access', _56 => _56.toJSON, 'optionalCall', _57 => _57()]) || {
2742
+ _optionalChain([error, 'access', _58 => _58.toJSON, 'optionalCall', _59 => _59()]) || {
2718
2743
  error: "server_error"
2719
2744
  }
2720
2745
  )
@@ -2783,7 +2808,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2783
2808
  } catch (error) {
2784
2809
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2785
2810
  JSON.stringify(
2786
- _optionalChain([error, 'access', _58 => _58.toJSON, 'optionalCall', _59 => _59()]) || {
2811
+ _optionalChain([error, 'access', _60 => _60.toJSON, 'optionalCall', _61 => _61()]) || {
2787
2812
  error: "server_error"
2788
2813
  }
2789
2814
  )
@@ -2841,8 +2866,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2841
2866
  const basicAuth = parseBasicAuthHeader(
2842
2867
  req.headers.authorization
2843
2868
  );
2844
- const clientId = _optionalChain([basicAuth, 'optionalAccess', _60 => _60.clientId]) || params.get("client_id") || "";
2845
- const clientSecret = _nullishCoalesce(_nullishCoalesce(_optionalChain([basicAuth, 'optionalAccess', _61 => _61.clientSecret]), () => ( params.get("client_secret"))), () => ( void 0));
2869
+ const clientId = _optionalChain([basicAuth, 'optionalAccess', _62 => _62.clientId]) || params.get("client_id") || "";
2870
+ const clientSecret = _nullishCoalesce(_nullishCoalesce(_optionalChain([basicAuth, 'optionalAccess', _63 => _63.clientSecret]), () => ( params.get("client_secret"))), () => ( void 0));
2846
2871
  let response;
2847
2872
  if (grantType === "authorization_code") {
2848
2873
  response = await oauthProxy.exchangeAuthorizationCode({
@@ -2872,7 +2897,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2872
2897
  const statusCode = error.statusCode || 400;
2873
2898
  res.writeHead(statusCode, OAUTH_CREDENTIAL_RESPONSE_HEADERS).end(
2874
2899
  JSON.stringify(
2875
- _optionalChain([error, 'access', _62 => _62.toJSON, 'optionalCall', _63 => _63()]) || {
2900
+ _optionalChain([error, 'access', _64 => _64.toJSON, 'optionalCall', _65 => _65()]) || {
2876
2901
  error: "invalid_request"
2877
2902
  }
2878
2903
  )
@@ -2898,7 +2923,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2898
2923
  * regardless (RFC 7235), so no config is needed just to get the header.
2899
2924
  */
2900
2925
  #httpStreamOAuthConfig() {
2901
- const resource = _optionalChain([this, 'access', _64 => _64.#options, 'access', _65 => _65.oauth, 'optionalAccess', _66 => _66.enabled]) ? _optionalChain([this, 'access', _67 => _67.#options, 'access', _68 => _68.oauth, 'access', _69 => _69.protectedResource, 'optionalAccess', _70 => _70.resource]) : void 0;
2926
+ const resource = _optionalChain([this, 'access', _66 => _66.#options, 'access', _67 => _67.oauth, 'optionalAccess', _68 => _68.enabled]) ? _optionalChain([this, 'access', _69 => _69.#options, 'access', _70 => _70.oauth, 'access', _71 => _71.protectedResource, 'optionalAccess', _72 => _72.resource]) : void 0;
2902
2927
  if (!resource) {
2903
2928
  return {};
2904
2929
  }
@@ -2982,8 +3007,17 @@ var FastMCP = class extends FastMCPEventEmitter {
2982
3007
  #parseRuntimeConfig(overrides) {
2983
3008
  const args = process.argv.slice(2);
2984
3009
  const getArg = (name) => {
2985
- const index = args.findIndex((arg) => arg === `--${name}`);
2986
- return index !== -1 && index + 1 < args.length ? args[index + 1] : void 0;
3010
+ const index = args.findIndex(
3011
+ (arg) => arg === `--${name}` || arg.startsWith(`--${name}=`)
3012
+ );
3013
+ if (index === -1) {
3014
+ return void 0;
3015
+ }
3016
+ if (args[index] !== `--${name}`) {
3017
+ return args[index].slice(name.length + 3);
3018
+ }
3019
+ const value = args[index + 1];
3020
+ return value === void 0 || value.startsWith("--") ? "" : value;
2987
3021
  };
2988
3022
  const transportArg = getArg("transport");
2989
3023
  const portArg = getArg("port");
@@ -2997,23 +3031,24 @@ var FastMCP = class extends FastMCPEventEmitter {
2997
3031
  const envBasePath = process.env.FASTMCP_BASE_PATH;
2998
3032
  const envStateless = process.env.FASTMCP_STATELESS;
2999
3033
  const envHost = process.env.FASTMCP_HOST;
3000
- const transportType = _optionalChain([overrides, 'optionalAccess', _71 => _71.transportType]) || (transportArg === "http-stream" ? "httpStream" : transportArg) || envTransport || "stdio";
3034
+ const transportType = _optionalChain([overrides, 'optionalAccess', _73 => _73.transportType]) || parseTransportSetting(transportArg, "--transport") || parseTransportSetting(envTransport, "FASTMCP_TRANSPORT") || "stdio";
3001
3035
  if (transportType === "httpStream") {
3002
3036
  const port = parseInt(
3003
- _optionalChain([overrides, 'optionalAccess', _72 => _72.httpStream, 'optionalAccess', _73 => _73.port, 'optionalAccess', _74 => _74.toString, 'call', _75 => _75()]) || portArg || envPort || "8080"
3037
+ _optionalChain([overrides, 'optionalAccess', _74 => _74.httpStream, 'optionalAccess', _75 => _75.port, 'optionalAccess', _76 => _76.toString, 'call', _77 => _77()]) || portArg || envPort || "8080"
3004
3038
  );
3005
- const host = _optionalChain([overrides, 'optionalAccess', _76 => _76.httpStream, 'optionalAccess', _77 => _77.host]) || hostArg || envHost || "localhost";
3006
- const endpoint = _optionalChain([overrides, 'optionalAccess', _78 => _78.httpStream, 'optionalAccess', _79 => _79.endpoint]) || endpointArg || envEndpoint || "/mcp";
3039
+ const host = _optionalChain([overrides, 'optionalAccess', _78 => _78.httpStream, 'optionalAccess', _79 => _79.host]) || hostArg || envHost || "localhost";
3040
+ const endpoint = _optionalChain([overrides, 'optionalAccess', _80 => _80.httpStream, 'optionalAccess', _81 => _81.endpoint]) || endpointArg || envEndpoint || "/mcp";
3007
3041
  const basePath = normalizeBasePath(
3008
- _optionalChain([overrides, 'optionalAccess', _80 => _80.httpStream, 'optionalAccess', _81 => _81.basePath]) || basePathArg || envBasePath
3042
+ _optionalChain([overrides, 'optionalAccess', _82 => _82.httpStream, 'optionalAccess', _83 => _83.basePath]) || basePathArg || envBasePath
3009
3043
  );
3010
- const enableJsonResponse = _optionalChain([overrides, 'optionalAccess', _82 => _82.httpStream, 'optionalAccess', _83 => _83.enableJsonResponse]) || false;
3011
- const stateless = _nullishCoalesce(_optionalChain([overrides, 'optionalAccess', _84 => _84.httpStream, 'optionalAccess', _85 => _85.stateless]), () => ( (statelessArg !== void 0 ? statelessArg === "true" : envStateless === "true")));
3012
- const cors = _optionalChain([overrides, 'optionalAccess', _86 => _86.httpStream, 'optionalAccess', _87 => _87.cors]);
3013
- const eventStore = _optionalChain([overrides, 'optionalAccess', _88 => _88.httpStream, 'optionalAccess', _89 => _89.eventStore]);
3014
- const sslCa = _optionalChain([overrides, 'optionalAccess', _90 => _90.httpStream, 'optionalAccess', _91 => _91.sslCa]);
3015
- const sslCert = _optionalChain([overrides, 'optionalAccess', _92 => _92.httpStream, 'optionalAccess', _93 => _93.sslCert]);
3016
- const sslKey = _optionalChain([overrides, 'optionalAccess', _94 => _94.httpStream, 'optionalAccess', _95 => _95.sslKey]);
3044
+ const enableJsonResponse = _optionalChain([overrides, 'optionalAccess', _84 => _84.httpStream, 'optionalAccess', _85 => _85.enableJsonResponse]) || false;
3045
+ const stateless = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([overrides, 'optionalAccess', _86 => _86.httpStream, 'optionalAccess', _87 => _87.stateless]), () => ( // A bare `--stateless` means `--stateless true`.
3046
+ (statelessArg === "" ? true : parseBooleanSetting(statelessArg, "--stateless")))), () => ( parseBooleanSetting(envStateless, "FASTMCP_STATELESS"))), () => ( false));
3047
+ const cors = _optionalChain([overrides, 'optionalAccess', _88 => _88.httpStream, 'optionalAccess', _89 => _89.cors]);
3048
+ const eventStore = _optionalChain([overrides, 'optionalAccess', _90 => _90.httpStream, 'optionalAccess', _91 => _91.eventStore]);
3049
+ const sslCa = _optionalChain([overrides, 'optionalAccess', _92 => _92.httpStream, 'optionalAccess', _93 => _93.sslCa]);
3050
+ const sslCert = _optionalChain([overrides, 'optionalAccess', _94 => _94.httpStream, 'optionalAccess', _95 => _95.sslCert]);
3051
+ const sslKey = _optionalChain([overrides, 'optionalAccess', _96 => _96.httpStream, 'optionalAccess', _97 => _97.sslKey]);
3017
3052
  return {
3018
3053
  httpStream: {
3019
3054
  basePath,
@@ -3105,7 +3140,7 @@ var FastMCP = class extends FastMCPEventEmitter {
3105
3140
  */
3106
3141
  #useEndpointAsResource(endpoint) {
3107
3142
  const oauth = this.#options.oauth;
3108
- if (this.#providerResourceBase === void 0 || !_optionalChain([oauth, 'optionalAccess', _96 => _96.protectedResource]))
3143
+ if (this.#providerResourceBase === void 0 || !_optionalChain([oauth, 'optionalAccess', _98 => _98.protectedResource]))
3109
3144
  return;
3110
3145
  this.#options = {
3111
3146
  ...this.#options,
@@ -3134,4 +3169,4 @@ var FastMCP = class extends FastMCPEventEmitter {
3134
3169
 
3135
3170
 
3136
3171
  exports.DiscoveryDocumentCache = DiscoveryDocumentCache; exports.jsonSchemaAdapter = jsonSchemaAdapter; exports.MEDIA_FETCH_TIMEOUT_MS = MEDIA_FETCH_TIMEOUT_MS; exports.imageContent = imageContent; exports.audioContent = audioContent; exports.FastMCPError = FastMCPError; exports.SessionError = SessionError; exports.UnexpectedStateError = UnexpectedStateError; exports.UserError = UserError; exports.ServerState = ServerState; exports.FastMCPSession = FastMCPSession; exports.FastMCP = FastMCP;
3137
- //# sourceMappingURL=chunk-44X25GBU.cjs.map
3172
+ //# sourceMappingURL=chunk-RE2YJZA6.cjs.map