@mcp-ts/sdk 2.5.2 → 2.6.0

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 (73) hide show
  1. package/README.md +3 -574
  2. package/dist/adapters/agui-adapter.d.mts +4 -4
  3. package/dist/adapters/agui-adapter.d.ts +4 -4
  4. package/dist/adapters/agui-middleware.d.mts +4 -4
  5. package/dist/adapters/agui-middleware.d.ts +4 -4
  6. package/dist/adapters/ai-adapter.d.mts +4 -4
  7. package/dist/adapters/ai-adapter.d.ts +4 -4
  8. package/dist/adapters/langchain-adapter.d.mts +4 -4
  9. package/dist/adapters/langchain-adapter.d.ts +4 -4
  10. package/dist/adapters/mastra-adapter.d.mts +3 -3
  11. package/dist/adapters/mastra-adapter.d.ts +3 -3
  12. package/dist/client/index.d.mts +2 -2
  13. package/dist/client/index.d.ts +2 -2
  14. package/dist/client/index.js +3 -0
  15. package/dist/client/index.js.map +1 -1
  16. package/dist/client/index.mjs +3 -0
  17. package/dist/client/index.mjs.map +1 -1
  18. package/dist/client/react.d.mts +10 -18
  19. package/dist/client/react.d.ts +10 -18
  20. package/dist/client/react.js +17 -0
  21. package/dist/client/react.js.map +1 -1
  22. package/dist/client/react.mjs +17 -0
  23. package/dist/client/react.mjs.map +1 -1
  24. package/dist/client/vue.d.mts +4 -4
  25. package/dist/client/vue.d.ts +4 -4
  26. package/dist/client/vue.js +3 -0
  27. package/dist/client/vue.js.map +1 -1
  28. package/dist/client/vue.mjs +3 -0
  29. package/dist/client/vue.mjs.map +1 -1
  30. package/dist/{index-B3GPsPsi.d.mts → index-BrDkbut5.d.ts} +2 -1
  31. package/dist/{index-gNdSQTSz.d.ts → index-DHQvlx4K.d.mts} +2 -1
  32. package/dist/index.d.mts +5 -5
  33. package/dist/index.d.ts +5 -5
  34. package/dist/index.js +1056 -863
  35. package/dist/index.js.map +1 -1
  36. package/dist/index.mjs +1048 -858
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/{multi-session-client-CB4oDrQX.d.ts → multi-session-client-CQzymvHl.d.ts} +183 -227
  39. package/dist/{multi-session-client-xj3iQIv6.d.mts → multi-session-client-C_Ja2TuV.d.mts} +183 -227
  40. package/dist/server/index.d.mts +338 -88
  41. package/dist/server/index.d.ts +338 -88
  42. package/dist/server/index.js +1054 -864
  43. package/dist/server/index.js.map +1 -1
  44. package/dist/server/index.mjs +1046 -859
  45. package/dist/server/index.mjs.map +1 -1
  46. package/dist/shared/index.d.mts +4 -4
  47. package/dist/shared/index.d.ts +4 -4
  48. package/dist/shared/index.js.map +1 -1
  49. package/dist/shared/index.mjs.map +1 -1
  50. package/dist/{tool-router-CsKVXbQB.d.mts → tool-router-C1n7OXbl.d.mts} +1 -1
  51. package/dist/{tool-router-C-Mw1_BQ.d.ts → tool-router-CBIawFnt.d.ts} +1 -1
  52. package/dist/{types-6SmXege4.d.mts → types-DX71u9gR.d.mts} +11 -3
  53. package/dist/{types-6SmXege4.d.ts → types-DX71u9gR.d.ts} +11 -3
  54. package/migrations/neon/20260513010000_install_mcp_sessions.sql +4 -34
  55. package/migrations/supabase/20260330195700_install_mcp_sessions.sql +5 -62
  56. package/package.json +2 -1
  57. package/src/client/core/sse-client.ts +5 -0
  58. package/src/client/react/use-mcp.ts +27 -28
  59. package/src/server/handlers/sse-handler.ts +671 -629
  60. package/src/server/index.ts +2 -1
  61. package/src/server/mcp/multi-session-client.ts +34 -10
  62. package/src/server/mcp/oauth-client.ts +197 -205
  63. package/src/server/mcp/storage-oauth-provider.ts +149 -29
  64. package/src/server/mcp/tool-policy-gateway.ts +11 -1
  65. package/src/server/storage/file-backend.ts +25 -21
  66. package/src/server/storage/index.ts +71 -0
  67. package/src/server/storage/memory-backend.ts +24 -12
  68. package/src/server/storage/neon-backend.ts +70 -72
  69. package/src/server/storage/redis-backend.ts +26 -36
  70. package/src/server/storage/sqlite-backend.ts +25 -39
  71. package/src/server/storage/supabase-backend.ts +45 -54
  72. package/src/server/storage/types.ts +22 -2
  73. package/src/shared/types.ts +13 -1
