@elliotding/ai-agent-mcp 0.1.24 → 0.1.26

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 (233) hide show
  1. package/README.md +27 -0
  2. package/package.json +4 -1
  3. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-generate-testcase.md +0 -101
  4. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-submit_zct_job.md +0 -158
  5. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-conf-status.md +0 -311
  6. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-sdk-log.md +0 -64
  7. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-zmb-log-errors.md +0 -84
  8. package/ai-resource-telemetry.json +0 -40
  9. package/dist/api/cached-client.d.ts +0 -48
  10. package/dist/api/cached-client.d.ts.map +0 -1
  11. package/dist/api/cached-client.js +0 -126
  12. package/dist/api/cached-client.js.map +0 -1
  13. package/dist/api/client.d.ts +0 -281
  14. package/dist/api/client.d.ts.map +0 -1
  15. package/dist/api/client.js +0 -371
  16. package/dist/api/client.js.map +0 -1
  17. package/dist/auth/index.d.ts +0 -8
  18. package/dist/auth/index.d.ts.map +0 -1
  19. package/dist/auth/index.js +0 -26
  20. package/dist/auth/index.js.map +0 -1
  21. package/dist/auth/middleware.d.ts +0 -36
  22. package/dist/auth/middleware.d.ts.map +0 -1
  23. package/dist/auth/middleware.js +0 -194
  24. package/dist/auth/middleware.js.map +0 -1
  25. package/dist/auth/permissions.d.ts +0 -60
  26. package/dist/auth/permissions.d.ts.map +0 -1
  27. package/dist/auth/permissions.js +0 -262
  28. package/dist/auth/permissions.js.map +0 -1
  29. package/dist/auth/token-validator.d.ts +0 -52
  30. package/dist/auth/token-validator.d.ts.map +0 -1
  31. package/dist/auth/token-validator.js +0 -215
  32. package/dist/auth/token-validator.js.map +0 -1
  33. package/dist/cache/cache-manager.d.ts +0 -49
  34. package/dist/cache/cache-manager.d.ts.map +0 -1
  35. package/dist/cache/cache-manager.js +0 -191
  36. package/dist/cache/cache-manager.js.map +0 -1
  37. package/dist/cache/index.d.ts +0 -6
  38. package/dist/cache/index.d.ts.map +0 -1
  39. package/dist/cache/index.js +0 -12
  40. package/dist/cache/index.js.map +0 -1
  41. package/dist/cache/redis-client.d.ts +0 -45
  42. package/dist/cache/redis-client.d.ts.map +0 -1
  43. package/dist/cache/redis-client.js +0 -210
  44. package/dist/cache/redis-client.js.map +0 -1
  45. package/dist/config/constants.d.ts +0 -28
  46. package/dist/config/constants.d.ts.map +0 -1
  47. package/dist/config/constants.js +0 -31
  48. package/dist/config/constants.js.map +0 -1
  49. package/dist/config/index.d.ts +0 -71
  50. package/dist/config/index.d.ts.map +0 -1
  51. package/dist/config/index.js +0 -190
  52. package/dist/config/index.js.map +0 -1
  53. package/dist/filesystem/manager.d.ts +0 -45
  54. package/dist/filesystem/manager.d.ts.map +0 -1
  55. package/dist/filesystem/manager.js +0 -246
  56. package/dist/filesystem/manager.js.map +0 -1
  57. package/dist/git/multi-source-manager.d.ts +0 -78
  58. package/dist/git/multi-source-manager.d.ts.map +0 -1
  59. package/dist/git/multi-source-manager.js +0 -577
  60. package/dist/git/multi-source-manager.js.map +0 -1
  61. package/dist/git/operations.d.ts +0 -27
  62. package/dist/git/operations.d.ts.map +0 -1
  63. package/dist/git/operations.js +0 -83
  64. package/dist/git/operations.js.map +0 -1
  65. package/dist/index.d.ts +0 -6
  66. package/dist/index.d.ts.map +0 -1
  67. package/dist/index.js +0 -122
  68. package/dist/index.js.map +0 -1
  69. package/dist/monitoring/health.d.ts +0 -35
  70. package/dist/monitoring/health.d.ts.map +0 -1
  71. package/dist/monitoring/health.js +0 -105
  72. package/dist/monitoring/health.js.map +0 -1
  73. package/dist/prompts/cache.d.ts +0 -69
  74. package/dist/prompts/cache.d.ts.map +0 -1
  75. package/dist/prompts/cache.js +0 -163
  76. package/dist/prompts/cache.js.map +0 -1
  77. package/dist/prompts/generator.d.ts +0 -49
  78. package/dist/prompts/generator.d.ts.map +0 -1
  79. package/dist/prompts/generator.js +0 -160
  80. package/dist/prompts/generator.js.map +0 -1
  81. package/dist/prompts/index.d.ts +0 -13
  82. package/dist/prompts/index.d.ts.map +0 -1
  83. package/dist/prompts/index.js +0 -24
  84. package/dist/prompts/index.js.map +0 -1
  85. package/dist/prompts/manager.d.ts +0 -169
  86. package/dist/prompts/manager.d.ts.map +0 -1
  87. package/dist/prompts/manager.js +0 -488
  88. package/dist/prompts/manager.js.map +0 -1
  89. package/dist/resources/index.d.ts +0 -6
  90. package/dist/resources/index.d.ts.map +0 -1
  91. package/dist/resources/index.js +0 -10
  92. package/dist/resources/index.js.map +0 -1
  93. package/dist/resources/loader.d.ts +0 -88
  94. package/dist/resources/loader.d.ts.map +0 -1
  95. package/dist/resources/loader.js +0 -492
  96. package/dist/resources/loader.js.map +0 -1
  97. package/dist/server/http.d.ts +0 -57
  98. package/dist/server/http.d.ts.map +0 -1
  99. package/dist/server/http.js +0 -435
  100. package/dist/server/http.js.map +0 -1
  101. package/dist/server.d.ts +0 -13
  102. package/dist/server.d.ts.map +0 -1
  103. package/dist/server.js +0 -200
  104. package/dist/server.js.map +0 -1
  105. package/dist/session/manager.d.ts +0 -91
  106. package/dist/session/manager.d.ts.map +0 -1
  107. package/dist/session/manager.js +0 -251
  108. package/dist/session/manager.js.map +0 -1
  109. package/dist/telemetry/index.d.ts +0 -3
  110. package/dist/telemetry/index.d.ts.map +0 -1
  111. package/dist/telemetry/index.js +0 -7
  112. package/dist/telemetry/index.js.map +0 -1
  113. package/dist/telemetry/manager.d.ts +0 -151
  114. package/dist/telemetry/manager.d.ts.map +0 -1
  115. package/dist/telemetry/manager.js +0 -367
  116. package/dist/telemetry/manager.js.map +0 -1
  117. package/dist/tools/index.d.ts +0 -12
  118. package/dist/tools/index.d.ts.map +0 -1
  119. package/dist/tools/index.js +0 -28
  120. package/dist/tools/index.js.map +0 -1
  121. package/dist/tools/manage-subscription.d.ts +0 -47
  122. package/dist/tools/manage-subscription.d.ts.map +0 -1
  123. package/dist/tools/manage-subscription.js +0 -314
  124. package/dist/tools/manage-subscription.js.map +0 -1
  125. package/dist/tools/registry.d.ts +0 -40
  126. package/dist/tools/registry.d.ts.map +0 -1
  127. package/dist/tools/registry.js +0 -85
  128. package/dist/tools/registry.js.map +0 -1
  129. package/dist/tools/search-resources.d.ts +0 -35
  130. package/dist/tools/search-resources.d.ts.map +0 -1
  131. package/dist/tools/search-resources.js +0 -159
  132. package/dist/tools/search-resources.js.map +0 -1
  133. package/dist/tools/sync-resources.d.ts +0 -54
  134. package/dist/tools/sync-resources.d.ts.map +0 -1
  135. package/dist/tools/sync-resources.js +0 -733
  136. package/dist/tools/sync-resources.js.map +0 -1
  137. package/dist/tools/track-usage.d.ts +0 -63
  138. package/dist/tools/track-usage.d.ts.map +0 -1
  139. package/dist/tools/track-usage.js +0 -90
  140. package/dist/tools/track-usage.js.map +0 -1
  141. package/dist/tools/uninstall-resource.d.ts +0 -30
  142. package/dist/tools/uninstall-resource.d.ts.map +0 -1
  143. package/dist/tools/uninstall-resource.js +0 -174
  144. package/dist/tools/uninstall-resource.js.map +0 -1
  145. package/dist/tools/upload-resource.d.ts +0 -81
  146. package/dist/tools/upload-resource.d.ts.map +0 -1
  147. package/dist/tools/upload-resource.js +0 -393
  148. package/dist/tools/upload-resource.js.map +0 -1
  149. package/dist/transport/sse.d.ts +0 -29
  150. package/dist/transport/sse.d.ts.map +0 -1
  151. package/dist/transport/sse.js +0 -271
  152. package/dist/transport/sse.js.map +0 -1
  153. package/dist/types/errors.d.ts +0 -60
  154. package/dist/types/errors.d.ts.map +0 -1
  155. package/dist/types/errors.js +0 -112
  156. package/dist/types/errors.js.map +0 -1
  157. package/dist/types/index.d.ts +0 -7
  158. package/dist/types/index.d.ts.map +0 -1
  159. package/dist/types/index.js +0 -23
  160. package/dist/types/index.js.map +0 -1
  161. package/dist/types/mcp.d.ts +0 -50
  162. package/dist/types/mcp.d.ts.map +0 -1
  163. package/dist/types/mcp.js +0 -6
  164. package/dist/types/mcp.js.map +0 -1
  165. package/dist/types/resources.d.ts +0 -109
  166. package/dist/types/resources.d.ts.map +0 -1
  167. package/dist/types/resources.js +0 -7
  168. package/dist/types/resources.js.map +0 -1
  169. package/dist/types/tools.d.ts +0 -235
  170. package/dist/types/tools.d.ts.map +0 -1
  171. package/dist/types/tools.js +0 -6
  172. package/dist/types/tools.js.map +0 -1
  173. package/dist/utils/cursor-paths.d.ts +0 -84
  174. package/dist/utils/cursor-paths.d.ts.map +0 -1
  175. package/dist/utils/cursor-paths.js +0 -166
  176. package/dist/utils/cursor-paths.js.map +0 -1
  177. package/dist/utils/log-cleaner.d.ts +0 -18
  178. package/dist/utils/log-cleaner.d.ts.map +0 -1
  179. package/dist/utils/log-cleaner.js +0 -112
  180. package/dist/utils/log-cleaner.js.map +0 -1
  181. package/dist/utils/logger.d.ts +0 -59
  182. package/dist/utils/logger.d.ts.map +0 -1
  183. package/dist/utils/logger.js +0 -292
  184. package/dist/utils/logger.js.map +0 -1
  185. package/dist/utils/validation.d.ts +0 -58
  186. package/dist/utils/validation.d.ts.map +0 -1
  187. package/dist/utils/validation.js +0 -214
  188. package/dist/utils/validation.js.map +0 -1
  189. package/src/api/cached-client.ts +0 -144
  190. package/src/api/client.ts +0 -697
  191. package/src/auth/index.ts +0 -11
  192. package/src/auth/middleware.ts +0 -244
  193. package/src/auth/permissions.ts +0 -323
  194. package/src/auth/token-validator.ts +0 -292
  195. package/src/cache/cache-manager.ts +0 -243
  196. package/src/cache/index.ts +0 -6
  197. package/src/cache/redis-client.ts +0 -249
  198. package/src/config/constants.ts +0 -33
  199. package/src/config/index.ts +0 -269
  200. package/src/filesystem/manager.ts +0 -235
  201. package/src/git/multi-source-manager.ts +0 -654
  202. package/src/git/operations.ts +0 -93
  203. package/src/index.ts +0 -157
  204. package/src/monitoring/health.ts +0 -132
  205. package/src/prompts/cache.ts +0 -140
  206. package/src/prompts/generator.ts +0 -143
  207. package/src/prompts/index.ts +0 -20
  208. package/src/prompts/manager.ts +0 -613
  209. package/src/resources/index.ts +0 -13
  210. package/src/resources/loader.ts +0 -563
  211. package/src/server/http.ts +0 -549
  212. package/src/server.ts +0 -204
  213. package/src/session/manager.ts +0 -296
  214. package/src/telemetry/index.ts +0 -10
  215. package/src/telemetry/manager.ts +0 -419
  216. package/src/tools/index.ts +0 -12
  217. package/src/tools/manage-subscription.ts +0 -385
  218. package/src/tools/registry.ts +0 -97
  219. package/src/tools/search-resources.ts +0 -185
  220. package/src/tools/sync-resources.ts +0 -827
  221. package/src/tools/track-usage.ts +0 -113
  222. package/src/tools/uninstall-resource.ts +0 -199
  223. package/src/tools/upload-resource.ts +0 -431
  224. package/src/transport/sse.ts +0 -308
  225. package/src/types/errors.ts +0 -146
  226. package/src/types/index.ts +0 -7
  227. package/src/types/mcp.ts +0 -61
  228. package/src/types/resources.ts +0 -141
  229. package/src/types/tools.ts +0 -284
  230. package/src/utils/cursor-paths.ts +0 -135
  231. package/src/utils/log-cleaner.ts +0 -92
  232. package/src/utils/logger.ts +0 -333
  233. package/src/utils/validation.ts +0 -262
