@mcp-ts/sdk 2.4.3 → 2.4.5

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 (56) hide show
  1. package/dist/adapters/agui-adapter.d.mts +3 -3
  2. package/dist/adapters/agui-adapter.d.ts +3 -3
  3. package/dist/adapters/agui-middleware.d.mts +3 -3
  4. package/dist/adapters/agui-middleware.d.ts +3 -3
  5. package/dist/adapters/ai-adapter.d.mts +3 -3
  6. package/dist/adapters/ai-adapter.d.ts +3 -3
  7. package/dist/adapters/langchain-adapter.d.mts +3 -3
  8. package/dist/adapters/langchain-adapter.d.ts +3 -3
  9. package/dist/adapters/mastra-adapter.d.mts +2 -2
  10. package/dist/adapters/mastra-adapter.d.ts +2 -2
  11. package/dist/client/index.d.mts +2 -2
  12. package/dist/client/index.d.ts +2 -2
  13. package/dist/client/react.d.mts +4 -5
  14. package/dist/client/react.d.ts +4 -5
  15. package/dist/client/react.js +2 -6
  16. package/dist/client/react.js.map +1 -1
  17. package/dist/client/react.mjs +2 -6
  18. package/dist/client/react.mjs.map +1 -1
  19. package/dist/client/vue.d.mts +4 -5
  20. package/dist/client/vue.d.ts +4 -5
  21. package/dist/client/vue.js.map +1 -1
  22. package/dist/client/vue.mjs.map +1 -1
  23. package/dist/{index-B8kJSrBJ.d.ts → index-CZk2gu2E.d.ts} +1 -1
  24. package/dist/{index-DiJsm_lK.d.mts → index-CkM3p0eE.d.mts} +1 -1
  25. package/dist/index.d.mts +4 -4
  26. package/dist/index.d.ts +4 -4
  27. package/dist/index.js +96 -104
  28. package/dist/index.js.map +1 -1
  29. package/dist/index.mjs +96 -104
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/{multi-session-client-DOBvtaQV.d.ts → multi-session-client-ChQrBZhF.d.mts} +4 -5
  32. package/dist/{multi-session-client-B6hsYO8V.d.mts → multi-session-client-QOOPdEVp.d.ts} +4 -5
  33. package/dist/server/index.d.mts +4 -4
  34. package/dist/server/index.d.ts +4 -4
  35. package/dist/server/index.js +96 -104
  36. package/dist/server/index.js.map +1 -1
  37. package/dist/server/index.mjs +96 -104
  38. package/dist/server/index.mjs.map +1 -1
  39. package/dist/shared/index.d.mts +4 -4
  40. package/dist/shared/index.d.ts +4 -4
  41. package/dist/shared/index.js +2 -6
  42. package/dist/shared/index.js.map +1 -1
  43. package/dist/shared/index.mjs +2 -6
  44. package/dist/shared/index.mjs.map +1 -1
  45. package/dist/{tool-router-CbG4Tum6.d.mts → tool-router-CGs3IDOJ.d.mts} +1 -1
  46. package/dist/{tool-router-ChIhPwgP.d.ts → tool-router-DQ-HrD7W.d.ts} +1 -1
  47. package/dist/{types-CjczQwNX.d.mts → types-Bf-7GOLW.d.mts} +0 -2
  48. package/dist/{types-CjczQwNX.d.ts → types-Bf-7GOLW.d.ts} +0 -2
  49. package/package.json +1 -1
  50. package/src/client/react/use-mcp.ts +0 -1
  51. package/src/client/vue/use-mcp.ts +0 -1
  52. package/src/server/handlers/sse-handler.ts +2 -2
  53. package/src/server/mcp/oauth-client.ts +94 -121
  54. package/src/server/storage/index.ts +1 -1
  55. package/src/shared/events.ts +0 -2
  56. package/src/shared/utils.ts +16 -14
