@mobilenext/mobile-mcp 1.0.15 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,19 @@ exports.describeCoordinateMapping = void 0;
4
4
  const RATIO_DECIMALS = 3;
5
5
  const formatRatio = (ratio) => Number(ratio.toFixed(RATIO_DECIMALS)).toString();
6
6
  const isPositive = (size) => size.width > 0 && size.height > 0;
7
+ const isLandscape = (size) => size.width > size.height;
8
+ /**
9
+ * The screenshot always shows the display as it is rotated right now, but the
10
+ * reported screen size can be the device's natural (portrait) size even while
11
+ * a landscape app is in front. Taps land in the current rotation's coordinates,
12
+ * so when the two disagree the screen size is turned to match the screenshot.
13
+ */
14
+ const orientLike = (screen, screenshot) => {
15
+ if (isLandscape(screen) === isLandscape(screenshot)) {
16
+ return screen;
17
+ }
18
+ return { width: screen.height, height: screen.width };
19
+ };
7
20
  /**
8
21
  * Plain-words instruction for a model that reads pixel positions off a
9
22
  * screenshot and taps in screen coordinates. The screenshot is usually
@@ -13,10 +26,11 @@ const isPositive = (size) => size.width > 0 && size.height > 0;
13
26
  * Returns null when either size is unknown, in which case no instruction is
14
27
  * better than a wrong one.
15
28
  */
