@mcp-ts/sdk 2.4.4 → 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.
@@ -112,13 +112,9 @@ var init_redis = __esm({
112
112
  }
113
113
  });
114
114
  var OAUTH_STATE_SEPARATOR = ".";
115
- var firstChar = customAlphabet(
116
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
117
- 1
118
- );
119
- var rest = customAlphabet(
120
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
121
- 11
115
+ var serverIdAlphabet = customAlphabet(
116
+ "abcdefghijklmnopqrstuvwxyz0123456789",
117
+ 12
122
118
  );
123
119
  function sanitizeServerLabel(name) {
124
120
  let sanitized = name.replace(/[^a-zA-Z0-9-_]/g, "_").replace(/_{2,}/g, "_").toLowerCase();
@@ -128,7 +124,10 @@ function sanitizeServerLabel(name) {
128
124
  return sanitized;
129
125
  }
130
126
  function generateSessionId() {
131
- return firstChar() + rest();
127
+ return "sess_" + nanoid(21);
128
+ }
129
+ function generateServerId() {
130
+ return serverIdAlphabet();
132
131
  }
133
132
  function formatOAuthState(nonce, sessionId) {
134
133
  return `${nonce}${OAUTH_STATE_SEPARATOR}${sessionId}`;
@@ -2314,7 +2313,15 @@ var MCPClient = class {
2314
2313
  if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2315
2314
  this.createdAt = Date.now();
2316
2315
  const updatedAt = this.createdAt;
2317
- console.log(`[MCPClient] Creating pending session ${this.sessionId} for connection setup`);
2316
+ this._onObservabilityEvent.fire({
2317
+ type: "mcp:client:session_created",
2318
+ level: "info",
2319
+ message: `Creating pending session ${this.sessionId} for connection setup`,
2320
+ sessionId: this.sessionId,
2321
+ serverId: this.serverId,
2322
+ timestamp: Date.now(),
2323
+ id: nanoid()
2324
+ });
2318
2325
  await sessions.create({
2319
2326
  sessionId: this.sessionId,
2320
2327
  userId: this.userId,
@@ -2444,7 +2451,15 @@ var MCPClient = class {
2444
2451
  this.transportType = transportType;
2445
2452
  this.emitStateChange("CONNECTED");
2446
2453
  this.emitProgress("Connected successfully");
2447
- console.log(`[MCPClient] Saving active session ${this.sessionId} (connect success)`);
2454
+ this._onObservabilityEvent.fire({
2455
+ type: "mcp:client:session_saved",
2456
+ level: "info",
2457
+ message: `Saving active session ${this.sessionId} (connect success)`,
2458
+ sessionId: this.sessionId,
2459
+ serverId: this.serverId,
2460
+ timestamp: Date.now(),
2461
+ id: nanoid()
2462
+ });
2448
2463
  await this.saveSession("active");
2449
2464
  } catch (error) {
2450
2465
  if (error instanceof UnauthorizedError$1 || error instanceof Error && error.message.toLowerCase().includes("unauthorized")) {
@@ -2461,7 +2476,15 @@ var MCPClient = class {
2461
2476
  throw new Error(message);
2462
2477
  }
2463
2478
  this.emitStateChange("AUTHENTICATING");
2464
- console.log(`[MCPClient] Saving pending OAuth session ${this.sessionId}`);
2479
+ this._onObservabilityEvent.fire({
2480
+ type: "mcp:client:session_saved",
2481
+ level: "info",
2482
+ message: `Saving pending OAuth session ${this.sessionId}`,
2483
+ sessionId: this.sessionId,
2484
+ serverId: this.serverId,
2485
+ timestamp: Date.now(),
2486
+ id: nanoid()
2487
+ });
2465
2488
  await this.saveSession("pending");
2466
2489
  if (this.serverId) {
2467
2490
  this._onConnectionEvent.fire({
@@ -2536,27 +2559,25 @@ var MCPClient = class {
2536
2559
  this.emitStateChange("AUTHENTICATED");
2537
2560
  authenticatedStateEmitted = true;
2538
2561
  }
2539
- this.emitProgress("Creating authenticated client...");
2540
- this.client = new Client(
2541
- {
2542
- name: MCP_CLIENT_NAME,
2543
- version: MCP_CLIENT_VERSION
2544
- },
2545
- {
2546
- capabilities: {
2547
- extensions: {
2548
- "io.modelcontextprotocol/ui": {
2549
- mimeTypes: ["text/html+mcp"]
2550
- }
2551
- }
2552
- }
2553
- }
2554
- );
2555
2562
  this.emitStateChange("CONNECTING");
2563
+ if (this.client.transport) {
2564
+ try {
2565
+ await this.client.close();
2566
+ } catch {
2567
+ }
2568
+ }
2556
2569
  await this.client.connect(this.transport);
2557
2570
  this.transportType = currentType;
2558
2571
  this.emitStateChange("CONNECTED");
2559
- console.log(`[MCPClient] Saving active session ${this.sessionId} (OAuth complete)`);
2572
+ this._onObservabilityEvent.fire({
2573
+ type: "mcp:client:session_saved",
2574
+ level: "info",
2575
+ message: `Saving active session ${this.sessionId} (OAuth complete)`,
2576
+ sessionId: this.sessionId,
2577
+ serverId: this.serverId,
2578
+ timestamp: Date.now(),
2579
+ id: nanoid()
2580
+ });
2560
2581
  await this.saveSession("active");
2561
2582
  return;
2562
2583
  } catch (error) {
@@ -2597,9 +2618,6 @@ var MCPClient = class {
2597
2618
  * @throws {Error} When client is not connected
2598
2619
  */
2599
2620
  async listTools() {
2600
- if (!this.client) {
2601
- throw new Error("Not connected to server");
2602
- }
2603
2621
  this.emitStateChange("DISCOVERING");
2604
2622
  try {
2605
2623
  const request = {
@@ -2637,9 +2655,6 @@ var MCPClient = class {
2637
2655
  * @throws {Error} When client is not connected
2638
2656
  */
2639
2657
  async callTool(toolName, toolArgs) {
2640
- if (!this.client) {
2641
- throw new Error("Not connected to server");
2642
- }
2643
2658
  const request = {
2644
2659
  method: "tools/call",
2645
2660
  params: {
@@ -2693,9 +2708,6 @@ var MCPClient = class {
2693
2708
  * @throws {Error} When client is not connected
2694
2709
  */
2695
2710
  async listPrompts() {
2696
- if (!this.client) {
2697
- throw new Error("Not connected to server");
2698
- }
2699
2711
  this.emitStateChange("DISCOVERING");
2700
2712
  try {
2701
2713
  const request = {
@@ -2723,9 +2735,6 @@ var MCPClient = class {
2723
2735
  * @throws {Error} When client is not connected
2724
2736
  */
2725
2737
  async getPrompt(name, args) {
2726
- if (!this.client) {
2727
- throw new Error("Not connected to server");
2728
- }
2729
2738
  const request = {
2730
2739
  method: "prompts/get",
2731
2740
  params: {
@@ -2743,9 +2752,6 @@ var MCPClient = class {
2743
2752
  * @throws {Error} When client is not connected
2744
2753
  */
2745
2754
  async listResources() {
2746
- if (!this.client) {
2747
- throw new Error("Not connected to server");
2748
- }
2749
2755
  this.emitStateChange("DISCOVERING");
2750
2756
  try {
2751
2757
  const request = {
@@ -2772,9 +2778,6 @@ var MCPClient = class {
2772
2778
  * @throws {Error} When client is not connected
2773
2779
  */
2774
2780
  async readResource(uri) {
2775
- if (!this.client) {
2776
- throw new Error("Not connected to server");
2777
- }
2778
2781
  const request = {
2779
2782
  method: "resources/read",
2780
2783
  params: {
@@ -2830,7 +2833,16 @@ var MCPClient = class {
2830
2833
  try {
2831
2834
  await this.ensureSession();
2832
2835
  } catch (error) {
2833
- console.warn("[MCPClient] Initialization failed during clearSession:", error);
2836
+ this._onObservabilityEvent.fire({
2837
+ type: "mcp:client:error",
2838
+ level: "warn",
2839
+ message: "Initialization failed during clearSession",
2840
+ sessionId: this.sessionId,
2841
+ serverId: this.serverId,
2842
+ payload: { error: String(error) },
2843
+ timestamp: Date.now(),
2844
+ id: nanoid()
2845
+ });
2834
2846
  }
2835
2847
  if (this.oauthProvider) {
2836
2848
  await this.oauthProvider.invalidateCredentials("all");
@@ -3307,7 +3319,7 @@ var SSEConnectionManager = class {
3307
3319
  async connect(params) {
3308
3320
  const { serverName, serverUrl, callbackUrl, transportType } = params;
3309
3321
  const headers = normalizeHeaders(params.headers);
3310
- const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : await sessions.generateSessionId();
3322
+ const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : generateServerId();
3311
3323
  const existingSessions = await sessions.list(this.userId);
3312
3324
  const duplicate = existingSessions.find(
3313
3325
  (s) => s.serverId === serverId || s.serverUrl === serverUrl