@elliotding/ai-agent-mcp 0.1.3 → 0.1.4

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 (98) hide show
  1. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-generate-testcase.md +101 -0
  2. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-submit_zct_job.md +158 -0
  3. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-conf-status.md +311 -0
  4. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-sdk-log.md +64 -0
  5. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-zmb-log-errors.md +84 -0
  6. package/ai-resource-telemetry.json +22 -0
  7. package/dist/api/client.d.ts +39 -0
  8. package/dist/api/client.d.ts.map +1 -1
  9. package/dist/api/client.js +21 -3
  10. package/dist/api/client.js.map +1 -1
  11. package/dist/auth/permissions.d.ts.map +1 -1
  12. package/dist/auth/permissions.js +6 -0
  13. package/dist/auth/permissions.js.map +1 -1
  14. package/dist/config/index.d.ts +6 -1
  15. package/dist/config/index.d.ts.map +1 -1
  16. package/dist/config/index.js +1 -3
  17. package/dist/config/index.js.map +1 -1
  18. package/dist/index.js +12 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/prompts/cache.d.ts +69 -0
  21. package/dist/prompts/cache.d.ts.map +1 -0
  22. package/dist/prompts/cache.js +163 -0
  23. package/dist/prompts/cache.js.map +1 -0
  24. package/dist/prompts/generator.d.ts +49 -0
  25. package/dist/prompts/generator.d.ts.map +1 -0
  26. package/dist/prompts/generator.js +158 -0
  27. package/dist/prompts/generator.js.map +1 -0
  28. package/dist/prompts/index.d.ts +13 -0
  29. package/dist/prompts/index.d.ts.map +1 -0
  30. package/dist/prompts/index.js +24 -0
  31. package/dist/prompts/index.js.map +1 -0
  32. package/dist/prompts/manager.d.ts +106 -0
  33. package/dist/prompts/manager.d.ts.map +1 -0
  34. package/dist/prompts/manager.js +263 -0
  35. package/dist/prompts/manager.js.map +1 -0
  36. package/dist/server/http.d.ts.map +1 -1
  37. package/dist/server/http.js +61 -17
  38. package/dist/server/http.js.map +1 -1
  39. package/dist/server.d.ts.map +1 -1
  40. package/dist/server.js +43 -0
  41. package/dist/server.js.map +1 -1
  42. package/dist/telemetry/index.d.ts +3 -0
  43. package/dist/telemetry/index.d.ts.map +1 -0
  44. package/dist/telemetry/index.js +7 -0
  45. package/dist/telemetry/index.js.map +1 -0
  46. package/dist/telemetry/manager.d.ts +149 -0
  47. package/dist/telemetry/manager.d.ts.map +1 -0
  48. package/dist/telemetry/manager.js +368 -0
  49. package/dist/telemetry/manager.js.map +1 -0
  50. package/dist/tools/index.d.ts +1 -0
  51. package/dist/tools/index.d.ts.map +1 -1
  52. package/dist/tools/index.js +1 -0
  53. package/dist/tools/index.js.map +1 -1
  54. package/dist/tools/manage-subscription.d.ts.map +1 -1
  55. package/dist/tools/manage-subscription.js +19 -4
  56. package/dist/tools/manage-subscription.js.map +1 -1
  57. package/dist/tools/search-resources.d.ts.map +1 -1
  58. package/dist/tools/search-resources.js +2 -3
  59. package/dist/tools/search-resources.js.map +1 -1
  60. package/dist/tools/sync-resources.d.ts +9 -4
  61. package/dist/tools/sync-resources.d.ts.map +1 -1
  62. package/dist/tools/sync-resources.js +121 -7
  63. package/dist/tools/sync-resources.js.map +1 -1
  64. package/dist/tools/track-usage.d.ts +63 -0
  65. package/dist/tools/track-usage.d.ts.map +1 -0
  66. package/dist/tools/track-usage.js +90 -0
  67. package/dist/tools/track-usage.js.map +1 -0
  68. package/dist/tools/uninstall-resource.d.ts.map +1 -1
  69. package/dist/tools/uninstall-resource.js +53 -3
  70. package/dist/tools/uninstall-resource.js.map +1 -1
  71. package/dist/tools/upload-resource.d.ts.map +1 -1
  72. package/dist/tools/upload-resource.js +49 -5
  73. package/dist/tools/upload-resource.js.map +1 -1
  74. package/dist/utils/cursor-paths.d.ts +10 -0
  75. package/dist/utils/cursor-paths.d.ts.map +1 -1
  76. package/dist/utils/cursor-paths.js +13 -0
  77. package/dist/utils/cursor-paths.js.map +1 -1
  78. package/package.json +1 -1
  79. package/src/api/client.ts +52 -3
  80. package/src/auth/permissions.ts +6 -0
  81. package/src/config/index.ts +11 -5
  82. package/src/index.ts +18 -0
  83. package/src/prompts/cache.ts +140 -0
  84. package/src/prompts/generator.ts +142 -0
  85. package/src/prompts/index.ts +20 -0
  86. package/src/prompts/manager.ts +342 -0
  87. package/src/server/http.ts +69 -17
  88. package/src/server.ts +13 -0
  89. package/src/telemetry/index.ts +10 -0
  90. package/src/telemetry/manager.ts +419 -0
  91. package/src/tools/index.ts +1 -0
  92. package/src/tools/manage-subscription.ts +19 -4
  93. package/src/tools/search-resources.ts +2 -4
  94. package/src/tools/sync-resources.ts +131 -7
  95. package/src/tools/track-usage.ts +113 -0
  96. package/src/tools/uninstall-resource.ts +62 -4
  97. package/src/tools/upload-resource.ts +52 -5
  98. package/src/utils/cursor-paths.ts +13 -0
