@mcp-ts/sdk 1.3.5 → 1.3.7

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 (70) hide show
  1. package/dist/adapters/agui-adapter.d.mts +1 -1
  2. package/dist/adapters/agui-adapter.d.ts +1 -1
  3. package/dist/adapters/agui-adapter.js +2 -2
  4. package/dist/adapters/agui-adapter.js.map +1 -1
  5. package/dist/adapters/agui-adapter.mjs +2 -2
  6. package/dist/adapters/agui-adapter.mjs.map +1 -1
  7. package/dist/adapters/agui-middleware.d.mts +1 -1
  8. package/dist/adapters/agui-middleware.d.ts +1 -1
  9. package/dist/adapters/agui-middleware.js.map +1 -1
  10. package/dist/adapters/agui-middleware.mjs.map +1 -1
  11. package/dist/adapters/ai-adapter.d.mts +1 -1
  12. package/dist/adapters/ai-adapter.d.ts +1 -1
  13. package/dist/adapters/ai-adapter.js +1 -1
  14. package/dist/adapters/ai-adapter.js.map +1 -1
  15. package/dist/adapters/ai-adapter.mjs +1 -1
  16. package/dist/adapters/ai-adapter.mjs.map +1 -1
  17. package/dist/adapters/langchain-adapter.d.mts +1 -1
  18. package/dist/adapters/langchain-adapter.d.ts +1 -1
  19. package/dist/adapters/langchain-adapter.js +1 -1
  20. package/dist/adapters/langchain-adapter.js.map +1 -1
  21. package/dist/adapters/langchain-adapter.mjs +1 -1
  22. package/dist/adapters/langchain-adapter.mjs.map +1 -1
  23. package/dist/adapters/mastra-adapter.d.mts +1 -1
  24. package/dist/adapters/mastra-adapter.d.ts +1 -1
  25. package/dist/adapters/mastra-adapter.js +1 -1
  26. package/dist/adapters/mastra-adapter.js.map +1 -1
  27. package/dist/adapters/mastra-adapter.mjs +1 -1
  28. package/dist/adapters/mastra-adapter.mjs.map +1 -1
  29. package/dist/bin/mcp-ts.d.mts +1 -0
  30. package/dist/bin/mcp-ts.d.ts +1 -0
  31. package/dist/bin/mcp-ts.js +105 -0
  32. package/dist/bin/mcp-ts.js.map +1 -0
  33. package/dist/bin/mcp-ts.mjs +82 -0
  34. package/dist/bin/mcp-ts.mjs.map +1 -0
  35. package/dist/index.d.mts +1 -1
  36. package/dist/index.d.ts +1 -1
  37. package/dist/index.js +411 -90
  38. package/dist/index.js.map +1 -1
  39. package/dist/index.mjs +350 -91
  40. package/dist/index.mjs.map +1 -1
  41. package/dist/{multi-session-client-BYLarghq.d.ts → multi-session-client-CHE8QpVE.d.ts} +75 -5
  42. package/dist/{multi-session-client-CzhMkE0k.d.mts → multi-session-client-CQsRbxYI.d.mts} +75 -5
  43. package/dist/server/index.d.mts +1 -1
  44. package/dist/server/index.d.ts +1 -1
  45. package/dist/server/index.js +394 -90
  46. package/dist/server/index.js.map +1 -1
  47. package/dist/server/index.mjs +350 -91
  48. package/dist/server/index.mjs.map +1 -1
  49. package/dist/shared/index.js +10 -2
  50. package/dist/shared/index.js.map +1 -1
  51. package/dist/shared/index.mjs +10 -2
  52. package/dist/shared/index.mjs.map +1 -1
  53. package/package.json +19 -6
  54. package/src/adapters/agui-adapter.ts +222 -222
  55. package/src/adapters/ai-adapter.ts +115 -115
  56. package/src/adapters/langchain-adapter.ts +127 -127
  57. package/src/adapters/mastra-adapter.ts +126 -126
  58. package/src/bin/mcp-ts.ts +102 -0
  59. package/src/server/handlers/nextjs-handler.ts +12 -12
  60. package/src/server/handlers/sse-handler.ts +61 -61
  61. package/src/server/mcp/multi-session-client.ts +135 -39
  62. package/src/server/storage/file-backend.ts +4 -16
  63. package/src/server/storage/index.ts +68 -25
  64. package/src/server/storage/memory-backend.ts +7 -16
  65. package/src/server/storage/redis-backend.ts +12 -16
  66. package/src/server/storage/sqlite-backend.ts +3 -6
  67. package/src/server/storage/supabase-backend.ts +228 -0
  68. package/src/shared/event-routing.ts +28 -28
  69. package/src/shared/utils.ts +22 -0
  70. package/supabase/migrations/20260330195700_install_mcp_sessions.sql +84 -0
