This repository was archived by the owner on May 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtelemetry.ts
More file actions
384 lines (332 loc) · 11.3 KB
/
Copy pathtelemetry.ts
File metadata and controls
384 lines (332 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import type { BaseTelemetryEvent, MCPAgentExecutionEventData } from './events.js'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { PostHog } from 'posthog-node'
import { v4 as uuidv4 } from 'uuid'
import { logger } from '../logging.js'
import { MCPAgentExecutionEvent } from './events.js'
import { getPackageVersion } from './utils.js'
// Environment detection function
function isNodeJSEnvironment(): boolean {
try {
// Check for Cloudflare Workers specifically
if (typeof navigator !== 'undefined' && navigator.userAgent?.includes('Cloudflare-Workers')) {
return false
}
// Check for other edge runtime indicators
if (typeof (globalThis as any).EdgeRuntime !== 'undefined' || typeof (globalThis as any).Deno !== 'undefined') {
return false
}
// Check for Node.js specific globals that are not available in edge environments
const hasNodeGlobals = (
typeof process !== 'undefined'
&& typeof process.platform !== 'undefined'
&& typeof __dirname !== 'undefined'
)
// Check for Node.js modules
const hasNodeModules = (
typeof fs !== 'undefined'
&& typeof os !== 'undefined'
&& typeof fs.existsSync === 'function'
)
return hasNodeGlobals && hasNodeModules
}
catch {
return false
}
}
// Simple Scarf event logger implementation
class ScarfEventLogger {
private endpoint: string
private timeout: number
constructor(endpoint: string, timeout: number = 3000) {
this.endpoint = endpoint
this.timeout = timeout
}
async logEvent(properties: Record<string, any>): Promise<void> {
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), this.timeout)
const response = await fetch(this.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(properties),
signal: controller.signal,
})
clearTimeout(timeoutId)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
}
catch (error) {
// Silently fail - telemetry should not break the application
logger.debug(`Failed to send Scarf event: ${error}`)
}
}
}
function getCacheHome(): string {
// Return a safe fallback for non-Node.js environments
if (!isNodeJSEnvironment()) {
return '/tmp/mcp_use_cache'
}
// XDG_CACHE_HOME for Linux and manually set envs
const envVar = process.env.XDG_CACHE_HOME
if (envVar && path.isAbsolute(envVar)) {
return envVar
}
const platform = process.platform
const homeDir = os.homedir()
if (platform === 'win32') {
const appdata = process.env.LOCALAPPDATA || process.env.APPDATA
if (appdata) {
return appdata
}
return path.join(homeDir, 'AppData', 'Local')
}
else if (platform === 'darwin') {
// macOS
return path.join(homeDir, 'Library', 'Caches')
}
else {
// Linux or other Unix
return path.join(homeDir, '.cache')
}
}
export class Telemetry {
private static instance: Telemetry | null = null
private readonly USER_ID_PATH = path.join(getCacheHome(), 'mcp_use_3', 'telemetry_user_id')
private readonly VERSION_DOWNLOAD_PATH = path.join(getCacheHome(), 'mcp_use', 'download_version')
private readonly PROJECT_API_KEY = 'phc_lyTtbYwvkdSbrcMQNPiKiiRWrrM1seyKIMjycSvItEI'
private readonly HOST = 'https://eu.i.posthog.com'
private readonly SCARF_GATEWAY_URL = 'https://mcpuse.gateway.scarf.sh/events-ts'
private readonly UNKNOWN_USER_ID = 'UNKNOWN_USER_ID'
private _currUserId: string | null = null
private _posthogClient: PostHog | null = null
private _scarfClient: ScarfEventLogger | null = null
private _source: string = 'typescript'
private constructor() {
// Check if we're in a Node.js environment first
const isNodeJS = isNodeJSEnvironment()
// Safely access environment variables
const telemetryDisabled = (typeof process !== 'undefined' && process.env?.MCP_USE_ANONYMIZED_TELEMETRY?.toLowerCase() === 'false') || false
// Check for source from environment variable, default to 'typescript'
this._source = (typeof process !== 'undefined' && process.env?.MCP_USE_TELEMETRY_SOURCE) || 'typescript'
if (telemetryDisabled) {
this._posthogClient = null
this._scarfClient = null
logger.debug('Telemetry disabled via environment variable')
}
else if (!isNodeJS) {
this._posthogClient = null
this._scarfClient = null
logger.debug('Telemetry disabled - non-Node.js environment detected (e.g., Cloudflare Workers)')
}
else {
logger.info('Anonymized telemetry enabled. Set MCP_USE_ANONYMIZED_TELEMETRY=false to disable.')
// Initialize PostHog
try {
this._posthogClient = new PostHog(
this.PROJECT_API_KEY,
{
host: this.HOST,
disableGeoip: false,
},
)
}
catch (e) {
logger.warn(`Failed to initialize PostHog telemetry: ${e}`)
this._posthogClient = null
}
// Initialize Scarf
try {
this._scarfClient = new ScarfEventLogger(this.SCARF_GATEWAY_URL, 3000)
}
catch (e) {
logger.warn(`Failed to initialize Scarf telemetry: ${e}`)
this._scarfClient = null
}
}
}
static getInstance(): Telemetry {
if (!Telemetry.instance) {
Telemetry.instance = new Telemetry()
}
return Telemetry.instance
}
/**
* Set the source identifier for telemetry events.
* This allows tracking usage from different applications.
* @param source - The source identifier (e.g., "my-app", "cli", "vs-code-extension")
*/
setSource(source: string): void {
this._source = source
logger.debug(`Telemetry source set to: ${source}`)
}
/**
* Get the current source identifier.
*/
getSource(): string {
return this._source
}
get userId(): string {
if (this._currUserId) {
return this._currUserId
}
// If we're not in a Node.js environment, just return a static user ID
if (!isNodeJSEnvironment()) {
this._currUserId = this.UNKNOWN_USER_ID
return this._currUserId
}
try {
const isFirstTime = !fs.existsSync(this.USER_ID_PATH)
if (isFirstTime) {
logger.debug(`Creating user ID path: ${this.USER_ID_PATH}`)
fs.mkdirSync(path.dirname(this.USER_ID_PATH), { recursive: true })
const newUserId = uuidv4()
fs.writeFileSync(this.USER_ID_PATH, newUserId)
this._currUserId = newUserId
logger.debug(`User ID path created: ${this.USER_ID_PATH}`)
}
else {
this._currUserId = fs.readFileSync(this.USER_ID_PATH, 'utf-8').trim()
}
// Always check for version-based download tracking
// Note: We can't await here since this is a getter, so we fire and forget
this.trackPackageDownload({
triggered_by: 'user_id_property',
}).catch(e => logger.debug(`Failed to track package download: ${e}`))
}
catch (e) {
logger.debug(`Failed to get/create user ID: ${e}`)
this._currUserId = this.UNKNOWN_USER_ID
}
return this._currUserId
}
async capture(event: BaseTelemetryEvent): Promise<void> {
if (!this._posthogClient && !this._scarfClient) {
return
}
// Send to PostHog
if (this._posthogClient) {
try {
// Add package version, language flag, and source to all events
const properties = { ...event.properties }
properties.mcp_use_version = getPackageVersion()
properties.language = 'typescript'
properties.source = this._source
this._posthogClient.capture({
distinctId: this.userId,
event: event.name,
properties,
})
}
catch (e) {
logger.debug(`Failed to track PostHog event ${event.name}: ${e}`)
}
}
// Send to Scarf (when implemented)
if (this._scarfClient) {
try {
// Add package version, user_id, language flag, and source to all events
const properties: Record<string, any> = {}
properties.mcp_use_version = getPackageVersion()
properties.user_id = this.userId
properties.event = event.name
properties.language = 'typescript'
properties.source = this._source
await this._scarfClient.logEvent(properties)
}
catch (e) {
logger.debug(`Failed to track Scarf event ${event.name}: ${e}`)
}
}
}
async trackPackageDownload(properties?: Record<string, any>): Promise<void> {
if (!this._scarfClient) {
return
}
// Skip tracking in non-Node.js environments
if (!isNodeJSEnvironment()) {
return
}
try {
const currentVersion = getPackageVersion()
let shouldTrack = false
let firstDownload = false
// Check if version file exists
if (!fs.existsSync(this.VERSION_DOWNLOAD_PATH)) {
// First download
shouldTrack = true
firstDownload = true
// Create directory and save version
fs.mkdirSync(path.dirname(this.VERSION_DOWNLOAD_PATH), { recursive: true })
fs.writeFileSync(this.VERSION_DOWNLOAD_PATH, currentVersion)
}
else {
// Read saved version
const savedVersion = fs.readFileSync(this.VERSION_DOWNLOAD_PATH, 'utf-8').trim()
// Compare versions (simple string comparison for now)
if (currentVersion > savedVersion) {
shouldTrack = true
firstDownload = false
// Update saved version
fs.writeFileSync(this.VERSION_DOWNLOAD_PATH, currentVersion)
}
}
if (shouldTrack) {
logger.debug(`Tracking package download event with properties: ${JSON.stringify(properties)}`)
// Add package version, user_id, language flag, and source to event
const eventProperties = { ...(properties || {}) }
eventProperties.mcp_use_version = currentVersion
eventProperties.user_id = this.userId
eventProperties.event = 'package_download'
eventProperties.first_download = firstDownload
eventProperties.language = 'typescript'
eventProperties.source = this._source
await this._scarfClient.logEvent(eventProperties)
}
}
catch (e) {
logger.debug(`Failed to track Scarf package_download event: ${e}`)
}
}
async trackAgentExecution(data: MCPAgentExecutionEventData): Promise<void> {
const event = new MCPAgentExecutionEvent(data)
await this.capture(event)
}
flush(): void {
// Flush PostHog
if (this._posthogClient) {
try {
this._posthogClient.flush()
logger.debug('PostHog client telemetry queue flushed')
}
catch (e) {
logger.debug(`Failed to flush PostHog client: ${e}`)
}
}
// Scarf events are sent immediately, no flush needed
if (this._scarfClient) {
logger.debug('Scarf telemetry events sent immediately (no flush needed)')
}
}
shutdown(): void {
// Shutdown PostHog
if (this._posthogClient) {
try {
this._posthogClient.shutdown()
logger.debug('PostHog client shutdown successfully')
}
catch (e) {
logger.debug(`Error shutting down PostHog client: ${e}`)
}
}
// Scarf doesn't require explicit shutdown
if (this._scarfClient) {
logger.debug('Scarf telemetry client shutdown (no action needed)')
}
}
}