resource-subscriptions.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * MCP 资源订阅登记表(纯内存,无 IO)。
  3. * 键 = (serverName, uri) → subscriberKey 登记;同一连接被多个 session 共享时只向 server 订阅一次。
  4. * 已成功的登记与每次调用的待确认申请分开计数,取消不能删除此前成功或其它在途调用的登记。
  5. * 向 server 的实际请求、重连重放与日志都在 adapter;这里只管计数与快照。
  6. */
  7. interface SubscriptionRegistration {
  8. committed: boolean;
  9. pending: number;
  10. }
  11. export class McpResourceSubscriptionRegistry {
  12. private readonly byServer = new Map<string, Map<string, Map<string, SubscriptionRegistration>>>();
  13. /** 在等待连接前登记本次申请;完成回调只能结算自己,不能复活被明确删除的登记。 */
  14. begin(
  15. serverName: string,
  16. uri: string,
  17. subscriberKey: string,
  18. ): {
  19. commit: () => void;
  20. release: () => boolean;
  21. } {
  22. const byUri =
  23. this.byServer.get(serverName) ?? new Map<string, Map<string, SubscriptionRegistration>>();
  24. this.byServer.set(serverName, byUri);
  25. const subscribers = byUri.get(uri) ?? new Map<string, SubscriptionRegistration>();
  26. const entry = subscribers.get(subscriberKey) ?? { committed: false, pending: 0 };
  27. entry.pending++;
  28. subscribers.set(subscriberKey, entry);
  29. byUri.set(uri, subscribers);
  30. let settled = false;
  31. const finish = (committed: boolean): boolean => {
  32. if (settled) return false;
  33. settled = true;
  34. if (subscribers.get(subscriberKey) !== entry) return false;
  35. entry.pending--;
  36. entry.committed ||= committed;
  37. if (entry.committed || entry.pending > 0) return false;
  38. return this.remove(serverName, uri, subscriberKey)?.last ?? false;
  39. };
  40. return {
  41. commit: () => {
  42. finish(true);
  43. },
  44. release: () => finish(false),
  45. };
  46. }
  47. /** 撤销一个订阅者;未登记返回 null,否则返回它是不是最后一个(此时该 uri 已从表中移除)。 */
  48. remove(serverName: string, uri: string, subscriberKey: string): { last: boolean } | null {
  49. const byUri = this.byServer.get(serverName);
  50. const subscribers = byUri?.get(uri);
  51. if (!byUri || !subscribers || !subscribers.delete(subscriberKey)) return null;
  52. if (subscribers.size > 0) return { last: false };
  53. byUri.delete(uri);
  54. return { last: true };
  55. }
  56. /** 按 subscriberKey 前缀清理(会话关闭);返回因此归零、需要向 server 退订的 (server, uri)。 */
  57. removeByPrefix(subscriberKeyPrefix: string): Array<{ serverName: string; uri: string }> {
  58. const released: Array<{ serverName: string; uri: string }> = [];
  59. for (const [serverName, byUri] of this.byServer) {
  60. for (const [uri, subscribers] of byUri) {
  61. for (const key of subscribers.keys()) {
  62. if (key.startsWith(subscriberKeyPrefix)) subscribers.delete(key);
  63. }
  64. if (subscribers.size === 0) {
  65. byUri.delete(uri);
  66. released.push({ serverName, uri });
  67. }
  68. }
  69. }
  70. return released;
  71. }
  72. /** 某 uri 的订阅者(通知 resources/updated 时附上)。 */
  73. subscribers(serverName: string, uri: string): string[] {
  74. return [...(this.byServer.get(serverName)?.get(uri)?.keys() ?? [])];
  75. }
  76. /** 某 server 全部订阅者去重(通知 resources/list_changed 时附上)。 */
  77. allSubscribers(serverName: string): string[] {
  78. const out = new Set<string>();
  79. for (const keys of this.byServer.get(serverName)?.values() ?? []) {
  80. for (const key of keys.keys()) out.add(key);
  81. }
  82. return [...out];
  83. }
  84. /** 某 server 登记过的 uri(重连后重放)。 */
  85. uris(serverName: string): string[] {
  86. return [...(this.byServer.get(serverName)?.keys() ?? [])];
  87. }
  88. /** 快照(测试 / 诊断):serverName → uri → 订阅者数。 */
  89. counts(): Record<string, Record<string, number>> {
  90. const out: Record<string, Record<string, number>> = {};
  91. for (const [serverName, byUri] of this.byServer) {
  92. out[serverName] = Object.fromEntries(
  93. [...byUri].map(([uri, subscribers]) => [uri, subscribers.size]),
  94. );
  95. }
  96. return out;
  97. }
  98. }