@@ -367,25 +367,95 @@ interface MultiSessionOptions {
367
367
  retryDelay?: number;
368
368
  }
369
369
  /**
370
- * Manages multiple MCP connections for a single user identity.
371
- * Allows aggregating tools from all connected servers.
370
+ * Manages multiple MCP client connections for a single user identity.
371
+ *
372
+ * On a traditional long-running server, you can cache this instance per user
373
+ * so the connections stay alive between requests. On serverless, a new instance
374
+ * will be created per invocation, but the underlying session data is always
375
+ * read from the storage backend so nothing is lost between calls.
372
376
  */
373
377
  declare class MultiSessionClient {
374
378
  private clients;
375
379
  private identity;
376
380
  private options;
381
+ /**
382
+ * Creates a new MultiSessionClient for the given user identity.
383
+ *
384
+ * @param identity - A unique string identifying the user (e.g. user ID or email).
385
+ * @param options - Optional tuning for connection timeout, retry count, and retry delay.
386
+ * Falls back to sensible defaults if not provided.
387
+ */
377
388
  constructor(identity: string, options?: MultiSessionOptions);
389
+ /**
390
+ * Fetches all sessions for this identity from storage and returns only the
391
+ * ones that are ready to connect.
392
+ *
393
+ * A session is considered connectable when:
394
+ * - It has a `serverId`, `serverUrl`, and `callbackUrl` (i.e. it was fully initialized)
395
+ * - Its `active` flag is not explicitly `false` — sessions with `active: false` are
396
+ * either mid-OAuth flow, auth-pending, or previously failed. We skip those here
397
+ * and let the OAuth flow complete separately before we try to reconnect them.
398
+ *
399
+ * Note: Sessions where `active` is `undefined` (legacy records) are included
400
+ * for backwards compatibility.
401
+ */
378
402
  private getActiveSessions;
403
+ /**
404
+ * Connects to a list of sessions in controlled batches of `CONNECTION_BATCH_SIZE`.
405
+ *
406
+ * Batching prevents overwhelming the event loop or external servers when a user
407
+ * has many active MCP sessions (e.g. 20+ servers). Within each batch, sessions
408
+ * are connected concurrently using `Promise.all` for speed.
409
+ */
379
410
  private connectInBatches;
411
+ private connectionPromises;
412
+ /**
413
+ * Connects a single session, with built-in deduplication to prevent race conditions.
414
+ *
415
+ * - If a client for this session already exists and is connected, returns immediately.
416
+ * - If a connection attempt for this session is already in-flight (e.g. from a
417
+ * concurrent call), it joins the existing promise instead of starting a new one.
418
+ * This is the key concurrency lock — the `connectionPromises` map acts as a
419
+ * per-session mutex so we never spin up two physical connections for the same session.
420
+ * - On completion (success or failure), the promise is cleaned up from the map.
421
+ */
380
422
  private connectSession;
381
- private createAndConnectClient;
423
+ /**
424
+ * The core connection loop for a single session.
425
+ *
426
+ * Attempts to establish a physical MCP connection, retrying up to `maxRetries` times
427
+ * if the connection fails. Each attempt:
428
+ * 1. Creates a fresh `MCPClient` instance from the session data.
429
+ * 2. Races the connect call against a timeout promise — if the server doesn't respond
430
+ * within `timeoutMs`, the attempt is aborted and counted as a failure.
431
+ * 3. On success, replaces any stale client entry for this session in the `clients` array.
432
+ * 4. On failure, waits `retryDelay` ms before the next attempt.
433
+ *
434
+ * If all attempts are exhausted, logs an error and returns silently (does not throw),
435
+ * so a single bad server doesn't block the rest of the batch from connecting.
436
+ */
437
+ private establishConnectionWithRetries;
438
+ /**
439
+ * The main entry point. Fetches all active sessions for this identity from
440
+ * storage and establishes connections to all of them in batches.
441
+ *
442
+ * Call this once after creating the client. On traditional servers, you can
443
+ * cache the `MultiSessionClient` instance after calling `connect()` to avoid
444
+ * re-fetching and re-connecting on every request.
445
+ */
382
446
  connect(): Promise<void>;
383
447
  /**
384
- * Returns the array of currently connected clients.
448
+ * Returns all currently connected `MCPClient` instances.
449
+ *
450
+ * Use this to enumerate available tools across all connected servers,
451
+ * or to route a tool call to the right client by `serverId`.
385
452
  */
386
453
  getClients(): MCPClient[];
387
454
  /**
388
- * Disconnects all clients.
455
+ * Gracefully disconnects all active MCP clients and clears the internal client list.
456
+ *
457
+ * Call this during server shutdown or when a user logs out to free up
458
+ * underlying transport resources (SSE streams, HTTP connections, etc.).
389
459
  */
390
460
  disconnect(): void;
391
461
  }