@@ -6,9 +6,10 @@ var streamableHttp_js = require('@modelcontextprotocol/sdk/client/streamableHttp
6
6
  var sse_js = require('@modelcontextprotocol/sdk/client/sse.js');
7
7
  var auth_js = require('@modelcontextprotocol/sdk/client/auth.js');
8
8
  var types_js = require('@modelcontextprotocol/sdk/types.js');
9
+ var async_hooks = require('async_hooks');
9
10
  var fs2 = require('fs');
10
11
  var path = require('path');
11
- var crypto = require('crypto');
12
+ var crypto$1 = require('crypto');
12
13
 
13
14
  function _interopNamespace(e) {
14
15
  if (e && e.__esModule) return e;
@@ -43,9 +44,9 @@ var __export = (target, all) => {
43
44
  };
44
45
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
45
46
 
46
- // node_modules/tsup/assets/cjs_shims.js
47
+ // ../../node_modules/tsup/assets/cjs_shims.js
47
48
  var init_cjs_shims = __esm({
48
- "node_modules/tsup/assets/cjs_shims.js"() {
49
+ "../../node_modules/tsup/assets/cjs_shims.js"() {
49
50
  }
50
51
  });
51
52
 
@@ -325,7 +326,6 @@ var RedisStorageBackend = class {
325
326
  constructor(redis2) {
326
327
  this.redis = redis2;
327
328
  __publicField(this, "KEY_PREFIX", "mcp:session:");
328
- __publicField(this, "CREDENTIALS_KEY_PREFIX", "mcp:credentials:");
329
329
  __publicField(this, "USER_ID_KEY_PREFIX", "mcp:userId:");
330
330
  __publicField(this, "USER_ID_KEY_SUFFIX", ":sessions");
331
331
  }
@@ -344,9 +344,6 @@ var RedisStorageBackend = class {
344
344
  getSessionKey(userId, sessionId) {
345
345
  return `${this.KEY_PREFIX}${userId}:${sessionId}`;
346
346
  }
347
- getCredentialsKey(userId, sessionId) {
348
- return `${this.CREDENTIALS_KEY_PREFIX}${userId}:${sessionId}`;
349
- }
350
347
  /**
351
348
  * Generates Redis key for tracking all sessions for a user
352
349
  * @private
@@ -436,48 +433,54 @@ var RedisStorageBackend = class {
436
433
  }
437
434
  async patchCredentials(userId, sessionId, data) {
438
435
  const sessionKey = this.getSessionKey(userId, sessionId);
439
- const credentialsKey = this.getCredentialsKey(userId, sessionId);
440
- const sessionExists = await this.redis.exists(sessionKey);
441
- if (!sessionExists) {
436
+ const currentStr = await this.redis.get(sessionKey);
437
+ if (!currentStr) {
442
438
  throw new Error(`Session ${sessionId} not found for userId ${userId}`);
443
439
  }
444
- const session = await this.get(userId, sessionId);
445
- const currentTtl = await this.redis.ttl(sessionKey);
446
- const effectiveTtl = currentTtl > 0 && session ? currentTtl : resolveSessionRedisTtlSeconds(session ?? { status: "pending" });
447
- const currentStr = await this.redis.get(credentialsKey);
448
- const current = currentStr ? JSON.parse(currentStr) : { sessionId, userId };
449
- const credentials = { ...current, ...data, sessionId, userId };
450
- if (effectiveTtl > 0) {
451
- await this.redis.set(credentialsKey, JSON.stringify(credentials), "EX", effectiveTtl);
452
- } else {
453
- await this.redis.set(credentialsKey, JSON.stringify(credentials));
454
- }
440
+ const current = JSON.parse(currentStr);
441
+ const updated = { ...current, ...data, sessionId, userId };
442
+ const effectiveTtl = resolveSessionRedisTtlSeconds(updated);
443
+ await this.redis.set(sessionKey, JSON.stringify(updated), "EX", effectiveTtl);
455
444
  }
456
- async get(userId, sessionId) {
445
+ async get(userId, sessionId, options) {
457
446
  try {
458
447
  const sessionKey = this.getSessionKey(userId, sessionId);
459
448
  const sessionDataStr = await this.redis.get(sessionKey);
460
- if (!sessionDataStr) {
461
- return null;
449
+ if (!sessionDataStr) return null;
450
+ const session = normalizeStoredSession(JSON.parse(sessionDataStr));
451
+ if (!options?.includeCredentials) {
452
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState, ...sessionOnly } = session;
453
+ return sessionOnly;
462
454
  }
463
- const session = JSON.parse(sessionDataStr);
464
- return normalizeStoredSession(session);
455
+ return session;
465
456
  } catch (error) {
466
457
  console.error("[RedisStorageBackend] Failed to get session:", error);
467
458
  return null;
468
459
  }
469
460
  }
470
461
  async getCredentials(userId, sessionId) {
471
- const session = await this.get(userId, sessionId);
462
+ const session = await this.get(userId, sessionId, { includeCredentials: true });
472
463
  if (!session) return null;
473
- const credentialsStr = await this.redis.get(this.getCredentialsKey(userId, sessionId));
474
- return credentialsStr ? JSON.parse(credentialsStr) : { sessionId, userId };
464
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState } = session;
465
+ return {
466
+ sessionId,
467
+ userId,
468
+ clientInformation,
469
+ tokens,
470
+ codeVerifier,
471
+ codeVerifierChallenge,
472
+ codeVerifierNonce,
473
+ clientId,
474
+ oauthState
475
+ };
475
476
  }
476
477
  async clearCredentials(userId, sessionId) {
477
478
  await this.patchCredentials(userId, sessionId, {
478
479
  clientInformation: null,
479
480
  tokens: null,
480
481
  codeVerifier: null,
482
+ codeVerifierChallenge: null,
483
+ codeVerifierNonce: null,
481
484
  clientId: null,
482
485
  oauthState: null
483
486
  });
@@ -510,10 +513,9 @@ var RedisStorageBackend = class {
510
513
  async delete(userId, sessionId) {
511
514
  try {
512
515
  const sessionKey = this.getSessionKey(userId, sessionId);
513
- const credentialsKey = this.getCredentialsKey(userId, sessionId);
514
516
  const userIdKey = this.getUserIdKey(userId);
515
517
  await this.redis.srem(userIdKey, sessionId);
516
- await this.redis.del(sessionKey, credentialsKey);
518
+ await this.redis.del(sessionKey);
517
519
  } catch (error) {
518
520
  console.error("[RedisStorageBackend] Failed to remove session:", error);
519
521
  }
@@ -544,9 +546,8 @@ var RedisStorageBackend = class {
544
546
  async clearAll() {
545
547
  try {
546
548
  const keys = await this.scanKeys(`${this.KEY_PREFIX}*`);
547
- const credentialKeys = await this.scanKeys(`${this.CREDENTIALS_KEY_PREFIX}*`);
548
549
  const userIdKeys = await this.scanKeys(`${this.USER_ID_KEY_PREFIX}*${this.USER_ID_KEY_SUFFIX}`);
549
- const allKeys = [...keys, ...credentialKeys, ...userIdKeys];
550
+ const allKeys = [...keys, ...userIdKeys];
550
551
  if (allKeys.length > 0) {
551
552
  await this.redis.del(...allKeys);
552
553
  }
@@ -570,7 +571,6 @@ var RedisStorageBackend = class {
570
571
  const staleSessionIds = sessionIds.filter((_, index) => existenceChecks[index] === 0);
571
572
  if (staleSessionIds.length > 0) {
572
573
  await this.redis.srem(userIdKey, ...staleSessionIds);
573
- await this.redis.del(...staleSessionIds.map((sessionId) => this.getCredentialsKey(userId, sessionId)));
574
574
  }
575
575
  const remainingCount = await this.redis.scard(userIdKey);
576
576
  if (remainingCount === 0) {
@@ -596,7 +596,6 @@ var MemoryStorageBackend = class {
596
596
  constructor() {
597
597
  // Map<userId:sessionId, Session>
598
598
  __publicField(this, "sessions", /* @__PURE__ */ new Map());
599
- __publicField(this, "credentials", /* @__PURE__ */ new Map());
600
599
  // Map<userId, Set<sessionId>>
601
600
  __publicField(this, "userIdSessions", /* @__PURE__ */ new Map());
602
601
  }
@@ -634,26 +633,46 @@ var MemoryStorageBackend = class {
634
633
  }
635
634
  async patchCredentials(userId, sessionId, data) {
636
635
  const sessionKey = this.getSessionKey(userId, sessionId);
637
- if (!this.sessions.has(sessionKey)) {
636
+ const current = this.sessions.get(sessionKey);
637
+ if (!current) {
638
638
  throw new Error(`Session ${sessionId} not found`);
639
639
  }
640
- const current = this.credentials.get(sessionKey) ?? { sessionId, userId };
641
- this.credentials.set(sessionKey, { ...current, ...data, sessionId, userId });
640
+ this.sessions.set(sessionKey, { ...current, ...data, sessionId, userId });
642
641
  }
643
- async get(userId, sessionId) {
642
+ async get(userId, sessionId, options) {
644
643
  const sessionKey = this.getSessionKey(userId, sessionId);
645
- return this.sessions.get(sessionKey) || null;
644
+ const session = this.sessions.get(sessionKey) || null;
645
+ if (!session) return session;
646
+ if (!options?.includeCredentials) {
647
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState, ...sessionOnly } = session;
648
+ return sessionOnly;
649
+ }
650
+ return session;
646
651
  }
647
652
  async getCredentials(userId, sessionId) {
648
653
  const sessionKey = this.getSessionKey(userId, sessionId);
649
- if (!this.sessions.has(sessionKey)) return null;
650
- return this.credentials.get(sessionKey) ?? { sessionId, userId };
654
+ const session = this.sessions.get(sessionKey);
655
+ if (!session) return null;
656
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState } = session;
657
+ return {
658
+ sessionId,
659
+ userId,
660
+ clientInformation,
661
+ tokens,
662
+ codeVerifier,
663
+ codeVerifierChallenge,
664
+ codeVerifierNonce,
665
+ clientId,
666
+ oauthState
667
+ };
651
668
  }
652
669
  async clearCredentials(userId, sessionId) {
653
670
  await this.patchCredentials(userId, sessionId, {
654
671
  clientInformation: null,
655
672
  tokens: null,
656
673
  codeVerifier: null,
674
+ codeVerifierChallenge: null,
675
+ codeVerifierNonce: null,
657
676
  clientId: null,
658
677
  oauthState: null
659
678
  });
@@ -677,7 +696,6 @@ var MemoryStorageBackend = class {
677
696
  async delete(userId, sessionId) {
678
697
  const sessionKey = this.getSessionKey(userId, sessionId);
679
698
  this.sessions.delete(sessionKey);
680
- this.credentials.delete(sessionKey);
681
699
  const set = this.userIdSessions.get(userId);
682
700
  if (set) {
683
701
  set.delete(sessionId);
@@ -691,14 +709,12 @@ var MemoryStorageBackend = class {
691
709
  }
692
710
  async clearAll() {
693
711
  this.sessions.clear();
694
- this.credentials.clear();
695
712
  this.userIdSessions.clear();
696
713
  }
697
714
  async cleanupExpired() {
698
715
  for (const [key, session] of this.sessions.entries()) {
699
716
  if (!isSessionExpired(session)) continue;
700
717
  this.sessions.delete(key);
701
- this.credentials.delete(key);
702
718
  const set = this.userIdSessions.get(session.userId);
703
719
  if (set) {
704
720
  set.delete(session.sessionId);
@@ -721,7 +737,6 @@ var FileStorageBackend = class {
721
737
  constructor(options = {}) {
722
738
  __publicField(this, "filePath");
723
739
  __publicField(this, "memoryCache", null);
724
- __publicField(this, "credentialsCache", null);
725
740
  __publicField(this, "initialized", false);
726
741
  this.filePath = options.path || "./sessions.json";
727
742
  }
@@ -736,22 +751,15 @@ var FileStorageBackend = class {
736
751
  const data = await fs2.promises.readFile(this.filePath, "utf-8");
737
752
  const json = JSON.parse(data);
738
753
  this.memoryCache = /* @__PURE__ */ new Map();
739
- this.credentialsCache = /* @__PURE__ */ new Map();
740
754
  if (Array.isArray(json.sessions)) {
741
755
  json.sessions.forEach((s) => {
742
756
  const session = normalizeStoredSession(s);
743
757
  this.memoryCache.set(this.getSessionKey(session.userId || "unknown", session.sessionId), session);
744
758
  });
745
759
  }
746
- if (Array.isArray(json.credentials)) {
747
- json.credentials.forEach((c) => {
748
- this.credentialsCache.set(this.getSessionKey(c.userId, c.sessionId), c);
749
- });
750
- }
751
760
  } catch (error) {
752
761
  if (error.code === "ENOENT") {
753
762
  this.memoryCache = /* @__PURE__ */ new Map();
754
- this.credentialsCache = /* @__PURE__ */ new Map();
755
763
  await this.flush();
756
764
  } else {
757
765
  console.error("[FileStorage] Failed to load sessions:", error);
@@ -765,10 +773,9 @@ var FileStorageBackend = class {
765
773
  if (!this.initialized) await this.init();
766
774
  }
767
775
  async flush() {
768
- if (!this.memoryCache || !this.credentialsCache) return;
776
+ if (!this.memoryCache) return;
769
777
  await fs2.promises.writeFile(this.filePath, JSON.stringify({
770
- sessions: Array.from(this.memoryCache.values()),
771
- credentials: Array.from(this.credentialsCache.values())
778
+ sessions: Array.from(this.memoryCache.values())
772
779
  }, null, 2), "utf-8");
773
780
  }
774
781
  getSessionKey(userId, sessionId) {
@@ -803,29 +810,49 @@ var FileStorageBackend = class {
803
810
  async patchCredentials(userId, sessionId, data) {
804
811
  await this.ensureInitialized();
805
812
  const sessionKey = this.getSessionKey(userId, sessionId);
806
- if (!this.memoryCache.has(sessionKey)) {
813
+ const current = this.memoryCache.get(sessionKey);
814
+ if (!current) {
807
815
  throw new Error(`Session ${sessionId} not found`);
808
816
  }
809
- const current = this.credentialsCache.get(sessionKey) ?? { sessionId, userId };
810
- this.credentialsCache.set(sessionKey, { ...current, ...data, sessionId, userId });
817
+ this.memoryCache.set(sessionKey, { ...current, ...data, sessionId, userId });
811
818
  await this.flush();
812
819
  }
813
- async get(userId, sessionId) {
820
+ async get(userId, sessionId, options) {
814
821
  await this.ensureInitialized();
815
822
  const sessionKey = this.getSessionKey(userId, sessionId);
816
- return this.memoryCache.get(sessionKey) || null;
823
+ const session = this.memoryCache.get(sessionKey) || null;
824
+ if (!session) return null;
825
+ if (!options?.includeCredentials) {
826
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState, ...sessionOnly } = session;
827
+ return sessionOnly;
828
+ }
829
+ return session;
817
830
  }
818
831
  async getCredentials(userId, sessionId) {
819
832
  await this.ensureInitialized();
820
833
  const sessionKey = this.getSessionKey(userId, sessionId);
821
- if (!this.memoryCache.has(sessionKey)) return null;
822
- return this.credentialsCache.get(sessionKey) ?? { sessionId, userId };
834
+ const session = this.memoryCache.get(sessionKey);
835
+ if (!session) return null;
836
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState } = session;
837
+ return {
838
+ sessionId,
839
+ userId,
840
+ clientInformation,
841
+ tokens,
842
+ codeVerifier,
843
+ codeVerifierChallenge,
844
+ codeVerifierNonce,
845
+ clientId,
846
+ oauthState
847
+ };
823
848
  }
824
849
  async clearCredentials(userId, sessionId) {
825
850
  await this.patchCredentials(userId, sessionId, {
826
851
  clientInformation: null,
827
852
  tokens: null,
828
853
  codeVerifier: null,
854
+ codeVerifierChallenge: null,
855
+ codeVerifierNonce: null,
829
856
  clientId: null,
830
857
  oauthState: null
831
858
  });
@@ -842,7 +869,6 @@ var FileStorageBackend = class {
842
869
  await this.ensureInitialized();
843
870
  const sessionKey = this.getSessionKey(userId, sessionId);
844
871
  const deleted = this.memoryCache.delete(sessionKey);
845
- this.credentialsCache.delete(sessionKey);
846
872
  if (deleted) {
847
873
  await this.flush();
848
874
  }
@@ -854,7 +880,6 @@ var FileStorageBackend = class {
854
880
  async clearAll() {
855
881
  await this.ensureInitialized();
856
882
  this.memoryCache.clear();
857
- this.credentialsCache.clear();
858
883
  await this.flush();
859
884
  }
860
885
  async cleanupExpired() {
@@ -863,7 +888,6 @@ var FileStorageBackend = class {
863
888
  for (const [key, session] of this.memoryCache.entries()) {
864
889
  if (!isSessionExpired(session)) continue;
865
890
  this.memoryCache.delete(key);
866
- this.credentialsCache.delete(key);
867
891
  changed = true;
868
892
  }
869
893
  if (changed) {
@@ -880,12 +904,10 @@ var SqliteStorage = class {
880
904
  constructor(options = {}) {
881
905
  __publicField(this, "db", null);
882
906
  __publicField(this, "table");
883
- __publicField(this, "credentialsTable");
884
907
  __publicField(this, "initialized", false);
885
908
  __publicField(this, "dbPath");
886
909
  this.dbPath = options.path || "./sessions.db";
887
910
  this.table = options.table || "mcp_sessions";
888
- this.credentialsTable = `${this.table}_credentials`;
889
911
  }
890
912
  async init() {
891
913
  if (this.initialized) return;
@@ -905,13 +927,6 @@ var SqliteStorage = class {
905
927
  expiresAt INTEGER
906
928
  );
907
929
  CREATE INDEX IF NOT EXISTS idx_${this.table}_userId ON ${this.table}(userId);
908
- CREATE TABLE IF NOT EXISTS ${this.credentialsTable} (
909
- sessionId TEXT NOT NULL,
910
- userId TEXT NOT NULL,
911
- data TEXT NOT NULL,
912
- PRIMARY KEY (userId, sessionId),
913
- FOREIGN KEY (sessionId) REFERENCES ${this.table}(sessionId) ON DELETE CASCADE
914
- );
915
930
  `);
916
931
  this.initialized = true;
917
932
  console.log(`[mcp-ts][Storage] SQLite: \u2713 database at ${this.dbPath} verified.`);
@@ -968,41 +983,54 @@ var SqliteStorage = class {
968
983
  }
969
984
  async patchCredentials(userId, sessionId, data) {
970
985
  this.ensureInitialized();
971
- if (!await this.get(userId, sessionId)) {
986
+ const currentSession = await this.get(userId, sessionId, { includeCredentials: true });
987
+ if (!currentSession) {
972
988
  throw new Error(`Session ${sessionId} not found for userId ${userId}`);
973
989
  }
974
- const current = await this.getCredentials(userId, sessionId) ?? { sessionId, userId };
975
- const credentials = { ...current, ...data, sessionId, userId };
990
+ const updated = { ...currentSession, ...data, sessionId, userId };
976
991
  const stmt = this.db.prepare(
977
- `INSERT INTO ${this.credentialsTable} (sessionId, userId, data)
978
- VALUES (?, ?, ?)
979
- ON CONFLICT(userId, sessionId) DO UPDATE SET data = excluded.data`
992
+ `UPDATE ${this.table} SET data = ? WHERE sessionId = ? AND userId = ?`
980
993
  );
981
- stmt.run(sessionId, userId, JSON.stringify(credentials));
994
+ stmt.run(JSON.stringify(updated), sessionId, userId);
982
995
  }
983
- async get(userId, sessionId) {
996
+ async get(userId, sessionId, options) {
984
997
  this.ensureInitialized();
985
998
  const stmt = this.db.prepare(
986
999
  `SELECT data FROM ${this.table} WHERE sessionId = ? AND userId = ?`
987
1000
  );
988
1001
  const row = stmt.get(sessionId, userId);
989
1002
  if (!row) return null;
990
- return normalizeStoredSession(JSON.parse(row.data));
1003
+ const session = normalizeStoredSession(JSON.parse(row.data));
1004
+ if (!options?.includeCredentials) {
1005
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState, ...sessionOnly } = session;
1006
+ return sessionOnly;
1007
+ }
1008
+ return session;
991
1009
  }
992
1010
  async getCredentials(userId, sessionId) {
993
1011
  this.ensureInitialized();
994
- if (!await this.get(userId, sessionId)) return null;
995
- const stmt = this.db.prepare(
996
- `SELECT data FROM ${this.credentialsTable} WHERE sessionId = ? AND userId = ?`
997
- );
998
- const row = stmt.get(sessionId, userId);
999
- return row ? JSON.parse(row.data) : { sessionId, userId };
1012
+ const session = await this.get(userId, sessionId, { includeCredentials: true });
1013
+ if (!session) return null;
1014
+ const { clientInformation, tokens, codeVerifier, codeVerifierChallenge, codeVerifierNonce, clientId, oauthState } = session;
1015
+ return {
1016
+ sessionId,
1017
+ userId,
1018
+ clientInformation,
1019
+ tokens,
1020
+ codeVerifier,
1021
+ codeVerifierChallenge,
1022
+ codeVerifierNonce,
1023
+ clientId,
1024
+ oauthState
1025
+ };
1000
1026
  }
1001
1027
  async clearCredentials(userId, sessionId) {
1002
1028
  await this.patchCredentials(userId, sessionId, {
1003
1029
  clientInformation: null,
1004
1030
  tokens: null,
1005
1031
  codeVerifier: null,
1032
+ codeVerifierChallenge: null,
1033
+ codeVerifierNonce: null,
1006
1034
  clientId: null,
1007
1035
  oauthState: null
1008
1036
  });
@@ -1028,9 +1056,6 @@ var SqliteStorage = class {
1028
1056
  const stmt = this.db.prepare(
1029
1057
  `DELETE FROM ${this.table} WHERE sessionId = ? AND userId = ?`
1030
1058
  );
1031
- this.db.prepare(
1032
- `DELETE FROM ${this.credentialsTable} WHERE sessionId = ? AND userId = ?`
1033
- ).run(sessionId, userId);
1034
1059
  stmt.run(sessionId, userId);
1035
1060
  }
1036
1061
  async listAllIds() {
@@ -1041,7 +1066,6 @@ var SqliteStorage = class {
1041
1066
  }
1042
1067
  async clearAll() {
1043
1068
  this.ensureInitialized();
1044
- this.db.prepare(`DELETE FROM ${this.credentialsTable}`).run();
1045
1069
  const stmt = this.db.prepare(`DELETE FROM ${this.table}`);
1046
1070
  stmt.run();
1047
1071
  }
@@ -1055,14 +1079,6 @@ var SqliteStorage = class {
1055
1079
  deleteStmt.run(row.sessionId, row.userId);
1056
1080
  }
1057
1081
  }
1058
- this.db.prepare(
1059
- `DELETE FROM ${this.credentialsTable}
1060
- WHERE NOT EXISTS (
1061
- SELECT 1 FROM ${this.table}
1062
- WHERE ${this.table}.sessionId = ${this.credentialsTable}.sessionId
1063
- AND ${this.table}.userId = ${this.credentialsTable}.userId
1064
- )`
1065
- ).run();
1066
1082
  }
1067
1083
  async disconnect() {
1068
1084
  if (this.db) {
@@ -1103,8 +1119,8 @@ function encryptObject(data) {
1103
1119
  }
1104
1120
  try {
1105
1121
  const text = JSON.stringify(data);
1106
- const iv = crypto.randomBytes(IV_LENGTH);
1107
- const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
1122
+ const iv = crypto$1.randomBytes(IV_LENGTH);
1123
+ const cipher = crypto$1.createCipheriv(ALGORITHM, key, iv);
1108
1124
  let encrypted = cipher.update(text, "utf-8", "hex");
1109
1125
  encrypted += cipher.final("hex");
1110
1126
  const authTag = cipher.getAuthTag().toString("hex");
@@ -1132,7 +1148,7 @@ function decryptObject(data) {
1132
1148
  const iv = Buffer.from(parts[2], "hex");
1133
1149
  const authTag = Buffer.from(parts[3], "hex");
1134
1150
  const encryptedText = parts[4];
1135
- const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
1151
+ const decipher = crypto$1.createDecipheriv(ALGORITHM, key, iv);
1136
1152
  decipher.setAuthTag(authTag);
1137
1153
  let decrypted = decipher.update(encryptedText, "hex", "utf-8");
1138
1154
  decrypted += decipher.final("utf-8");
@@ -1150,7 +1166,6 @@ var SupabaseStorageBackend = class {
1150
1166
  }
1151
1167
  async init() {
1152
1168
  await this.assertTable("mcp_sessions", "session_id");
1153
- await this.assertTable("mcp_credentials", "session_id");
1154
1169
  console.log("[mcp-ts][Storage] Supabase: storage tables verified.");
1155
1170
  }
1156
1171
  async assertTable(table, column) {
@@ -1181,18 +1196,13 @@ var SupabaseStorageBackend = class {
1181
1196
  headers: decryptObject(row.headers),
1182
1197
  authUrl: row.auth_url,
1183
1198
  status: row.status ?? "pending",
1184
- toolPolicy: normalizeToolPolicy(row.tool_policy)
1185
- };
1186
- }
1187
- mapRowToCredentials(row, userId, sessionId) {
1188
- return {
1189
- sessionId,
1190
- userId,
1191
- clientInformation: decryptObject(row?.client_information),
1192
- tokens: decryptObject(row?.tokens),
1193
- codeVerifier: decryptObject(row?.code_verifier),
1194
- clientId: row?.client_id,
1195
- oauthState: row?.oauth_state
1199
+ toolPolicy: normalizeToolPolicy(row.tool_policy),
1200
+ clientInformation: decryptObject(row.client_information),
1201
+ tokens: decryptObject(row.tokens),
1202
+ codeVerifier: decryptObject(row.code_verifier),
1203
+ clientId: row.client_id,
1204
+ oauthState: row.oauth_state,
1205
+ enabled: row.enabled ?? true
1196
1206
  };
1197
1207
  }
1198
1208
  hasCredentialData(data) {
@@ -1250,6 +1260,7 @@ var SupabaseStorageBackend = class {
1250
1260
  if ("headers" in data) updateData.headers = encryptObject(data.headers);
1251
1261
  if ("authUrl" in data) updateData.auth_url = data.authUrl ?? null;
1252
1262
  if ("toolPolicy" in data) updateData.tool_policy = normalizeToolPolicy(data.toolPolicy);
1263
+ if ("enabled" in data) updateData.enabled = data.enabled;
1253
1264
  const shouldUpdateSession = Object.keys(updateData).some((key) => key !== "updated_at");
1254
1265
  let updatedRows = null;
1255
1266
  if (shouldUpdateSession) {
@@ -1271,23 +1282,22 @@ var SupabaseStorageBackend = class {
1271
1282
  }
1272
1283
  async patchCredentials(userId, sessionId, data) {
1273
1284
  if (!this.hasCredentialData(data)) return;
1274
- const row = {
1275
- user_id: userId,
1276
- session_id: sessionId,
1285
+ const updateData = {
1277
1286
  updated_at: (/* @__PURE__ */ new Date()).toISOString()
1278
1287
  };
1279
- if ("clientInformation" in data) row.client_information = data.clientInformation == null ? null : encryptObject(data.clientInformation);
1280
- if ("tokens" in data) row.tokens = data.tokens == null ? null : encryptObject(data.tokens);
1281
- if ("codeVerifier" in data) row.code_verifier = data.codeVerifier == null ? null : encryptObject(data.codeVerifier);
1282
- if ("clientId" in data) row.client_id = data.clientId ?? null;
1283
- if ("oauthState" in data) row.oauth_state = data.oauthState ?? null;
1284
- const { error } = await this.supabase.from("mcp_credentials").upsert(row, { onConflict: "user_id,session_id" });
1288
+ if ("clientInformation" in data) updateData.client_information = data.clientInformation == null ? null : encryptObject(data.clientInformation);
1289
+ if ("tokens" in data) updateData.tokens = data.tokens == null ? null : encryptObject(data.tokens);
1290
+ if ("codeVerifier" in data) updateData.code_verifier = data.codeVerifier == null ? null : encryptObject(data.codeVerifier);
1291
+ if ("clientId" in data) updateData.client_id = data.clientId ?? null;
1292
+ if ("oauthState" in data) updateData.oauth_state = data.oauthState ?? null;
1293
+ const { error } = await this.supabase.from("mcp_sessions").update(updateData).eq("user_id", userId).eq("session_id", sessionId);
1285
1294
  if (error) {
1286
1295
  throw new Error(`Failed to update credentials: ${error.message}`);
1287
1296
  }
1288
1297
  }
1289
- async get(userId, sessionId) {
1290
- const { data, error } = await this.supabase.from("mcp_sessions").select("*").eq("user_id", userId).eq("session_id", sessionId).maybeSingle();
1298
+ async get(userId, sessionId, options) {
1299
+ const selection = options?.includeCredentials ? "*" : "session_id, user_id, server_id, server_name, server_url, transport_type, callback_url, created_at, updated_at, expires_at, headers, auth_url, status, tool_policy, enabled";
1300
+ const { data, error } = await this.supabase.from("mcp_sessions").select(selection).eq("user_id", userId).eq("session_id", sessionId).maybeSingle();
1291
1301
  if (error) {
1292
1302
  console.error("[SupabaseStorage] Failed to get session:", error);
1293
1303
  return null;
@@ -1296,19 +1306,21 @@ var SupabaseStorageBackend = class {
1296
1306
  return this.mapRowToSessionData(data);
1297
1307
  }
1298
1308
  async getCredentials(userId, sessionId) {
1299
- const { data, error } = await this.supabase.from("mcp_credentials").select("*").eq("user_id", userId).eq("session_id", sessionId).maybeSingle();
1309
+ const { data, error } = await this.supabase.from("mcp_sessions").select("client_information, tokens, code_verifier, client_id, oauth_state").eq("user_id", userId).eq("session_id", sessionId).maybeSingle();
1300
1310
  if (error) {
1301
1311
  console.error("[SupabaseStorage] Failed to get credentials:", error);
1302
1312
  return null;
1303
1313
  }
1304
- if (data) {
1305
- return this.mapRowToCredentials(data, userId, sessionId);
1306
- }
1307
- const { data: sessionRows, error: sessionError } = await this.supabase.from("mcp_sessions").select("id").eq("user_id", userId).eq("session_id", sessionId);
1308
- if (sessionError || !sessionRows || sessionRows.length === 0) {
1309
- return null;
1310
- }
1311
- return { sessionId, userId };
1314
+ if (!data) return null;
1315
+ return {
1316
+ sessionId,
1317
+ userId,
1318
+ clientInformation: decryptObject(data.client_information),
1319
+ tokens: decryptObject(data.tokens),
1320
+ codeVerifier: decryptObject(data.code_verifier),
1321
+ clientId: data.client_id,
1322
+ oauthState: data.oauth_state
1323
+ };
1312
1324
  }
1313
1325
  async list(userId) {
1314
1326
  const { data, error } = await this.supabase.from("mcp_sessions").select("*").eq("user_id", userId);
@@ -1319,7 +1331,14 @@ var SupabaseStorageBackend = class {
1319
1331
  return data.map((row) => this.mapRowToSessionData(row));
1320
1332
  }
1321
1333
  async clearCredentials(userId, sessionId) {
1322
- const { error } = await this.supabase.from("mcp_credentials").delete().eq("user_id", userId).eq("session_id", sessionId);
1334
+ const { error } = await this.supabase.from("mcp_sessions").update({
1335
+ client_information: null,
1336
+ tokens: null,
1337
+ code_verifier: null,
1338
+ client_id: null,
1339
+ oauth_state: null,
1340
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
1341
+ }).eq("user_id", userId).eq("session_id", sessionId);
1323
1342
  if (error) {
1324
1343
  throw new Error(`Failed to clear credentials: ${error.message}`);
1325
1344
  }
@@ -1347,10 +1366,6 @@ var SupabaseStorageBackend = class {
1347
1366
  return data.map((row) => row.session_id);
1348
1367
  }
1349
1368
  async clearAll() {
1350
- const { error: credentialsError } = await this.supabase.from("mcp_credentials").delete().neq("session_id", "");
1351
- if (credentialsError) {
1352
- console.error("[SupabaseStorage] Failed to clear credentials:", credentialsError);
1353
- }
1354
1369
  const { error } = await this.supabase.from("mcp_sessions").delete().neq("session_id", "");
1355
1370
  if (error) {
1356
1371
  console.error("[SupabaseStorage] Failed to clear sessions:", error);
@@ -1377,16 +1392,12 @@ var NeonStorageBackend = class {
1377
1392
  constructor(sql, options = {}) {
1378
1393
  this.sql = sql;
1379
1394
  __publicField(this, "tableName");
1380
- __publicField(this, "credentialsTableName");
1381
1395
  const schema = options.schema || "public";
1382
1396
  const table = options.table || "mcp_sessions";
1383
- const credentialsTable = options.credentialsTable || "mcp_credentials";
1384
1397
  this.tableName = `${this.quoteIdentifier(schema)}.${this.quoteIdentifier(table)}`;
1385
- this.credentialsTableName = `${this.quoteIdentifier(schema)}.${this.quoteIdentifier(credentialsTable)}`;
1386
1398
  }
1387
1399
  async init() {
1388
1400
  await this.assertTable(this.tableName, "mcp_sessions");
1389
- await this.assertTable(this.credentialsTableName, "mcp_credentials");
1390
1401
  console.log("[mcp-ts][Storage] Neon: storage tables verified.");
1391
1402
  }
1392
1403
  async assertTable(qualifiedName, displayName) {
@@ -1424,18 +1435,13 @@ var NeonStorageBackend = class {
1424
1435
  headers: decryptObject(row.headers),
1425
1436
  authUrl: row.auth_url ?? void 0,
1426
1437
  status: row.status ?? "pending",
1427
- toolPolicy: normalizeToolPolicy(row.tool_policy)
1428
- };
1429
- }
1430
- mapRowToCredentials(row, userId, sessionId) {
1431
- return {
1432
- sessionId,
1433
- userId,
1438
+ toolPolicy: normalizeToolPolicy(row.tool_policy),
1434
1439
  clientInformation: decryptObject(row.client_information),
1435
1440
  tokens: decryptObject(row.tokens),
1436
1441
  codeVerifier: decryptObject(row.code_verifier),
1437
1442
  clientId: row.client_id ?? void 0,
1438
- oauthState: row.oauth_state
1443
+ oauthState: row.oauth_state,
1444
+ enabled: row.enabled ?? true
1439
1445
  };
1440
1446
  }
1441
1447
  hasCredentialData(data) {
@@ -1505,7 +1511,7 @@ var NeonStorageBackend = class {
1505
1511
  const updatedSession = { ...currentSession, ...data };
1506
1512
  const status = updatedSession.status ?? "pending";
1507
1513
  const expiresAt = resolveSessionExpiresAt(status);
1508
- const shouldUpdateSession = "serverId" in data || "serverName" in data || "serverUrl" in data || "transportType" in data || "callbackUrl" in data || "status" in data || "headers" in data || "authUrl" in data || "toolPolicy" in data;
1514
+ const shouldUpdateSession = "serverId" in data || "serverName" in data || "serverUrl" in data || "transportType" in data || "callbackUrl" in data || "status" in data || "headers" in data || "authUrl" in data || "toolPolicy" in data || "enabled" in data;
1509
1515
  if (shouldUpdateSession) {
1510
1516
  const setClauses = [];
1511
1517
  const values = [];
@@ -1528,6 +1534,9 @@ var NeonStorageBackend = class {
1528
1534
  const toolPolicy = normalizeToolPolicy(updatedSession.toolPolicy, policyUpdatedAt) ?? { mode: "all", toolIds: [], updatedAt: policyUpdatedAt };
1529
1535
  addSet("tool_policy", toolPolicy);
1530
1536
  }
1537
+ if ("enabled" in data) {
1538
+ addSet("enabled", updatedSession.enabled);
1539
+ }
1531
1540
  setClauses.push("updated_at = now()");
1532
1541
  const updatedRows = await this.sql.query(
1533
1542
  `UPDATE ${this.tableName}
@@ -1543,45 +1552,45 @@ var NeonStorageBackend = class {
1543
1552
  }
1544
1553
  async patchCredentials(userId, sessionId, data) {
1545
1554
  if (!this.hasCredentialData(data)) return;
1546
- await this.sql.query(
1547
- `INSERT INTO ${this.credentialsTableName} (
1548
- user_id,
1549
- session_id,
1550
- client_information,
1551
- tokens,
1552
- code_verifier,
1553
- client_id,
1554
- oauth_state,
1555
- updated_at
1556
- ) VALUES ($1, $2, $3, $4, $5, $6, $7, now())
1557
- ON CONFLICT (user_id, session_id)
1558
- DO UPDATE SET
1559
- client_information = CASE WHEN $8 THEN EXCLUDED.client_information ELSE ${this.credentialsTableName}.client_information END,
1560
- tokens = CASE WHEN $9 THEN EXCLUDED.tokens ELSE ${this.credentialsTableName}.tokens END,
1561
- code_verifier = CASE WHEN $10 THEN EXCLUDED.code_verifier ELSE ${this.credentialsTableName}.code_verifier END,
1562
- client_id = CASE WHEN $11 THEN EXCLUDED.client_id ELSE ${this.credentialsTableName}.client_id END,
1563
- oauth_state = CASE WHEN $12 THEN EXCLUDED.oauth_state ELSE ${this.credentialsTableName}.oauth_state END,
1564
- updated_at = now()`,
1565
- [
1566
- userId,
1567
- sessionId,
1568
- "clientInformation" in data ? data.clientInformation == null ? null : encryptObject(data.clientInformation) : null,
1569
- "tokens" in data ? data.tokens == null ? null : encryptObject(data.tokens) : null,
1570
- "codeVerifier" in data ? data.codeVerifier == null ? null : encryptObject(data.codeVerifier) : null,
1571
- "clientId" in data ? data.clientId ?? null : null,
1572
- "oauthState" in data ? data.oauthState ?? null : null,
1573
- "clientInformation" in data,
1574
- "tokens" in data,
1575
- "codeVerifier" in data,
1576
- "clientId" in data,
1577
- "oauthState" in data
1578
- ]
1555
+ const setClauses = [];
1556
+ const values = [];
1557
+ let paramIndex = 1;
1558
+ const addSet = (column, value) => {
1559
+ setClauses.push(`${column} = $${paramIndex++}`);
1560
+ values.push(value);
1561
+ };
1562
+ if ("clientInformation" in data) {
1563
+ addSet("client_information", data.clientInformation == null ? null : encryptObject(data.clientInformation));
1564
+ }
1565
+ if ("tokens" in data) {
1566
+ addSet("tokens", data.tokens == null ? null : encryptObject(data.tokens));
1567
+ }
1568
+ if ("codeVerifier" in data) {
1569
+ addSet("code_verifier", data.codeVerifier == null ? null : encryptObject(data.codeVerifier));
1570
+ }
1571
+ if ("clientId" in data) {
1572
+ addSet("client_id", data.clientId ?? null);
1573
+ }
1574
+ if ("oauthState" in data) {
1575
+ addSet("oauth_state", data.oauthState ?? null);
1576
+ }
1577
+ setClauses.push("updated_at = now()");
1578
+ const updatedRows = await this.sql.query(
1579
+ `UPDATE ${this.tableName}
1580
+ SET ${setClauses.join(", ")}
1581
+ WHERE user_id = $${paramIndex++} AND session_id = $${paramIndex++}
1582
+ RETURNING id`,
1583
+ [...values, userId, sessionId]
1579
1584
  );
1585
+ if (updatedRows.length === 0) {
1586
+ throw new Error(`Session ${sessionId} not found for userId ${userId}`);
1587
+ }
1580
1588
  }
1581
- async get(userId, sessionId) {
1589
+ async get(userId, sessionId, options) {
1582
1590
  try {
1591
+ const selection = options?.includeCredentials ? "*" : "session_id, user_id, server_id, server_name, server_url, transport_type, callback_url, created_at, updated_at, expires_at, headers, auth_url, status, tool_policy, enabled";
1583
1592
  const rows = await this.sql.query(
1584
- `SELECT * FROM ${this.tableName} WHERE user_id = $1 AND session_id = $2`,
1593
+ `SELECT ${selection} FROM ${this.tableName} WHERE user_id = $1 AND session_id = $2`,
1585
1594
  [userId, sessionId]
1586
1595
  );
1587
1596
  if (!rows[0]) return null;
@@ -1593,18 +1602,22 @@ var NeonStorageBackend = class {
1593
1602
  }
1594
1603
  async getCredentials(userId, sessionId) {
1595
1604
  try {
1596
- const credentialRows = await this.sql.query(
1597
- `SELECT * FROM ${this.credentialsTableName} WHERE user_id = $1 AND session_id = $2`,
1598
- [userId, sessionId]
1599
- );
1600
- if (credentialRows[0]) {
1601
- return this.mapRowToCredentials(credentialRows[0], userId, sessionId);
1602
- }
1603
- const sessionRows = await this.sql.query(
1604
- `SELECT id FROM ${this.tableName} WHERE user_id = $1 AND session_id = $2`,
1605
+ const rows = await this.sql.query(
1606
+ `SELECT client_information, tokens, code_verifier, client_id, oauth_state
1607
+ FROM ${this.tableName} WHERE user_id = $1 AND session_id = $2`,
1605
1608
  [userId, sessionId]
1606
1609
  );
1607
- return sessionRows[0] ? { sessionId, userId } : null;
1610
+ if (!rows[0]) return null;
1611
+ const row = rows[0];
1612
+ return {
1613
+ sessionId,
1614
+ userId,
1615
+ clientInformation: decryptObject(row.client_information),
1616
+ tokens: decryptObject(row.tokens),
1617
+ codeVerifier: decryptObject(row.code_verifier),
1618
+ clientId: row.client_id ?? void 0,
1619
+ oauthState: row.oauth_state
1620
+ };
1608
1621
  } catch (error) {
1609
1622
  console.error("[NeonStorage] Failed to get credentials:", error);
1610
1623
  return null;
@@ -1625,7 +1638,9 @@ var NeonStorageBackend = class {
1625
1638
  async clearCredentials(userId, sessionId) {
1626
1639
  try {
1627
1640
  await this.sql.query(
1628
- `DELETE FROM ${this.credentialsTableName} WHERE user_id = $1 AND session_id = $2`,
1641
+ `UPDATE ${this.tableName}
1642
+ SET client_information = null, tokens = null, code_verifier = null, client_id = null, oauth_state = null, updated_at = now()
1643
+ WHERE user_id = $1 AND session_id = $2`,
1629
1644
  [userId, sessionId]
1630
1645
  );
1631
1646
  } catch (error) {
@@ -1667,7 +1682,6 @@ var NeonStorageBackend = class {
1667
1682
  }
1668
1683
  async clearAll() {
1669
1684
  try {
1670
- await this.sql.query(`DELETE FROM ${this.credentialsTableName}`);
1671
1685
  await this.sql.query(`DELETE FROM ${this.tableName}`);
1672
1686
  } catch (error) {
1673
1687
  console.error("[NeonStorage] Failed to clear sessions:", error);
@@ -1912,6 +1926,60 @@ function onSessionMutation(listener) {
1912
1926
  sessionMutationListeners.delete(listener);
1913
1927
  };
1914
1928
  }
1929
+ function withDbObservability(store, emit) {
1930
+ const readMethods = /* @__PURE__ */ new Set([
1931
+ "get",
1932
+ "getCredentials",
1933
+ "list",
1934
+ "listIds",
1935
+ "listAllIds"
1936
+ ]);
1937
+ return new Proxy(store, {
1938
+ get(target, prop, receiver) {
1939
+ const original = Reflect.get(target, prop, receiver);
1940
+ if (typeof original !== "function") return original;
1941
+ return (...args) => {
1942
+ const method = prop;
1943
+ const isRead = readMethods.has(prop);
1944
+ const start = performance.now();
1945
+ const emitEvent = (error) => {
1946
+ emit({
1947
+ type: isRead ? "db:read" : "db:write",
1948
+ level: error ? "error" : "debug",
1949
+ message: `${method}(${args.map(
1950
+ (a) => typeof a === "string" ? a.slice(0, 24) : typeof a
1951
+ ).join(", ")})`,
1952
+ sessionId: typeof args[1] === "string" ? args[1] : void 0,
1953
+ payload: {
1954
+ method,
1955
+ argTypes: args.map((a) => typeof a),
1956
+ durationMs: performance.now() - start,
1957
+ ...error ? { error } : {}
1958
+ },
1959
+ timestamp: Date.now()
1960
+ });
1961
+ };
1962
+ try {
1963
+ const result = original.apply(target, args);
1964
+ if (result instanceof Promise) {
1965
+ return result.then((r) => {
1966
+ emitEvent();
1967
+ return r;
1968
+ }).catch((e) => {
1969
+ emitEvent(String(e));
1970
+ throw e;
1971
+ });
1972
+ }
1973
+ emitEvent();
1974
+ return result;
1975
+ } catch (e) {
1976
+ emitEvent(String(e));
1977
+ throw e;
1978
+ }
1979
+ };
1980
+ }
1981
+ });
1982
+ }
1915
1983
  var sessions = new Proxy({}, {
1916
1984
  get(_target, prop) {
1917
1985
  return async (...args) => {
@@ -1931,6 +1999,24 @@ var sessions = new Proxy({}, {
1931
1999
  });
1932
2000
 
1933
2001
  // src/server/mcp/storage-oauth-provider.ts
2002
+ var codeVerifierContext = new async_hooks.AsyncLocalStorage();
2003
+ function runWithCodeVerifierState(verifier, method, fn) {
2004
+ return codeVerifierContext.run({ verifier, method }, fn);
2005
+ }
2006
+ function base64UrlEncode(bytes) {
2007
+ let binary = "";
2008
+ for (const byte of bytes) {
2009
+ binary += String.fromCharCode(byte);
2010
+ }
2011
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
2012
+ }
2013
+ async function createCodeChallenge(verifier) {
2014
+ const digest = await crypto.subtle.digest(
2015
+ "SHA-256",
2016
+ new TextEncoder().encode(verifier)
2017
+ );
2018
+ return base64UrlEncode(new Uint8Array(digest));
2019
+ }
1934
2020
  var StorageOAuthClientProvider = class {
1935
2021
  /**
1936
2022
  * Creates a new session-backed OAuth provider
@@ -1946,8 +2032,12 @@ var StorageOAuthClientProvider = class {
1946
2032
  __publicField(this, "logoUri");
1947
2033
  __publicField(this, "policyUri");
1948
2034
  __publicField(this, "clientSecret");
2035
+ __publicField(this, "_store");
1949
2036
  __publicField(this, "_authUrl");
1950
2037
  __publicField(this, "_clientId");
2038
+ __publicField(this, "_cachedCodeVerifier");
2039
+ __publicField(this, "_hasCodeVerifier", false);
2040
+ __publicField(this, "_cachedTokens");
1951
2041
  __publicField(this, "onRedirectCallback");
1952
2042
  this.userId = options.userId;
1953
2043
  this.serverId = options.serverId;
@@ -1959,6 +2049,8 @@ var StorageOAuthClientProvider = class {
1959
2049
  this.policyUri = options.policyUri;
1960
2050
  this._clientId = options.clientId;
1961
2051
  this.clientSecret = options.clientSecret;
2052
+ this._cachedTokens = options.cachedTokens;
2053
+ this._store = options.sessionStore ?? sessions;
1962
2054
  this.onRedirectCallback = options.onRedirect;
1963
2055
  }
1964
2056
  get clientMetadata() {
@@ -1986,7 +2078,7 @@ var StorageOAuthClientProvider = class {
1986
2078
  * @private
1987
2079
  */
1988
2080
  async getCredentials() {
1989
- const data = await sessions.getCredentials(this.userId, this.sessionId);
2081
+ const data = await this._store.getCredentials(this.userId, this.sessionId);
1990
2082
  if (!data) {
1991
2083
  return { userId: this.userId, sessionId: this.sessionId };
1992
2084
  }
@@ -1999,26 +2091,33 @@ var StorageOAuthClientProvider = class {
1999
2091
  * @throws Error if session doesn't exist (session must be created by controller layer)
2000
2092
  */
2001
2093
  async patchCredentials(data) {
2002
- await sessions.patchCredentials(this.userId, this.sessionId, data);
2094
+ await this._store.patchCredentials(this.userId, this.sessionId, data);
2003
2095
  }
2004
2096
  /**
2005
- * Retrieves stored OAuth client information
2097
+ * Retrieves stored OAuth client information.
2006
2098
  */
2007
2099
  async clientInformation() {
2008
- const data = await this.getCredentials();
2009
- if (data.clientId && !this._clientId) {
2010
- this._clientId = data.clientId;
2100
+ if (this._clientId) {
2101
+ return {
2102
+ client_id: this._clientId,
2103
+ ...this.clientSecret ? { client_secret: this.clientSecret } : {}
2104
+ };
2011
2105
  }
2012
- if (data.clientInformation) {
2013
- return data.clientInformation;
2106
+ const data = await this.getCredentials();
2107
+ if (this._cachedTokens === void 0) {
2108
+ this._cachedTokens = data.tokens ?? null;
2014
2109
  }
2015
- if (!this._clientId) {
2016
- return void 0;
2110
+ if (data.clientId) {
2111
+ this._clientId = data.clientId;
2112
+ if (data.clientInformation) {
2113
+ return data.clientInformation;
2114
+ }
2115
+ return {
2116
+ client_id: data.clientId,
2117
+ ...this.clientSecret ? { client_secret: this.clientSecret } : {}
2118
+ };
2017
2119
  }
2018
- return {
2019
- client_id: this._clientId,
2020
- ...this.clientSecret ? { client_secret: this.clientSecret } : {}
2021
- };
2120
+ return void 0;
2022
2121
  }
2023
2122
  /**
2024
2123
  * Stores OAuth client information
@@ -2035,11 +2134,30 @@ var StorageOAuthClientProvider = class {
2035
2134
  */
2036
2135
  async saveTokens(tokens) {
2037
2136
  await this.patchCredentials({ tokens });
2137
+ this._cachedTokens = tokens;
2138
+ }
2139
+ /**
2140
+ * Persists static client credentials to DB immediately on creation.
2141
+ * Ensures getOrCreateClient() finds them on rehydration, even when the
2142
+ * server allows anonymous connect and never triggers DCR.
2143
+ */
2144
+ async initializeCredentials() {
2145
+ if (this._clientId) {
2146
+ await this.patchCredentials({
2147
+ clientId: this._clientId,
2148
+ clientInformation: {
2149
+ client_id: this._clientId,
2150
+ ...this.clientSecret ? { client_secret: this.clientSecret } : {}
2151
+ }
2152
+ });
2153
+ }
2038
2154
  }
2039
2155
  get authUrl() {
2040
2156
  return this._authUrl;
2041
2157
  }
2042
2158
  async state() {
2159
+ this._cachedCodeVerifier = void 0;
2160
+ this._hasCodeVerifier = false;
2043
2161
  const nonce = nanoid.nanoid(32);
2044
2162
  await this.patchCredentials({
2045
2163
  oauthState: {
@@ -2060,7 +2178,7 @@ var StorageOAuthClientProvider = class {
2060
2178
  if (parsed.sessionId !== this.sessionId) {
2061
2179
  return { valid: false, error: "OAuth state mismatch" };
2062
2180
  }
2063
- const data = await sessions.getCredentials(this.userId, parsed.sessionId);
2181
+ const data = await this._store.getCredentials(this.userId, parsed.sessionId);
2064
2182
  if (!data) {
2065
2183
  return { valid: false, error: "Session not found" };
2066
2184
  }
@@ -2085,49 +2203,77 @@ var StorageOAuthClientProvider = class {
2085
2203
  }
2086
2204
  async redirectToAuthorization(authUrl) {
2087
2205
  this._authUrl = authUrl.toString();
2088
- await sessions.update(this.userId, this.sessionId, { authUrl: this._authUrl });
2206
+ const codeChallenge = authUrl.searchParams.get("code_challenge");
2207
+ const state = authUrl.searchParams.get("state");
2208
+ if (this._cachedCodeVerifier && codeChallenge && state) {
2209
+ const expectedChallenge = await createCodeChallenge(this._cachedCodeVerifier);
2210
+ if (expectedChallenge === codeChallenge) {
2211
+ const parsed = parseOAuthState(state);
2212
+ if (parsed) {
2213
+ await this.patchCredentials({
2214
+ codeVerifier: this._cachedCodeVerifier
2215
+ });
2216
+ }
2217
+ }
2218
+ }
2089
2219
  if (this.onRedirectCallback) {
2090
2220
  this.onRedirectCallback(authUrl.toString());
2091
2221
  }
2092
2222
  }
2093
2223
  async invalidateCredentials(scope) {
2094
2224
  if (scope === "all") {
2095
- await sessions.delete(this.userId, this.sessionId);
2225
+ this._cachedTokens = void 0;
2226
+ await this._store.delete(this.userId, this.sessionId);
2096
2227
  } else {
2097
2228
  const updates = {};
2098
2229
  if (scope === "client") {
2099
2230
  updates.clientInformation = null;
2100
2231
  updates.clientId = null;
2101
2232
  } else if (scope === "tokens") {
2233
+ this._cachedTokens = void 0;
2102
2234
  updates.tokens = null;
2103
2235
  } else if (scope === "verifier") {
2236
+ this._cachedCodeVerifier = void 0;
2237
+ this._hasCodeVerifier = false;
2104
2238
  updates.codeVerifier = null;
2105
2239
  }
2106
2240
  await this.patchCredentials(updates);
2107
2241
  }
2108
2242
  }
2109
2243
  async saveCodeVerifier(verifier) {
2110
- const data = await this.getCredentials();
2111
- if (data.codeVerifier) {
2244
+ if (this._hasCodeVerifier) {
2112
2245
  return;
2113
2246
  }
2114
- await this.patchCredentials({ codeVerifier: verifier });
2247
+ this._cachedCodeVerifier = verifier;
2248
+ this._hasCodeVerifier = true;
2115
2249
  }
2116
2250
  async codeVerifier() {
2117
- const data = await this.getCredentials();
2118
- if (data.clientId && !this._clientId) {
2119
- this._clientId = data.clientId;
2251
+ if (this._cachedCodeVerifier) {
2252
+ return this._cachedCodeVerifier;
2253
+ }
2254
+ const ctx = codeVerifierContext.getStore();
2255
+ if (ctx?.verifier) {
2256
+ return ctx.verifier;
2120
2257
  }
2121
- if (!data.codeVerifier) {
2122
- throw new Error("No code verifier found");
2258
+ const data = await this.getCredentials();
2259
+ if (data.codeVerifier) {
2260
+ return data.codeVerifier;
2123
2261
  }
2124
- return data.codeVerifier;
2262
+ throw new Error("No code verifier found");
2125
2263
  }
2126
2264
  async deleteCodeVerifier() {
2127
- await this.patchCredentials({ codeVerifier: null });
2265
+ this._cachedCodeVerifier = void 0;
2266
+ this._hasCodeVerifier = false;
2267
+ await this.patchCredentials({
2268
+ codeVerifier: null
2269
+ });
2128
2270
  }
2129
2271
  async tokens() {
2272
+ if (this._cachedTokens !== void 0) {
2273
+ return this._cachedTokens ?? void 0;
2274
+ }
2130
2275
  const data = await this.getCredentials();
2276
+ this._cachedTokens = data.tokens ?? null;
2131
2277
  if (data.clientId && !this._clientId) {
2132
2278
  this._clientId = data.clientId;
2133
2279
  }
@@ -2222,23 +2368,10 @@ var MCPClient = class {
2222
2368
  __publicField(this, "client");
2223
2369
  __publicField(this, "oauthProvider", null);
2224
2370
  __publicField(this, "transport", null);
2225
- __publicField(this, "userId");
2226
- __publicField(this, "serverId");
2227
- __publicField(this, "sessionId");
2228
- __publicField(this, "serverName");
2229
- __publicField(this, "transportType");
2230
- __publicField(this, "serverUrl");
2231
- __publicField(this, "callbackUrl");
2232
- __publicField(this, "onRedirect");
2233
- __publicField(this, "clientId");
2234
- __publicField(this, "clientSecret");
2235
- __publicField(this, "headers");
2236
- /** OAuth Client Metadata */
2237
- __publicField(this, "clientName");
2238
- __publicField(this, "clientUri");
2239
- __publicField(this, "logoUri");
2240
- __publicField(this, "policyUri");
2371
+ __publicField(this, "config");
2241
2372
  __publicField(this, "createdAt");
2373
+ __publicField(this, "_serverInfo");
2374
+ __publicField(this, "_store");
2242
2375
  /** Event emitters for connection lifecycle */
2243
2376
  __publicField(this, "_onConnectionEvent", new Emitter());
2244
2377
  __publicField(this, "onConnectionEvent", this._onConnectionEvent.event);
@@ -2254,21 +2387,8 @@ var MCPClient = class {
2254
2387
  * the memory when the client is no longer needed.
2255
2388
  */
2256
2389
  __publicField(this, "cachedTools", null);
2257
- this.serverUrl = options.serverUrl;
2258
- this.serverName = options.serverName;
2259
- this.callbackUrl = options.callbackUrl;
2260
- this.onRedirect = options.onRedirect;
2261
- this.userId = options.userId;
2262
- this.serverId = options.serverId;
2263
- this.sessionId = options.sessionId;
2264
- this.transportType = options.transportType;
2265
- this.clientId = options.clientId;
2266
- this.clientSecret = options.clientSecret;
2267
- this.headers = options.headers;
2268
- this.clientName = options.clientName;
2269
- this.clientUri = options.clientUri;
2270
- this.logoUri = options.logoUri;
2271
- this.policyUri = options.policyUri;
2390
+ this.config = { ...options };
2391
+ this._store = options.sessionStore ?? sessions;
2272
2392
  this.client = new index_js.Client(
2273
2393
  {
2274
2394
  name: MCP_CLIENT_NAME,
@@ -2285,6 +2405,21 @@ var MCPClient = class {
2285
2405
  }
2286
2406
  );
2287
2407
  }
2408
+ /** Shared session-shaped data for ensureSession and saveSession */
2409
+ get session() {
2410
+ return {
2411
+ sessionId: this.config.sessionId,
2412
+ userId: this.config.userId,
2413
+ serverId: this.config.serverId,
2414
+ serverName: this.config.serverName,
2415
+ serverUrl: this.config.serverUrl,
2416
+ callbackUrl: this.config.callbackUrl,
2417
+ transportType: this.config.transportType || "streamable-http",
2418
+ headers: this.config.headers,
2419
+ createdAt: this.createdAt ?? Date.now(),
2420
+ updatedAt: Date.now()
2421
+ };
2422
+ }
2288
2423
  /**
2289
2424
  * Emit a connection state change event
2290
2425
  * @private
@@ -2292,13 +2427,13 @@ var MCPClient = class {
2292
2427
  emitStateChange(newState) {
2293
2428
  const previousState = this.currentState;
2294
2429
  this.currentState = newState;
2295
- if (!this.serverId) return;
2430
+ if (!this.config.serverId) return;
2296
2431
  this._onConnectionEvent.fire({
2297
2432
  type: "state_changed",
2298
- sessionId: this.sessionId,
2299
- serverId: this.serverId,
2300
- serverName: this.serverName || this.serverId,
2301
- serverUrl: this.serverUrl || "",
2433
+ sessionId: this.config.sessionId,
2434
+ serverId: this.config.serverId,
2435
+ serverName: this.config.serverName || this.config.serverId,
2436
+ serverUrl: this.config.serverUrl || "",
2302
2437
  createdAt: this.createdAt,
2303
2438
  state: newState,
2304
2439
  previousState,
@@ -2309,8 +2444,8 @@ var MCPClient = class {
2309
2444
  level: "info",
2310
2445
  message: `Connection state: ${previousState} \u2192 ${newState}`,
2311
2446
  displayMessage: `State changed to ${newState}`,
2312
- sessionId: this.sessionId,
2313
- serverId: this.serverId,
2447
+ sessionId: this.config.sessionId,
2448
+ serverId: this.config.serverId,
2314
2449
  payload: { previousState, newState },
2315
2450
  timestamp: Date.now(),
2316
2451
  id: nanoid.nanoid()
@@ -2321,11 +2456,11 @@ var MCPClient = class {
2321
2456
  * @private
2322
2457
  */
2323
2458
  emitError(error, errorType = "unknown") {
2324
- if (!this.serverId) return;
2459
+ if (!this.config.serverId) return;
2325
2460
  this._onConnectionEvent.fire({
2326
2461
  type: "error",
2327
- sessionId: this.sessionId,
2328
- serverId: this.serverId,
2462
+ sessionId: this.config.sessionId,
2463
+ serverId: this.config.serverId,
2329
2464
  error,
2330
2465
  errorType,
2331
2466
  timestamp: Date.now()
@@ -2335,8 +2470,8 @@ var MCPClient = class {
2335
2470
  level: "error",
2336
2471
  message: error,
2337
2472
  displayMessage: error,
2338
- sessionId: this.sessionId,
2339
- serverId: this.serverId,
2473
+ sessionId: this.config.sessionId,
2474
+ serverId: this.config.serverId,
2340
2475
  payload: { errorType, error },
2341
2476
  timestamp: Date.now(),
2342
2477
  id: nanoid.nanoid()
@@ -2347,11 +2482,11 @@ var MCPClient = class {
2347
2482
  * @private
2348
2483
  */
2349
2484
  emitProgress(message) {
2350
- if (!this.serverId) return;
2485
+ if (!this.config.serverId) return;
2351
2486
  this._onConnectionEvent.fire({
2352
2487
  type: "progress",
2353
- sessionId: this.sessionId,
2354
- serverId: this.serverId,
2488
+ sessionId: this.config.sessionId,
2489
+ serverId: this.config.serverId,
2355
2490
  message,
2356
2491
  timestamp: Date.now()
2357
2492
  });
@@ -2369,14 +2504,14 @@ var MCPClient = class {
2369
2504
  * @private
2370
2505
  */
2371
2506
  getTransport(type) {
2372
- if (!this.serverUrl) {
2507
+ if (!this.config.serverUrl) {
2373
2508
  throw new Error("Server URL is required to create transport");
2374
2509
  }
2375
- const baseUrl = new URL(this.serverUrl);
2376
- const hasAuthorizationHeader = Object.keys(this.headers || {}).some((key) => key.toLowerCase() === "authorization");
2510
+ const baseUrl = new URL(this.config.serverUrl);
2511
+ const hasAuthorizationHeader = Object.keys(this.config.headers || {}).some((key) => key.toLowerCase() === "authorization");
2377
2512
  const transportOptions = {
2378
2513
  ...!hasAuthorizationHeader && { authProvider: this.oauthProvider },
2379
- ...this.headers && { requestInit: { headers: this.headers } },
2514
+ ...this.config.headers && { requestInit: { headers: this.config.headers } },
2380
2515
  /**
2381
2516
  * Custom fetch implementation to handle connection timeouts.
2382
2517
  * Observation: SDK 1.24.0+ connections may hang indefinitely in some environments.
@@ -2414,78 +2549,74 @@ var MCPClient = class {
2414
2549
  * @private
2415
2550
  */
2416
2551
  async ensureSession() {
2417
- if (this.oauthProvider) {
2418
- return;
2419
- }
2552
+ if (this.oauthProvider) return;
2420
2553
  this.emitStateChange("INITIALIZING");
2421
2554
  this.emitProgress("Loading session configuration...");
2422
- let existingSession = null;
2423
- if (!this.serverUrl || !this.callbackUrl || !this.serverId) {
2424
- existingSession = await sessions.get(this.userId, this.sessionId);
2425
- if (!existingSession) {
2426
- throw new Error(`Session not found: ${this.sessionId}`);
2555
+ if (!this.config.serverUrl || !this.config.callbackUrl || !this.config.serverId) {
2556
+ const existingSession2 = await this._store.get(this.config.userId, this.config.sessionId);
2557
+ if (!existingSession2) {
2558
+ throw new Error(`Session not found: ${this.config.sessionId}`);
2427
2559
  }
2428
- this.serverUrl = this.serverUrl || existingSession.serverUrl;
2429
- this.callbackUrl = this.callbackUrl || existingSession.callbackUrl;
2430
- this.serverName = this.serverName || existingSession.serverName;
2431
- this.serverId = this.serverId || existingSession.serverId || "unknown";
2432
- this.headers = this.headers || existingSession.headers;
2433
- this.createdAt = existingSession.createdAt;
2434
- }
2435
- if (!this.serverUrl || !this.callbackUrl || !this.serverId) {
2560
+ this.config.serverUrl = this.config.serverUrl || existingSession2.serverUrl;
2561
+ this.config.callbackUrl = this.config.callbackUrl || existingSession2.callbackUrl;
2562
+ this.config.serverName = this.config.serverName || existingSession2.serverName;
2563
+ this.config.serverId = this.config.serverId || existingSession2.serverId || "unknown";
2564
+ this.config.headers = this.config.headers || existingSession2.headers;
2565
+ this.createdAt = existingSession2.createdAt;
2566
+ }
2567
+ if (!this.config.serverUrl || !this.config.callbackUrl || !this.config.serverId) {
2436
2568
  throw new Error("Missing required connection metadata");
2437
2569
  }
2438
- if (!this.oauthProvider) {
2439
- if (!this.serverId) {
2440
- throw new Error("serverId required for OAuth provider initialization");
2441
- }
2442
- this.oauthProvider = new StorageOAuthClientProvider({
2443
- userId: this.userId,
2444
- serverId: this.serverId,
2445
- sessionId: this.sessionId,
2446
- redirectUrl: this.callbackUrl,
2447
- clientName: this.clientName,
2448
- clientUri: this.clientUri,
2449
- logoUri: this.logoUri,
2450
- policyUri: this.policyUri,
2451
- clientId: this.clientId,
2452
- clientSecret: this.clientSecret,
2453
- onRedirect: (redirectUrl) => {
2454
- if (this.onRedirect) {
2455
- this.onRedirect(redirectUrl);
2456
- }
2570
+ this.oauthProvider = new StorageOAuthClientProvider({
2571
+ userId: this.config.userId,
2572
+ serverId: this.config.serverId,
2573
+ sessionId: this.config.sessionId,
2574
+ redirectUrl: this.config.callbackUrl,
2575
+ clientName: this.config.clientName,
2576
+ clientUri: this.config.clientUri,
2577
+ logoUri: this.config.logoUri,
2578
+ policyUri: this.config.policyUri,
2579
+ clientId: this.config.clientId,
2580
+ clientSecret: this.config.clientSecret,
2581
+ cachedTokens: this.config.cachedCredentials?.tokens,
2582
+ sessionStore: this._store,
2583
+ onRedirect: (redirectUrl) => {
2584
+ if (this.config.serverId) {
2585
+ this._onConnectionEvent.fire({
2586
+ type: "auth_required",
2587
+ sessionId: this.config.sessionId,
2588
+ serverId: this.config.serverId,
2589
+ authUrl: redirectUrl,
2590
+ timestamp: Date.now()
2591
+ });
2457
2592
  }
2458
- });
2459
- }
2460
- if (existingSession === null) {
2461
- existingSession = await sessions.get(this.userId, this.sessionId);
2462
- }
2463
- if (!existingSession && this.serverId && this.serverUrl && this.callbackUrl) {
2593
+ if (this.config.onRedirect) {
2594
+ this.config.onRedirect(redirectUrl);
2595
+ }
2596
+ }
2597
+ });
2598
+ const existingSession = this.config.hasSession ? {} : await this._store.get(this.config.userId, this.config.sessionId);
2599
+ if (!existingSession) {
2464
2600
  this.createdAt = Date.now();
2465
2601
  const updatedAt = this.createdAt;
2466
2602
  this._onObservabilityEvent.fire({
2467
2603
  type: "mcp:client:session_created",
2468
2604
  level: "info",
2469
- message: `Creating pending session ${this.sessionId} for connection setup`,
2470
- sessionId: this.sessionId,
2471
- serverId: this.serverId,
2605
+ message: `Creating pending session ${this.config.sessionId} for connection setup`,
2606
+ sessionId: this.config.sessionId,
2607
+ serverId: this.config.serverId,
2472
2608
  timestamp: Date.now(),
2473
2609
  id: nanoid.nanoid()
2474
2610
  });
2475
- await sessions.create({
2476
- sessionId: this.sessionId,
2477
- userId: this.userId,
2478
- serverId: this.serverId,
2479
- serverName: this.serverName,
2480
- serverUrl: this.serverUrl,
2481
- callbackUrl: this.callbackUrl,
2482
- transportType: this.transportType || "streamable-http",
2483
- headers: this.headers,
2484
- createdAt: this.createdAt,
2611
+ await this._store.create({
2612
+ ...this.session,
2485
2613
  updatedAt,
2486
2614
  status: "pending"
2487
2615
  });
2488
2616
  }
2617
+ if (!existingSession && this.oauthProvider instanceof StorageOAuthClientProvider) {
2618
+ await this.oauthProvider.initializeCredentials();
2619
+ }
2489
2620
  }
2490
2621
  /**
2491
2622
  * Saves current session state to the session store
@@ -2493,34 +2624,14 @@ var MCPClient = class {
2493
2624
  * @param status - Session lifecycle status used by storage cleanup
2494
2625
  * @private
2495
2626
  */
2496
- async saveSession(status = "active", existingSession) {
2497
- if (!this.sessionId || !this.serverId || !this.serverUrl || !this.callbackUrl) {
2627
+ async saveSession(status = "active") {
2628
+ if (!this.config.sessionId || !this.config.serverId || !this.config.serverUrl || !this.config.callbackUrl) {
2498
2629
  return;
2499
2630
  }
2500
- const sessionData = {
2501
- sessionId: this.sessionId,
2502
- userId: this.userId,
2503
- serverId: this.serverId,
2504
- serverName: this.serverName,
2505
- serverUrl: this.serverUrl,
2506
- callbackUrl: this.callbackUrl,
2507
- transportType: this.transportType || "streamable-http",
2508
- headers: this.headers,
2509
- createdAt: this.createdAt || Date.now(),
2510
- updatedAt: Date.now(),
2631
+ await this._store.update(this.config.userId, this.config.sessionId, {
2632
+ ...this.session,
2511
2633
  status
2512
- };
2513
- if (status === "active") {
2514
- sessionData.authUrl = null;
2515
- }
2516
- if (existingSession === void 0) {
2517
- existingSession = await sessions.get(this.userId, this.sessionId);
2518
- }
2519
- if (existingSession) {
2520
- await sessions.update(this.userId, this.sessionId, sessionData);
2521
- } else {
2522
- await sessions.create(sessionData);
2523
- }
2634
+ });
2524
2635
  }
2525
2636
  /**
2526
2637
  * Removes transient setup/auth sessions without masking the original error.
@@ -2528,7 +2639,7 @@ var MCPClient = class {
2528
2639
  */
2529
2640
  async deleteTransientSession() {
2530
2641
  try {
2531
- await sessions.delete(this.userId, this.sessionId);
2642
+ await this._store.delete(this.config.userId, this.config.sessionId);
2532
2643
  } catch {
2533
2644
  }
2534
2645
  }
@@ -2538,7 +2649,7 @@ var MCPClient = class {
2538
2649
  * @private
2539
2650
  */
2540
2651
  async tryConnect() {
2541
- const transportsToTry = this.transportType ? [this.transportType] : ["streamable-http", "sse"];
2652
+ const transportsToTry = this.config.transportType ? [this.config.transportType] : ["streamable-http", "sse"];
2542
2653
  let lastError;
2543
2654
  for (const currentType of transportsToTry) {
2544
2655
  const isLastAttempt = currentType === transportsToTry[transportsToTry.length - 1];
@@ -2546,6 +2657,7 @@ var MCPClient = class {
2546
2657
  const transport = this.getTransport(currentType);
2547
2658
  this.transport = transport;
2548
2659
  await this.client.connect(transport);
2660
+ this._serverInfo = this.client.getServerVersion();
2549
2661
  return { transportType: currentType };
2550
2662
  } catch (error) {
2551
2663
  lastError = error;
@@ -2561,8 +2673,8 @@ var MCPClient = class {
2561
2673
  this._onObservabilityEvent.fire({
2562
2674
  level: "warn",
2563
2675
  message: `Transport ${currentType} failed, falling back`,
2564
- sessionId: this.sessionId,
2565
- serverId: this.serverId,
2676
+ sessionId: this.config.sessionId,
2677
+ serverId: this.config.serverId,
2566
2678
  metadata: {
2567
2679
  failedTransport: currentType,
2568
2680
  error: errorMessage
@@ -2605,15 +2717,15 @@ var MCPClient = class {
2605
2717
  try {
2606
2718
  this.emitStateChange("CONNECTING");
2607
2719
  const { transportType } = await this.tryConnect();
2608
- this.transportType = transportType;
2720
+ this.config.transportType = transportType;
2609
2721
  this.emitStateChange("CONNECTED");
2610
2722
  this.emitProgress("Connected successfully");
2611
2723
  this._onObservabilityEvent.fire({
2612
2724
  type: "mcp:client:session_saved",
2613
2725
  level: "info",
2614
- message: `Saving active session ${this.sessionId} (connect success)`,
2615
- sessionId: this.sessionId,
2616
- serverId: this.serverId,
2726
+ message: `Saving active session ${this.config.sessionId} (connect success)`,
2727
+ sessionId: this.config.sessionId,
2728
+ serverId: this.config.serverId,
2617
2729
  timestamp: Date.now(),
2618
2730
  id: nanoid.nanoid()
2619
2731
  });
@@ -2636,23 +2748,23 @@ var MCPClient = class {
2636
2748
  this._onObservabilityEvent.fire({
2637
2749
  type: "mcp:client:session_saved",
2638
2750
  level: "info",
2639
- message: `Saving pending OAuth session ${this.sessionId}`,
2640
- sessionId: this.sessionId,
2641
- serverId: this.serverId,
2751
+ message: `Saving pending OAuth session ${this.config.sessionId}`,
2752
+ sessionId: this.config.sessionId,
2753
+ serverId: this.config.serverId,
2642
2754
  timestamp: Date.now(),
2643
2755
  id: nanoid.nanoid()
2644
2756
  });
2645
2757
  await this.saveSession("pending");
2646
- if (this.serverId) {
2758
+ if (this.config.serverId) {
2647
2759
  this._onConnectionEvent.fire({
2648
2760
  type: "auth_required",
2649
- sessionId: this.sessionId,
2650
- serverId: this.serverId,
2761
+ sessionId: this.config.sessionId,
2762
+ serverId: this.config.serverId,
2651
2763
  authUrl,
2652
2764
  timestamp: Date.now()
2653
2765
  });
2654
- if (authUrl && this.onRedirect) {
2655
- this.onRedirect(authUrl);
2766
+ if (authUrl && this.config.onRedirect) {
2767
+ this.config.onRedirect(authUrl);
2656
2768
  }
2657
2769
  }
2658
2770
  throw new UnauthorizedError("OAuth authorization required");
@@ -2661,9 +2773,9 @@ var MCPClient = class {
2661
2773
  this.emitError(errorMessage, "connection");
2662
2774
  this.emitStateChange("FAILED");
2663
2775
  try {
2664
- const existingSession = await sessions.get(this.userId, this.sessionId);
2776
+ const existingSession = await this._store.get(this.config.userId, this.config.sessionId);
2665
2777
  if (!existingSession || existingSession.status !== "active") {
2666
- await sessions.delete(this.userId, this.sessionId);
2778
+ await this._store.delete(this.config.userId, this.config.sessionId);
2667
2779
  }
2668
2780
  } catch {
2669
2781
  }
@@ -2688,16 +2800,16 @@ var MCPClient = class {
2688
2800
  throw new Error(error);
2689
2801
  }
2690
2802
  if (state) {
2691
- const stateCheck = await this.oauthProvider.checkState(state);
2692
- if (!stateCheck.valid) {
2693
- const error = stateCheck.error || "Invalid OAuth state";
2694
- this.emitError(error, "auth");
2803
+ try {
2804
+ await this.oauthProvider.consumeState(state);
2805
+ } catch (error) {
2806
+ const msg = error instanceof Error ? error.message : "Invalid OAuth state";
2807
+ this.emitError(msg, "auth");
2695
2808
  this.emitStateChange("FAILED");
2696
- throw new Error(error);
2809
+ throw error;
2697
2810
  }
2698
- await this.oauthProvider.consumeState(state);
2699
2811
  }
2700
- const transportsToTry = this.transportType ? [this.transportType] : ["streamable-http", "sse"];
2812
+ const transportsToTry = this.config.transportType ? [this.config.transportType] : ["streamable-http", "sse"];
2701
2813
  let lastError;
2702
2814
  let tokensExchanged = false;
2703
2815
  let authenticatedStateEmitted = false;
@@ -2724,14 +2836,15 @@ var MCPClient = class {
2724
2836
  }
2725
2837
  }
2726
2838
  await this.client.connect(this.transport);
2727
- this.transportType = currentType;
2839
+ this._serverInfo = this.client.getServerVersion();
2840
+ this.config.transportType = currentType;
2728
2841
  this.emitStateChange("CONNECTED");
2729
2842
  this._onObservabilityEvent.fire({
2730
2843
  type: "mcp:client:session_saved",
2731
2844
  level: "info",
2732
- message: `Saving active session ${this.sessionId} (OAuth complete)`,
2733
- sessionId: this.sessionId,
2734
- serverId: this.serverId,
2845
+ message: `Saving active session ${this.config.sessionId} (OAuth complete)`,
2846
+ sessionId: this.config.sessionId,
2847
+ serverId: this.config.serverId,
2735
2848
  timestamp: Date.now(),
2736
2849
  id: nanoid.nanoid()
2737
2850
  });
@@ -2804,11 +2917,11 @@ var MCPClient = class {
2804
2917
  this.emitStateChange("DISCOVERING");
2805
2918
  try {
2806
2919
  const result = await this.fetchTools();
2807
- if (this.serverId) {
2920
+ if (this.config.serverId) {
2808
2921
  this._onConnectionEvent.fire({
2809
2922
  type: "tools_discovered",
2810
- sessionId: this.sessionId,
2811
- serverId: this.serverId,
2923
+ sessionId: this.config.sessionId,
2924
+ serverId: this.config.serverId,
2812
2925
  toolCount: result.tools.length,
2813
2926
  tools: result.tools,
2814
2927
  timestamp: Date.now()
@@ -2848,8 +2961,8 @@ var MCPClient = class {
2848
2961
  level: "info",
2849
2962
  message: `Tool ${toolName} called successfully`,
2850
2963
  displayMessage: `Called tool ${toolName}`,
2851
- sessionId: this.sessionId,
2852
- serverId: this.serverId,
2964
+ sessionId: this.config.sessionId,
2965
+ serverId: this.config.serverId,
2853
2966
  payload: {
2854
2967
  toolName,
2855
2968
  args: toolArgs
@@ -2865,8 +2978,8 @@ var MCPClient = class {
2865
2978
  level: "error",
2866
2979
  message: errorMessage,
2867
2980
  displayMessage: `Failed to call tool ${toolName}`,
2868
- sessionId: this.sessionId,
2869
- serverId: this.serverId,
2981
+ sessionId: this.config.sessionId,
2982
+ serverId: this.config.serverId,
2870
2983
  payload: {
2871
2984
  errorType: "tool_execution",
2872
2985
  error: errorMessage,
@@ -3014,8 +3127,8 @@ var MCPClient = class {
3014
3127
  type: "mcp:client:error",
3015
3128
  level: "warn",
3016
3129
  message: "Initialization failed during clearSession",
3017
- sessionId: this.sessionId,
3018
- serverId: this.serverId,
3130
+ sessionId: this.config.sessionId,
3131
+ serverId: this.config.serverId,
3019
3132
  payload: { error: String(error) },
3020
3133
  timestamp: Date.now(),
3021
3134
  id: nanoid.nanoid()
@@ -3024,7 +3137,7 @@ var MCPClient = class {
3024
3137
  if (this.oauthProvider) {
3025
3138
  await this.oauthProvider.invalidateCredentials("all");
3026
3139
  }
3027
- await sessions.delete(this.userId, this.sessionId);
3140
+ await this._store.delete(this.config.userId, this.config.sessionId);
3028
3141
  await this.disconnect();
3029
3142
  }
3030
3143
  /**
@@ -3058,19 +3171,19 @@ var MCPClient = class {
3058
3171
  }
3059
3172
  this.oauthProvider = null;
3060
3173
  this.transport = null;
3061
- if (this.serverId) {
3174
+ if (this.config.serverId) {
3062
3175
  this._onConnectionEvent.fire({
3063
3176
  type: "disconnected",
3064
- sessionId: this.sessionId,
3065
- serverId: this.serverId,
3177
+ sessionId: this.config.sessionId,
3178
+ serverId: this.config.serverId,
3066
3179
  timestamp: Date.now()
3067
3180
  });
3068
3181
  this._onObservabilityEvent.fire({
3069
3182
  type: "mcp:client:disconnect",
3070
3183
  level: "info",
3071
- message: `Disconnected from ${this.serverId}`,
3072
- sessionId: this.sessionId,
3073
- serverId: this.serverId,
3184
+ message: `Disconnected from ${this.config.serverId}`,
3185
+ sessionId: this.config.sessionId,
3186
+ serverId: this.config.serverId,
3074
3187
  payload: {},
3075
3188
  timestamp: Date.now(),
3076
3189
  id: nanoid.nanoid()
@@ -3095,42 +3208,52 @@ var MCPClient = class {
3095
3208
  * @returns Server URL or empty string if not set
3096
3209
  */
3097
3210
  getServerUrl() {
3098
- return this.serverUrl || "";
3211
+ return this.config.serverUrl || "";
3099
3212
  }
3100
3213
  /**
3101
3214
  * Gets the OAuth callback URL
3102
3215
  * @returns Callback URL or empty string if not set
3103
3216
  */
3104
3217
  getCallbackUrl() {
3105
- return this.callbackUrl || "";
3218
+ return this.config.callbackUrl || "";
3106
3219
  }
3107
3220
  /**
3108
3221
  * Gets the transport type being used
3109
3222
  * @returns Transport type (defaults to 'streamable-http')
3110
3223
  */
3111
3224
  getTransportType() {
3112
- return this.transportType || "streamable-http";
3225
+ return this.config.transportType || "streamable-http";
3226
+ }
3227
+ /**
3228
+ * Gets the full server metadata from the MCP initialize response.
3229
+ * Includes name, version, icons, title, description, and website URL.
3230
+ * Returns undefined if the client hasn't connected yet.
3231
+ */
3232
+ getServerInfo() {
3233
+ return this._serverInfo;
3113
3234
  }
3114
3235
  /**
3115
- * Gets the human-readable server name
3236
+ * Gets the human-readable server name.
3237
+ * Prefers the server's reported title/name from the initialize response,
3238
+ * falling back to the name provided at construction or session metadata.
3116
3239
  * @returns Server name or undefined
3117
3240
  */
3118
3241
  getServerName() {
3119
- return this.serverName;
3242
+ return this.config.serverName;
3120
3243
  }
3121
3244
  /**
3122
3245
  * Gets the server ID
3123
3246
  * @returns Server ID or undefined
3124
3247
  */
3125
3248
  getServerId() {
3126
- return this.serverId;
3249
+ return this.config.serverId;
3127
3250
  }
3128
3251
  /**
3129
3252
  * Gets the session ID
3130
3253
  * @returns Session ID
3131
3254
  */
3132
3255
  getSessionId() {
3133
- return this.sessionId;
3256
+ return this.config.sessionId;
3134
3257
  }
3135
3258
  };
3136
3259
 
@@ -3157,6 +3280,14 @@ var ToolPolicyGateway = class {
3157
3280
  getServerId() {
3158
3281
  return this.client.getServerId?.();
3159
3282
  }
3283
+ /**
3284
+ * Returns the full server metadata from the underlying client, if available.
3285
+ * Includes name, version, icons, title, description, and website URL.
3286
+ * Available only after the client has connected and completed initialization.
3287
+ */
3288
+ getServerInfo() {
3289
+ return this.client.getServerInfo?.();
3290
+ }
3160
3291
  /**
3161
3292
  * Returns the human-readable server name from the underlying client, if available.
3162
3293
  */
@@ -3285,6 +3416,7 @@ var MultiSessionClient = class {
3285
3416
  __publicField(this, "connectionPromises", /* @__PURE__ */ new Map());
3286
3417
  __publicField(this, "userId");
3287
3418
  __publicField(this, "options");
3419
+ __publicField(this, "_store");
3288
3420
  this.userId = userId;
3289
3421
  this.options = {
3290
3422
  timeout: DEFAULT_TIMEOUT_MS,
@@ -3292,6 +3424,7 @@ var MultiSessionClient = class {
3292
3424
  retryDelay: DEFAULT_RETRY_DELAY_MS,
3293
3425
  ...options
3294
3426
  };
3427
+ this._store = options.onObservabilityEvent ? withDbObservability(options.sessionStore ?? sessions, options.onObservabilityEvent) : options.sessionStore ?? sessions;
3295
3428
  }
3296
3429
  // -----------------------------------------------------------------------
3297
3430
  // Public API
@@ -3375,9 +3508,9 @@ var MultiSessionClient = class {
3375
3508
  * to querying the storage backend via `sessions.list(userId)`.
3376
3509
  */
3377
3510
  async fetchActiveSessions() {
3378
- const sessionList = this.options.sessionProvider ? await this.options.sessionProvider() : await sessions.list(this.userId);
3511
+ const sessionList = this.options.sessionProvider ? await this.options.sessionProvider() : await this._store.list(this.userId);
3379
3512
  return sessionList.filter(
3380
- (s) => s.serverId && s.serverUrl && s.callbackUrl && s.status === "active"
3513
+ (s) => s.serverId && s.serverUrl && s.callbackUrl && s.status === "active" && s.enabled !== false
3381
3514
  );
3382
3515
  }
3383
3516
  /**
@@ -3450,8 +3583,13 @@ var MultiSessionClient = class {
3450
3583
  callbackUrl: session.callbackUrl,
3451
3584
  serverName: session.serverName,
3452
3585
  transportType: session.transportType,
3453
- headers: session.headers
3586
+ headers: session.headers,
3587
+ hasSession: true,
3588
+ sessionStore: this._store
3454
3589
  });
3590
+ if (this.options.onObservabilityEvent) {
3591
+ client.onObservabilityEvent(this.options.onObservabilityEvent);
3592
+ }
3455
3593
  const timeoutMs = this.options.timeout;
3456
3594
  let timeoutTimer;
3457
3595
  const timeoutPromise = new Promise((_, reject) => {
@@ -3510,130 +3648,188 @@ function isConnectionEvent(event) {
3510
3648
  }
3511
3649
 
3512
3650
  // src/server/handlers/sse-handler.ts
3513
- var DEFAULT_HEARTBEAT_INTERVAL = 3e4;
3651
+ var DEFAULT_HEARTBEAT_MS = 3e4;
3514
3652
  function normalizeHeaders(headers) {
3515
3653
  if (!headers || typeof headers !== "object") return void 0;
3516
- const entries = Object.entries(headers).map(([key, value]) => [key.trim(), String(value).trim()]).filter(([key, value]) => key.length > 0 && value.length > 0);
3654
+ const entries = Object.entries(headers).map(([k, v]) => [k.trim(), String(v).trim()]).filter(([k, v]) => k.length > 0 && v.length > 0);
3517
3655
  return entries.length > 0 ? Object.fromEntries(entries) : void 0;
3518
3656
  }
3657
+ function validatingStateEvent(sessionId, session) {
3658
+ return {
3659
+ type: "state_changed",
3660
+ sessionId,
3661
+ serverId: session.serverId ?? "unknown",
3662
+ serverName: session.serverName ?? "Unknown",
3663
+ serverUrl: session.serverUrl,
3664
+ state: "VALIDATING",
3665
+ previousState: "DISCONNECTED",
3666
+ timestamp: Date.now()
3667
+ };
3668
+ }
3669
+ function connectionErrorEvent(sessionId, serverId, error, errorType) {
3670
+ return {
3671
+ type: "error",
3672
+ sessionId,
3673
+ serverId: serverId ?? "unknown",
3674
+ error: error instanceof Error ? error.message : "Connection failed",
3675
+ errorType,
3676
+ timestamp: Date.now()
3677
+ };
3678
+ }
3519
3679
  var SSEConnectionManager = class {
3680
+ // -----------------------------------------------------------------------
3681
+ // Constructor
3682
+ // -----------------------------------------------------------------------
3683
+ /**
3684
+ * @param options - Handler configuration (userId, auth, metadata, heartbeat, observability).
3685
+ * @param sendEvent - Callback that writes a typed event onto the SSE stream.
3686
+ */
3520
3687
  constructor(options, sendEvent) {
3521
3688
  this.options = options;
3522
3689
  this.sendEvent = sendEvent;
3690
+ // -----------------------------------------------------------------------
3691
+ // State
3692
+ // -----------------------------------------------------------------------
3523
3693
  __publicField(this, "userId");
3694
+ /** Active MCP transports keyed by sessionId. */
3524
3695
  __publicField(this, "clients", /* @__PURE__ */ new Map());
3696
+ /** Instrumented session store — always wraps `sessions` with DB observability. */
3697
+ __publicField(this, "observedStore");
3525
3698
  __publicField(this, "heartbeatTimer");
3526
- __publicField(this, "isActive", true);
3699
+ __publicField(this, "active", true);
3527
3700
  this.userId = options.userId;
3701
+ this.observedStore = withDbObservability(sessions, (event) => this.sendEvent(event));
3528
3702
  this.startHeartbeat();
3529
3703
  }
3704
+ // -----------------------------------------------------------------------
3705
+ // Public API
3706
+ // -----------------------------------------------------------------------
3530
3707
  /**
3531
- * Get resolved client metadata (dynamic > static > defaults)
3532
- */
3533
- async getResolvedClientMetadata(request) {
3534
- let metadata = {};
3535
- if (this.options.clientDefaults) {
3536
- metadata = { ...this.options.clientDefaults };
3537
- }
3538
- if (this.options.getClientMetadata) {
3539
- const dynamicMetadata = await this.options.getClientMetadata(request);
3540
- metadata = { ...metadata, ...dynamicMetadata };
3541
- }
3542
- return metadata;
3543
- }
3544
- /**
3545
- * Start heartbeat to keep connection alive
3546
- */
3547
- startHeartbeat() {
3548
- const interval = this.options.heartbeatInterval ?? DEFAULT_HEARTBEAT_INTERVAL;
3549
- this.heartbeatTimer = setInterval(() => {
3550
- if (this.isActive) {
3551
- this.sendEvent({
3552
- level: "debug",
3553
- message: "heartbeat",
3554
- timestamp: Date.now()
3555
- });
3556
- }
3557
- }, interval);
3558
- }
3559
- /**
3560
- * Handle incoming RPC requests
3561
- * Returns the RPC response directly for immediate HTTP response (bypassing SSE latency)
3708
+ * Dispatches an incoming RPC request, emits timing observability,
3709
+ * and returns the response.
3710
+ *
3711
+ * Emits `rpc:start` before dispatch and `rpc:end` on completion
3712
+ * (success or error), each carrying the method name, sessionId, and
3713
+ * duration. All events flow through the unified `onObservability`
3714
+ * and the SSE stream.
3715
+ *
3716
+ * @param request - The deserialized RPC envelope.
3717
+ * @returns The RPC response (success or error).
3562
3718
  */
3563
3719
  async handleRequest(request) {
3720
+ const method = request.method;
3721
+ const sessionId = request.params?.sessionId;
3722
+ const t0 = performance.now();
3723
+ this.sendEvent({
3724
+ type: "rpc:start",
3725
+ level: "debug",
3726
+ message: method,
3727
+ sessionId,
3728
+ timestamp: Date.now()
3729
+ });
3564
3730
  try {
3565
- let result;
3566
- switch (request.method) {
3567
- case "listSessions":
3568
- result = await this.listSessions();
3569
- break;
3570
- case "connect":
3571
- result = await this.connect(request.params);
3572
- break;
3573
- case "reconnect":
3574
- result = await this.reconnect(request.params);
3575
- break;
3576
- case "disconnect":
3577
- result = await this.disconnect(request.params);
3578
- break;
3579
- case "listTools":
3580
- result = await this.listTools(request.params);
3581
- break;
3582
- case "setToolPolicy":
3583
- result = await this.setToolPolicy(request.params);
3584
- break;
3585
- case "getToolPolicy":
3586
- result = await this.getToolPolicy(request.params);
3587
- break;
3588
- case "callTool":
3589
- result = await this.callTool(request.params);
3590
- break;
3591
- case "getSession":
3592
- result = await this.getSession(request.params);
3593
- break;
3594
- case "finishAuth":
3595
- result = await this.finishAuth(request.params);
3596
- break;
3597
- case "listPrompts":
3598
- result = await this.listPrompts(request.params);
3599
- break;
3600
- case "getPrompt":
3601
- result = await this.getPrompt(request.params);
3602
- break;
3603
- case "listResources":
3604
- result = await this.listResources(request.params);
3605
- break;
3606
- case "readResource":
3607
- result = await this.readResource(request.params);
3608
- break;
3609
- default:
3610
- throw new Error(`Unknown method: ${request.method}`);
3611
- }
3612
- const response = {
3613
- id: request.id,
3614
- result
3615
- };
3616
- this.sendEvent(response);
3731
+ const result = await this.dispatchImpl(request);
3732
+ this.sendEvent({
3733
+ type: "rpc:end",
3734
+ level: "debug",
3735
+ message: method,
3736
+ sessionId,
3737
+ payload: { durationMs: performance.now() - t0 },
3738
+ timestamp: Date.now()
3739
+ });
3740
+ const response = { id: request.id, result };
3741
+ this.sendEvent(response);
3617
3742
  return response;
3618
3743
  } catch (error) {
3619
- const errorResponse = {
3744
+ this.sendEvent({
3745
+ type: "rpc:end",
3746
+ level: "error",
3747
+ message: method,
3748
+ sessionId,
3749
+ payload: { durationMs: performance.now() - t0, error: String(error) },
3750
+ timestamp: Date.now()
3751
+ });
3752
+ const response = {
3620
3753
  id: request.id,
3621
3754
  error: {
3622
3755
  code: RpcErrorCodes.EXECUTION_ERROR,
3623
3756
  message: error instanceof Error ? error.message : "Unknown error"
3624
3757
  }
3625
3758
  };
3626
- this.sendEvent(errorResponse);
3627
- return errorResponse;
3759
+ this.sendEvent(response);
3760
+ return response;
3761
+ }
3762
+ }
3763
+ /**
3764
+ * Tears down all active MCP transports and stops the heartbeat timer.
3765
+ *
3766
+ * Disconnects are issued in parallel across all sessions. After calling
3767
+ * this method the manager instance should be discarded.
3768
+ */
3769
+ async dispose() {
3770
+ this.active = false;
3771
+ if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
3772
+ await Promise.all(
3773
+ Array.from(this.clients.values()).map((c) => c.disconnect())
3774
+ );
3775
+ this.clients.clear();
3776
+ }
3777
+ // -----------------------------------------------------------------------
3778
+ // RPC Dispatch (raw — called by handleRequest which adds timing)
3779
+ // -----------------------------------------------------------------------
3780
+ /**
3781
+ * Routes an RPC method name to the appropriate private handler.
3782
+ *
3783
+ * @throws {Error} When the method name is unrecognized.
3784
+ */
3785
+ async dispatchImpl(request) {
3786
+ switch (request.method) {
3787
+ case "listSessions":
3788
+ return this.listSessions();
3789
+ case "connect":
3790
+ return this.connect(request.params);
3791
+ case "reconnect":
3792
+ return this.reconnect(request.params);
3793
+ case "disconnect":
3794
+ return this.disconnect(request.params);
3795
+ case "listTools":
3796
+ return this.listTools(request.params);
3797
+ case "setToolPolicy":
3798
+ return this.setToolPolicy(request.params);
3799
+ case "getToolPolicy":
3800
+ return this.getToolPolicy(request.params);
3801
+ case "updateSession":
3802
+ return this.updateSession(request.params);
3803
+ case "callTool":
3804
+ return this.callTool(request.params);
3805
+ case "getSession":
3806
+ return this.getSession(request.params);
3807
+ case "finishAuth":
3808
+ return this.finishAuth(request.params);
3809
+ case "listPrompts":
3810
+ return this.listPrompts(request.params);
3811
+ case "getPrompt":
3812
+ return this.getPrompt(request.params);
3813
+ case "listResources":
3814
+ return this.listResources(request.params);
3815
+ case "readResource":
3816
+ return this.readResource(request.params);
3817
+ default:
3818
+ throw new Error(`Unknown RPC method: ${request.method}`);
3628
3819
  }
3629
3820
  }
3821
+ // -----------------------------------------------------------------------
3822
+ // Session Query
3823
+ // -----------------------------------------------------------------------
3630
3824
  /**
3631
- * Get all sessions for the current userId
3825
+ * Lists all sessions owned by the current user.
3826
+ *
3827
+ * Returns a lightweight view — session metadata only, no credential fields.
3632
3828
  */
3633
3829
  async listSessions() {
3634
- const sessionList = await sessions.list(this.userId);
3830
+ const all = await sessions.list(this.userId);
3635
3831
  return {
3636
- sessions: sessionList.map((s) => ({
3832
+ sessions: all.map((s) => ({
3637
3833
  sessionId: s.sessionId,
3638
3834
  serverId: s.serverId,
3639
3835
  serverName: s.serverName,
@@ -3642,211 +3838,203 @@ var SSEConnectionManager = class {
3642
3838
  createdAt: s.createdAt,
3643
3839
  updatedAt: s.updatedAt ?? s.createdAt,
3644
3840
  status: s.status ?? "pending",
3645
- toolPolicy: s.toolPolicy
3841
+ toolPolicy: s.toolPolicy,
3842
+ enabled: s.enabled ?? true
3646
3843
  }))
3647
3844
  };
3648
3845
  }
3649
3846
  /**
3650
- * Connect to an MCP server
3847
+ * Restores and validates a previously persisted session.
3848
+ *
3849
+ * Loads the full session row (including credentials) from storage, creates
3850
+ * a new MCP transport, connects to the remote server, and emits the
3851
+ * discovered (policy-filtered) tool list via SSE.
3651
3852
  */
3652
- async connect(params) {
3653
- const { serverName, serverUrl, callbackUrl, transportType } = params;
3654
- const headers = normalizeHeaders(params.headers);
3655
- const serverId = params.serverId && params.serverId.length <= 12 ? params.serverId : generateServerId();
3656
- const existingSessions = await sessions.list(this.userId);
3657
- const duplicate = existingSessions.find(
3658
- (s) => s.serverId === serverId || s.serverUrl === serverUrl
3659
- );
3660
- if (duplicate) {
3661
- if (duplicate.status === "pending") {
3662
- await this.getSession({ sessionId: duplicate.sessionId });
3663
- return {
3664
- sessionId: duplicate.sessionId,
3665
- success: true
3666
- };
3667
- }
3668
- throw new Error(`Connection already exists for server: ${duplicate.serverUrl || duplicate.serverId} (${duplicate.serverName})`);
3669
- }
3670
- const sessionId = await sessions.generateSessionId();
3853
+ async getSession(params) {
3854
+ const session = await this.requireSession(params.sessionId);
3855
+ this.sendEvent(validatingStateEvent(params.sessionId, session));
3671
3856
  try {
3672
- const clientMetadata = await this.getResolvedClientMetadata();
3673
- const client = new MCPClient({
3674
- userId: this.userId,
3675
- sessionId,
3676
- serverId,
3677
- serverName,
3678
- serverUrl,
3679
- callbackUrl,
3680
- transportType,
3681
- headers,
3682
- ...clientMetadata
3683
- // Spread client metadata (clientName, clientUri, logoUri, policyUri)
3684
- });
3685
- this.clients.set(sessionId, client);
3686
- client.onConnectionEvent((event) => {
3687
- this.emitConnectionEvent(event);
3688
- });
3689
- client.onObservabilityEvent((event) => {
3690
- this.sendEvent(event);
3691
- });
3857
+ const client = this.restoreClient(session);
3858
+ this.attachClientEvents(client);
3692
3859
  await client.connect();
3693
- await this.listPolicyFilteredTools(sessionId);
3694
- return {
3695
- sessionId,
3696
- success: true
3697
- };
3860
+ this.clients.set(params.sessionId, client);
3861
+ const { result } = await this.listPolicyFilteredTools(params.sessionId);
3862
+ return { success: true, toolCount: result.tools.length };
3698
3863
  } catch (error) {
3699
- if (error instanceof UnauthorizedError) {
3700
- this.clients.delete(sessionId);
3701
- return {
3702
- sessionId,
3703
- success: true
3704
- };
3705
- }
3706
- this.emitConnectionEvent({
3707
- type: "error",
3708
- sessionId,
3709
- serverId,
3710
- error: error instanceof Error ? error.message : "Connection failed",
3711
- errorType: "connection",
3712
- timestamp: Date.now()
3713
- });
3714
- this.clients.delete(sessionId);
3864
+ this.sendEvent(connectionErrorEvent(params.sessionId, session.serverId, error, "validation"));
3715
3865
  throw error;
3716
3866
  }
3717
3867
  }
3868
+ // -----------------------------------------------------------------------
3869
+ // Connection Lifecycle
3870
+ // -----------------------------------------------------------------------
3718
3871
  /**
3719
- * Reconnect to an MCP server — tears down the active client transport/connection
3720
- * and creates a fresh connection while reusing the existing session credentials in a single RPC call.
3872
+ * Initiates a connection to a new MCP server.
3873
+ *
3874
+ * If a session for the same `serverId` or `serverUrl` already exists and
3875
+ * is still in a `pending` (OAuth) state, the existing session is resumed
3876
+ * instead of creating a duplicate.
3877
+ *
3878
+ * `UnauthorizedError` is treated as a pending-auth state and returned as
3879
+ * a successful result (the client will then redirect to the auth URL).
3721
3880
  */
3722
- async reconnect(params) {
3723
- const { serverId: rawServerId, serverName, serverUrl, callbackUrl, transportType } = params;
3881
+ async connect(params) {
3724
3882
  const headers = normalizeHeaders(params.headers);
3725
- const serverId = rawServerId && rawServerId.length <= 12 ? rawServerId : generateServerId();
3726
- const existingSessions = await sessions.list(this.userId);
3727
- const duplicate = existingSessions.find(
3728
- (s) => s.serverId === serverId || s.serverUrl === serverUrl
3729
- );
3730
- const sessionId = duplicate ? duplicate.sessionId : await sessions.generateSessionId();
3731
- if (duplicate) {
3732
- const existingClient = this.clients.get(duplicate.sessionId);
3733
- if (existingClient) {
3734
- await existingClient.disconnect();
3735
- this.clients.delete(duplicate.sessionId);
3883
+ const serverId = this.normalizeServerId(params.serverId);
3884
+ const existing = await this.findExistingSession(serverId, params.serverUrl);
3885
+ if (existing) {
3886
+ if (existing.status === "pending") {
3887
+ return this.getSession({ sessionId: existing.sessionId }).then(() => ({
3888
+ sessionId: existing.sessionId,
3889
+ success: true
3890
+ }));
3736
3891
  }
3892
+ throw new Error(
3893
+ `Connection already exists for server: ${existing.serverUrl ?? existing.serverId} (${existing.serverName})`
3894
+ );
3737
3895
  }
3738
- try {
3739
- const clientMetadata = await this.getResolvedClientMetadata();
3740
- const client = new MCPClient({
3741
- userId: this.userId,
3742
- sessionId,
3743
- serverId,
3744
- serverName,
3745
- serverUrl,
3746
- callbackUrl,
3747
- transportType,
3748
- headers,
3749
- ...clientMetadata
3750
- });
3751
- this.clients.set(sessionId, client);
3752
- client.onConnectionEvent((event) => {
3753
- this.emitConnectionEvent(event);
3754
- });
3755
- client.onObservabilityEvent((event) => {
3756
- this.sendEvent(event);
3757
- });
3758
- await client.connect();
3759
- await this.listPolicyFilteredTools(sessionId);
3760
- return { sessionId, success: true };
3761
- } catch (error) {
3762
- if (error instanceof UnauthorizedError) {
3763
- this.clients.delete(sessionId);
3764
- return { sessionId, success: true };
3896
+ const sessionId = await sessions.generateSessionId();
3897
+ const metadata = await this.getResolvedClientMetadata();
3898
+ const client = new MCPClient({
3899
+ userId: this.userId,
3900
+ sessionId,
3901
+ serverId,
3902
+ serverName: params.serverName,
3903
+ serverUrl: params.serverUrl,
3904
+ callbackUrl: params.callbackUrl,
3905
+ transportType: params.transportType,
3906
+ headers,
3907
+ sessionStore: this.observedStore,
3908
+ ...metadata
3909
+ });
3910
+ this.cacheClient(sessionId, client);
3911
+ return this.connectAndDiscover(client, sessionId, serverId);
3912
+ }
3913
+ /**
3914
+ * Reconnects to an MCP server by tearing down the active transport if one
3915
+ * exists and instantiating a fresh connection, reusing the existing session
3916
+ * (and its stored credentials / DCR client info) from the database.
3917
+ */
3918
+ async reconnect(params) {
3919
+ const headers = normalizeHeaders(params.headers);
3920
+ const serverId = this.normalizeServerId(params.serverId);
3921
+ const existing = await this.findExistingSession(serverId, params.serverUrl);
3922
+ const sessionId = existing ? existing.sessionId : await sessions.generateSessionId();
3923
+ if (existing) {
3924
+ const staleClient = this.clients.get(existing.sessionId);
3925
+ if (staleClient) {
3926
+ await staleClient.disconnect();
3927
+ this.clients.delete(existing.sessionId);
3765
3928
  }
3766
- this.emitConnectionEvent({
3767
- type: "error",
3768
- sessionId,
3769
- serverId,
3770
- error: error instanceof Error ? error.message : "Connection failed",
3771
- errorType: "connection",
3772
- timestamp: Date.now()
3773
- });
3774
- this.clients.delete(sessionId);
3775
- throw error;
3776
3929
  }
3930
+ const metadata = await this.getResolvedClientMetadata();
3931
+ const client = new MCPClient({
3932
+ userId: this.userId,
3933
+ sessionId,
3934
+ serverId,
3935
+ serverName: params.serverName,
3936
+ serverUrl: params.serverUrl,
3937
+ callbackUrl: params.callbackUrl,
3938
+ transportType: params.transportType,
3939
+ headers,
3940
+ sessionStore: this.observedStore,
3941
+ ...metadata
3942
+ });
3943
+ this.cacheClient(sessionId, client);
3944
+ return this.connectAndDiscover(client, sessionId, serverId);
3777
3945
  }
3778
3946
  /**
3779
- * Disconnect from an MCP server
3947
+ * Disconnects from an MCP server.
3948
+ *
3949
+ * If an active in-memory transport exists it delegates to `clearSession()`
3950
+ * (which sends the server an HTTP DELETE per the Streamable spec). If no
3951
+ * active transport is available (orphaned session from a failed OAuth flow),
3952
+ * the session row is removed directly from storage.
3780
3953
  */
3781
3954
  async disconnect(params) {
3782
- const { sessionId } = params;
3783
- const client = this.clients.get(sessionId);
3955
+ const client = this.clients.get(params.sessionId);
3784
3956
  if (client) {
3785
3957
  await client.clearSession();
3786
- this.clients.delete(sessionId);
3958
+ this.clients.delete(params.sessionId);
3787
3959
  } else {
3788
- await sessions.delete(this.userId, sessionId);
3960
+ await sessions.delete(this.userId, params.sessionId);
3789
3961
  }
3790
3962
  return { success: true };
3791
3963
  }
3964
+ // -----------------------------------------------------------------------
3965
+ // OAuth
3966
+ // -----------------------------------------------------------------------
3792
3967
  /**
3793
- * Get an existing client or create and connect a new one for the session.
3968
+ * Completes the OAuth 2.1 authorization code flow for a pending session.
3969
+ *
3970
+ * Loads the stored session (with credentials), creates a fresh `MCPClient`,
3971
+ * and calls `finishAuth` inside a {@link runWithCodeVerifierState} context
3972
+ * so the PKCE code verifier is available without a DB read.
3973
+ *
3974
+ * The session's stored `transportType` is intentionally **omitted** from the
3975
+ * client config — this lets `MCPClient` auto-negotiate (try Streamable HTTP
3976
+ * first, fall back to SSE), which is required for servers like Neon that
3977
+ * only support SSE transport.
3794
3978
  */
3795
- async getOrCreateClient(sessionId) {
3796
- const existing = this.clients.get(sessionId);
3797
- if (existing) {
3798
- return existing;
3799
- }
3800
- const session = await sessions.get(this.userId, sessionId);
3801
- if (!session) {
3802
- throw new Error("Session not found");
3979
+ async finishAuth(params) {
3980
+ const parsed = parseOAuthState(params.state);
3981
+ const sessionId = parsed?.sessionId ?? params.state;
3982
+ const session = await this.requireSession(sessionId);
3983
+ try {
3984
+ const clientId = session.clientId ?? void 0;
3985
+ const clientSecret = session.clientInformation?.client_secret ?? void 0;
3986
+ const client = new MCPClient({
3987
+ userId: this.userId,
3988
+ sessionId,
3989
+ serverId: session.serverId,
3990
+ serverName: session.serverName,
3991
+ serverUrl: session.serverUrl,
3992
+ callbackUrl: session.callbackUrl,
3993
+ headers: session.headers,
3994
+ clientId,
3995
+ clientSecret,
3996
+ hasSession: true,
3997
+ cachedCredentials: { tokens: session.tokens ?? void 0 },
3998
+ sessionStore: this.observedStore
3999
+ });
4000
+ this.attachClientEvents(client);
4001
+ await runWithCodeVerifierState(
4002
+ session.codeVerifier ?? "",
4003
+ "S256",
4004
+ () => client.finishAuth(params.code, params.state)
4005
+ );
4006
+ this.clients.set(sessionId, client);
4007
+ const { result } = await this.listPolicyFilteredTools(sessionId);
4008
+ return { success: true, toolCount: result.tools.length };
4009
+ } catch (error) {
4010
+ this.sendEvent(connectionErrorEvent(sessionId, session.serverId, error, "auth"));
4011
+ throw error;
3803
4012
  }
3804
- const client = new MCPClient({
3805
- userId: this.userId,
3806
- sessionId,
3807
- // These fields are optional in MCPClient, but when rehydrating a known
3808
- // stored session on the server we pass them explicitly to preserve the
3809
- // original transport/connection metadata instead of relying on lazy
3810
- // reloading during initialize().
3811
- serverId: session.serverId,
3812
- serverName: session.serverName,
3813
- serverUrl: session.serverUrl,
3814
- callbackUrl: session.callbackUrl,
3815
- transportType: session.transportType,
3816
- headers: session.headers
3817
- });
3818
- client.onConnectionEvent((event) => this.emitConnectionEvent(event));
3819
- client.onObservabilityEvent((event) => this.sendEvent(event));
3820
- await client.connect();
3821
- this.clients.set(sessionId, client);
3822
- return client;
3823
4013
  }
4014
+ // -----------------------------------------------------------------------
4015
+ // Tool Discovery & Policy
4016
+ // -----------------------------------------------------------------------
3824
4017
  /**
3825
- * Fetches all tools from the remote MCP server and emits a `tools_discovered` event.
4018
+ * Fetches the complete tool list from the remote MCP server and emits a
4019
+ * `tools_discovered` SSE event with two lists:
3826
4020
  *
3827
- * Two lists are always published together:
3828
- * - `tools` policy-filtered list that agents are allowed to call.
3829
- * - `allTools` — the complete, unfiltered list used by the management UI so
3830
- * that blocked tools still appear as checkboxes in the dialog.
4021
+ * - `tools` — policy-filtered (what agents are allowed to call)
4022
+ * - `allTools` complete, unfiltered (used by the management UI)
3831
4023
  *
3832
- * `fetchTools()` is called first (populates the in-memory cache), then
3833
- * `gateway.listTools()` re-uses that cache internally — so only one remote
3834
- * network round-trip is made regardless of how many callers follow.
4024
+ * Only a single network round-trip is made: `fetchTools()` populates the
4025
+ * in-memory cache, then `gateway.listTools()` reuses that cache.
3835
4026
  *
3836
- * @param sessionId - The session whose tools should be discovered.
3837
- * @returns The session record and the policy-filtered tool list.
3838
- * @throws {Error} When the session does not exist in the store.
4027
+ * @param sessionId - The session to discover tools for.
4028
+ * @returns The session record and the filtered tool result.
4029
+ * @throws {Error} If the session does not exist in storage.
3839
4030
  */
3840
4031
  async listPolicyFilteredTools(sessionId) {
3841
- const session = await sessions.get(this.userId, sessionId);
3842
- if (!session) {
3843
- throw new Error("Session not found");
3844
- }
4032
+ const session = await this.requireSession(sessionId);
3845
4033
  const client = await this.getOrCreateClient(sessionId);
3846
4034
  const allTools = await client.fetchTools().catch(() => ({ tools: [] }));
3847
4035
  const gateway = createToolPolicyGateway(this.userId, sessionId, client);
3848
4036
  const result = await gateway.listTools();
3849
- this.emitConnectionEvent({
4037
+ this.sendEvent({
3850
4038
  type: "tools_discovered",
3851
4039
  sessionId,
3852
4040
  serverId: session.serverId ?? "unknown",
@@ -3858,254 +4046,254 @@ var SSEConnectionManager = class {
3858
4046
  return { session, result };
3859
4047
  }
3860
4048
  /**
3861
- * Returns the policy-filtered tool list for a session (agent-facing).
3862
- * Internally re-uses `listPolicyFilteredTools` which also emits a
3863
- * `tools_discovered` SSE event to keep client state up to date.
4049
+ * Returns the policy-filtered tool list for a session.
4050
+ *
4051
+ * Delegates to {@link listPolicyFilteredTools}, which also emits a
4052
+ * `tools_discovered` SSE event to keep the client state current.
3864
4053
  */
3865
4054
  async listTools(params) {
3866
- const { sessionId } = params;
3867
- const { result } = await this.listPolicyFilteredTools(sessionId);
4055
+ const { result } = await this.listPolicyFilteredTools(params.sessionId);
3868
4056
  return { tools: result.tools };
3869
4057
  }
3870
4058
  /**
3871
- * Get all raw tools with effective access state for management UI.
4059
+ * Returns all raw tools annotated with their effective policy state
4060
+ * for display in the management UI.
3872
4061
  */
3873
4062
  async getToolPolicy(params) {
3874
- const { sessionId } = params;
3875
- const session = await sessions.get(this.userId, sessionId);
3876
- if (!session) {
3877
- throw new Error("Session not found");
3878
- }
3879
- const client = await this.getOrCreateClient(sessionId);
4063
+ const session = await this.requireSession(params.sessionId);
4064
+ const client = await this.getOrCreateClient(params.sessionId);
3880
4065
  const allTools = await client.fetchTools();
3881
- const toolPolicy = session.toolPolicy ?? {
4066
+ const policy = session.toolPolicy ?? {
3882
4067
  mode: "all",
3883
4068
  toolIds: [],
3884
4069
  updatedAt: session.updatedAt ?? session.createdAt
3885
4070
  };
3886
4071
  const serverId = session.serverId ?? "unknown";
3887
- const tools = allTools.tools.map((tool) => ({
3888
- ...tool,
3889
- toolId: createToolId(serverId, tool.name),
3890
- allowed: isToolAllowed(toolPolicy, tool.name, session.serverId)
4072
+ const tools = allTools.tools.map((t) => ({
4073
+ ...t,
4074
+ toolId: createToolId(serverId, t.name),
4075
+ allowed: isToolAllowed(policy, t.name, session.serverId)
3891
4076
  }));
3892
4077
  return {
3893
- toolPolicy,
4078
+ toolPolicy: policy,
3894
4079
  tools,
3895
4080
  toolCount: tools.length,
3896
- allowedToolCount: tools.filter((tool) => tool.allowed).length
4081
+ allowedToolCount: tools.filter((t) => t.allowed).length
3897
4082
  };
3898
4083
  }
3899
4084
  /**
3900
- * Persists a new tool access policy for a session and broadcasts the updated
3901
- * filtered tool list to all connected browser clients via a `tools_discovered` event.
4085
+ * Persists a new tool access policy and broadcasts the updated filtered
4086
+ * tool list via a `tools_discovered` SSE event.
3902
4087
  *
3903
- * Both `tools` (policy-filtered) and `allTools` (complete list) are emitted
3904
- * so the management UI can immediately reflect the new checkbox states without
3905
- * an additional round-trip to the server.
3906
- *
3907
- * @param params - Session ID and the new `{ mode, toolIds }` policy to apply.
3908
- * @throws {Error} When the session does not exist or the policy references unknown tool IDs.
4088
+ * @throws {Error} If the session does not exist or the policy references
4089
+ * tool IDs that don't match any known tool.
3909
4090
  */
3910
4091
  async setToolPolicy(params) {
3911
- const { sessionId } = params;
3912
- const session = await sessions.get(this.userId, sessionId);
3913
- if (!session) {
3914
- throw new Error("Session not found");
3915
- }
3916
- const client = await this.getOrCreateClient(sessionId);
4092
+ const session = await this.requireSession(params.sessionId);
4093
+ const client = await this.getOrCreateClient(params.sessionId);
3917
4094
  const allTools = await client.fetchTools();
3918
- const toolPolicy = normalizeToolPolicyForUpdate(params.toolPolicy);
3919
- validateToolPolicyAgainstTools(toolPolicy, allTools.tools, session.serverId);
3920
- await sessions.update(this.userId, sessionId, { toolPolicy });
3921
- const filteredTools = createToolPolicyGateway(this.userId, sessionId, client).filterTools({ ...session, toolPolicy }, allTools.tools);
3922
- this.emitConnectionEvent({
4095
+ const policy = normalizeToolPolicyForUpdate(params.toolPolicy);
4096
+ validateToolPolicyAgainstTools(policy, allTools.tools, session.serverId);
4097
+ await sessions.update(this.userId, params.sessionId, { toolPolicy: policy });
4098
+ const filtered = createToolPolicyGateway(
4099
+ this.userId,
4100
+ params.sessionId,
4101
+ client
4102
+ ).filterTools({ ...session, toolPolicy: policy }, allTools.tools);
4103
+ this.sendEvent({
3923
4104
  type: "tools_discovered",
3924
- sessionId,
4105
+ sessionId: params.sessionId,
3925
4106
  serverId: session.serverId ?? "unknown",
3926
- toolCount: filteredTools.length,
3927
- tools: filteredTools,
4107
+ toolCount: filtered.length,
4108
+ tools: filtered,
3928
4109
  allTools: allTools.tools,
3929
4110
  timestamp: Date.now()
3930
4111
  });
3931
- return {
3932
- success: true,
3933
- toolPolicy,
3934
- tools: filteredTools,
3935
- toolCount: filteredTools.length
3936
- };
3937
- }
3938
- /**
3939
- * Proxies a tool invocation to the remote MCP server.
3940
- * Resolves the client for the given session and delegates to the tool router.
3941
- */
3942
- async callTool(params) {
3943
- const { sessionId, toolName, toolArgs } = params;
3944
- const client = await this.getOrCreateClient(sessionId);
3945
- const result = await createToolPolicyGateway(this.userId, sessionId, client).callTool(toolName, toolArgs);
3946
- const meta = result._meta || {};
3947
- return {
3948
- ...result,
3949
- _meta: {
3950
- ...meta,
3951
- sessionId
3952
- }
3953
- };
4112
+ return { success: true, toolPolicy: policy, tools: filtered, toolCount: filtered.length };
3954
4113
  }
3955
4114
  /**
3956
- * Restore and validate an existing session
4115
+ * Enables or disables a session for agent tool discovery.
4116
+ *
4117
+ * Disabled sessions retain their OAuth tokens and connection metadata
4118
+ * but are hidden from `MultiSessionClient.connect()` and blocked from
4119
+ * RPC tool access. Re-enabling does not require re-authentication.
4120
+ *
4121
+ * @param params - `{ sessionId, enabled: boolean }`
4122
+ * @returns `{ success: true }`
4123
+ * @throws {Error} If the session does not exist.
3957
4124
  */
3958
- async getSession(params) {
3959
- const { sessionId } = params;
3960
- const session = await sessions.get(this.userId, sessionId);
3961
- if (!session) {
3962
- throw new Error("Session not found");
3963
- }
3964
- this.emitConnectionEvent({
3965
- type: "state_changed",
3966
- sessionId,
3967
- serverId: session.serverId ?? "unknown",
3968
- serverName: session.serverName ?? "Unknown",
3969
- serverUrl: session.serverUrl,
3970
- state: "VALIDATING",
3971
- previousState: "DISCONNECTED",
3972
- timestamp: Date.now()
3973
- });
3974
- try {
3975
- const clientMetadata = await this.getResolvedClientMetadata();
3976
- const client = new MCPClient({
3977
- userId: this.userId,
3978
- sessionId,
3979
- // These fields are optional in MCPClient, but when rehydrating a known
3980
- // stored session on the server we pass them explicitly to preserve the
3981
- // original transport/connection metadata instead of relying on lazy
3982
- // reloading during initialize().
3983
- serverId: session.serverId,
3984
- serverName: session.serverName,
3985
- serverUrl: session.serverUrl,
3986
- callbackUrl: session.callbackUrl,
3987
- transportType: session.transportType,
3988
- headers: session.headers,
3989
- ...clientMetadata
3990
- });
3991
- client.onConnectionEvent((event) => this.emitConnectionEvent(event));
3992
- client.onObservabilityEvent((event) => this.sendEvent(event));
3993
- await client.connect();
3994
- this.clients.set(sessionId, client);
3995
- const { result: tools } = await this.listPolicyFilteredTools(sessionId);
3996
- return { success: true, toolCount: tools.tools.length };
3997
- } catch (error) {
3998
- this.emitConnectionEvent({
3999
- type: "error",
4000
- sessionId,
4001
- serverId: session.serverId ?? "unknown",
4002
- error: error instanceof Error ? error.message : "Validation failed",
4003
- errorType: "validation",
4004
- timestamp: Date.now()
4005
- });
4006
- throw error;
4007
- }
4125
+ async updateSession(params) {
4126
+ await this.requireSession(params.sessionId);
4127
+ await sessions.update(this.userId, params.sessionId, { enabled: params.enabled });
4128
+ return { success: true };
4008
4129
  }
4130
+ // -----------------------------------------------------------------------
4131
+ // Tool Execution
4132
+ // -----------------------------------------------------------------------
4009
4133
  /**
4010
- * Complete OAuth authorization flow
4134
+ * Proxies a tool invocation to the remote MCP server.
4135
+ *
4136
+ * Resolves (or creates) the transport for the given session, runs the call
4137
+ * through the tool-policy gateway, and injects `sessionId` into the result
4138
+ * metadata so the client can route the response without scanning all sessions.
4011
4139
  */
4012
- async finishAuth(params) {
4013
- const { code } = params;
4014
- const oauthState = params.state;
4015
- const parsedState = parseOAuthState(oauthState);
4016
- const sessionId = parsedState?.sessionId || oauthState;
4017
- const session = await sessions.get(this.userId, sessionId);
4018
- if (!session) {
4019
- throw new Error("Session not found");
4020
- }
4021
- try {
4022
- const client = new MCPClient({
4023
- userId: this.userId,
4024
- sessionId,
4025
- // These fields are optional in MCPClient, but when rehydrating a known
4026
- // stored session on the server we pass them explicitly to preserve the
4027
- // original connection metadata instead of relying on lazy
4028
- // reloading during initialize().
4029
- serverId: session.serverId,
4030
- serverName: session.serverName,
4031
- serverUrl: session.serverUrl,
4032
- callbackUrl: session.callbackUrl,
4033
- // NOTE: transportType is intentionally omitted here.
4034
- // The session's stored transportType is a placeholder ('streamable-http')
4035
- // set before transport negotiation. Omitting it lets MCPClient auto-negotiate
4036
- // (try streamable_http → SSE fallback), which is critical for servers like
4037
- // Neon that only support SSE transport.
4038
- headers: session.headers
4039
- });
4040
- client.onConnectionEvent((event) => this.emitConnectionEvent(event));
4041
- await client.finishAuth(code, oauthState);
4042
- this.clients.set(sessionId, client);
4043
- const { result: tools } = await this.listPolicyFilteredTools(sessionId);
4044
- return { success: true, toolCount: tools.tools.length };
4045
- } catch (error) {
4046
- this.emitConnectionEvent({
4047
- type: "error",
4048
- sessionId,
4049
- serverId: session.serverId ?? "unknown",
4050
- error: error instanceof Error ? error.message : "OAuth completion failed",
4051
- errorType: "auth",
4052
- timestamp: Date.now()
4053
- });
4054
- throw error;
4055
- }
4140
+ async callTool(params) {
4141
+ const client = await this.getOrCreateClient(params.sessionId);
4142
+ const result = await createToolPolicyGateway(
4143
+ this.userId,
4144
+ params.sessionId,
4145
+ client
4146
+ ).callTool(params.toolName, params.toolArgs);
4147
+ return { ...result, _meta: { ...result._meta ?? {}, sessionId: params.sessionId } };
4056
4148
  }
4057
- /**
4058
- * List prompts from a session
4059
- */
4149
+ // -----------------------------------------------------------------------
4150
+ // Prompts
4151
+ // -----------------------------------------------------------------------
4152
+ /** Lists all prompts exposed by the remote MCP server. */
4060
4153
  async listPrompts(params) {
4061
- const { sessionId } = params;
4062
- const client = await this.getOrCreateClient(sessionId);
4154
+ const client = await this.getOrCreateClient(params.sessionId);
4063
4155
  const result = await client.listPrompts();
4064
4156
  return { prompts: result.prompts };
4065
4157
  }
4066
- /**
4067
- * Get a specific prompt
4068
- */
4158
+ /** Retrieves a specific prompt by name with optional arguments. */
4069
4159
  async getPrompt(params) {
4070
- const { sessionId, name, args } = params;
4071
- const client = await this.getOrCreateClient(sessionId);
4072
- return await client.getPrompt(name, args);
4160
+ const client = await this.getOrCreateClient(params.sessionId);
4161
+ return client.getPrompt(params.name, params.args);
4073
4162
  }
4074
- /**
4075
- * List resources from a session
4076
- */
4163
+ // -----------------------------------------------------------------------
4164
+ // Resources
4165
+ // -----------------------------------------------------------------------
4166
+ /** Lists all resources exposed by the remote MCP server. */
4077
4167
  async listResources(params) {
4078
- const { sessionId } = params;
4079
- const client = await this.getOrCreateClient(sessionId);
4168
+ const client = await this.getOrCreateClient(params.sessionId);
4080
4169
  const result = await client.listResources();
4081
4170
  return { resources: result.resources };
4082
4171
  }
4172
+ /** Reads a specific resource identified by URI. */
4173
+ async readResource(params) {
4174
+ const client = await this.getOrCreateClient(params.sessionId);
4175
+ return client.readResource(params.uri);
4176
+ }
4177
+ // -----------------------------------------------------------------------
4178
+ // Internal Helpers
4179
+ // -----------------------------------------------------------------------
4180
+ /** Resolves client metadata: `getClientMetadata()` → `clientDefaults` → `{}`. */
4181
+ async getResolvedClientMetadata(request) {
4182
+ let metadata = this.options.clientDefaults ? { ...this.options.clientDefaults } : {};
4183
+ if (this.options.getClientMetadata) {
4184
+ metadata = { ...metadata, ...await this.options.getClientMetadata(request) };
4185
+ }
4186
+ return metadata;
4187
+ }
4188
+ /** Ensures the given session exists in storage and throws otherwise. */
4189
+ async requireSession(sessionId) {
4190
+ const session = await sessions.get(this.userId, sessionId, { includeCredentials: true });
4191
+ if (!session) throw new Error(`Session ${sessionId} not found`);
4192
+ return session;
4193
+ }
4194
+ /** Finds an existing session matching the given serverId or serverUrl. */
4195
+ async findExistingSession(serverId, serverUrl) {
4196
+ const all = await sessions.list(this.userId);
4197
+ return all.find((s) => s.serverId === serverId || s.serverUrl === serverUrl);
4198
+ }
4199
+ /** Normalizes a serverId to max 12 chars (DeepSeek/OpenAI 64-char tool-name limit). */
4200
+ normalizeServerId(raw) {
4201
+ return raw && raw.length <= 12 ? raw : generateServerId();
4202
+ }
4083
4203
  /**
4084
- * Read a specific resource
4204
+ * Returns the cached in-memory transport for `sessionId`, or creates one
4205
+ * from the persisted session row (with credentials) and connects it.
4085
4206
  */
4086
- async readResource(params) {
4087
- const { sessionId, uri } = params;
4088
- const client = await this.getOrCreateClient(sessionId);
4089
- return client.readResource(uri);
4207
+ async getOrCreateClient(sessionId) {
4208
+ const existing = this.clients.get(sessionId);
4209
+ if (existing) return existing;
4210
+ const session = await this.requireSession(sessionId);
4211
+ if (session.enabled === false) {
4212
+ throw new Error("Session is disabled \u2014 re-enable it via updateSession to access tools");
4213
+ }
4214
+ const client = this.restoreClient(session);
4215
+ this.attachClientEvents(client);
4216
+ await client.connect();
4217
+ this.clients.set(sessionId, client);
4218
+ return client;
4090
4219
  }
4091
4220
  /**
4092
- * Emit connection event
4221
+ * Builds an `MCPClient` from a stored session row.
4222
+ *
4223
+ * Extracts `clientId` and `clientSecret` from the session's credential
4224
+ * fields and passes `hasSession: true` + `cachedCredentials` so the client
4225
+ * can skip redundant existence checks and credential reads.
4093
4226
  */
4094
- emitConnectionEvent(event) {
4095
- this.sendEvent(event);
4227
+ restoreClient(session) {
4228
+ const clientId = session.clientId ?? void 0;
4229
+ const clientSecret = session.clientInformation?.client_secret ?? void 0;
4230
+ return new MCPClient({
4231
+ userId: this.userId,
4232
+ sessionId: session.sessionId,
4233
+ serverId: session.serverId,
4234
+ serverName: session.serverName,
4235
+ serverUrl: session.serverUrl,
4236
+ callbackUrl: session.callbackUrl,
4237
+ transportType: session.transportType,
4238
+ headers: session.headers,
4239
+ clientId,
4240
+ clientSecret,
4241
+ hasSession: true,
4242
+ cachedCredentials: { tokens: session.tokens ?? void 0 },
4243
+ sessionStore: this.observedStore
4244
+ });
4096
4245
  }
4097
4246
  /**
4098
- * Cleanup and close all connections
4247
+ * Wires connection and observability events from the client into the
4248
+ * unified observability channel. Connection events go to `sendEvent`
4249
+ * (SSE stream); observability events go to `emitObs` (user callback
4250
+ * + SSE stream).
4099
4251
  */
4100
- async dispose() {
4101
- this.isActive = false;
4102
- if (this.heartbeatTimer) {
4103
- clearInterval(this.heartbeatTimer);
4252
+ attachClientEvents(client) {
4253
+ client.onConnectionEvent((e) => this.sendEvent(e));
4254
+ client.onObservabilityEvent((e) => this.sendEvent(e));
4255
+ }
4256
+ /**
4257
+ * Registers the client in the in-memory cache and attaches its event
4258
+ * listeners to the unified observability channel.
4259
+ */
4260
+ cacheClient(sessionId, client) {
4261
+ this.attachClientEvents(client);
4262
+ this.clients.set(sessionId, client);
4263
+ }
4264
+ /**
4265
+ * Attempts a `client.connect()` and, on success, discovers tools.
4266
+ *
4267
+ * If the server responds with `UnauthorizedError`, the session is
4268
+ * treated as pending OAuth — a successful result is returned with
4269
+ * the sessionId so the browser client can redirect to the auth URL.
4270
+ *
4271
+ * For any other error an `error` connection event is emitted and the
4272
+ * client is removed from the in-memory cache before re-throwing.
4273
+ */
4274
+ async connectAndDiscover(client, sessionId, serverId) {
4275
+ try {
4276
+ await client.connect();
4277
+ await this.listPolicyFilteredTools(sessionId);
4278
+ return { sessionId, success: true };
4279
+ } catch (error) {
4280
+ if (error instanceof UnauthorizedError) {
4281
+ this.clients.delete(sessionId);
4282
+ return { sessionId, success: true };
4283
+ }
4284
+ this.sendEvent(connectionErrorEvent(sessionId, serverId, error, "connection"));
4285
+ this.clients.delete(sessionId);
4286
+ throw error;
4104
4287
  }
4105
- await Promise.all(
4106
- Array.from(this.clients.values()).map((client) => client.disconnect())
4107
- );
4108
- this.clients.clear();
4288
+ }
4289
+ /** Starts the periodic heartbeat timer. */
4290
+ startHeartbeat() {
4291
+ const ms = this.options.heartbeatInterval ?? DEFAULT_HEARTBEAT_MS;
4292
+ this.heartbeatTimer = setInterval(() => {
4293
+ if (this.active) {
4294
+ this.sendEvent({ level: "debug", message: "heartbeat", timestamp: Date.now() });
4295
+ }
4296
+ }, ms);
4109
4297
  }
4110
4298
  };
4111
4299
  function createSSEHandler(options) {
@@ -4134,8 +4322,7 @@ function createSSEHandler(options) {
4134
4322
  });
4135
4323
  req.on("end", async () => {
4136
4324
  try {
4137
- const request = JSON.parse(body);
4138
- await manager.handleRequest(request);
4325
+ await manager.handleRequest(JSON.parse(body));
4139
4326
  } catch {
4140
4327
  }
4141
4328
  });
@@ -4310,8 +4497,11 @@ exports.StorageOAuthClientProvider = StorageOAuthClientProvider;
4310
4497
  exports.UnauthorizedError = UnauthorizedError;
4311
4498
  exports.createNextMcpHandler = createNextMcpHandler;
4312
4499
  exports.createSSEHandler = createSSEHandler;
4500
+ exports.decryptObject = decryptObject;
4501
+ exports.encryptObject = encryptObject;
4313
4502
  exports.onSessionMutation = onSessionMutation;
4314
4503
  exports.sanitizeServerLabel = sanitizeServerLabel;
4315
4504
  exports.sessions = sessions;
4505
+ exports.withDbObservability = withDbObservability;
4316
4506
  //# sourceMappingURL=index.js.map
4317
4507
  //# sourceMappingURL=index.js.map