Skip to content

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.

feat: use sips on mac for image scaling

Gil Megidisha3829f604817
diff --git a/src/image-utils.ts b/src/image-utils.ts
index aaebab6..e4ce545 100644
--- a/src/image-utils.ts
+++ b/src/image-utils.ts
@@ -1,4 +1,8 @@
import { execFileSync, spawnSync } from "child_process";
+import os from "node:os";
+import fs from "node:fs";
+import path from "node:path";
+import { trace } from "./logger";
const DEFAULT_JPEG_QUALITY = 75;
@@ -27,7 +31,81 @@ export class ImageTransformer {
}
public toBuffer(): Buffer {
- const proc = spawnSync("magick", ["-", "-resize", `${this.newWidth}x`, "-quality", `${this.jpegOptions.quality}`, `${this.newFormat}:-`], {
+ if (isSipsInstalled()) {
+ try {
+ return this.toBufferWithSips();
+ } catch (error) {
+ trace(`Sips failed, falling back to ImageMagick: ${error}`);
+ }
+ }
+
+ try {
+ return this.toBufferWithImageMagick();
+ } catch (error) {
+ trace(`ImageMagick failed: ${error}`);
+ throw new Error("Image scaling unavailable (requires Sips or ImageMagick).");
+ }
+ }
+
+ private qualityToSips(q: number): "low" | "normal" | "high" | "best" {
+ if (q >= 90) {
+ return "best";
+ }
+
+ if (q >= 75) {
+ return "high";
+ }
+
+ if (q >= 50) {
+ return "normal";
+ }
+
+ return "low";
+ }
+
+ private toBufferWithSips(): Buffer {
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "image-"));
+ const inputFile = path.join(tempDir, "input");
+ const outputFile = path.join(tempDir, `output.${this.newFormat === "jpg" ? "jpg" : "png"}`);
+
+ try {
+ fs.writeFileSync(inputFile, this.buffer);
+
+ const args = ["-s", "format", this.newFormat === "jpg" ? "jpeg" : "png"];
+ if (this.newFormat === "jpg") {
+ args.push("-s", "formatOptions", this.qualityToSips(this.jpegOptions.quality));
+ }
+
+ args.push("-Z", `${this.newWidth}`);
+ args.push("--out", outputFile);
+ args.push(inputFile);
+
+ trace(`Running sips command: /usr/bin/sips ${args.join(" ")}`);
+ const proc = spawnSync("/usr/bin/sips", args, {
+ maxBuffer: 8 * 1024 * 1024
+ });
+
+ if (proc.status !== 0) {
+ throw new Error(`Sips failed with status ${proc.status}`);
+ }
+
+ const outputBuffer = fs.readFileSync(outputFile);
+ trace("Sips returned buffer of size: " + outputBuffer.length);
+ return outputBuffer;
+ } finally {
+ try {
+ fs.rmSync(tempDir, { recursive: true, force: true });
+ } catch (error) {
+ // Ignore cleanup errors
+ }
+ }
+ }
+
+ private toBufferWithImageMagick(): Buffer {
+ const magickArgs = ["-", "-resize", `${this.newWidth}x`, "-quality", `${this.jpegOptions.quality}`, `${this.newFormat}:-`];
+ trace(`Running magick command: magick ${magickArgs.join(" ")}`);
+
+ const proc = spawnSync("magick", magickArgs, {
maxBuffer: 8 * 1024 * 1024,
input: this.buffer
});
@@ -52,6 +130,23 @@ export class Image {
}
}
+const isDarwin = (): boolean => {
+ return os.platform() === "darwin";
+};
+
+export const isSipsInstalled = (): boolean => {
+ if (!isDarwin()) {
+ return false;
+ }
+
+ try {
+ execFileSync("/usr/bin/sips", ["--version"]);
+ return true;
+ } catch (error) {
+ return false;
+ }
+};
+
export const isImageMagickInstalled = (): boolean => {
try {
return execFileSync("magick", ["--version"])
@@ -63,3 +158,7 @@ export const isImageMagickInstalled = (): boolean => {
return false;
}
};
+
+export const isScalingAvailable = (): boolean => {
+ return isImageMagickInstalled() || isSipsInstalled();
+};
diff --git a/src/server.ts b/src/server.ts
index 288fd6e..53be8b9 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -11,7 +11,7 @@ import { ActionableError, Robot } from "./robot";
import { SimctlManager } from "./iphone-simulator";
import { IosManager, IosRobot } from "./ios";
import { PNG } from "./png";
-import { isImageMagickInstalled, Image } from "./image-utils";
+import { isScalingAvailable, Image } from "./image-utils";
export const getAgentVersion = (): string => {
const json = require("../package.json");
@@ -446,8 +446,8 @@ export const createMcpServer = (): McpServer => {
throw new ActionableError("Screenshot is invalid. Please try again.");
}
- if (isImageMagickInstalled()) {
- trace("ImageMagick is installed, resizing screenshot");
+ if (isScalingAvailable()) {
+ trace("Image scaling is available, resizing screenshot");
const image = Image.fromBuffer(screenshot);
const beforeSize = screenshot.length;
screenshot = image.resize(Math.floor(pngSize.width / screenSize.scale))