-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
76 lines (68 loc) · 1.99 KB
/
instrumentation.ts
File metadata and controls
76 lines (68 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import logger from "../logger.js";
import { getBrowserStackAuth } from "./get-auth.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";
interface MCPEventPayload {
event_type: string;
event_properties: {
mcp_version: string;
tool_name: string;
mcp_client: string;
success?: boolean;
mode?: string;
error_message?: string;
error_type?: string;
};
}
export function trackMCP(
toolName: string,
clientInfo: { name?: string; version?: string },
error?: unknown,
config?: any,
): void {
const instrumentationEndpoint = "https://api.browserstack.com/sdk/v1/event";
const isSuccess = !error;
const mcpClient = clientInfo?.name || "unknown";
// Log client information
if (clientInfo?.name) {
logger.info(
`Client connected: ${clientInfo.name} (version: ${clientInfo.version})`,
);
} else {
logger.info("Client connected: unknown client");
}
const event: MCPEventPayload = {
event_type: "MCPInstrumentation",
event_properties: {
mcp_version: packageJson.version,
tool_name: toolName,
mcp_client: mcpClient,
success: isSuccess,
},
};
// Add error details if applicable
if (error) {
event.event_properties.error_message =
error instanceof Error ? error.message : String(error);
event.event_properties.error_type =
error instanceof Error ? error.constructor.name : "Unknown";
}
event.event_properties.mode = detectRunMode();
let authHeader = undefined;
if (config) {
const authString = getBrowserStackAuth(config);
authHeader = `Basic ${Buffer.from(authString).toString("base64")}`;
}
axios
.post(instrumentationEndpoint, event, {
headers: {
"Content-Type": "application/json",
...(authHeader ? { Authorization: authHeader } : {}),
},
timeout: 2000,
})
.catch(() => {});
}