@@ -1,419 +0,0 @@
1
- /**
2
- * TelemetryManager: records local AI resource invocation events and periodically
3
- * flushes them to the remote telemetry API.
4
- *
5
- * Local storage: {MCP Server CWD}/ai-resource-telemetry.json
6
- *
7
- * Multi-user design:
8
- * - The file is keyed by user token so that data for different users is stored
9
- * and reported independently. Each top-level key in the `users` map is a
10
- * user token; all events, rules and MCPs belong to that token's owner.
11
- * - On flush, each user's data is sent with that user's own token, so the
12
- * server can attribute the telemetry to the correct account.
13
- *
14
- * Other design notes:
15
- * - File is stored in the MCP Server's runtime working directory (not ~/.cursor/).
16
- * - Atomic write-then-rename pattern prevents file corruption on concurrent
17
- * writes or unexpected process termination.
18
- * - Periodic flush is fire-and-forget; failures retry up to MAX_RETRIES times
19
- * with exponential back-off, then silently drop — main tool flow is never blocked.
20
- * - Rules cannot track individual invocations (Cursor injects them silently).
21
- * We report the subscribed list as a snapshot on every flush instead.
22
- * - MCPs are tracked as a configured-list snapshot only.
23
- * - jira_id is an optional per-invocation annotation stored separately per key.
24
- */
25
-
26
- import * as fs from 'fs';
27
- import * as path from 'path';
28
-
29
- export type ResourceCategory = 'command' | 'skill' | 'mcp';
30
-
31
- export interface InvocationEvent {
32
- resource_id: string;
33
- resource_type: ResourceCategory;
34
- resource_name: string;
35
- invocation_count: number;
36
- first_invoked_at: string;
37
- last_invoked_at: string;
38
- /** Optional Jira Issue ID for usage correlation (e.g. "PROJ-12345"). */
39
- jira_id?: string;
40
- }
41
-
42
- export interface SubscribedRule {
43
- resource_id: string;
44
- resource_name: string;
45
- subscribed_at: string;
46
- }
47
-
48
- export interface ConfiguredMcp {
49
- resource_id: string;
50
- resource_name: string;
51
- configured_at: string;
52
- }
53
-
54
- /** Per-user telemetry data stored under `users[token]`. */
55
- export interface UserTelemetry {
56
- last_reported_at: string | null;
57
- pending_events: InvocationEvent[];
58
- subscribed_rules: SubscribedRule[];
59
- configured_mcps: ConfiguredMcp[];
60
- }
61
-
62
- /** Top-level file structure (v2: multi-user). */
63
- export interface TelemetryFile {
64
- client_version: string;
65
- /** Map of user token → per-user telemetry data. */
66
- users: Record<string, UserTelemetry>;
67
- }
68
-
69
- export interface TelemetryReportPayload {
70
- client_version: string;
71
- reported_at: string;
72
- events: InvocationEvent[];
73
- subscribed_rules: SubscribedRule[];
74
- configured_mcps: ConfiguredMcp[];
75
- }
76
-
77
- // Injected at flush time by the server; avoids circular import with api/client
78
- export type ReportFn = (payload: TelemetryReportPayload, userToken: string) => Promise<void>;
79
-
80
- /** Default file name placed in the MCP Server's CWD. */
81
- const DEFAULT_FILE_NAME = 'ai-resource-telemetry.json';
82
-
83
- const DEFAULT_VERSION = '0.1.5';
84
- const MAX_RETRIES = 3;
85
- const RETRY_BASE_MS = 500;
86
-
87
- /** Build the aggregation key for an invocation event. */
88
- function aggregationKey(resourceId: string, jiraId?: string): string {
89
- return jiraId ? `${resourceId}|${jiraId}` : resourceId;
90
- }
91
-
92
- /** Return an empty per-user telemetry record. */
93
- function emptyUserTelemetry(): UserTelemetry {
94
- return {
95
- last_reported_at: null,
96
- pending_events: [],
97
- subscribed_rules: [],
98
- configured_mcps: [],
99
- };
100
- }
101
-
102
- export class TelemetryManager {
103
- private filePath: string;
104
- private clientVersion: string;
105
- private timer: ReturnType<typeof setInterval> | null = null;
106
- private reportFn: ReportFn | null = null;
107
- /** Tracks all tokens seen from active SSE connections for multi-user flush. */
108
- private activeTokens: Set<string> = new Set();
109
- /** Simple mutex: true while a file write is in progress. */
110
- private writing = false;
111
- private writeQueue: Array<() => void> = [];
112
-
113
- /**
114
- * @param filePath Absolute path to the telemetry JSON file.
115
- * Defaults to `{CWD}/ai-resource-telemetry.json`.
116
- * @param clientVersion Reported client version string.
117
- */
118
- constructor(filePath?: string, clientVersion?: string) {
119
- this.filePath = filePath ?? path.join(process.cwd(), DEFAULT_FILE_NAME);
120
- this.clientVersion = clientVersion ?? DEFAULT_VERSION;
121
- }
122
-
123
- // ---------------------------------------------------------------------------
124
- // Public API
125
- // ---------------------------------------------------------------------------
126
-
127
- /**
128
- * Configure the function used to send telemetry to the server.
129
- * Called during server initialisation to inject the API client without
130
- * creating a circular dependency.
131
- *
132
- * All user tokens must arrive via setUserToken() from authenticated SSE
133
- * connections — no environment variable fallback.
134
- */
135
- configure(reportFn: ReportFn): void {
136
- this.reportFn = reportFn;
137
- }
138
-
139
- /**
140
- * Register a token from a newly authenticated SSE connection.
141
- *
142
- * - Adds the token to the active-token set (used for multi-user flush).
143
- * - Initialises the per-user slot in the file if it does not yet exist.
144
- */
145
- setUserToken(token: string): void {
146
- this.activeTokens.add(token);
147
- // Ensure the user slot exists without overwriting existing data.
148
- this.withFileLock(async () => {
149
- const data = this.readFile();
150
- if (!data.users[token]) {
151
- data.users[token] = emptyUserTelemetry();
152
- this.writeFile(data);
153
- }
154
- }).catch(() => { /* best-effort */ });
155
- }
156
-
157
- /**
158
- * Record one invocation of a Command or Skill resource for a specific user.
159
- *
160
- * Events are aggregated by (resource_id, jira_id) key — successive calls for
161
- * the same key increment the counter rather than appending duplicate entries.
162
- *
163
- * @param resourceId Canonical resource ID.
164
- * @param resourceType 'command' | 'skill'
165
- * @param resourceName Human-readable name.
166
- * @param userToken Token of the user who invoked the resource.
167
- * @param jiraId Optional Jira Issue ID for correlation.
168
- */
169
- async recordInvocation(
170
- resourceId: string,
171
- resourceType: ResourceCategory,
172
- resourceName: string,
173
- userToken: string,
174
- jiraId?: string,
175
- ): Promise<void> {
176
- await this.withFileLock(async () => {
177
- const data = this.readFile();
178
- const user = this.ensureUserSlot(data, userToken);
179
- const now = new Date().toISOString();
180
- const key = aggregationKey(resourceId, jiraId);
181
-
182
- const existing = user.pending_events.find(
183
- (e) => aggregationKey(e.resource_id, e.jira_id) === key,
184
- );
185
-
186
- if (existing) {
187
- existing.invocation_count += 1;
188
- existing.last_invoked_at = now;
189
- } else {
190
- const event: InvocationEvent = {
191
- resource_id: resourceId,
192
- resource_type: resourceType,
193
- resource_name: resourceName,
194
- invocation_count: 1,
195
- first_invoked_at: now,
196
- last_invoked_at: now,
197
- };
198
- // Only attach jira_id when defined (field must be absent, not null).
199
- if (jiraId) event.jira_id = jiraId;
200
- user.pending_events.push(event);
201
- }
202
- this.writeFile(data);
203
- });
204
- }
205
-
206
- /**
207
- * Replace the full list of subscribed Rules for a specific user.
208
- * Called after sync_resources or manage_subscription completes.
209
- */
210
- async updateSubscribedRules(rules: SubscribedRule[], userToken: string): Promise<void> {
211
- await this.withFileLock(async () => {
212
- const data = this.readFile();
213
- this.ensureUserSlot(data, userToken).subscribed_rules = rules;
214
- this.writeFile(data);
215
- });
216
- }
217
-
218
- /**
219
- * Replace the full list of configured MCPs for a specific user.
220
- * Called after sync_resources or manage_subscription completes for MCP resources.
221
- */
222
- async updateConfiguredMcps(mcps: ConfiguredMcp[], userToken: string): Promise<void> {
223
- await this.withFileLock(async () => {
224
- const data = this.readFile();
225
- this.ensureUserSlot(data, userToken).configured_mcps = mcps;
226
- this.writeFile(data);
227
- });
228
- }
229
-
230
- /**
231
- * Flush pending telemetry for ALL active users.
232
- *
233
- * Each user's data is sent with that user's own token so the server can
234
- * attribute it to the correct account. The periodic timer calls this so
235
- * that all connected users are reported in the same tick.
236
- */
237
- async flush(): Promise<void> {
238
- if (!this.reportFn) return;
239
-
240
- // Only flush tokens from authenticated SSE connections.
241
- // No environment variable fallback — tokens must arrive via setUserToken().
242
- const tokens = new Set(this.activeTokens);
243
-
244
- if (tokens.size === 0) return;
245
-
246
- const data = await new Promise<TelemetryFile>((resolve) => {
247
- this.withFileLock(async () => {
248
- resolve(this.readFile());
249
- }).catch(() => resolve(this.readFile()));
250
- });
251
-
252
- await Promise.all(
253
- Array.from(tokens).map((token) => this.flushUser(token, data)),
254
- );
255
- }
256
-
257
- /** Start the periodic flush timer (flushes all active users each tick). */
258
- startPeriodicFlush(intervalMs = 10_000): void {
259
- if (this.timer) return;
260
- this.timer = setInterval(() => {
261
- this.flush().catch(() => { /* already handled inside flush */ });
262
- }, intervalMs);
263
- if (this.timer.unref) this.timer.unref();
264
- }
265
-
266
- /** Stop the periodic flush timer (call before final flush on shutdown). */
267
- stopPeriodicFlush(): void {
268
- if (this.timer) {
269
- clearInterval(this.timer);
270
- this.timer = null;
271
- }
272
- }
273
-
274
- /**
275
- * Trigger an immediate flush when a client reconnects to the MCP server.
276
- * Fire-and-forget — errors are already handled inside flush().
277
- */
278
- flushOnReconnect(): void {
279
- this.flush().catch(() => { /* handled inside flush */ });
280
- }
281
-
282
- // ---------------------------------------------------------------------------
283
- // Private helpers
284
- // ---------------------------------------------------------------------------
285
-
286
- /** Flush one user's pending data using that user's own token. */
287
- private async flushUser(token: string, data: TelemetryFile): Promise<void> {
288
- if (!this.reportFn) return;
289
- const user = data.users[token];
290
- if (!user) return;
291
-
292
- const payload: TelemetryReportPayload = {
293
- client_version: this.clientVersion,
294
- reported_at: new Date().toISOString(),
295
- events: user.pending_events,
296
- subscribed_rules: user.subscribed_rules,
297
- configured_mcps: user.configured_mcps,
298
- };
299
-
300
- await this.reportWithRetry(payload, token);
301
- }
302
-
303
- /** Get or create the per-user slot, mutating `data.users` in place. */
304
- private ensureUserSlot(data: TelemetryFile, token: string): UserTelemetry {
305
- if (!data.users[token]) {
306
- data.users[token] = emptyUserTelemetry();
307
- }
308
- return data.users[token]!;
309
- }
310
-
311
- private readFile(): TelemetryFile {
312
- try {
313
- const raw = fs.readFileSync(this.filePath, 'utf8');
314
- const parsed = JSON.parse(raw) as Partial<TelemetryFile> & {
315
- // v1 compat: flat structure without `users`
316
- pending_events?: InvocationEvent[];
317
- subscribed_rules?: SubscribedRule[];
318
- configured_mcps?: ConfiguredMcp[];
319
- };
320
-
321
- // Migrate v1 flat file to v2 multi-user structure.
322
- // We cannot recover the original token, so v1 data is discarded.
323
- if (!parsed.users) {
324
- return { client_version: this.clientVersion, users: {} };
325
- }
326
-
327
- return {
328
- client_version: parsed.client_version ?? this.clientVersion,
329
- users: parsed.users,
330
- };
331
- } catch {
332
- return { client_version: this.clientVersion, users: {} };
333
- }
334
- }
335
-
336
- private writeFile(data: TelemetryFile): void {
337
- const dir = path.dirname(this.filePath);
338
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
339
- const tmp = `${this.filePath}.${process.pid}.tmp`;
340
- fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf8');
341
- fs.renameSync(tmp, this.filePath);
342
- }
343
-
344
- /** Serialises file access to prevent concurrent write conflicts. */
345
- private async withFileLock(fn: () => Promise<void>): Promise<void> {
346
- return new Promise<void>((resolve, reject) => {
347
- const run = async () => {
348
- this.writing = true;
349
- try {
350
- await fn();
351
- resolve();
352
- } catch (err) {
353
- reject(err);
354
- } finally {
355
- this.writing = false;
356
- const next = this.writeQueue.shift();
357
- if (next) next();
358
- }
359
- };
360
-
361
- if (this.writing) {
362
- this.writeQueue.push(run);
363
- } else {
364
- run();
365
- }
366
- });
367
- }
368
-
369
- private async reportWithRetry(payload: TelemetryReportPayload, token: string): Promise<void> {
370
- let lastErr: unknown;
371
- for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
372
- try {
373
- await this.reportFn!(payload, token);
374
- // Success — subtract only the events that were included in this payload.
375
- // New events may have been appended to the file between the time we read
376
- // the snapshot and now, so we must NOT blindly wipe pending_events=[].
377
- // Instead, re-read the file under lock and decrement each reported
378
- // event's invocation_count; remove it only when the count reaches zero.
379
- await this.withFileLock(async () => {
380
- const data = this.readFile();
381
- const user = data.users[token];
382
- if (!user) return;
383
-
384
- for (const reported of payload.events) {
385
- const key = aggregationKey(reported.resource_id, reported.jira_id);
386
- const idx = user.pending_events.findIndex(
387
- (e) => aggregationKey(e.resource_id, e.jira_id) === key,
388
- );
389
- if (idx === -1) continue;
390
- const live = user.pending_events[idx]!;
391
- live.invocation_count -= reported.invocation_count;
392
- if (live.invocation_count <= 0) {
393
- user.pending_events.splice(idx, 1);
394
- }
395
- }
396
-
397
- user.last_reported_at = payload.reported_at;
398
- this.writeFile(data);
399
- });
400
- return;
401
- } catch (err) {
402
- lastErr = err;
403
- if (attempt < MAX_RETRIES - 1) {
404
- await sleep(RETRY_BASE_MS * Math.pow(2, attempt));
405
- }
406
- }
407
- }
408
- if (process.env.NODE_ENV !== 'test') {
409
- process.stderr.write(`[telemetry] flush failed after ${MAX_RETRIES} retries: ${lastErr}\n`);
410
- }
411
- }
412
- }
413
-
414
- function sleep(ms: number): Promise<void> {
415
- return new Promise((res) => setTimeout(res, ms));
416
- }
417
-
418
- /** Singleton instance shared across the server process. */
419
- export const telemetry = new TelemetryManager();
@@ -1,12 +0,0 @@
1
- /**
2
- * Tools Index
3
- * Exports all MCP tools
4
- */
5
-
6
- export * from './sync-resources';
7
- export * from './manage-subscription';
8
- export * from './search-resources';
9
- export * from './upload-resource';
10
- export * from './uninstall-resource';
11
- export * from './track-usage';
12
- export * from './registry';