Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ process.on("exit", () => {

export { setLogger } from "./logger.js";
export { BrowserStackMcpServer } from "./server-factory.js";
export { trackMCP } from "./lib/instrumentation.js";
28 changes: 28 additions & 0 deletions src/lib/device-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import fs from "fs";
import os from "os";
import path from "path";
import { apiClient } from "./apiClient.js";
import config from "../config.js";

const CACHE_DIR = path.join(os.homedir(), ".browserstack", "combined_cache");
const CACHE_FILE = path.join(CACHE_DIR, "data.json");
const TTL_MS = 24 * 60 * 60 * 1000; // 1 day
const TTL_STARTED_MS = 3 * 60 * 60 * 1000; // 3 Hours

export enum BrowserStackProducts {
LIVE = "live",
Expand Down Expand Up @@ -64,3 +66,29 @@ export async function getDevicesAndBrowsers(

return cache[type];
}

// Rate limiter for started event (3H)
export function shouldSendStartedEvent(): boolean {
try {
if (config && config.REMOTE_MCP) {
return false;
}
if (!fs.existsSync(CACHE_DIR)) {
fs.mkdirSync(CACHE_DIR, { recursive: true });
}
let cache: Record<string, any> = {};
if (fs.existsSync(CACHE_FILE)) {
const raw = fs.readFileSync(CACHE_FILE, "utf8");
cache = JSON.parse(raw || "{}");
const last = parseInt(cache.lastStartedEvent, 10);
if (!isNaN(last) && Date.now() - last < TTL_STARTED_MS) {
return false;
}
}
cache.lastStartedEvent = Date.now();
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2), "utf8");
return true;
} catch {
return true;
}
}
5 changes: 4 additions & 1 deletion src/oninitialized.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { trackMCP } from "./lib/instrumentation.js";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { shouldSendStartedEvent } from "./lib/device-cache.js";

export function setupOnInitialized(server: McpServer, config?: any) {
const nodeVersion = process.versions.node;
Expand All @@ -12,6 +13,8 @@ export function setupOnInitialized(server: McpServer, config?: any) {
}

server.server.oninitialized = () => {
trackMCP("started", server.server.getClientVersion()!, undefined, config);
if (shouldSendStartedEvent()) {
trackMCP("started", server.server.getClientVersion()!, undefined, config);
}
};
}