fastmcp 4.22.9 → 4.22.11

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.
@@ -1308,7 +1308,7 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1308
1308
  let result;
1309
1309
  try {
1310
1310
  result = await prompt.load(
1311
- args,
1311
+ _nullishCoalesce(args, () => ( {})),
1312
1312
  this.#auth,
1313
1313
  this.#createLoadContext(_optionalChain([request, 'access', _30 => _30.params, 'optionalAccess', _31 => _31._meta]))
1314
1314
  );
@@ -2005,7 +2005,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2005
2005
  *
2006
2006
  * The transport's lifecycle belongs to the caller: `stop()` does not close
2007
2007
  * transports passed here. Close the returned session (or the transport) when
2008
- * 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`.
2009
2010
  *
2010
2011
  * @param transport - An already-constructed MCP server transport.
2011
2012
  * @param auth - Session auth, equivalent to what `authenticate` would return.
@@ -2013,21 +2014,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2013
2014
  */
2014
2015
  async connect(transport, auth) {
2015
2016
  const session = this.#createSession(auth);
2016
- await session.connect(transport);
2017
- this.#sessions.push(session);
2018
- session.once("error", () => {
2019
- this.#removeSession(session);
2020
- });
2021
- const originalOnClose = transport.onclose;
2022
- transport.onclose = () => {
2023
- this.#removeSession(session);
2024
- if (originalOnClose) {
2025
- originalOnClose();
2026
- }
2027
- };
2028
- this.emit("connect", {
2029
- session
2030
- });
2017
+ await this.#connectSession(session, transport);
2031
2018
  this.#serverState = "running" /* Running */;
2032
2019
  return session;
2033
2020
  }
@@ -2265,7 +2252,6 @@ var FastMCP = class extends FastMCPEventEmitter {
2265
2252
  version: this.#options.version,
2266
2253
  websiteUrl: this.#options.websiteUrl
2267
2254
  });
2268
- await session.connect(transport);
2269
2255
  let stdinClosed = false;
2270
2256
  const onStdinClose = () => {
2271
2257
  if (stdinClosed) return;
@@ -2277,29 +2263,9 @@ var FastMCP = class extends FastMCPEventEmitter {
2277
2263
  };
2278
2264
  process.stdin.on("close", onStdinClose);
2279
2265
  process.stdin.on("end", onStdinClose);
2280
- this.#sessions.push(session);
2281
- session.once("error", () => {
2282
- this.#removeSession(session);
2283
- });
2284
- if (transport.onclose) {
2285
- const originalOnClose = transport.onclose;
2286
- transport.onclose = () => {
2287
- process.stdin.off("close", onStdinClose);
2288
- process.stdin.off("end", onStdinClose);
2289
- this.#removeSession(session);
2290
- if (originalOnClose) {
2291
- originalOnClose();
2292
- }
2293
- };
2294
- } else {
2295
- transport.onclose = () => {
2296
- process.stdin.off("close", onStdinClose);
2297
- process.stdin.off("end", onStdinClose);
2298
- this.#removeSession(session);
2299
- };
2300
- }
2301
- this.emit("connect", {
2302
- session
2266
+ await this.#connectSession(session, transport, () => {
2267
+ process.stdin.off("close", onStdinClose);
2268
+ process.stdin.off("end", onStdinClose);
2303
2269
  });
2304
2270
  this.#serverState = "running" /* Running */;
2305
2271
  } else if (config.transportType === "httpStream") {
@@ -2421,6 +2387,32 @@ var FastMCP = class extends FastMCPEventEmitter {
2421
2387
  }
2422
2388
  this.#serverState = "stopped" /* Stopped */;
2423
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
+ }
2424
2416
  /**
2425
2417
  * Creates a new FastMCPSession instance with the current configuration.
2426
2418
  * Used both for regular sessions and stateless requests.
@@ -2471,7 +2463,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2471
2463
  */
2472
2464
  #createUnauthorizedResponse(message) {
2473
2465
  const oauth = this.#options.oauth;
2474
- 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;
2475
2467
  const wwwAuthenticateParts = [
2476
2468
  'error="invalid_token"',
2477
2469
  `error_description="${message.replace(/"/g, '\\"')}"`