@@ -1,4 +1,4 @@
1
- import { m as Event, M as McpConnectionEvent, t as McpObservabilityEvent, s as McpConnectionState, A as ToolClientProvider } from './types-CjczQwNX.js';
1
+ import { m as Event, M as McpConnectionEvent, t as McpObservabilityEvent, s as McpConnectionState, A as ToolClientProvider } from './types-Bf-7GOLW.mjs';
2
2
  import { ListToolsResult, CallToolResult, ListPromptsResult, GetPromptResult, ListResourcesResult, ReadResourceResult } from '@modelcontextprotocol/sdk/types.js';
3
3
  import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
4
4
  import { OAuthClientMetadata, OAuthClientInformationMixed, OAuthClientInformationFull, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
@@ -184,12 +184,11 @@ declare class MCPClient {
184
184
  */
185
185
  private getTransport;
186
186
  /**
187
- * Initializes client components (client, transport, OAuth provider)
188
- * Loads missing configuration from Redis session store if needed
189
- * This method is idempotent and safe to call multiple times
187
+ * Ensures session metadata and OAuth provider are loaded.
188
+ * Does NOT create the SDK Client callers that need one create it themselves.
190
189
  * @private
191
190
  */
192
- private initialize;
191
+ private ensureSession;
193
192
  /**
194
193
  * Saves current session state to the session store
195
194
  * Creates new session if it doesn't exist, updates if it does
@@ -1,4 +1,4 @@
1
- import { m as Event, M as McpConnectionEvent, t as McpObservabilityEvent, s as McpConnectionState, A as ToolClientProvider } from './types-CjczQwNX.mjs';
1
+ import { m as Event, M as McpConnectionEvent, t as McpObservabilityEvent, s as McpConnectionState, A as ToolClientProvider } from './types-Bf-7GOLW.js';
2
2
  import { ListToolsResult, CallToolResult, ListPromptsResult, GetPromptResult, ListResourcesResult, ReadResourceResult } from '@modelcontextprotocol/sdk/types.js';
3
3
  import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
4
4
  import { OAuthClientMetadata, OAuthClientInformationMixed, OAuthClientInformationFull, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
@@ -184,12 +184,11 @@ declare class MCPClient {
184
184
  */
185
185
  private getTransport;
186
186
  /**
187
- * Initializes client components (client, transport, OAuth provider)
188
- * Loads missing configuration from Redis session store if needed
189
- * This method is idempotent and safe to call multiple times
187
+ * Ensures session metadata and OAuth provider are loaded.
188
+ * Does NOT create the SDK Client callers that need one create it themselves.
190
189
  * @private
191
190
  */
192
- private initialize;
191
+ private ensureSession;
193
192
  /**
194
193
  * Saves current session state to the session store
195
194
  * Creates new session if it doesn't exist, updates if it does
@@ -1,8 +1,8 @@
1
- import { c as SessionMutationListener, e as SessionStore } from '../multi-session-client-B6hsYO8V.mjs';
2
- export { M as MCPClient, a as MultiSessionClient, S as Session, b as SessionMutationEvent, d as SessionMutationType, f as StorageOAuthClientProvider } from '../multi-session-client-B6hsYO8V.mjs';
1
+ import { c as SessionMutationListener, e as SessionStore } from '../multi-session-client-ChQrBZhF.mjs';
2
+ export { M as MCPClient, a as MultiSessionClient, S as Session, b as SessionMutationEvent, d as SessionMutationType, f as StorageOAuthClientProvider } from '../multi-session-client-ChQrBZhF.mjs';
3
3
  export { U as UnauthorizedError, s as sanitizeServerLabel } from '../utils-DELRKQPU.mjs';
4
- import { M as McpConnectionEvent, t as McpObservabilityEvent, x as McpRpcResponse, w as McpRpcRequest } from '../types-CjczQwNX.mjs';
5
- export { a as CallToolRequest, b as CallToolResponse, f as ConnectRequest, g as ConnectResponse, k as Disposable, E as Emitter, m as Event, q as ListToolsResponse, s as McpConnectionState, T as ToolClient, A as ToolClientProvider, B as ToolInfo } from '../types-CjczQwNX.mjs';
4
+ import { M as McpConnectionEvent, t as McpObservabilityEvent, x as McpRpcResponse, w as McpRpcRequest } from '../types-Bf-7GOLW.mjs';
5
+ export { a as CallToolRequest, b as CallToolResponse, f as ConnectRequest, g as ConnectResponse, k as Disposable, E as Emitter, m as Event, q as ListToolsResponse, s as McpConnectionState, T as ToolClient, A as ToolClientProvider, B as ToolInfo } from '../types-Bf-7GOLW.mjs';
6
6
  export { OAuthClientInformation, OAuthClientInformationFull, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
7
7
  export { CallToolResult, ListToolsResult, Tool } from '@modelcontextprotocol/sdk/types.js';
8
8
  import '@modelcontextprotocol/sdk/client/auth.js';
@@ -1,8 +1,8 @@
1
- import { c as SessionMutationListener, e as SessionStore } from '../multi-session-client-DOBvtaQV.js';
2
- export { M as MCPClient, a as MultiSessionClient, S as Session, b as SessionMutationEvent, d as SessionMutationType, f as StorageOAuthClientProvider } from '../multi-session-client-DOBvtaQV.js';
1
+ import { c as SessionMutationListener, e as SessionStore } from '../multi-session-client-QOOPdEVp.js';
2
+ export { M as MCPClient, a as MultiSessionClient, S as Session, b as SessionMutationEvent, d as SessionMutationType, f as StorageOAuthClientProvider } from '../multi-session-client-QOOPdEVp.js';
3
3
  export { U as UnauthorizedError, s as sanitizeServerLabel } from '../utils-DELRKQPU.js';
4
- import { M as McpConnectionEvent, t as McpObservabilityEvent, x as McpRpcResponse, w as McpRpcRequest } from '../types-CjczQwNX.js';
5
- export { a as CallToolRequest, b as CallToolResponse, f as ConnectRequest, g as ConnectResponse, k as Disposable, E as Emitter, m as Event, q as ListToolsResponse, s as McpConnectionState, T as ToolClient, A as ToolClientProvider, B as ToolInfo } from '../types-CjczQwNX.js';
4
+ import { M as McpConnectionEvent, t as McpObservabilityEvent, x as McpRpcResponse, w as McpRpcRequest } from '../types-Bf-7GOLW.js';
5
+ export { a as CallToolRequest, b as CallToolResponse, f as ConnectRequest, g as ConnectResponse, k as Disposable, E as Emitter, m as Event, q as ListToolsResponse, s as McpConnectionState, T as ToolClient, A as ToolClientProvider, B as ToolInfo } from '../types-Bf-7GOLW.js';
6
6
  export { OAuthClientInformation, OAuthClientInformationFull, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
7
7
  export { CallToolResult, ListToolsResult, Tool } from '@modelcontextprotocol/sdk/types.js';
8
8
  import '@modelcontextprotocol/sdk/client/auth.js';
@@ -159,13 +159,9 @@ init_cjs_shims();
159
159
  // src/shared/utils.ts
160
160
  init_cjs_shims();
161
161
  var OAUTH_STATE_SEPARATOR = ".";
162
- var firstChar = nanoid.customAlphabet(
163
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
164
- 1
165
- );
166
- var rest = nanoid.customAlphabet(
167
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
168
- 11
162
+ var serverIdAlphabet = nanoid.customAlphabet(
163
+ "abcdefghijklmnopqrstuvwxyz0123456789",
164
+ 12
169
165
  );
170
166
  function sanitizeServerLabel(name) {
171
167
  let sanitized = name.replace(/[^a-zA-Z0-9-_]/g, "_").replace(/_{2,}/g, "_").toLowerCase();
@@ -175,7 +171,10 @@ function sanitizeServerLabel(name) {
175
171
  return sanitized;
176
172
  }
177
173
  function generateSessionId() {
178
- return firstChar() + rest();
174
+ return "sess_" + nanoid.nanoid(21);
175
+ }
176
+ function generateServerId() {
177
+ return serverIdAlphabet();
179
178
  }
180
179
  function formatOAuthState(nonce, sessionId) {
181
180
  return `${nonce}${OAUTH_STATE_SEPARATOR}${sessionId}`;
@@ -2149,7 +2148,7 @@ var MCPClient = class {
2149
2148
  * @param options - Client configuration options
2150
2149
  */
2151
2150
  constructor(options) {
2152
- __publicField(this, "client", null);
2151
+ __publicField(this, "client");
2153
2152
  __publicField(this, "oauthProvider", null);
2154
2153
  __publicField(this, "transport", null);
2155
2154
  __publicField(this, "userId");
@@ -2190,6 +2189,21 @@ var MCPClient = class {
2190
2189
  this.clientUri = options.clientUri;
2191
2190
  this.logoUri = options.logoUri;
2192
2191
  this.policyUri = options.policyUri;
2192
+ this.client = new index_js.Client(
2193
+ {
2194
+ name: MCP_CLIENT_NAME,
2195
+ version: MCP_CLIENT_VERSION
2196
+ },
2197
+ {
2198
+ capabilities: {
2199
+ extensions: {
2200
+ "io.modelcontextprotocol/ui": {
2201
+ mimeTypes: ["text/html+mcp"]
2202
+ }
2203
+ }
2204
+ }
2205
+ }
2206
+ );
2193
2207
  }
2194
2208
  /**
2195
2209
  * Emit a connection state change event
@@ -2315,13 +2329,12 @@ var MCPClient = class {
2315
2329
  }
2316
2330
  }
2317
2331
  /**
2318
- * Initializes client components (client, transport, OAuth provider)
2319
- * Loads missing configuration from Redis session store if needed
2320
- * This method is idempotent and safe to call multiple times
2332
+ * Ensures session metadata and OAuth provider are loaded.
2333
+ * Does NOT create the SDK Client callers that need one create it themselves.
2321
2334
  * @private
2322
2335
  */
2323
- async initialize() {
2324
- if (this.client && this.oauthProvider) {
2336
+ async ensureSession() {
2337
+ if (this.oauthProvider) {
2325
2338
  return;
2326
2339
  }
2327
2340
  this.emitStateChange("INITIALIZING");
@@ -2364,30 +2377,21 @@ var MCPClient = class {
2364
2377
  }
2365
2378
  });
2366
2379
  }
2367
- if (!this.client) {
2368
- this.client = new index_js.Client(
2369
- {
2370
- name: MCP_CLIENT_NAME,
2371
- version: MCP_CLIENT_VERSION
2372
- },
2373
- {
2374
- capabilities: {
2375
- extensions: {
2376
- "io.modelcontextprotocol/ui": {
2377
- mimeTypes: ["text/html+mcp"]
2378
- }
2379
- }
2380
- }
2381
- }
2382
- );
2383
- }
2384
2380
  if (existingSession === null) {
2385
2381
  existingSession = await sessions.get(this.userId, this.sessionId);
2386
2382
  }
2387
2383
  if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2388
2384
  this.createdAt = Date.now();
2389
2385
  const updatedAt = this.createdAt;
2390
- console.log(`[MCPClient] Creating pending session ${this.sessionId} for connection setup`);
2386
+ this._onObservabilityEvent.fire({
2387
+ type: "mcp:client:session_created",
2388
+ level: "info",
2389
+ message: `Creating pending session ${this.sessionId} for connection setup`,
2390
+ sessionId: this.sessionId,
2391
+ serverId: this.serverId,
2392
+ timestamp: Date.now(),
2393
+ id: nanoid.nanoid()
2394
+ });
2391
2395
  await sessions.create({
2392
2396
  sessionId: this.sessionId,
2393
2397
  userId: this.userId,
@@ -2497,17 +2501,16 @@ var MCPClient = class {
2497
2501
  * @throws {Error} When connection fails for other reasons
2498
2502
  */
2499
2503
  async connect() {
2500
- if (this.client?.transport) {
2504
+ if (this.client.transport) {
2501
2505
  this.transport = null;
2502
2506
  try {
2503
2507
  await this.client.close();
2504
2508
  } catch {
2505
2509
  }
2506
- this.client = null;
2507
2510
  }
2508
- await this.initialize();
2509
- if (!this.client || !this.oauthProvider) {
2510
- const error = "Client or OAuth provider not initialized";
2511
+ await this.ensureSession();
2512
+ if (!this.oauthProvider) {
2513
+ const error = "OAuth provider not initialized";
2511
2514
  this.emitError(error, "connection");
2512
2515
  this.emitStateChange("FAILED");
2513
2516
  throw new Error(error);
@@ -2518,7 +2521,15 @@ var MCPClient = class {
2518
2521
  this.transportType = transportType;
2519
2522
  this.emitStateChange("CONNECTED");
2520
2523
  this.emitProgress("Connected successfully");
2521
- console.log(`[MCPClient] Saving active session ${this.sessionId} (connect success)`);
2524
+ this._onObservabilityEvent.fire({
2525
+ type: "mcp:client:session_saved",
2526
+ level: "info",
2527
+ message: `Saving active session ${this.sessionId} (connect success)`,
2528
+ sessionId: this.sessionId,
2529
+ serverId: this.serverId,
2530
+ timestamp: Date.now(),
2531
+ id: nanoid.nanoid()
2532
+ });
2522
2533
  await this.saveSession("active");
2523
2534
  } catch (error) {
2524
2535
  if (error instanceof auth_js.UnauthorizedError || error instanceof Error && error.message.toLowerCase().includes("unauthorized")) {
@@ -2535,7 +2546,15 @@ var MCPClient = class {
2535
2546
  throw new Error(message);
2536
2547
  }
2537
2548
  this.emitStateChange("AUTHENTICATING");
2538
- console.log(`[MCPClient] Saving pending OAuth session ${this.sessionId}`);
2549
+ this._onObservabilityEvent.fire({
2550
+ type: "mcp:client:session_saved",
2551
+ level: "info",
2552
+ message: `Saving pending OAuth session ${this.sessionId}`,
2553
+ sessionId: this.sessionId,
2554
+ serverId: this.serverId,
2555
+ timestamp: Date.now(),
2556
+ id: nanoid.nanoid()
2557
+ });
2539
2558
  await this.saveSession("pending");
2540
2559
  if (this.serverId) {
2541
2560
  this._onConnectionEvent.fire({
@@ -2574,7 +2593,7 @@ var MCPClient = class {
2574
2593
  async finishAuth(authCode, state) {
2575
2594
  this.emitStateChange("AUTHENTICATING");
2576
2595
  this.emitProgress("Exchanging authorization code for tokens...");
2577
- await this.initialize();
2596
+ await this.ensureSession();
2578
2597
  if (!this.oauthProvider) {
2579
2598
  const error = "OAuth provider not initialized";
2580
2599
  this.emitError(error, "auth");
@@ -2610,27 +2629,25 @@ var MCPClient = class {
2610
2629
  this.emitStateChange("AUTHENTICATED");
2611
2630
  authenticatedStateEmitted = true;
2612
2631
  }
2613
- this.emitProgress("Creating authenticated client...");
2614
- this.client = new index_js.Client(
2615
- {
2616
- name: MCP_CLIENT_NAME,
2617
- version: MCP_CLIENT_VERSION
2618
- },
2619
- {
2620
- capabilities: {
2621
- extensions: {
2622
- "io.modelcontextprotocol/ui": {
2623
- mimeTypes: ["text/html+mcp"]
2624
- }
2625
- }
2626
- }
2627
- }
2628
- );
2629
2632
  this.emitStateChange("CONNECTING");
2633
+ if (this.client.transport) {
2634
+ try {
2635
+ await this.client.close();
2636
+ } catch {
2637
+ }
2638
+ }
2630
2639
  await this.client.connect(this.transport);
2631
2640
  this.transportType = currentType;
2632
2641
  this.emitStateChange("CONNECTED");
2633
- console.log(`[MCPClient] Saving active session ${this.sessionId} (OAuth complete)`);
2642
+ this._onObservabilityEvent.fire({
2643
+ type: "mcp:client:session_saved",
2644
+ level: "info",
2645
+ message: `Saving active session ${this.sessionId} (OAuth complete)`,
2646
+ sessionId: this.sessionId,
2647
+ serverId: this.serverId,
2648
+ timestamp: Date.now(),
2649
+ id: nanoid.nanoid()
2650
+ });
2634
2651
  await this.saveSession("active");
2635
2652
  return;
2636
2653
  } catch (error) {
@@ -2671,9 +2688,6 @@ var MCPClient = class {
2671
2688
  * @throws {Error} When client is not connected
2672
2689
  */
2673
2690
  async listTools() {
2674
- if (!this.client) {
2675
- throw new Error("Not connected to server");
2676
- }
2677
2691
  this.emitStateChange("DISCOVERING");
2678
2692
  try {
2679
2693
  const request = {
@@ -2711,9 +2725,6 @@ var MCPClient = class {
2711
2725
  * @throws {Error} When client is not connected
2712
2726
  */
2713
2727
  async callTool(toolName, toolArgs) {
2714
- if (!this.client) {
2715
- throw new Error("Not connected to server");
2716
- }
2717
2728
  const request = {
2718
2729
  method: "tools/call",
2719
2730
  params: {
@@ -2767,9 +2778,6 @@ var MCPClient = class {
2767
2778
  * @throws {Error} When client is not connected
2768
2779
  */
2769
2780
  async listPrompts() {
2770
- if (!this.client) {
2771
- throw new Error("Not connected to server");
2772
- }
2773
2781
  this.emitStateChange("DISCOVERING");
2774
2782
  try {
2775
2783
  const request = {
@@ -2797,9 +2805,6 @@ var MCPClient = class {
2797
2805
  * @throws {Error} When client is not connected
2798
2806
  */
2799
2807
  async getPrompt(name, args) {
2800
- if (!this.client) {
2801
- throw new Error("Not connected to server");
2802
- }
2803
2808
  const request = {
2804
2809
  method: "prompts/get",
2805
2810
  params: {
@@ -2817,9 +2822,6 @@ var MCPClient = class {
2817
2822
  * @throws {Error} When client is not connected
2818
2823
  */
2819
2824
  async listResources() {
2820
- if (!this.client) {
2821
- throw new Error("Not connected to server");
2822
- }
2823
2825
  this.emitStateChange("DISCOVERING");
2824
2826
  try {
2825
2827
  const request = {
@@ -2846,9 +2848,6 @@ var MCPClient = class {
2846
2848
  * @throws {Error} When client is not connected
2847
2849
  */
2848
2850
  async readResource(uri) {
2849
- if (!this.client) {
2850
- throw new Error("Not connected to server");
2851
- }
2852
2851
  const request = {
2853
2852
  method: "resources/read",
2854
2853
  params: {
@@ -2874,13 +2873,12 @@ var MCPClient = class {
2874
2873
  return await fn();
2875
2874
  } catch (error) {
2876
2875
  if (!(error instanceof Error && error.message.includes("MCP_SESSION_EXPIRED"))) throw error;
2877
- if (this.client) {
2876
+ if (this.client.transport) {
2878
2877
  try {
2879
2878
  await this.client.close();
2880
2879
  } catch {
2881
2880
  }
2882
2881
  this.transport = null;
2883
- this.client = null;
2884
2882
  }
2885
2883
  await this.reconnect();
2886
2884
  return await fn();
@@ -2893,26 +2891,9 @@ var MCPClient = class {
2893
2891
  * @throws {Error} When OAuth provider is not initialized
2894
2892
  */
2895
2893
  async reconnect() {
2896
- await this.initialize();
2897
- if (!this.oauthProvider) {
2898
- throw new Error("OAuth provider not initialized");
2899
- }
2900
- if (this.client) {
2901
- try {
2902
- await this.client.close();
2903
- } catch {
2904
- }
2905
- }
2906
- this.client = new index_js.Client(
2907
- {
2908
- name: MCP_CLIENT_NAME,
2909
- version: MCP_CLIENT_VERSION
2910
- },
2911
- { capabilities: {} }
2912
- );
2913
- const tt = this.transportType || "streamable-http";
2914
- this.transport = this.getTransport(tt);
2915
- await this.client.connect(this.transport);
2894
+ await this.ensureSession();
2895
+ if (!this.oauthProvider) throw new Error("OAuth provider not initialized");
2896
+ await this.connect();
2916
2897
  }
2917
2898
  /**
2918
2899
  * Completely removes the session from Redis including all OAuth data
@@ -2920,9 +2901,18 @@ var MCPClient = class {
2920
2901
  */
2921
2902
  async clearSession() {
2922
2903
  try {
2923
- await this.initialize();
2904
+ await this.ensureSession();
2924
2905
  } catch (error) {
2925
- console.warn("[MCPClient] Initialization failed during clearSession:", error);
2906
+ this._onObservabilityEvent.fire({
2907
+ type: "mcp:client:error",
2908
+ level: "warn",
2909
+ message: "Initialization failed during clearSession",
2910
+ sessionId: this.sessionId,
2911
+ serverId: this.serverId,
2912
+ payload: { error: String(error) },
2913
+ timestamp: Date.now(),
2914
+ id: nanoid.nanoid()
2915
+ });
2926
2916
  }
2927
2917
  if (this.oauthProvider) {
2928
2918
  await this.oauthProvider.invalidateCredentials("all");
@@ -2935,7 +2925,7 @@ var MCPClient = class {
2935
2925
  * @returns True if connected, false otherwise
2936
2926
  */
2937
2927
  isConnected() {
2938
- return this.client !== null;
2928
+ return this.client.transport !== void 0;
2939
2929
  }
2940
2930
  /**
2941
2931
  * Disconnects from the MCP server and cleans up resources.
@@ -2953,10 +2943,12 @@ var MCPClient = class {
2953
2943
  } catch {
2954
2944
  }
2955
2945
  }
2956
- if (this.client) {
2957
- this.client.close();
2946
+ if (this.client.transport) {
2947
+ try {
2948
+ await this.client.close();
2949
+ } catch {
2950
+ }
2958
2951
  }
2959
- this.client = null;
2960
2952
  this.oauthProvider = null;
2961
2953
  this.transport = null;
2962
2954
  if (this.serverId) {
@@ -3402,7 +3394,7 @@ var SSEConnectionManager = class {
3402
3394
  async connect(params) {
3403
3395
  const { serverName, serverUrl, callbackUrl, transportType } = params;
3404
3396
  const headers = normalizeHeaders(params.headers);
3405
- const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : await sessions.generateSessionId();
3397
+ const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : generateServerId();
3406
3398
  const existingSessions = await sessions.list(this.userId);
3407
3399
  const duplicate = existingSessions.find(
3408
3400
  (s) => s.serverId === serverId || s.serverUrl === serverUrl