@@ -0,0 +1,149 @@
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
+ export type ResourceCategory = 'command' | 'skill' | 'mcp';
26
+ export interface InvocationEvent {
27
+ resource_id: string;
28
+ resource_type: ResourceCategory;
29
+ resource_name: string;
30
+ invocation_count: number;
31
+ first_invoked_at: string;
32
+ last_invoked_at: string;
33
+ /** Optional Jira Issue ID for usage correlation (e.g. "PROJ-12345"). */
34
+ jira_id?: string;
35
+ }
36
+ export interface SubscribedRule {
37
+ resource_id: string;
38
+ resource_name: string;
39
+ subscribed_at: string;
40
+ }
41
+ export interface ConfiguredMcp {
42
+ resource_id: string;
43
+ resource_name: string;
44
+ configured_at: string;
45
+ }
46
+ /** Per-user telemetry data stored under `users[token]`. */
47
+ export interface UserTelemetry {
48
+ last_reported_at: string | null;
49
+ pending_events: InvocationEvent[];
50
+ subscribed_rules: SubscribedRule[];
51
+ configured_mcps: ConfiguredMcp[];
52
+ }
53
+ /** Top-level file structure (v2: multi-user). */
54
+ export interface TelemetryFile {
55
+ client_version: string;
56
+ /** Map of user token → per-user telemetry data. */
57
+ users: Record<string, UserTelemetry>;
58
+ }
59
+ export interface TelemetryReportPayload {
60
+ client_version: string;
61
+ reported_at: string;
62
+ events: InvocationEvent[];
63
+ subscribed_rules: SubscribedRule[];
64
+ configured_mcps: ConfiguredMcp[];
65
+ }
66
+ export type ReportFn = (payload: TelemetryReportPayload, userToken: string) => Promise<void>;
67
+ export declare class TelemetryManager {
68
+ private filePath;
69
+ private clientVersion;
70
+ private timer;
71
+ private reportFn;
72
+ private userTokenFn;
73
+ /** Tracks all tokens seen from active SSE connections for multi-user flush. */
74
+ private activeTokens;
75
+ /** Simple mutex: true while a file write is in progress. */
76
+ private writing;
77
+ private writeQueue;
78
+ /**
79
+ * @param filePath Absolute path to the telemetry JSON file.
80
+ * Defaults to `{CWD}/ai-resource-telemetry.json`.
81
+ * @param clientVersion Reported client version string.
82
+ */
83
+ constructor(filePath?: string, clientVersion?: string);
84
+ /**
85
+ * Configure the function used to send telemetry to the server.
86
+ * Called during server initialisation to inject the API client without
87
+ * creating a circular dependency.
88
+ */
89
+ configure(reportFn: ReportFn, userTokenFn: () => string | undefined): void;
90
+ /**
91
+ * Register a token from a newly authenticated SSE connection.
92
+ *
93
+ * - Adds the token to the active-token set (used for multi-user flush).
94
+ * - Initialises the per-user slot in the file if it does not yet exist.
95
+ */
96
+ setUserToken(token: string): void;
97
+ /**
98
+ * Record one invocation of a Command or Skill resource for a specific user.
99
+ *
100
+ * Events are aggregated by (resource_id, jira_id) key — successive calls for
101
+ * the same key increment the counter rather than appending duplicate entries.
102
+ *
103
+ * @param resourceId Canonical resource ID.
104
+ * @param resourceType 'command' | 'skill'
105
+ * @param resourceName Human-readable name.
106
+ * @param userToken Token of the user who invoked the resource.
107
+ * @param jiraId Optional Jira Issue ID for correlation.
108
+ */
109
+ recordInvocation(resourceId: string, resourceType: ResourceCategory, resourceName: string, userToken: string, jiraId?: string): Promise<void>;
110
+ /**
111
+ * Replace the full list of subscribed Rules for a specific user.
112
+ * Called after sync_resources or manage_subscription completes.
113
+ */
114
+ updateSubscribedRules(rules: SubscribedRule[], userToken: string): Promise<void>;
115
+ /**
116
+ * Replace the full list of configured MCPs for a specific user.
117
+ * Called after sync_resources or manage_subscription completes for MCP resources.
118
+ */
119
+ updateConfiguredMcps(mcps: ConfiguredMcp[], userToken: string): Promise<void>;
120
+ /**
121
+ * Flush pending telemetry for ALL active users.
122
+ *
123
+ * Each user's data is sent with that user's own token so the server can
124
+ * attribute it to the correct account. The periodic timer calls this so
125
+ * that all connected users are reported in the same tick.
126
+ */
127
+ flush(): Promise<void>;
128
+ /** Start the periodic flush timer (flushes all active users each tick). */
129
+ startPeriodicFlush(intervalMs?: number): void;
130
+ /** Stop the periodic flush timer (call before final flush on shutdown). */
131
+ stopPeriodicFlush(): void;
132
+ /**
133
+ * Trigger an immediate flush when a client reconnects to the MCP server.
134
+ * Fire-and-forget — errors are already handled inside flush().
135
+ */
136
+ flushOnReconnect(): void;
137
+ /** Flush one user's pending data using that user's own token. */
138
+ private flushUser;
139
+ /** Get or create the per-user slot, mutating `data.users` in place. */
140
+ private ensureUserSlot;
141
+ private readFile;
142
+ private writeFile;
143
+ /** Serialises file access to prevent concurrent write conflicts. */
144
+ private withFileLock;
145
+ private reportWithRetry;
146
+ }
147
+ /** Singleton instance shared across the server process. */
148
+ export declare const telemetry: TelemetryManager;
149
+ //# sourceMappingURL=manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/telemetry/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAKH,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,OAAO,GAAG,KAAK,CAAC;AAE3D,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,gBAAgB,CAAC;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,wEAAwE;IACxE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,2DAA2D;AAC3D,MAAM,WAAW,aAAa;IAC5B,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,cAAc,EAAE,eAAe,EAAE,CAAC;IAClC,gBAAgB,EAAE,cAAc,EAAE,CAAC;IACnC,eAAe,EAAE,aAAa,EAAE,CAAC;CAClC;AAED,iDAAiD;AACjD,MAAM,WAAW,aAAa;IAC5B,cAAc,EAAE,M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AAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQtF;;;OAGG;IACG,oBAAoB,CAAC,IAAI,EAAE,aAAa,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQnF;;;;;;OAMG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB5B,2EAA2E;IAC3E,kBAAkB,CAAC,UAAU,SAAS,GAAG,IAAI;IAQ7C,2EAA2E;IAC3E,iBAAiB,IAAI,IAAI;IAOzB;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAQxB,iEAAiE;YACnD,SAAS;IAgBvB,uEAAuE;IACvE,OAAO,CAAC,cAAc;IAOtB,OAAO,CAAC,QAAQ;IAyBhB,OAAO,CAAC,SAAS;IAQjB,oEAAoE;YACtD,YAAY;YAwBZ,eAAe;CA2C9B;AAMD,2DAA2D;AAC3D,eAAO,MAAM,SAAS,kBAAyB,CAAC"}
@@ -0,0 +1,368 @@
1
+ "use strict";
2
+ /**
3
+ * TelemetryManager: records local AI resource invocation events and periodically
4
+ * flushes them to the remote telemetry API.
5
+ *
6
+ * Local storage: {MCP Server CWD}/ai-resource-telemetry.json
7
+ *
8
+ * Multi-user design:
9
+ * - The file is keyed by user token so that data for different users is stored
10
+ * and reported independently. Each top-level key in the `users` map is a
11
+ * user token; all events, rules and MCPs belong to that token's owner.
12
+ * - On flush, each user's data is sent with that user's own token, so the
13
+ * server can attribute the telemetry to the correct account.
14
+ *
15
+ * Other design notes:
16
+ * - File is stored in the MCP Server's runtime working directory (not ~/.cursor/).
17
+ * - Atomic write-then-rename pattern prevents file corruption on concurrent
18
+ * writes or unexpected process termination.
19
+ * - Periodic flush is fire-and-forget; failures retry up to MAX_RETRIES times
20
+ * with exponential back-off, then silently drop — main tool flow is never blocked.
21
+ * - Rules cannot track individual invocations (Cursor injects them silently).
22
+ * We report the subscribed list as a snapshot on every flush instead.
23
+ * - MCPs are tracked as a configured-list snapshot only.
24
+ * - jira_id is an optional per-invocation annotation stored separately per key.
25
+ */
26
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ var desc = Object.getOwnPropertyDescriptor(m, k);
29
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
30
+ desc = { enumerable: true, get: function() { return m[k]; } };
31
+ }
32
+ Object.defineProperty(o, k2, desc);
33
+ }) : (function(o, m, k, k2) {
34
+ if (k2 === undefined) k2 = k;
35
+ o[k2] = m[k];
36
+ }));
37
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
38
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
39
+ }) : function(o, v) {
40
+ o["default"] = v;
41
+ });
42
+ var __importStar = (this && this.__importStar) || (function () {
43
+ var ownKeys = function(o) {
44
+ ownKeys = Object.getOwnPropertyNames || function (o) {
45
+ var ar = [];
46
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
47
+ return ar;
48
+ };
49
+ return ownKeys(o);
50
+ };
51
+ return function (mod) {
52
+ if (mod && mod.__esModule) return mod;
53
+ var result = {};
54
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
55
+ __setModuleDefault(result, mod);
56
+ return result;
57
+ };
58
+ })();
59
+ Object.defineProperty(exports, "__esModule", { value: true });
60
+ exports.telemetry = exports.TelemetryManager = void 0;
61
+ const fs = __importStar(require("fs"));
62
+ const path = __importStar(require("path"));
63
+ /** Default file name placed in the MCP Server's CWD. */
64
+ const DEFAULT_FILE_NAME = 'ai-resource-telemetry.json';
65
+ const DEFAULT_VERSION = '0.1.5';
66
+ const MAX_RETRIES = 3;
67
+ const RETRY_BASE_MS = 500;
68
+ /** Build the aggregation key for an invocation event. */
69
+ function aggregationKey(resourceId, jiraId) {
70
+ return jiraId ? `${resourceId}|${jiraId}` : resourceId;
71
+ }
72
+ /** Return an empty per-user telemetry record. */
73
+ function emptyUserTelemetry() {
74
+ return {
75
+ last_reported_at: null,
76
+ pending_events: [],
77
+ subscribed_rules: [],
78
+ configured_mcps: [],
79
+ };
80
+ }
81
+ class TelemetryManager {
82
+ filePath;
83
+ clientVersion;
84
+ timer = null;
85
+ reportFn = null;
86
+ userTokenFn = null;
87
+ /** Tracks all tokens seen from active SSE connections for multi-user flush. */
88
+ activeTokens = new Set();
89
+ /** Simple mutex: true while a file write is in progress. */
90
+ writing = false;
91
+ writeQueue = [];
92
+ /**
93
+ * @param filePath Absolute path to the telemetry JSON file.
94
+ * Defaults to `{CWD}/ai-resource-telemetry.json`.
95
+ * @param clientVersion Reported client version string.
96
+ */
97
+ constructor(filePath, clientVersion) {
98
+ this.filePath = filePath ?? path.join(process.cwd(), DEFAULT_FILE_NAME);
99
+ this.clientVersion = clientVersion ?? DEFAULT_VERSION;
100
+ }
101
+ // ---------------------------------------------------------------------------
102
+ // Public API
103
+ // ---------------------------------------------------------------------------
104
+ /**
105
+ * Configure the function used to send telemetry to the server.
106
+ * Called during server initialisation to inject the API client without
107
+ * creating a circular dependency.
108
+ */
109
+ configure(reportFn, userTokenFn) {
110
+ this.reportFn = reportFn;
111
+ this.userTokenFn = userTokenFn;
112
+ }
113
+ /**
114
+ * Register a token from a newly authenticated SSE connection.
115
+ *
116
+ * - Adds the token to the active-token set (used for multi-user flush).
117
+ * - Initialises the per-user slot in the file if it does not yet exist.
118
+ */
119
+ setUserToken(token) {
120
+ this.activeTokens.add(token);
121
+ // Ensure the user slot exists without overwriting existing data.
122
+ this.withFileLock(async () => {
123
+ const data = this.readFile();
124
+ if (!data.users[token]) {
125
+ data.users[token] = emptyUserTelemetry();
126
+ this.writeFile(data);
127
+ }
128
+ }).catch(() => { });
129
+ }
130
+ /**
131
+ * Record one invocation of a Command or Skill resource for a specific user.
132
+ *
133
+ * Events are aggregated by (resource_id, jira_id) key — successive calls for
134
+ * the same key increment the counter rather than appending duplicate entries.
135
+ *
136
+ * @param resourceId Canonical resource ID.
137
+ * @param resourceType 'command' | 'skill'
138
+ * @param resourceName Human-readable name.
139
+ * @param userToken Token of the user who invoked the resource.
140
+ * @param jiraId Optional Jira Issue ID for correlation.
141
+ */
142
+ async recordInvocation(resourceId, resourceType, resourceName, userToken, jiraId) {
143
+ await this.withFileLock(async () => {
144
+ const data = this.readFile();
145
+ const user = this.ensureUserSlot(data, userToken);
146
+ const now = new Date().toISOString();
147
+ const key = aggregationKey(resourceId, jiraId);
148
+ const existing = user.pending_events.find((e) => aggregationKey(e.resource_id, e.jira_id) === key);
149
+ if (existing) {
150
+ existing.invocation_count += 1;
151
+ existing.last_invoked_at = now;
152
+ }
153
+ else {
154
+ const event = {
155
+ resource_id: resourceId,
156
+ resource_type: resourceType,
157
+ resource_name: resourceName,
158
+ invocation_count: 1,
159
+ first_invoked_at: now,
160
+ last_invoked_at: now,
161
+ };
162
+ // Only attach jira_id when defined (field must be absent, not null).
163
+ if (jiraId)
164
+ event.jira_id = jiraId;
165
+ user.pending_events.push(event);
166
+ }
167
+ this.writeFile(data);
168
+ });
169
+ }
170
+ /**
171
+ * Replace the full list of subscribed Rules for a specific user.
172
+ * Called after sync_resources or manage_subscription completes.
173
+ */
174
+ async updateSubscribedRules(rules, userToken) {
175
+ await this.withFileLock(async () => {
176
+ const data = this.readFile();
177
+ this.ensureUserSlot(data, userToken).subscribed_rules = rules;
178
+ this.writeFile(data);
179
+ });
180
+ }
181
+ /**
182
+ * Replace the full list of configured MCPs for a specific user.
183
+ * Called after sync_resources or manage_subscription completes for MCP resources.
184
+ */
185
+ async updateConfiguredMcps(mcps, userToken) {
186
+ await this.withFileLock(async () => {
187
+ const data = this.readFile();
188
+ this.ensureUserSlot(data, userToken).configured_mcps = mcps;
189
+ this.writeFile(data);
190
+ });
191
+ }
192
+ /**
193
+ * Flush pending telemetry for ALL active users.
194
+ *
195
+ * Each user's data is sent with that user's own token so the server can
196
+ * attribute it to the correct account. The periodic timer calls this so
197
+ * that all connected users are reported in the same tick.
198
+ */
199
+ async flush() {
200
+ if (!this.reportFn)
201
+ return;
202
+ // Collect tokens: active SSE tokens + env fallback (stdio / tests)
203
+ const tokens = new Set(this.activeTokens);
204
+ const envToken = this.userTokenFn?.();
205
+ if (envToken)
206
+ tokens.add(envToken);
207
+ if (tokens.size === 0)
208
+ return;
209
+ const data = await new Promise((resolve) => {
210
+ this.withFileLock(async () => {
211
+ resolve(this.readFile());
212
+ }).catch(() => resolve(this.readFile()));
213
+ });
214
+ await Promise.all(Array.from(tokens).map((token) => this.flushUser(token, data)));
215
+ }
216
+ /** Start the periodic flush timer (flushes all active users each tick). */
217
+ startPeriodicFlush(intervalMs = 10_000) {
218
+ if (this.timer)
219
+ return;
220
+ this.timer = setInterval(() => {
221
+ this.flush().catch(() => { });
222
+ }, intervalMs);
223
+ if (this.timer.unref)
224
+ this.timer.unref();
225
+ }
226
+ /** Stop the periodic flush timer (call before final flush on shutdown). */
227
+ stopPeriodicFlush() {
228
+ if (this.timer) {
229
+ clearInterval(this.timer);
230
+ this.timer = null;
231
+ }
232
+ }
233
+ /**
234
+ * Trigger an immediate flush when a client reconnects to the MCP server.
235
+ * Fire-and-forget — errors are already handled inside flush().
236
+ */
237
+ flushOnReconnect() {
238
+ this.flush().catch(() => { });
239
+ }
240
+ // ---------------------------------------------------------------------------
241
+ // Private helpers
242
+ // ---------------------------------------------------------------------------
243
+ /** Flush one user's pending data using that user's own token. */
244
+ async flushUser(token, data) {
245
+ if (!this.reportFn)
246
+ return;
247
+ const user = data.users[token];
248
+ if (!user)
249
+ return;
250
+ const payload = {
251
+ client_version: this.clientVersion,
252
+ reported_at: new Date().toISOString(),
253
+ events: user.pending_events,
254
+ subscribed_rules: user.subscribed_rules,
255
+ configured_mcps: user.configured_mcps,
256
+ };
257
+ await this.reportWithRetry(payload, token);
258
+ }
259
+ /** Get or create the per-user slot, mutating `data.users` in place. */
260
+ ensureUserSlot(data, token) {
261
+ if (!data.users[token]) {
262
+ data.users[token] = emptyUserTelemetry();
263
+ }
264
+ return data.users[token];
265
+ }
266
+ readFile() {
267
+ try {
268
+ const raw = fs.readFileSync(this.filePath, 'utf8');
269
+ const parsed = JSON.parse(raw);
270
+ // Migrate v1 flat file to v2 multi-user structure.
271
+ // We cannot recover the original token, so v1 data is discarded.
272
+ if (!parsed.users) {
273
+ return { client_version: this.clientVersion, users: {} };
274
+ }
275
+ return {
276
+ client_version: parsed.client_version ?? this.clientVersion,
277
+ users: parsed.users,
278
+ };
279
+ }
280
+ catch {
281
+ return { client_version: this.clientVersion, users: {} };
282
+ }
283
+ }
284
+ writeFile(data) {
285
+ const dir = path.dirname(this.filePath);
286
+ if (!fs.existsSync(dir))
287
+ fs.mkdirSync(dir, { recursive: true });
288
+ const tmp = `${this.filePath}.${process.pid}.tmp`;
289
+ fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf8');
290
+ fs.renameSync(tmp, this.filePath);
291
+ }
292
+ /** Serialises file access to prevent concurrent write conflicts. */
293
+ async withFileLock(fn) {
294
+ return new Promise((resolve, reject) => {
295
+ const run = async () => {
296
+ this.writing = true;
297
+ try {
298
+ await fn();
299
+ resolve();
300
+ }
301
+ catch (err) {
302
+ reject(err);
303
+ }
304
+ finally {
305
+ this.writing = false;
306
+ const next = this.writeQueue.shift();
307
+ if (next)
308
+ next();
309
+ }
310
+ };
311
+ if (this.writing) {
312
+ this.writeQueue.push(run);
313
+ }
314
+ else {
315
+ run();
316
+ }
317
+ });
318
+ }
319
+ async reportWithRetry(payload, token) {
320
+ let lastErr;
321
+ for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
322
+ try {
323
+ await this.reportFn(payload, token);
324
+ // Success — subtract only the events that were included in this payload.
325
+ // New events may have been appended to the file between the time we read
326
+ // the snapshot and now, so we must NOT blindly wipe pending_events=[].
327
+ // Instead, re-read the file under lock and decrement each reported
328
+ // event's invocation_count; remove it only when the count reaches zero.
329
+ await this.withFileLock(async () => {
330
+ const data = this.readFile();
331
+ const user = data.users[token];
332
+ if (!user)
333
+ return;
334
+ for (const reported of payload.events) {
335
+ const key = aggregationKey(reported.resource_id, reported.jira_id);
336
+ const idx = user.pending_events.findIndex((e) => aggregationKey(e.resource_id, e.jira_id) === key);
337
+ if (idx === -1)
338
+ continue;
339
+ const live = user.pending_events[idx];
340
+ live.invocation_count -= reported.invocation_count;
341
+ if (live.invocation_count <= 0) {
342
+ user.pending_events.splice(idx, 1);
343
+ }
344
+ }
345
+ user.last_reported_at = payload.reported_at;
346
+ this.writeFile(data);
347
+ });
348
+ return;
349
+ }
350
+ catch (err) {
351
+ lastErr = err;
352
+ if (attempt < MAX_RETRIES - 1) {
353
+ await sleep(RETRY_BASE_MS * Math.pow(2, attempt));
354
+ }
355
+ }
356
+ }
357
+ if (process.env.NODE_ENV !== 'test') {
358
+ process.stderr.write(`[telemetry] flush failed after ${MAX_RETRIES} retries: ${lastErr}\n`);
359
+ }
360
+ }
361
+ }
362
+ exports.TelemetryManager = TelemetryManager;
363
+ function sleep(ms) {
364
+ return new Promise((res) => setTimeout(res, ms));
365
+ }
366
+ /** Singleton instance shared across the server process. */
367
+ exports.telemetry = new TelemetryManager();
368
+ //# sourceMappingURL=manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../src/telemetry/manager.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,uCAAyB;AACzB,2CAA6B;AAqD7B,wDAAwD;AACxD,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AAEvD,MAAM,eAAe,GAAG,OAAO,CAAC;AAChC,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,aAAa,GAAG,GAAG,CAAC;AAE1B,yDAAyD;AACzD,SAAS,cAAc,CAAC,UAAkB,EAAE,MAAe;IACzD,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,UAAU,IA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AAC,cAAc,CAAC,GAAG,CAAE,CAAC;wBACvC,IAAI,CAAC,gBAAgB,IAAI,QAAQ,CAAC,gBAAgB,CAAC;wBACnD,IAAI,IAAI,CAAC,gBAAgB,IAAI,CAAC,EAAE,CAAC;4BAC/B,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;wBACrC,CAAC;oBACH,CAAC;oBAED,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC;oBAC5C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC,CAAC,CAAC;gBACH,OAAO;YACT,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,GAAG,GAAG,CAAC;gBACd,IAAI,OAAO,GAAG,WAAW,GAAG,CAAC,EAAE,CAAC;oBAC9B,MAAM,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;gBACpD,CAAC;YACH,CAAC;QACH,CAAC;QACD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;YACpC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,kCAAkC,WAAW,aAAa,OAAO,IAAI,CAAC,CAAC;QAC9F,CAAC;IACH,CAAC;CACF;AAtTD,4CAsTC;AAED,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,2DAA2D;AAC9C,QAAA,SAAS,GAAG,IAAI,gBAAgB,EAAE,CAAC"}
@@ -7,5 +7,6 @@ export * from './manage-subscription';
7
7
  export * from './search-resources';
