-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
73 lines (66 loc) · 1.88 KB
/
Copy pathinstrumentation.ts
File metadata and controls
73 lines (66 loc) · 1.88 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
import logger from "../logger.js";
import config from "../config.js";
import { createRequire } from "module";
import { DOMAINS } from "./domains.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;
error_message?: string;
error_type?: string;
};
}
export function trackMCP(
toolName: string,
clientInfo: { name?: string; version?: string },
error?: unknown,
): void {
if (config.DEV_MODE) {
logger.info("Tracking MCP is disabled in dev mode");
return;
}
const instrumentationEndpoint = `${DOMAINS.API}/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";
}
axios
.post(instrumentationEndpoint, event, {
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${Buffer.from(
`${config.browserstackUsername}:${config.browserstackAccessKey}`,
).toString("base64")}`,
},
timeout: 2000,
})
.catch(() => {});
}