Skip to content

Commit df49786

Browse files
committed
feat: add mode detection for npx and local execution in MCP tracking
1 parent eee8f17 commit df49786

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lib/instrumentation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logger from "../logger.js";
22
import config from "../config.js";
33
import { createRequire } from "module";
4+
import { isRunningViaNpx } from "./utils.js";
45
const require = createRequire(import.meta.url);
56
const packageJson = require("../../package.json");
67
import axios from "axios";
@@ -12,6 +13,7 @@ interface MCPEventPayload {
1213
tool_name: string;
1314
mcp_client: string;
1415
success?: boolean;
16+
mode?: string;
1517
error_message?: string;
1618
error_type?: string;
1719
};
@@ -58,6 +60,12 @@ export function trackMCP(
5860
error instanceof Error ? error.constructor.name : "Unknown";
5961
}
6062

63+
if (isRunningViaNpx()) {
64+
event.event_properties.mode = "npx";
65+
} else {
66+
event.event_properties.mode = "local";
67+
}
68+
6169
axios
6270
.post(instrumentationEndpoint, event, {
6371
headers: {

src/lib/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sharp from "sharp";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
24

35
export function sanitizeUrlParam(param: string): string {
46
// Remove any characters that could be used for command injection
@@ -34,3 +36,10 @@ export async function assertOkResponse(response: Response, action: string) {
3436
);
3537
}
3638
}
39+
40+
export function isRunningViaNpx() {
41+
const scriptPath = fileURLToPath(import.meta.url);
42+
const normalizedPath = path.normalize(scriptPath);
43+
const npxPattern = path.sep + "_npx" + path.sep;
44+
return normalizedPath.includes(npxPattern);
45+
}

0 commit comments

Comments
 (0)