@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.
@@ -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}`;
@@ -2384,7 +2383,15 @@ var MCPClient = class {
2384
2383
  if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2385
2384
  this.createdAt = Date.now();
2386
2385
  const updatedAt = this.createdAt;
2387
- 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
+ });
2388
2395
  await sessions.create({
2389
2396
  sessionId: this.sessionId,
2390
2397
  userId: this.userId,
@@ -2514,7 +2521,15 @@ var MCPClient = class {
2514
2521
  this.transportType = transportType;
2515
2522
  this.emitStateChange("CONNECTED");
2516
2523
  this.emitProgress("Connected successfully");
2517
- 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
+ });
2518
2533
  await this.saveSession("active");
2519
2534
  } catch (error) {
2520
2535
  if (error instanceof auth_js.UnauthorizedError || error instanceof Error && error.message.toLowerCase().includes("unauthorized")) {
@@ -2531,7 +2546,15 @@ var MCPClient = class {
2531
2546
  throw new Error(message);
2532
2547
  }
2533
2548
  this.emitStateChange("AUTHENTICATING");
2534
- 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
+ });
2535
2558
  await this.saveSession("pending");
2536
2559
  if (this.serverId) {
2537
2560
  this._onConnectionEvent.fire({
@@ -2606,27 +2629,25 @@ var MCPClient = class {
2606
2629
  this.emitStateChange("AUTHENTICATED");
2607
2630
  authenticatedStateEmitted = true;
2608
2631
  }
2609
- this.emitProgress("Creating authenticated client...");
2610
- this.client = new index_js.Client(
2611
- {
2612
- name: MCP_CLIENT_NAME,
2613
- version: MCP_CLIENT_VERSION
2614
- },
2615
- {
2616
- capabilities: {
2617
- extensions: {
2618
- "io.modelcontextprotocol/ui": {
2619
- mimeTypes: ["text/html+mcp"]
2620
- }
2621
- }
2622
- }
2623
- }
2624
- );
2625
2632
  this.emitStateChange("CONNECTING");
2633
+ if (this.client.transport) {
2634
+ try {
2635
+ await this.client.close();
2636
+ } catch {
2637
+ }
2638
+ }
2626
2639
  await this.client.connect(this.transport);
2627
2640
  this.transportType = currentType;
2628
2641
  this.emitStateChange("CONNECTED");
2629
- 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
+ });
2630
2651
  await this.saveSession("active");
2631
2652
  return;
2632
2653
  } catch (error) {
@@ -2667,9 +2688,6 @@ var MCPClient = class {
2667
2688
  * @throws {Error} When client is not connected
2668
2689
  */
2669
2690
  async listTools() {
2670
- if (!this.client) {
2671
- throw new Error("Not connected to server");
2672
- }
2673
2691
  this.emitStateChange("DISCOVERING");
2674
2692
  try {
2675
2693
  const request = {
@@ -2707,9 +2725,6 @@ var MCPClient = class {
2707
2725
  * @throws {Error} When client is not connected
2708
2726
  */
2709
2727
  async callTool(toolName, toolArgs) {
2710
- if (!this.client) {
2711
- throw new Error("Not connected to server");
2712
- }
2713
2728
  const request = {
2714
2729
  method: "tools/call",
2715
2730
  params: {
@@ -2763,9 +2778,6 @@ var MCPClient = class {
2763
2778
  * @throws {Error} When client is not connected
2764
2779
  */
2765
2780
  async listPrompts() {
2766
- if (!this.client) {
2767
- throw new Error("Not connected to server");
2768
- }
2769
2781
  this.emitStateChange("DISCOVERING");
2770
2782
  try {
2771
2783
  const request = {
@@ -2793,9 +2805,6 @@ var MCPClient = class {
2793
2805
  * @throws {Error} When client is not connected
2794
2806
  */
2795
2807
  async getPrompt(name, args) {
2796
- if (!this.client) {
2797
- throw new Error("Not connected to server");
2798
- }
2799
2808
  const request = {
2800
2809
  method: "prompts/get",
2801
2810
  params: {
@@ -2813,9 +2822,6 @@ var MCPClient = class {
2813
2822
  * @throws {Error} When client is not connected
2814
2823
  */
2815
2824
  async listResources() {
2816
- if (!this.client) {
2817
- throw new Error("Not connected to server");
2818
- }
2819
2825
  this.emitStateChange("DISCOVERING");
2820
2826
  try {
2821
2827
  const request = {
@@ -2842,9 +2848,6 @@ var MCPClient = class {
2842
2848
  * @throws {Error} When client is not connected
2843
2849
  */
2844
2850
  async readResource(uri) {
2845
- if (!this.client) {
2846
- throw new Error("Not connected to server");
2847
- }
2848
2851
  const request = {
2849
2852
  method: "resources/read",
2850
2853
  params: {
@@ -2900,7 +2903,16 @@ var MCPClient = class {
2900
2903
  try {
2901
2904
  await this.ensureSession();
2902
2905
  } catch (error) {
2903
- 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
+ });
2904
2916
  }
2905
2917
  if (this.oauthProvider) {
2906
2918
  await this.oauthProvider.invalidateCredentials("all");
@@ -3382,7 +3394,7 @@ var SSEConnectionManager = class {
3382
3394
  async connect(params) {
3383
3395
  const { serverName, serverUrl, callbackUrl, transportType } = params;
3384
3396
  const headers = normalizeHeaders(params.headers);
3385
- 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();
3386
3398
  const existingSessions = await sessions.list(this.userId);
3387
3399
  const duplicate = existingSessions.find(
3388
3400
  (s) => s.serverId === serverId || s.serverUrl === serverUrl