8
8
  export * from './upload-resource';
9
9
  export * from './uninstall-resource';
10
+ export * from './track-usage';
10
11
  export * from './registry';
11
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
@@ -23,5 +23,6 @@ __exportStar(require("./manage-subscription"), exports);
23
23
  __exportStar(require("./search-resources"), exports);
24
24
  __exportStar(require("./upload-resource"), exports);
25
25
  __exportStar(require("./uninstall-resource"), exports);
26
+ __exportStar(require("./track-usage"), exports);
26
27
  __exportStar(require("./registry"), exports);
27
28
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;AAEH,mDAAiC;AACjC,wDAAsC;AACtC,qDAAmC;AACnC,oDAAkC;AAClC,uDAAqC;AACrC,6CAA2B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;AAEH,mDAAiC;AACjC,wDAAsC;AACtC,qDAAmC;AACnC,oDAAkC;AAClC,uDAAqC;AACrC,gDAA8B;AAC9B,6CAA2B"}
@@ -1 +1 @@
1
- {"version":3,"file":"manage-subscription.d.ts","sourceRoot":"","sources":["../../src/tools/manage-subscription.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAA4B,wBAAwB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAIrG,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAC,CA6RvG;AAGD,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkDlC,CAAC"}
1
+ {"version":3,"file":"manage-subscription.d.ts","sourceRoot":"","sources":["../../src/tools/manage-subscription.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAA4B,wBAAwB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAKrG,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAC,CA4SvG;AAGD,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiDlC,CAAC"}
@@ -11,6 +11,7 @@ const client_1 = require("../api/client");
11
11
  const errors_1 = require("../types/errors");
12
12
  const sync_resources_1 = require("./sync-resources");
13
13
  const uninstall_resource_1 = require("./uninstall-resource");
14
+ const index_js_1 = require("../prompts/index.js");
14
15
  async function manageSubscription(params) {
15
16
  const startTime = Date.now();
16
17
  // Type assertion for params
@@ -81,9 +82,24 @@ async function manageSubscription(params) {
81
82
  // Cancel server-side subscription
82
83
  await client_1.apiClient.unsubscribe(typedParams.resource_ids, typedParams.user_token);
83
84
  logger_1.logger.info({ count: typedParams.resource_ids.length }, 'Server-side subscriptions removed');
84
- // Uninstall local files and MCP config for each resource
85
+ // Uninstall local files and MCP config for each resource.
86
+ // For Command/Skill: unregister MCP Prompt instead of deleting local files.
85
87
  const uninstallResults = [];
86
88
  for (const resourceId of typedParams.resource_ids) {
89
+ // Determine if this is a Command or Skill by checking the prompt registry.
90
+ // Resource IDs follow the pattern: cmd-<source>-<name> or skill-<source>-<name>.
91
+ const isCommand = resourceId.startsWith('cmd-');
92
+ const isSkill = resourceId.startsWith('skill-');
93
+ if (isCommand || isSkill) {
94
+ const resourceType = isCommand ? 'command' : 'skill';
95
+ // Extract resource name from the ID (cmd-<team>-<name> or skill-<team>-<name>).
96
+ const parts = resourceId.split('-');
97
+ const resourceName = parts.slice(2).join('-') || resourceId;
98
+ index_js_1.promptManager.unregisterPrompt(resourceId, resourceType, resourceName);
99
+ uninstallResults.push({ id: resourceId, removed: true, detail: `Unregistered MCP Prompt for "${resourceName}"` });
100
+ logger_1.logger.info({ resourceId, resourceType }, 'MCP Prompt unregistered on unsubscribe');
101
+ continue;
102
+ }
87
103
  // Use the last segment of the resource ID as the search pattern
88
104
  // e.g. "mcp-client-sdk-ai-hub-jenkins" → "jenkins"
89
105
  // "rule-csp-elliotTest" → "elliotTest"
@@ -270,9 +286,8 @@ exports.manageSubscriptionTool = {
270
286
  },
271
287
  user_token: {
272
288
  type: 'string',
273
- description: 'CSP API token for the current user. Read this from the CSP_API_TOKEN environment ' +
274
- 'variable configured in the user\'s mcp.json. When provided, this token is used ' +
275
- 'for all CSP API calls in this request instead of the server-level fallback token.',
289
+ description: 'DO NOT set this field it is automatically injected by the MCP server from ' +
290
+ 'the authenticated SSE connection. The server always provides the correct token.',
276
291
  },
277
292
  },
278
293
  required: ['action'],