Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,6 +13,7 @@ interface MCPEventPayload {
tool_name: string;
mcp_client: string;
success?: boolean;
mode?: string;
error_message?: string;
error_type?: string;
};
Expand Down Expand Up @@ -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: {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,3 +36,10 @@ export async function assertOkResponse(response: Response, action: string) {
);
}
}

export function isRunningViaNpx() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add try catch

const scriptPath = fileURLToPath(import.meta.url);
const normalizedPath = path.normalize(scriptPath);
const npxPattern = path.sep + "_npx" + path.sep;
return normalizedPath.includes(npxPattern);
}