From df4978646c7979cb27814b25c2016e2808435003 Mon Sep 17 00:00:00 2001 From: tech-sushant Date: Fri, 30 May 2025 14:53:37 +0530 Subject: [PATCH 1/5] feat: add mode detection for npx and local execution in MCP tracking --- src/lib/instrumentation.ts | 8 ++++++++ src/lib/utils.ts | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/lib/instrumentation.ts b/src/lib/instrumentation.ts index e6eccb4..9b960b4 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 { isRunningViaNpx } 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,12 @@ export function trackMCP( error instanceof Error ? error.constructor.name : "Unknown"; } + if (isRunningViaNpx()) { + event.event_properties.mode = "npx"; + } else { + event.event_properties.mode = "local"; + } + axios .post(instrumentationEndpoint, event, { headers: { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 00cf097..0b19f8e 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,10 @@ export async function assertOkResponse(response: Response, action: string) { ); } } + +export function isRunningViaNpx() { + const scriptPath = fileURLToPath(import.meta.url); + const normalizedPath = path.normalize(scriptPath); + const npxPattern = path.sep + "_npx" + path.sep; + return normalizedPath.includes(npxPattern); +} From 48d48f9fd59152a142a1b4e95eefc308f1b67a74 Mon Sep 17 00:00:00 2001 From: tech-sushant Date: Wed, 4 Jun 2025 11:36:15 +0530 Subject: [PATCH 2/5] fix: add error handling and logging in isRunningViaNpx function --- src/lib/utils.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0b19f8e..cb5de8a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,6 +1,7 @@ import sharp from "sharp"; import path from "path"; import { fileURLToPath } from "url"; +import logger from "../logger.js"; export function sanitizeUrlParam(param: string): string { // Remove any characters that could be used for command injection @@ -38,8 +39,13 @@ export async function assertOkResponse(response: Response, action: string) { } export function isRunningViaNpx() { - const scriptPath = fileURLToPath(import.meta.url); - const normalizedPath = path.normalize(scriptPath); - const npxPattern = path.sep + "_npx" + path.sep; - return normalizedPath.includes(npxPattern); + try { + const scriptPath = fileURLToPath(import.meta.url); + const normalizedPath = path.normalize(scriptPath); + const npxPattern = path.sep + "_npx" + path.sep; + return normalizedPath.includes(npxPattern); + } catch (err) { + logger.error("Error checking if running via npx:", err); + return false; + } } From 72713fb9dcb7bd41d7fc33e308128fbe9d0925ad Mon Sep 17 00:00:00 2001 From: tech-sushant Date: Wed, 4 Jun 2025 12:04:03 +0530 Subject: [PATCH 3/5] refactor: replace isRunningViaNpx with detectRunMode for improved mode detection --- src/lib/instrumentation.ts | 8 ++------ src/lib/utils.ts | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/lib/instrumentation.ts b/src/lib/instrumentation.ts index 9b960b4..8a3bdb8 100644 --- a/src/lib/instrumentation.ts +++ b/src/lib/instrumentation.ts @@ -1,7 +1,7 @@ import logger from "../logger.js"; import config from "../config.js"; import { createRequire } from "module"; -import { isRunningViaNpx } from "./utils.js"; +import { detectRunMode } from "./utils.js"; const require = createRequire(import.meta.url); const packageJson = require("../../package.json"); import axios from "axios"; @@ -60,11 +60,7 @@ export function trackMCP( error instanceof Error ? error.constructor.name : "Unknown"; } - if (isRunningViaNpx()) { - event.event_properties.mode = "npx"; - } else { - event.event_properties.mode = "local"; - } + event.event_properties.mode = detectRunMode(); axios .post(instrumentationEndpoint, event, { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index cb5de8a..7729d57 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -38,14 +38,14 @@ export async function assertOkResponse(response: Response, action: string) { } } -export function isRunningViaNpx() { +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); + return normalizedPath.includes(npxPattern) ? "npx" : "local"; } catch (err) { - logger.error("Error checking if running via npx:", err); - return false; + logger.error("Error determining execution mode:", err); + return "unknown"; } } From 62ae1bf737320d266404d386da457828781df756 Mon Sep 17 00:00:00 2001 From: tech-sushant Date: Wed, 4 Jun 2025 12:09:09 +0530 Subject: [PATCH 4/5] removing unnecessary error logging --- src/lib/utils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 7729d57..8595bd2 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -44,8 +44,7 @@ export function detectRunMode(): "npx" | "local" | "unknown" { const normalizedPath = path.normalize(scriptPath); const npxPattern = path.sep + "_npx" + path.sep; return normalizedPath.includes(npxPattern) ? "npx" : "local"; - } catch (err) { - logger.error("Error determining execution mode:", err); + } catch { return "unknown"; } } From 5ea77ae45ac0dc9d7c0c39f27274817eb5537955 Mon Sep 17 00:00:00 2001 From: tech-sushant Date: Wed, 4 Jun 2025 12:09:46 +0530 Subject: [PATCH 5/5] remove unused logger import --- src/lib/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 8595bd2..787ad3c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,7 +1,6 @@ import sharp from "sharp"; import path from "path"; import { fileURLToPath } from "url"; -import logger from "../logger.js"; export function sanitizeUrlParam(param: string): string { // Remove any characters that could be used for command injection