@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.
package/dist/index.mjs CHANGED
@@ -113,13 +113,9 @@ var init_redis = __esm({
113
113
  }
114
114
  });
115
115
  var OAUTH_STATE_SEPARATOR = ".";
116
- var firstChar = customAlphabet(
117
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
118
- 1
119
- );
120
- var rest = customAlphabet(
121
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
122
- 11
116
+ var serverIdAlphabet = customAlphabet(
117
+ "abcdefghijklmnopqrstuvwxyz0123456789",
118
+ 12
123
119
  );
124
120
  function sanitizeServerLabel(name) {
125
121
  let sanitized = name.replace(/[^a-zA-Z0-9-_]/g, "_").replace(/_{2,}/g, "_").toLowerCase();
@@ -129,7 +125,10 @@ function sanitizeServerLabel(name) {
129
125
  return sanitized;
130
126
  }
131
127
  function generateSessionId() {
132
- return firstChar() + rest();
128
+ return "sess_" + nanoid(21);
129
+ }
130
+ function generateServerId() {
131
+ return serverIdAlphabet();
133
132
  }
134
133
  function formatOAuthState(nonce, sessionId) {
135
134
  return `${nonce}${OAUTH_STATE_SEPARATOR}${sessionId}`;
@@ -2386,7 +2385,15 @@ var MCPClient = class {
2386
2385
  if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2387
2386
  this.createdAt = Date.now();
2388
2387
  const updatedAt = this.createdAt;
2389
- console.log(`[MCPClient] Creating pending session ${this.sessionId} for connection setup`);
2388
+ this._onObservabilityEvent.fire({
2389
+ type: "mcp:client:session_created",
2390
+ level: "info",
2391
+ message: `Creating pending session ${this.sessionId} for connection setup`,
2392
+ sessionId: this.sessionId,
2393
+ serverId: this.serverId,
2394
+ timestamp: Date.now(),
2395
+ id: nanoid()
2396
+ });
2390
2397
  await sessions.create({
2391
2398
  sessionId: this.sessionId,
2392
2399
  userId: this.userId,
@@ -2516,7 +2523,15 @@ var MCPClient = class {
2516
2523
  this.transportType = transportType;
2517
2524
  this.emitStateChange("CONNECTED");
2518
2525
  this.emitProgress("Connected successfully");
2519
- console.log(`[MCPClient] Saving active session ${this.sessionId} (connect success)`);
2526
+ this._onObservabilityEvent.fire({
2527
+ type: "mcp:client:session_saved",
2528
+ level: "info",
2529
+ message: `Saving active session ${this.sessionId} (connect success)`,
2530
+ sessionId: this.sessionId,
2531
+ serverId: this.serverId,
2532
+ timestamp: Date.now(),
2533
+ id: nanoid()
2534
+ });
2520
2535
  await this.saveSession("active");
2521
2536
  } catch (error) {
2522
2537
  if (error instanceof UnauthorizedError$1 || error instanceof Error && error.message.toLowerCase().includes("unauthorized")) {
@@ -2533,7 +2548,15 @@ var MCPClient = class {
2533
2548
  throw new Error(message);
2534
2549
  }
2535
2550
  this.emitStateChange("AUTHENTICATING");
2536
- console.log(`[MCPClient] Saving pending OAuth session ${this.sessionId}`);
2551
+ this._onObservabilityEvent.fire({
2552
+ type: "mcp:client:session_saved",
2553
+ level: "info",
2554
+ message: `Saving pending OAuth session ${this.sessionId}`,
2555
+ sessionId: this.sessionId,
2556
+ serverId: this.serverId,
2557
+ timestamp: Date.now(),
2558
+ id: nanoid()
2559
+ });
2537
2560
  await this.saveSession("pending");
2538
2561
  if (this.serverId) {
2539
2562
  this._onConnectionEvent.fire({
@@ -2608,27 +2631,25 @@ var MCPClient = class {
2608
2631
  this.emitStateChange("AUTHENTICATED");
2609
2632
  authenticatedStateEmitted = true;
2610
2633
  }
2611
- this.emitProgress("Creating authenticated client...");
2612
- this.client = new Client(
2613
- {
2614
- name: MCP_CLIENT_NAME,
2615
- version: MCP_CLIENT_VERSION
2616
- },
2617
- {
2618
- capabilities: {
2619
- extensions: {
2620
- "io.modelcontextprotocol/ui": {
2621
- mimeTypes: ["text/html+mcp"]
2622
- }
2623
- }
2624
- }
2625
- }
2626
- );
2627
2634
  this.emitStateChange("CONNECTING");
2635
+ if (this.client.transport) {
2636
+ try {
2637
+ await this.client.close();
2638
+ } catch {
2639
+ }
2640
+ }
2628
2641
  await this.client.connect(this.transport);
2629
2642
  this.transportType = currentType;
2630
2643
  this.emitStateChange("CONNECTED");
2631
- console.log(`[MCPClient] Saving active session ${this.sessionId} (OAuth complete)`);
2644
+ this._onObservabilityEvent.fire({
2645
+ type: "mcp:client:session_saved",
2646
+ level: "info",
2647
+ message: `Saving active session ${this.sessionId} (OAuth complete)`,
2648
+ sessionId: this.sessionId,
2649
+ serverId: this.serverId,
2650
+ timestamp: Date.now(),
2651
+ id: nanoid()
2652
+ });
2632
2653
  await this.saveSession("active");
2633
2654
  return;
2634
2655
  } catch (error) {
@@ -2669,9 +2690,6 @@ var MCPClient = class {
2669
2690
  * @throws {Error} When client is not connected
2670
2691
  */
2671
2692
  async listTools() {
2672
- if (!this.client) {
2673
- throw new Error("Not connected to server");
2674
- }
2675
2693
  this.emitStateChange("DISCOVERING");
2676
2694
  try {
2677
2695
  const request = {
@@ -2709,9 +2727,6 @@ var MCPClient = class {
2709
2727
  * @throws {Error} When client is not connected
2710
2728
  */
2711
2729
  async callTool(toolName, toolArgs) {
2712
- if (!this.client) {
2713
- throw new Error("Not connected to server");
2714
- }
2715
2730
  const request = {
2716
2731
  method: "tools/call",
2717
2732
  params: {
@@ -2765,9 +2780,6 @@ var MCPClient = class {
2765
2780
  * @throws {Error} When client is not connected
2766
2781
  */
2767
2782
  async listPrompts() {
2768
- if (!this.client) {
2769
- throw new Error("Not connected to server");
2770
- }
2771
2783
  this.emitStateChange("DISCOVERING");
2772
2784
  try {
2773
2785
  const request = {
@@ -2795,9 +2807,6 @@ var MCPClient = class {
2795
2807
  * @throws {Error} When client is not connected
2796
2808
  */
2797
2809
  async getPrompt(name, args) {
2798
- if (!this.client) {
2799
- throw new Error("Not connected to server");
2800
- }
2801
2810
  const request = {
2802
2811
  method: "prompts/get",
2803
2812
  params: {
@@ -2815,9 +2824,6 @@ var MCPClient = class {
2815
2824
  * @throws {Error} When client is not connected
2816
2825
  */
2817
2826
  async listResources() {
2818
- if (!this.client) {
2819
- throw new Error("Not connected to server");
2820
- }
2821
2827
  this.emitStateChange("DISCOVERING");
2822
2828
  try {
2823
2829
  const request = {
@@ -2844,9 +2850,6 @@ var MCPClient = class {
2844
2850
  * @throws {Error} When client is not connected
2845
2851
  */
2846
2852
  async readResource(uri) {
2847
- if (!this.client) {
2848
- throw new Error("Not connected to server");
2849
- }
2850
2853
  const request = {
2851
2854
  method: "resources/read",
2852
2855
  params: {
@@ -2902,7 +2905,16 @@ var MCPClient = class {
2902
2905
  try {
2903
2906
  await this.ensureSession();
2904
2907
  } catch (error) {
2905
- console.warn("[MCPClient] Initialization failed during clearSession:", error);
2908
+ this._onObservabilityEvent.fire({
2909
+ type: "mcp:client:error",
2910
+ level: "warn",
2911
+ message: "Initialization failed during clearSession",
2912
+ sessionId: this.sessionId,
2913
+ serverId: this.serverId,
2914
+ payload: { error: String(error) },
2915
+ timestamp: Date.now(),
2916
+ id: nanoid()
2917
+ });
2906
2918
  }
2907
2919
  if (this.oauthProvider) {
2908
2920
  await this.oauthProvider.invalidateCredentials("all");
@@ -3379,7 +3391,7 @@ var SSEConnectionManager = class {
3379
3391
  async connect(params) {
3380
3392
  const { serverName, serverUrl, callbackUrl, transportType } = params;
3381
3393
  const headers = normalizeHeaders(params.headers);
3382
- const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : await sessions.generateSessionId();
3394
+ const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : generateServerId();
3383
3395
  const existingSessions = await sessions.list(this.userId);
3384
3396
  const duplicate = existingSessions.find(
3385
3397
  (s) => s.serverId === serverId || s.serverUrl === serverUrl