16
- const describeCoordinateMapping = (screenshot, screen) => {
17
- if (!isPositive(screenshot) || !isPositive(screen)) {
29
+ const describeCoordinateMapping = (screenshot, reportedScreen) => {
30
+ if (!isPositive(screenshot) || !isPositive(reportedScreen)) {
18
31
  return null;
19
32
  }
33
+ const screen = orientLike(reportedScreen, screenshot);
20
34
  if (screenshot.width === screen.width && screenshot.height === screen.height) {
21
35
  return `Screenshot is ${screenshot.width}x${screenshot.height} and its coordinates match the screen.`;
22
36
  }
@@ -196,6 +196,18 @@ class MobileDevice {
196
196
  }
197
197
  return elements.flatMap(element => flattenUIElement(element));
198
198
  }
199
+ async getRawUIHierarchy() {
200
+ const response = this.runJsonCommand(["dump", "ui", "--format", "raw", "--full"]);
201
+ const rawData = response.data?.rawData;
202
+ if (rawData === undefined || rawData === null) {
203
+ throw new robot_1.ActionableError("No raw UI hierarchy was returned for this device. Its screen may be off or locked, or the installed mobilecli version may not support raw UI dumps");
204
+ }
205
+ // mobilecli returns android rawData as a json string, ios as an object
206
+ if (typeof rawData === "string") {
207
+ return JSON.parse(rawData);
208
+ }
209
+ return rawData;
210
+ }
199
211
  async setOrientation(orientation) {
200
212
  this.runCommand(["device", "orientation", "set", orientation]);
201
213
  }
package/lib/server.js CHANGED
@@ -21,6 +21,7 @@ const mobilecli_1 = require("./mobilecli");
21
21
  const mobile_device_1 = require("./mobile-device");
22
22
  const utils_1 = require("./utils");
23
23
  const format_elements_1 = require("./format-elements");
24
+ const telemetry_quota_1 = require("./telemetry-quota");
24
25
  const ALLOWED_LOG_EXTENSIONS = [".log", ".txt", ".jsonl"];
25
26
  const DEFAULT_DEVICE_LOG_ENTRIES = 100;
26
27
  const MAX_DEVICE_LOG_ENTRIES = 10000;
@@ -142,6 +143,10 @@ const createMcpServer = () => {
142
143
  if (process.env.MOBILEMCP_DISABLE_TELEMETRY) {
143
144
  return;
144
145
  }
146
+ const toolName = typeof properties.ToolName === "string" ? properties.ToolName : undefined;
147
+ if (!(0, telemetry_quota_1.shouldSendEvent)(event, toolName)) {
148
+ return;
149
+ }
145
150
  try {
146
151
  const url = "https://us.i.posthog.com/i/v0/e/";
147
152
  const api_key = "phc_KHRTZmkDsU7A8EbydEK8s4lJpPoTDyyBhSlwer694cS";
@@ -575,9 +580,15 @@ const createMcpServer = () => {
575
580
  });
576
581
  tool("mobile_list_elements_on_screen", "List Screen Elements", "List elements on screen with their ref, coordinates, and display text or accessibility label. Use the ref with mobile_click_on_screen_at_coordinates, mobile_double_tap_on_screen or mobile_long_press_on_screen_at_coordinates. Refs and coordinates stay valid as long as the screen does not change; re-list only after navigation or a layout change.", {
577
582
  device: zod_1.z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you."),
578
- format: zod_1.z.enum(["text", "json"]).optional().describe("Output format. \"text\" (default) is one compact line per element, \"json\" is a json array"),
579
- }, { readOnlyHint: true, openWorldHint: true }, async ({ device, format = "text" }) => {
583
+ format: zod_1.z.enum(["text", "json", "raw"]).default("text").describe("Output format. \"text\" (default) is one compact line per element, \"json\" is a json array, \"raw\" is the unfiltered platform-specific hierarchy as JSON (mobilecli only). Raw nodes may be offscreen and do not have element refs"),
584
+ }, { readOnlyHint: true, openWorldHint: true }, async ({ device, format }) => {
580
585
  const robot = getRobotFromDevice(device);
586
+ if (format === "raw") {
587
+ if (!robot.getRawUIHierarchy) {
588
+ throw new robot_1.ActionableError("Raw UI hierarchy is not supported in legacy robot mode");
589
+ }
590
+ return JSON.stringify(await robot.getRawUIHierarchy());
591
+ }
581
592
  const elements = await robot.getElementsOnScreen();
582
593
  return (0, format_elements_1.formatElements)(elements, format);
583
594
  });
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.shouldSendEvent = void 0;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_os_1 = __importDefault(require("node:os"));
9
+ const node_path_1 = __importDefault(require("node:path"));
10
+ // events not listed here are always sent (get_more_tools' $mcp_missing_capability)
11
+ const DAILY_CAPS = {
12
+ "launch": 1,
13
+ "tool_invoked": 1,
14
+ "tool_failed": 100,
15
+ };
16
+ // get_more_tools is rare and is the signal we want the most, never cap it
17
+ const UNCAPPED_TOOLS = ["get_more_tools"];
18
+ // share mobilecli's state dir (it keeps daemon.pid/sock/log there), honoring the same override
19
+ const stateDir = () => process.env.MOBILECLI_HOME || node_path_1.default.join(node_os_1.default.homedir(), ".mobilecli");
20
+ const defaultQuotaPath = () => node_path_1.default.join(stateDir(), "mobile-mcp-telemetry.json");
21
+ const today = (now) => now.toISOString().slice(0, 10);
22
+ const isValidCounts = (counts) => {
23
+ if (typeof counts !== "object" || counts === null || Array.isArray(counts)) {
24
+ return false;
25
+ }
26
+ return Object.values(counts).every(count => Number.isInteger(count) && count >= 0);
27
+ };
28
+ const readState = (statePath, day) => {
29
+ try {
30
+ const state = JSON.parse(node_fs_1.default.readFileSync(statePath, "utf8"));
31
+ if (state?.day === day && isValidCounts(state.counts)) {
32
+ return state;
33
+ }
34
+ }
35
+ catch {
36
+ // missing or corrupt file, start a fresh day
37
+ }
38
+ return { day, counts: {} };
39
+ };
40
+ const writeState = (statePath, state) => {
41
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(statePath), { recursive: true, mode: 0o700 });
42
+ // write a temp file and rename over the target, so a planted link at statePath is replaced, not written through
43
+ const tempPath = `${statePath}.${process.pid}.tmp`;
44
+ node_fs_1.default.writeFileSync(tempPath, JSON.stringify(state), { mode: 0o600 });
45
+ node_fs_1.default.renameSync(tempPath, statePath);
46
+ };
47
+ // ponytail: read-modify-write without a lock, so concurrent launches may slightly exceed a cap.
48
+ // add a lock file if that ever matters.
49
+ const shouldSendEvent = (event, toolName, statePath = defaultQuotaPath(), now = new Date()) => {
50
+ const cap = DAILY_CAPS[event];
51
+ if (cap === undefined) {
52
+ return true;
53
+ }
54
+ if (toolName !== undefined && UNCAPPED_TOOLS.includes(toolName)) {
55
+ return true;
56
+ }
57
+ try {
58
+ const state = readState(statePath, today(now));
59
+ const sent = state.counts[event] ?? 0;
60
+ if (sent >= cap) {
61
+ return false;
62
+ }
63
+ writeState(statePath, { day: state.day, counts: { ...state.counts, [event]: sent + 1 } });
64
+ return true;
65
+ }
66
+ catch {
67
+ // can't persist the quota (read-only home, etc.), drop the event rather than flood
68
+ return false;
69
+ }
70
+ };
71
+ exports.shouldSendEvent = shouldSendEvent;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mobilenext/mobile-mcp",
3
3
  "mcpName": "io.github.mobile-next/mobile-mcp",
4
- "version": "1.0.15",
4
+ "version": "1.0.17",
5
5
  "description": "Mobile MCP",
6
6
  "repository": {
7
7
  "type": "git",
@@ -34,7 +34,7 @@
34
34
  "commander": "14.0.0",
35
35
  "express": "5.1.0",
36
36
  "fast-xml-parser": "5.8.0",
37
- "mobilewright": "0.0.67",
37
+ "mobilewright": "0.0.69",
38
38
  "qs": "^6.15.0",
39
39
  "zod": "^4.2.0",
40
40
  "zod-to-json-schema": "3.25.0"