diff --git a/src/lib/instrumentation.ts b/src/lib/instrumentation.ts index e6eccb4..8a3bdb8 100644 --- a/src/lib/instrumentation.ts +++ b/src/lib/instrumentation.ts @@ -1,6 +1,7 @@ import logger from "../logger.js"; import config from "../config.js"; import { createRequire } from "module"; +import { detectRunMode } from "./utils.js"; const require = createRequire(import.meta.url); const packageJson = require("../../package.json"); import axios from "axios"; @@ -12,6 +13,7 @@ interface MCPEventPayload { tool_name: string; mcp_client: string; success?: boolean; + mode?: string; error_message?: string; error_type?: string; }; @@ -58,6 +60,8 @@ export function trackMCP( error instanceof Error ? error.constructor.name : "Unknown"; } + event.event_properties.mode = detectRunMode(); + axios .post(instrumentationEndpoint, event, { headers: { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 00cf097..787ad3c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,4 +1,6 @@ import sharp from "sharp"; +import path from "path"; +import { fileURLToPath } from "url"; export function sanitizeUrlParam(param: string): string { // Remove any characters that could be used for command injection @@ -34,3 +36,14 @@ export async function assertOkResponse(response: Response, action: string) { ); } } + +export function detectRunMode(): "npx" | "local" | "unknown" { + try { + const scriptPath = fileURLToPath(import.meta.url); + const normalizedPath = path.normalize(scriptPath); + const npxPattern = path.sep + "_npx" + path.sep; + return normalizedPath.includes(npxPattern) ? "npx" : "local"; + } catch { + return "unknown"; + } +}