@@ -367,25 +367,95 @@ interface MultiSessionOptions {
367
367
  retryDelay?: number;
368
368
  }
369
369
  /**
370
- * Manages multiple MCP connections for a single user identity.
371
- * Allows aggregating tools from all connected servers.
370
+ * Manages multiple MCP client connections for a single user identity.
371
+ *
372
+ * On a traditional long-running server, you can cache this instance per user
373
+ * so the connections stay alive between requests. On serverless, a new instance
374
+ * will be created per invocation, but the underlying session data is always
375
+ * read from the storage backend so nothing is lost between calls.
372
376
  */
373
377
  declare class MultiSessionClient {
374
378
  private clients;
375
379
  private identity;
376
380
  private options;
381
+ /**
382
+ * Creates a new MultiSessionClient for the given user identity.
383
+ *
384
+ * @param identity - A unique string identifying the user (e.g. user ID or email).
385
+ * @param options - Optional tuning for connection timeout, retry count, and retry delay.
386
+ * Falls back to sensible defaults if not provided.
387
+ */
377
388
  constructor(identity: string, options?: MultiSessionOptions);
389
+ /**
390
+ * Fetches all sessions for this identity from storage and returns only the
391
+ * ones that are ready to connect.
392
+ *
393
+ * A session is considered connectable when:
394
+ * - It has a `serverId`, `serverUrl`, and `callbackUrl` (i.e. it was fully initialized)
395
+ * - Its `active` flag is not explicitly `false` — sessions with `active: false` are
396
+ * either mid-OAuth flow, auth-pending, or previously failed. We skip those here
397
+ * and let the OAuth flow complete separately before we try to reconnect them.
398
+ *
399
+ * Note: Sessions where `active` is `undefined` (legacy records) are included
400
+ * for backwards compatibility.
401
+ */
378
402
  private getActiveSessions;
403
+ /**
404
+ * Connects to a list of sessions in controlled batches of `CONNECTION_BATCH_SIZE`.
405
+ *
406
+ * Batching prevents overwhelming the event loop or external servers when a user
407
+ * has many active MCP sessions (e.g. 20+ servers). Within each batch, sessions
408
+ * are connected concurrently using `Promise.all` for speed.
409
+ */
379
410
  private connectInBatches;
411
+ private connectionPromises;
412
+ /**
413
+ * Connects a single session, with built-in deduplication to prevent race conditions.
414
+ *
415
+ * - If a client for this session already exists and is connected, returns immediately.
416
+ * - If a connection attempt for this session is already in-flight (e.g. from a
417
+ * concurrent call), it joins the existing promise instead of starting a new one.
418
+ * This is the key concurrency lock — the `connectionPromises` map acts as a
419
+ * per-session mutex so we never spin up two physical connections for the same session.
420
+ * - On completion (success or failure), the promise is cleaned up from the map.
421
+ */
380
422
  private connectSession;
381
- private createAndConnectClient;
423
+ /**
424
+ * The core connection loop for a single session.
425
+ *
426
+ * Attempts to establish a physical MCP connection, retrying up to `maxRetries` times
427
+ * if the connection fails. Each attempt:
428
+ * 1. Creates a fresh `MCPClient` instance from the session data.
429
+ * 2. Races the connect call against a timeout promise — if the server doesn't respond
430
+ * within `timeoutMs`, the attempt is aborted and counted as a failure.
431
+ * 3. On success, replaces any stale client entry for this session in the `clients` array.
432
+ * 4. On failure, waits `retryDelay` ms before the next attempt.
433
+ *
434
+ * If all attempts are exhausted, logs an error and returns silently (does not throw),
435
+ * so a single bad server doesn't block the rest of the batch from connecting.
436
+ */
437
+ private establishConnectionWithRetries;
438
+ /**
439
+ * The main entry point. Fetches all active sessions for this identity from
440
+ * storage and establishes connections to all of them in batches.
441
+ *
442
+ * Call this once after creating the client. On traditional servers, you can
443
+ * cache the `MultiSessionClient` instance after calling `connect()` to avoid
444
+ * re-fetching and re-connecting on every request.
445
+ */
382
446
  connect(): Promise<void>;
383
447
  /**
384
- * Returns the array of currently connected clients.
448
+ * Returns all currently connected `MCPClient` instances.
449
+ *
450
+ * Use this to enumerate available tools across all connected servers,
451
+ * or to route a tool call to the right client by `serverId`.
385
452
  */
386
453
  getClients(): MCPClient[];
387
454
  /**
388
- * Disconnects all clients.
455
+ * Gracefully disconnects all active MCP clients and clears the internal client list.
456
+ *
457
+ * Call this during server shutdown or when a user logs out to free up
458
+ * underlying transport resources (SSE streams, HTTP connections, etc.).
389
459
  */
390
460
  disconnect(): void;
391
461
  }
@@ -1,4 +1,4 @@
1
- export { M as MCPClient, a as MultiSessionClient, S as StorageOAuthClientProvider } from '../multi-session-client-CzhMkE0k.mjs';
1
+ export { M as MCPClient, a as MultiSessionClient, S as StorageOAuthClientProvider } from '../multi-session-client-CQsRbxYI.mjs';
2
2
  export { U as UnauthorizedError, s as sanitizeServerLabel } from '../utils-0qmYrqoa.mjs';
3
3
  import { OAuthClientInformationMixed, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
4
4
  export { OAuthClientInformation, OAuthClientInformationFull, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
@@ -1,4 +1,4 @@
1
- export { M as MCPClient, a as MultiSessionClient, S as StorageOAuthClientProvider } from '../multi-session-client-BYLarghq.js';
1
+ export { M as MCPClient, a as MultiSessionClient, S as StorageOAuthClientProvider } from '../multi-session-client-CHE8QpVE.js';
2
2
  export { U as UnauthorizedError, s as sanitizeServerLabel } from '../utils-0qmYrqoa.js';
3
3
  import { OAuthClientInformationMixed, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
4
4
  export { OAuthClientInformation, OAuthClientInformationFull, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';