@mcp-use/client 2.2.0 → 2.2.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.
package/dist/index.js CHANGED
@@ -1526,7 +1526,7 @@ init_connector_telemetry();
1526
1526
  init_logging();
1527
1527
 
1528
1528
  // src/utils/version.ts
1529
- var VERSION = "2.2.0";
1529
+ var VERSION = "2.2.1";
1530
1530
  function getPackageVersion() {
1531
1531
  return VERSION;
1532
1532
  }
@@ -4143,6 +4143,12 @@ var BaseMCPClient = class {
4143
4143
  * Map of server names to their active sessions.
4144
4144
  */
4145
4145
  sessions = {};
4146
+ /**
4147
+ * Tracks teardown by session identity so overlapping cleanup paths share the
4148
+ * same disconnect. In particular, createSession() can replace a session while
4149
+ * closeSession() is already disconnecting it.
4150
+ */
4151
+ sessionDisconnects = /* @__PURE__ */ new WeakMap();
4146
4152
  /**
4147
4153
  * List of server names that have active sessions.
4148
4154
  * This array is kept in sync with the sessions map and can be used
@@ -4176,6 +4182,15 @@ var BaseMCPClient = class {
4176
4182
  this.config = config;
4177
4183
  }
4178
4184
  }
4185
+ disconnectSession(session) {
4186
+ const existingDisconnect = this.sessionDisconnects.get(session);
4187
+ if (existingDisconnect) {
4188
+ return existingDisconnect;
4189
+ }
4190
+ const disconnect = Promise.resolve().then(() => session.disconnect());
4191
+ this.sessionDisconnects.set(session, disconnect);
4192
+ return disconnect;
4193
+ }
4179
4194
  /**
4180
4195
  * Creates a client instance from a configuration dictionary.
4181
4196
  *
@@ -4321,7 +4336,9 @@ var BaseMCPClient = class {
4321
4336
  * lifecycle of connections and provide methods for calling tools, listing
4322
4337
  * resources, and more.
4323
4338
  *
4324
- * If a session already exists for the server, it will be replaced with a new one.
4339
+ * If a session already exists for the server, it will be replaced with a new
4340
+ * one and the previous session is disconnected; any previously returned
4341
+ * reference to it becomes unusable.
4325
4342
  *
4326
4343
  * @param serverName - The name of the server as defined in the client configuration
4327
4344
  * @param autoInitialize - Whether to automatically initialize the session (default: true)
@@ -4394,10 +4411,21 @@ var BaseMCPClient = class {
4394
4411
  await completeOAuthFlow(oauthProvider, httpConfig.url);
4395
4412
  session = await openSession();
4396
4413
  }
4414
+ const previous = this.sessions[serverName];
4397
4415
  this.sessions[serverName] = session;
4398
4416
  if (!this.activeSessions.includes(serverName)) {
4399
4417
  this.activeSessions.push(serverName);
4400
4418
  }
4419
+ if (previous && previous !== session) {
4420
+ try {
4421
+ logger.debug(`Disconnecting replaced session for server ${serverName}`);
4422
+ await this.disconnectSession(previous);
4423
+ } catch (e) {
4424
+ logger.error(
4425
+ `Error disconnecting replaced session for server '${serverName}': ${e}`
4426
+ );
4427
+ }
4428
+ }
4401
4429
  return session;
4402
4430
  }
4403
4431
  /**
@@ -4576,7 +4604,7 @@ var BaseMCPClient = class {
4576
4604
  }
4577
4605
  try {
4578
4606
  logger.debug(`Closing session for server ${serverName}`);
4579
- await session.disconnect();
4607
+ await this.disconnectSession(session);
4580
4608
  } catch (e) {
4581
4609
  logger.error(`Error closing session for server '${serverName}': ${e}`);
4582
4610
  } finally {