@@ -2610,7 +2602,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2610
2602
  }
2611
2603
  }
2612
2604
  const oauthConfig = this.#options.oauth;
2613
- if (_optionalChain([oauthConfig, 'optionalAccess', _49 => _49.enabled]) && req.method === "GET") {
2605
+ if (_optionalChain([oauthConfig, 'optionalAccess', _51 => _51.enabled]) && req.method === "GET") {
2614
2606
  const url2 = new URL(req.url || "", `http://${host}`);
2615
2607
  const authorizationServerMetadataPath = joinPaths(
2616
2608
  "",
@@ -2644,8 +2636,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2644
2636
  }
2645
2637
  }
2646
2638
  }
2647
- const oauthProxy = _optionalChain([oauthConfig, 'optionalAccess', _50 => _50.proxy]);
2648
- 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])) {
2649
2641
  const url2 = new URL(req.url || "", `http://${host}`);
2650
2642
  const oauthPath = basePathRelativePath;
2651
2643
  try {
@@ -2698,7 +2690,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2698
2690
  const statusCode = error.statusCode || 400;
2699
2691
  res.writeHead(statusCode, OAUTH_CREDENTIAL_RESPONSE_HEADERS).end(
2700
2692
  JSON.stringify(
2701
- _optionalChain([error, 'access', _52 => _52.toJSON, 'optionalCall', _53 => _53()]) || {
2693
+ _optionalChain([error, 'access', _54 => _54.toJSON, 'optionalCall', _55 => _55()]) || {
2702
2694
  error: "invalid_request"
2703
2695
  }
2704
2696
  )
@@ -2725,7 +2717,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2725
2717
  } catch (error) {
2726
2718
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2727
2719
  JSON.stringify(
2728
- _optionalChain([error, 'access', _54 => _54.toJSON, 'optionalCall', _55 => _55()]) || {
2720
+ _optionalChain([error, 'access', _56 => _56.toJSON, 'optionalCall', _57 => _57()]) || {
2729
2721
  error: "invalid_request"
2730
2722
  }
2731
2723
  )
@@ -2747,7 +2739,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2747
2739
  } catch (error) {
2748
2740
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2749
2741
  JSON.stringify(
2750
- _optionalChain([error, 'access', _56 => _56.toJSON, 'optionalCall', _57 => _57()]) || {
2742
+ _optionalChain([error, 'access', _58 => _58.toJSON, 'optionalCall', _59 => _59()]) || {
2751
2743
  error: "server_error"
2752
2744
  }
2753
2745
  )
@@ -2816,7 +2808,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2816
2808
  } catch (error) {
2817
2809
  res.writeHead(400, { "Content-Type": "application/json" }).end(
2818
2810
  JSON.stringify(
2819
- _optionalChain([error, 'access', _58 => _58.toJSON, 'optionalCall', _59 => _59()]) || {
2811
+ _optionalChain([error, 'access', _60 => _60.toJSON, 'optionalCall', _61 => _61()]) || {
2820
2812
  error: "server_error"
2821
2813
  }
2822
2814
  )
@@ -2874,8 +2866,8 @@ var FastMCP = class extends FastMCPEventEmitter {
2874
2866
  const basicAuth = parseBasicAuthHeader(
2875
2867
  req.headers.authorization
2876
2868
  );
2877
- const clientId = _optionalChain([basicAuth, 'optionalAccess', _60 => _60.clientId]) || params.get("client_id") || "";
2878
- 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));
2879
2871
  let response;
2880
2872
  if (grantType === "authorization_code") {
2881
2873
  response = await oauthProxy.exchangeAuthorizationCode({
@@ -2905,7 +2897,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2905
2897
  const statusCode = error.statusCode || 400;
2906
2898
  res.writeHead(statusCode, OAUTH_CREDENTIAL_RESPONSE_HEADERS).end(
2907
2899
  JSON.stringify(
2908
- _optionalChain([error, 'access', _62 => _62.toJSON, 'optionalCall', _63 => _63()]) || {
2900
+ _optionalChain([error, 'access', _64 => _64.toJSON, 'optionalCall', _65 => _65()]) || {
2909
2901
  error: "invalid_request"
2910
2902
  }
2911
2903
  )
@@ -2931,7 +2923,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2931
2923
  * regardless (RFC 7235), so no config is needed just to get the header.
2932
2924
  */
2933
2925
  #httpStreamOAuthConfig() {
2934
- 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;
2935
2927
  if (!resource) {
2936
2928
  return {};
2937
2929
  }
@@ -3039,24 +3031,24 @@ var FastMCP = class extends FastMCPEventEmitter {
3039
3031
  const envBasePath = process.env.FASTMCP_BASE_PATH;
3040
3032
  const envStateless = process.env.FASTMCP_STATELESS;
3041
3033
  const envHost = process.env.FASTMCP_HOST;
3042
- const transportType = _optionalChain([overrides, 'optionalAccess', _71 => _71.transportType]) || parseTransportSetting(transportArg, "--transport") || parseTransportSetting(envTransport, "FASTMCP_TRANSPORT") || "stdio";
3034
+ const transportType = _optionalChain([overrides, 'optionalAccess', _73 => _73.transportType]) || parseTransportSetting(transportArg, "--transport") || parseTransportSetting(envTransport, "FASTMCP_TRANSPORT") || "stdio";
3043
3035
  if (transportType === "httpStream") {
3044
3036
  const port = parseInt(
3045
- _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"
3046
3038
  );
3047
- const host = _optionalChain([overrides, 'optionalAccess', _76 => _76.httpStream, 'optionalAccess', _77 => _77.host]) || hostArg || envHost || "localhost";
3048
- 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";
3049
3041
  const basePath = normalizeBasePath(
3050
- _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
3051
3043
  );
3052
- const enableJsonResponse = _optionalChain([overrides, 'optionalAccess', _82 => _82.httpStream, 'optionalAccess', _83 => _83.enableJsonResponse]) || false;
3053
- const stateless = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([overrides, 'optionalAccess', _84 => _84.httpStream, 'optionalAccess', _85 => _85.stateless]), () => ( // A bare `--stateless` means `--stateless true`.
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`.
3054
3046
  (statelessArg === "" ? true : parseBooleanSetting(statelessArg, "--stateless")))), () => ( parseBooleanSetting(envStateless, "FASTMCP_STATELESS"))), () => ( false));
3055
- const cors = _optionalChain([overrides, 'optionalAccess', _86 => _86.httpStream, 'optionalAccess', _87 => _87.cors]);
3056
- const eventStore = _optionalChain([overrides, 'optionalAccess', _88 => _88.httpStream, 'optionalAccess', _89 => _89.eventStore]);
3057
- const sslCa = _optionalChain([overrides, 'optionalAccess', _90 => _90.httpStream, 'optionalAccess', _91 => _91.sslCa]);
3058
- const sslCert = _optionalChain([overrides, 'optionalAccess', _92 => _92.httpStream, 'optionalAccess', _93 => _93.sslCert]);
3059
- const sslKey = _optionalChain([overrides, 'optionalAccess', _94 => _94.httpStream, 'optionalAccess', _95 => _95.sslKey]);
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]);
3060
3052
  return {
3061
3053
  httpStream: {
3062
3054
  basePath,
@@ -3148,7 +3140,7 @@ var FastMCP = class extends FastMCPEventEmitter {
3148
3140
  */
3149
3141
  #useEndpointAsResource(endpoint) {
3150
3142
  const oauth = this.#options.oauth;
3151
- if (this.#providerResourceBase === void 0 || !_optionalChain([oauth, 'optionalAccess', _96 => _96.protectedResource]))
3143
+ if (this.#providerResourceBase === void 0 || !_optionalChain([oauth, 'optionalAccess', _98 => _98.protectedResource]))
3152
3144
  return;
3153
3145
  this.#options = {
3154
3146
  ...this.#options,
@@ -3177,4 +3169,4 @@ var FastMCP = class extends FastMCPEventEmitter {
3177
3169
 
3178
3170
 
3179
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;
3180
- //# sourceMappingURL=chunk-YDWOFVQN.cjs.map
3172
+ //# sourceMappingURL=chunk-X7KNHPR4.cjs.map