Skip to content

Commit 8a6398d

Browse files
authored
Merge pull request #12 from mcp-use/fix/fix-telemetry-on-V8-engine
Fix telemetry on V8 engine
2 parents cebaab7 + 1afd4b4 commit 8a6398d

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/telemetry/telemetry.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@ import { logger } from '../logging.js'
88
import { MCPAgentExecutionEvent } from './events.js'
99
import { getPackageVersion } from './utils.js'
1010

11+
// Environment detection function
12+
function isNodeJSEnvironment(): boolean {
13+
try {
14+
// Check for Cloudflare Workers specifically
15+
if (typeof navigator !== 'undefined' && navigator.userAgent?.includes('Cloudflare-Workers')) {
16+
return false
17+
}
18+
19+
// Check for other edge runtime indicators
20+
if (typeof (globalThis as any).EdgeRuntime !== 'undefined' || typeof (globalThis as any).Deno !== 'undefined') {
21+
return false
22+
}
23+
24+
// Check for Node.js specific globals that are not available in edge environments
25+
const hasNodeGlobals = (
26+
typeof process !== 'undefined'
27+
&& typeof process.platform !== 'undefined'
28+
&& typeof __dirname !== 'undefined'
29+
)
30+
31+
// Check for Node.js modules
32+
const hasNodeModules = (
33+
typeof fs !== 'undefined'
34+
&& typeof os !== 'undefined'
35+
&& typeof fs.existsSync === 'function'
36+
)
37+
38+
return hasNodeGlobals && hasNodeModules
39+
}
40+
catch {
41+
return false
42+
}
43+
}
44+
1145
// Simple Scarf event logger implementation
1246
class ScarfEventLogger {
1347
private endpoint: string
@@ -46,6 +80,11 @@ class ScarfEventLogger {
4680
}
4781

4882
function getCacheHome(): string {
83+
// Return a safe fallback for non-Node.js environments
84+
if (!isNodeJSEnvironment()) {
85+
return '/tmp/mcp_use_cache'
86+
}
87+
4988
// XDG_CACHE_HOME for Linux and manually set envs
5089
const envVar = process.env.XDG_CACHE_HOME
5190
if (envVar && path.isAbsolute(envVar)) {
@@ -88,15 +127,24 @@ export class Telemetry {
88127
private _source: string = 'typescript'
89128

90129
private constructor() {
91-
const telemetryDisabled = process.env.MCP_USE_ANONYMIZED_TELEMETRY?.toLowerCase() === 'false'
130+
// Check if we're in a Node.js environment first
131+
const isNodeJS = isNodeJSEnvironment()
132+
133+
// Safely access environment variables
134+
const telemetryDisabled = (typeof process !== 'undefined' && process.env?.MCP_USE_ANONYMIZED_TELEMETRY?.toLowerCase() === 'false') || false
92135

93136
// Check for source from environment variable, default to 'typescript'
94-
this._source = process.env.MCP_USE_TELEMETRY_SOURCE || 'typescript'
137+
this._source = (typeof process !== 'undefined' && process.env?.MCP_USE_TELEMETRY_SOURCE) || 'typescript'
95138

96139
if (telemetryDisabled) {
97140
this._posthogClient = null
98141
this._scarfClient = null
99-
logger.debug('Telemetry disabled')
142+
logger.debug('Telemetry disabled via environment variable')
143+
}
144+
else if (!isNodeJS) {
145+
this._posthogClient = null
146+
this._scarfClient = null
147+
logger.debug('Telemetry disabled - non-Node.js environment detected (e.g., Cloudflare Workers)')
100148
}
101149
else {
102150
logger.info('Anonymized telemetry enabled. Set MCP_USE_ANONYMIZED_TELEMETRY=false to disable.')
@@ -156,6 +204,12 @@ export class Telemetry {
156204
return this._currUserId
157205
}
158206

207+
// If we're not in a Node.js environment, just return a static user ID
208+
if (!isNodeJSEnvironment()) {
209+
this._currUserId = this.UNKNOWN_USER_ID
210+
return this._currUserId
211+
}
212+
159213
try {
160214
const isFirstTime = !fs.existsSync(this.USER_ID_PATH)
161215

@@ -234,6 +288,11 @@ export class Telemetry {
234288
return
235289
}
236290

291+
// Skip tracking in non-Node.js environments
292+
if (!isNodeJSEnvironment()) {
293+
return
294+
}
295+
237296
try {
238297
const currentVersion = getPackageVersion()
239298
let shouldTrack = false

src/telemetry/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import * as path from 'node:path'
44

55
export function getPackageVersion(): string {
66
try {
7+
// Check if we're in a Node.js environment with file system access
8+
if (typeof __dirname === 'undefined' || typeof fs === 'undefined') {
9+
return 'unknown'
10+
}
11+
712
const packagePath = path.join(__dirname, '../../package.json')
813
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'))
914
return packageJson.version || 'unknown'

0 commit comments

Comments
 (0)