@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.js CHANGED
@@ -163,13 +163,9 @@ init_cjs_shims();
163
163
  // src/shared/utils.ts
164
164
  init_cjs_shims();
165
165
  var OAUTH_STATE_SEPARATOR = ".";
166
- var firstChar = nanoid.customAlphabet(
167
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
168
- 1
169
- );
170
- var rest = nanoid.customAlphabet(
171
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
172
- 11
166
+ var serverIdAlphabet = nanoid.customAlphabet(
167
+ "abcdefghijklmnopqrstuvwxyz0123456789",
168
+ 12
173
169
  );
174
170
  function sanitizeServerLabel(name) {
175
171
  let sanitized = name.replace(/[^a-zA-Z0-9-_]/g, "_").replace(/_{2,}/g, "_").toLowerCase();
@@ -179,7 +175,10 @@ function sanitizeServerLabel(name) {
179
175
  return sanitized;
180
176
  }
181
177
  function generateSessionId() {
182
- return firstChar() + rest();
178
+ return "sess_" + nanoid.nanoid(21);
179
+ }
180
+ function generateServerId() {
181
+ return serverIdAlphabet();
183
182
  }
184
183
  function formatOAuthState(nonce, sessionId) {
185
184
  return `${nonce}${OAUTH_STATE_SEPARATOR}${sessionId}`;
@@ -2459,7 +2458,15 @@ var MCPClient = class {
2459
2458
  if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2460
2459
  this.createdAt = Date.now();
2461
2460
  const updatedAt = this.createdAt;
2462
- console.log(`[MCPClient] Creating pending session ${this.sessionId} for connection setup`);
2461
+ this._onObservabilityEvent.fire({
2462
+ type: "mcp:client:session_created",
2463
+ level: "info",
2464
+ message: `Creating pending session ${this.sessionId} for connection setup`,
2465
+ sessionId: this.sessionId,
2466
+ serverId: this.serverId,
2467
+ timestamp: Date.now(),
2468
+ id: nanoid.nanoid()
2469
+ });
2463
2470
  await sessions.create({
2464
2471
  sessionId: this.sessionId,
2465
2472
  userId: this.userId,
@@ -2589,7 +2596,15 @@ var MCPClient = class {
2589
2596
  this.transportType = transportType;
2590
2597
  this.emitStateChange("CONNECTED");
2591
2598
  this.emitProgress("Connected successfully");
2592
- console.log(`[MCPClient] Saving active session ${this.sessionId} (connect success)`);
2599
+ this._onObservabilityEvent.fire({
2600
+ type: "mcp:client:session_saved",
2601
+ level: "info",
2602
+ message: `Saving active session ${this.sessionId} (connect success)`,
2603
+ sessionId: this.sessionId,
2604
+ serverId: this.serverId,
2605
+ timestamp: Date.now(),
2606
+ id: nanoid.nanoid()
2607
+ });
2593
2608
  await this.saveSession("active");
2594
2609
  } catch (error) {
2595
2610
  if (error instanceof auth_js.UnauthorizedError || error instanceof Error && error.message.toLowerCase().includes("unauthorized")) {
@@ -2606,7 +2621,15 @@ var MCPClient = class {
2606
2621
  throw new Error(message);
2607
2622
  }
2608
2623
  this.emitStateChange("AUTHENTICATING");
2609
- console.log(`[MCPClient] Saving pending OAuth session ${this.sessionId}`);
2624
+ this._onObservabilityEvent.fire({
2625
+ type: "mcp:client:session_saved",
2626
+ level: "info",
2627
+ message: `Saving pending OAuth session ${this.sessionId}`,
2628
+ sessionId: this.sessionId,
2629
+ serverId: this.serverId,
2630
+ timestamp: Date.now(),
2631
+ id: nanoid.nanoid()
2632
+ });
2610
2633
  await this.saveSession("pending");
2611
2634
  if (this.serverId) {
2612
2635
  this._onConnectionEvent.fire({
@@ -2681,27 +2704,25 @@ var MCPClient = class {
2681
2704
  this.emitStateChange("AUTHENTICATED");
2682
2705
  authenticatedStateEmitted = true;
2683
2706
  }
2684
- this.emitProgress("Creating authenticated client...");
2685
- this.client = new index_js.Client(
2686
- {
2687
- name: MCP_CLIENT_NAME,
2688
- version: MCP_CLIENT_VERSION
2689
- },
2690
- {
2691
- capabilities: {
2692
- extensions: {
2693
- "io.modelcontextprotocol/ui": {
2694
- mimeTypes: ["text/html+mcp"]
2695
- }
2696
- }
2697
- }
2698
- }
2699
- );
2700
2707
  this.emitStateChange("CONNECTING");
2708
+ if (this.client.transport) {
2709
+ try {
2710
+ await this.client.close();
2711
+ } catch {
2712
+ }
2713
+ }
2701
2714
  await this.client.connect(this.transport);
2702
2715
  this.transportType = currentType;
2703
2716
  this.emitStateChange("CONNECTED");
2704
- console.log(`[MCPClient] Saving active session ${this.sessionId} (OAuth complete)`);
2717
+ this._onObservabilityEvent.fire({
2718
+ type: "mcp:client:session_saved",
2719
+ level: "info",
2720
+ message: `Saving active session ${this.sessionId} (OAuth complete)`,
2721
+ sessionId: this.sessionId,
2722
+ serverId: this.serverId,
2723
+ timestamp: Date.now(),
2724
+ id: nanoid.nanoid()
2725
+ });
2705
2726
  await this.saveSession("active");
2706
2727
  return;
2707
2728
  } catch (error) {
@@ -2742,9 +2763,6 @@ var MCPClient = class {
2742
2763
  * @throws {Error} When client is not connected
2743
2764
  */
2744
2765
  async listTools() {
2745
- if (!this.client) {
2746
- throw new Error("Not connected to server");
2747
- }
2748
2766
  this.emitStateChange("DISCOVERING");
2749
2767
  try {
2750
2768
  const request = {
@@ -2782,9 +2800,6 @@ var MCPClient = class {
2782
2800
  * @throws {Error} When client is not connected
2783
2801
  */
2784
2802
  async callTool(toolName, toolArgs) {
2785
- if (!this.client) {
2786
- throw new Error("Not connected to server");
2787
- }
2788
2803
  const request = {
2789
2804
  method: "tools/call",
2790
2805
  params: {
@@ -2838,9 +2853,6 @@ var MCPClient = class {
2838
2853
  * @throws {Error} When client is not connected
2839
2854
  */
2840
2855
  async listPrompts() {
2841
- if (!this.client) {
2842
- throw new Error("Not connected to server");
2843
- }
2844
2856
  this.emitStateChange("DISCOVERING");
2845
2857
  try {
2846
2858
  const request = {
@@ -2868,9 +2880,6 @@ var MCPClient = class {
2868
2880
  * @throws {Error} When client is not connected
2869
2881
  */
2870
2882
  async getPrompt(name, args) {
2871
- if (!this.client) {
2872
- throw new Error("Not connected to server");
2873
- }
2874
2883
  const request = {
2875
2884
  method: "prompts/get",
2876
2885
  params: {
@@ -2888,9 +2897,6 @@ var MCPClient = class {
2888
2897
  * @throws {Error} When client is not connected
2889
2898
  */
2890
2899
  async listResources() {
2891
- if (!this.client) {
2892
- throw new Error("Not connected to server");
2893
- }
2894
2900
  this.emitStateChange("DISCOVERING");
2895
2901
  try {
2896
2902
  const request = {
@@ -2917,9 +2923,6 @@ var MCPClient = class {
2917
2923
  * @throws {Error} When client is not connected
2918
2924
  */
2919
2925
  async readResource(uri) {
2920
- if (!this.client) {
2921
- throw new Error("Not connected to server");
2922
- }
2923
2926
  const request = {
2924
2927
  method: "resources/read",
2925
2928
  params: {
@@ -2975,7 +2978,16 @@ var MCPClient = class {
2975
2978
  try {
2976
2979
  await this.ensureSession();
2977
2980
  } catch (error) {
2978
- console.warn("[MCPClient] Initialization failed during clearSession:", error);
2981
+ this._onObservabilityEvent.fire({
2982
+ type: "mcp:client:error",
2983
+ level: "warn",
2984
+ message: "Initialization failed during clearSession",
2985
+ sessionId: this.sessionId,
2986
+ serverId: this.serverId,
2987
+ payload: { error: String(error) },
2988
+ timestamp: Date.now(),
2989
+ id: nanoid.nanoid()
2990
+ });
2979
2991
  }
2980
2992
  if (this.oauthProvider) {
2981
2993
  await this.oauthProvider.invalidateCredentials("all");
@@ -3457,7 +3469,7 @@ var SSEConnectionManager = class {
3457
3469
  async connect(params) {
3458
3470
  const { serverName, serverUrl, callbackUrl, transportType } = params;
3459
3471
  const headers = normalizeHeaders(params.headers);
3460
- const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : await sessions.generateSessionId();
3472
+ const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : generateServerId();
3461
3473
  const existingSessions = await sessions.list(this.userId);
3462
3474
  const duplicate = existingSessions.find(
3463
3475
  (s) => s.serverId === serverId || s.serverUrl === serverUrl