Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.
TypeScript agentandroidandroid-automationappium-alternativeclaude-codecodexcursoremulatoriosios-automationmcpmcp-servermobilemobile-automationmobile-testingreal-devicesimulatorskills
feat: cap telemetry events per user per day (#547)
Gil Megidishc6aad0ebfe5e
diff --git a/src/server.ts b/src/server.tsindex 8541414..4f875d1 100644--- a/src/server.ts+++ b/src/server.ts@@ -17,6 +17,7 @@ import { Mobilecli } from "./mobilecli";import { MobileDevice } from "./mobile-device";import { validateOutputPath, validateFileExtension, withLowercaseScheme, writeOutputFile, createPrivateOutputPath, finishRecording } from "./utils";import { formatElements } from "./format-elements";+import { shouldSendEvent } from "./telemetry-quota";type ScreenshotContent = { type: "text", text: string } | { type: "image", data: string, mimeType: string };@@ -180,6 +181,11 @@ export const createMcpServer = (): McpServer => {return;}+ const toolName = typeof properties.ToolName === "string" ? properties.ToolName : undefined;+ if (!shouldSendEvent(event, toolName)) {+ return;+ }+try {const url = "https://us.i.posthog.com/i/v0/e/";const api_key = "phc_KHRTZmkDsU7A8EbydEK8s4lJpPoTDyyBhSlwer694cS";diff --git a/src/telemetry-quota.ts b/src/telemetry-quota.tsnew file mode 100644index 0000000..ddb52cc--- /dev/null+++ b/src/telemetry-quota.ts@@ -0,0 +1,82 @@+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++type QuotaState = {+ day: string;+ counts: Record<string, number>;+};++// events not listed here are always sent (get_more_tools' $mcp_missing_capability)+const DAILY_CAPS: Record<string, number> = {+ "launch": 1,+ "tool_invoked": 1,+ "tool_failed": 100,+};++// get_more_tools is rare and is the signal we want the most, never cap it+const UNCAPPED_TOOLS = ["get_more_tools"];++// share mobilecli's state dir (it keeps daemon.pid/sock/log there), honoring the same override+const stateDir = (): string => process.env.MOBILECLI_HOME || path.join(os.homedir(), ".mobilecli");++const defaultQuotaPath = (): string => path.join(stateDir(), "mobile-mcp-telemetry.json");++const today = (now: Date): string => now.toISOString().slice(0, 10);++const isValidCounts = (counts: unknown): boolean => {+ if (typeof counts !== "object" || counts === null || Array.isArray(counts)) {+ return false;+ }++ return Object.values(counts).every(count => Number.isInteger(count) && count >= 0);+};++const readState = (statePath: string, day: string): QuotaState => {+ try {+ const state = JSON.parse(fs.readFileSync(statePath, "utf8"));+ if (state?.day === day && isValidCounts(state.counts)) {+ return state;+ }++ } catch {+ // missing or corrupt file, start a fresh day+ }++ return { day, counts: {} };+};++const writeState = (statePath: string, state: QuotaState): void => {+ fs.mkdirSync(path.dirname(statePath), { recursive: true, mode: 0o700 });+ // write a temp file and rename over the target, so a planted link at statePath is replaced, not written through+ const tempPath = `${statePath}.${process.pid}.tmp`;+ fs.writeFileSync(tempPath, JSON.stringify(state), { mode: 0o600 });+ fs.renameSync(tempPath, statePath);+};++// ponytail: read-modify-write without a lock, so concurrent launches may slightly exceed a cap.+// add a lock file if that ever matters.+export const shouldSendEvent = (event: string, toolName: string | undefined, statePath = defaultQuotaPath(), now = new Date()): boolean => {+ const cap = DAILY_CAPS[event];+ if (cap === undefined) {+ return true;+ }++ if (toolName !== undefined && UNCAPPED_TOOLS.includes(toolName)) {+ return true;+ }++ try {+ const state = readState(statePath, today(now));+ const sent = state.counts[event] ?? 0;+ if (sent >= cap) {+ return false;+ }++ writeState(statePath, { day: state.day, counts: { ...state.counts, [event]: sent + 1 } });+ return true;+ } catch {+ // can't persist the quota (read-only home, etc.), drop the event rather than flood+ return false;+ }+};diff --git a/test/telemetry-quota.test.ts b/test/telemetry-quota.test.tsnew file mode 100644index 0000000..758b562--- /dev/null+++ b/test/telemetry-quota.test.ts@@ -0,0 +1,84 @@+import { expect, test } from "@playwright/test";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++import { shouldSendEvent } from "../src/telemetry-quota";++const MONDAY = new Date("2026-11-02T10:00:00Z");+const TUESDAY = new Date("2026-11-03T10:00:00Z");++const freshQuotaFile = (): string => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "telemetry-quota-"));+ return path.join(dir, "nested", "telemetry.json");+};++const countSentEvents = (event: string, toolName: string | undefined, attempts: number, statePath: string, now: Date): number => {+ let sent = 0;+ for (let i = 0; i < attempts; i++) {+ if (shouldSendEvent(event, toolName, statePath, now)) {+ sent++;+ }++ }++ return sent;+};++test("sends only one launch per day", () => {+ const quotaFile = freshQuotaFile();+ expect(countSentEvents("launch", undefined, 5, quotaFile, MONDAY)).toBe(1);+});++test("sends only one tool_invoked per day", () => {+ const quotaFile = freshQuotaFile();+ expect(countSentEvents("tool_invoked", "mobile_take_screenshot", 5, quotaFile, MONDAY)).toBe(1);+});++test("sends at most 100 tool_failed per day", () => {+ const quotaFile = freshQuotaFile();+ expect(countSentEvents("tool_failed", "mobile_click_on_screen_at_coordinates", 150, quotaFile, MONDAY)).toBe(100);+});++test("always sends get_more_tools invocations and missing capabilities", () => {+ const quotaFile = freshQuotaFile();+ expect(countSentEvents("tool_invoked", "get_more_tools", 5, quotaFile, MONDAY)).toBe(5);+ expect(countSentEvents("$mcp_missing_capability", undefined, 5, quotaFile, MONDAY)).toBe(5);+});++test("caps reset on the next day", () => {+ const quotaFile = freshQuotaFile();+ countSentEvents("launch", undefined, 3, quotaFile, MONDAY);+ expect(countSentEvents("launch", undefined, 3, quotaFile, TUESDAY)).toBe(1);+});++test("each event has its own cap", () => {+ const quotaFile = freshQuotaFile();+ countSentEvents("launch", undefined, 1, quotaFile, MONDAY);+ expect(countSentEvents("tool_invoked", "mobile_list_apps", 1, quotaFile, MONDAY)).toBe(1);+});++test("a corrupt quota file starts a fresh day", () => {+ const quotaFile = freshQuotaFile();+ fs.mkdirSync(path.dirname(quotaFile), { recursive: true });+ fs.writeFileSync(quotaFile, "not json");+ expect(countSentEvents("launch", undefined, 2, quotaFile, MONDAY)).toBe(1);+});++test("a quota file with an invalid count starts a fresh day", () => {+ const quotaFile = freshQuotaFile();+ fs.mkdirSync(path.dirname(quotaFile), { recursive: true });+ fs.writeFileSync(quotaFile, JSON.stringify({ day: "2026-11-02", counts: { launch: "x", tool_failed: -5 } }));+ expect(countSentEvents("launch", undefined, 3, quotaFile, MONDAY)).toBe(1);+});++test("a planted link is replaced, not written through", () => {+ const quotaFile = freshQuotaFile();+ const victim = path.join(path.dirname(path.dirname(quotaFile)), "victim.txt");+ fs.writeFileSync(victim, "untouched");+ fs.mkdirSync(path.dirname(quotaFile), { recursive: true });+ fs.symlinkSync(victim, quotaFile);+ shouldSendEvent("launch", undefined, quotaFile, MONDAY);+ expect(fs.readFileSync(victim, "utf8")).toBe("untouched");+ expect(fs.lstatSync(quotaFile).isSymbolicLink()).toBe(false);+});