Skip to main content

iphone_cli/
mcp.rs

1//! `iph mcp`: the commands as MCP tools over stdio (JSON-RPC 2.0, one message
2//! per line). Each tool forwards to the same daemon the CLI uses, so results
3//! have the same shape.
4
5use anyhow::Result;
6use serde_json::{Map, Value, json};
7use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
8
9use crate::{
10    client, connect,
11    reply::{Fail, envelope},
12    script,
13};
14
15const PROTOCOL_VERSION: &str = "2025-06-18";
16
17const SCRIPT_DOC: &str = "Run JavaScript that drives the phone, in one call: loops, conditions and several steps without a round trip each. \
18The commands are functions that return what the tool of the same name returns: read(opts), find(selector), tap(target, opts), swipe(direction | {from, to}, opts), \
19pinch(direction | {scale, rotate, at}, opts), scroll(direction, {until, max}), type(text, {clear, submit}), key(name), button(name), launch(app, {restart, arguments, environment}), apps(). Also waitFor(selector, {timeout_ms}), \
20readField({selector, take, regex}) → text, readList({row, columns, group, stop_below, max_scrolls}) → {rows, scrolls, ended}, sleep(ms), log(…), output(key, value). \
21tap(target) waits up to 5 s for a selector to appear and hold still; tap([a, b, c]) presses a keypad without re-reading. \
22A call that fails throws an Error with .code, .outcome, .hint and .result (the full result object); uncaught, it ends the script and the result says which call and line. \
23After a call whose outcome is \"unknown\" the script is stopped and cannot go on. \
24For type(), verified means read back, not text matched; inspect value_changed and warning, never automatically retype. submit withholds Return with unknown / type_not_verified when the effect is uncertain. \
25No files, network or timers; Date, JSON, RegExp, Map and the rest of the language are there. The result has outputs (the returned object, plus output() calls), logs, calls and inputs_sent.";
26
27const SELECTOR_DOC: &str = "Selector: a plain string is an exact label (the name part of a \"name, value\" row also counts). \
28Or combine terms: label=\"exact\", label~=\"contains\", label/regex/, role=button, id=<identifier or element id>, \
29value~=…, within=\"<selector>\", index=N. An object works too: {label, label_contains, label_regex, value, \
30value_contains, role, id, id_contains, id_regex, within, index}. More than one match is an error that lists the candidates.";
31
32fn tool(name: &str, description: &str, properties: Value, required: &[&str]) -> Value {
33    json!({
34        "name": name,
35        "description": description,
36        "inputSchema": {
37            "type": "object",
38            "properties": properties,
39            "required": required,
40            "additionalProperties": false,
41        },
42    })
43}
44
45fn tools() -> Vec<Value> {
46    let selector = json!({ "description": SELECTOR_DOC, "type": ["string", "object"] });
47    vec![
48        tool(
49            "iph_connect",
50            "Verify and save the selected paired iPhone's IP address when automatic discovery fails. Requires prior USB trust on this computer. Checks a paired session and reported UDID before saving. Subsequent tools and scripts use the address; this does not establish developer-service readiness or repair Apple's discovery list.",
51            json!({ "ip": { "type": "string", "description": "The iPhone's IPv4 or IPv6 address. Link-local IPv6 needs the computer's numeric interface index, e.g. fe80::1234%12." } }),
52            &["ip"],
53        ),
54        tool(
55            "iph_read",
56            "Read the iPhone's current screen as a list of elements (id, role, label, value, frame in points, traits, enabled). \
57             Read before acting; coordinates and labels come from here. Only on-screen elements unless all=true.",
58            json!({
59                "app": { "type": "boolean", "description": "Only the foreground app's elements." },
60                "all": { "type": "boolean", "description": "Include elements outside the screen." },
61                "full": { "type": "boolean", "description": "Wait ~0.6 s more for accessibility identifiers." },
62                "max_age_ms": { "type": "integer", "description": "Accept a cached reading this recent instead of asking the phone (a fresh read takes about 1.2 s)." },
63            }),
64            &[],
65        ),
66        tool(
67            "iph_find",
68            "Find elements on screen that match a selector.",
69            json!({ "selector": selector, "all": { "type": "boolean" } }),
70            &["selector"],
71        ),
72        tool(
73            "iph_tap",
74            "Tap an element (selector) or a point (\"x,y\" in points). Waits for the screen to change and hold still, \
75             then returns it (`elements`, as iph_read does) and whether it changed. \
76             Never retry a tap whose outcome is \"unknown\".",
77            json!({
78                "target": { "description": format!("A selector, or \"x,y\". {SELECTOR_DOC}"), "type": ["string", "object"] },
79                "long_ms": { "type": "integer", "description": "Hold this long instead of a short tap." },
80                "double": { "type": "boolean" },
81                "no_wait": { "type": "boolean", "description": "Read the screen once right after the tap instead of waiting for it to settle; the result then has no elements." },
82            }),
83            &["target"],
84        ),
85        tool(
86            "iph_swipe",
87            "Swipe the finger up, down, left or right across the middle of the screen, or between two points.",
88            json!({
89                "direction": { "type": "string", "enum": ["up", "down", "left", "right"] },
90                "from": { "type": "array", "items": { "type": "number" }, "minItems": 2, "maxItems": 2 },
91                "to": { "type": "array", "items": { "type": "number" }, "minItems": 2, "maxItems": 2 },
92                "duration_ms": { "type": "integer" },
93                "drag": { "type": "boolean", "description": "Slow to a stop before lifting (a drag) instead of flicking." },
94            }),
95            &[],
96        ),
97        tool(
98            "iph_pinch",
99            "Two fingers on the screen: pinch in or out to zoom, turn to rotate (a map, an image, a web page). Give a direction, or a scale and/or a rotation. \
100             The fingers meet in the middle of the screen, or at `at`. `screen_changed: false` is a fair answer on a screen that does not zoom. \
101             Never retry one whose outcome is \"unknown\".",
102            json!({
103                "direction": { "type": "string", "enum": ["in", "out"], "description": "in = scale 0.5, out = scale 2. Not together with scale." },
104                "scale": { "type": "number", "minimum": 0.25, "maximum": 4, "description": "End distance between the fingers over the start distance: above 1 spreads them (zoom in), below 1 pinches (zoom out)." },
105                "rotate": { "type": "number", "minimum": -180, "maximum": 180, "description": "Degrees, positive clockwise." },
106                "at": { "description": format!("Where the fingers meet: a selector, or \"x,y\" in points. Default: the middle of the screen. {SELECTOR_DOC}"), "type": ["string", "object"] },
107                "duration_ms": { "type": "integer", "description": "How long the fingers move (default 500, 100..10000)." },
108            }),
109            &[],
110        ),
111        tool(
112            "iph_scroll",
113            "Scroll the content. \"down\" shows what is further down. With `until`, keeps scrolling until that element is on screen.",
114            json!({
115                "direction": { "type": "string", "enum": ["up", "down"] },
116                "until": selector,
117                "max": { "type": "integer", "description": "Most scrolls to try (default 10)." },
118            }),
119            &["direction"],
120        ),
121        tool(
122            "iph_type",
123            "Type text into the field that has the keyboard (tap the field first). Pasted through the clipboard, so Korean and emoji work; \
124             the user's clipboard is put back (not content copied in an app: see clipboard_note). verified means the value was read, not that it matches the intended text. \
125             Inspect value_before, value, value_changed (true/false/null) and warning. An unchanged value can mean blocked paste, masking or identical replacement; never automatically retype. \
126             screen_changed compares accessibility readings after keyboard restoration (null if unavailable). submit withholds Return with outcome unknown / type_not_verified when the input effect is uncertain.",
127            json!({
128                "text": { "type": "string" },
129                "clear": { "type": "boolean", "description": "Replace what the field holds." },
130                "submit": { "type": "boolean", "description": "Press Return only after observing a changed, nonempty value (or an empty value for clear-only). Otherwise withhold Return and return unknown; inspect before taking another action." },
131            }),
132            &["text"],
133        ),
134        tool(
135            "iph_key",
136            "Press a key: return, esc, tab, space, backspace, delete, arrows, or a combination like cmd+a.",
137            json!({ "key": { "type": "string" } }),
138            &["key"],
139        ),
140        tool(
141            "iph_button",
142            "Press a hardware button.",
143            json!({ "button": { "type": "string", "enum": ["home", "lock", "volume_up", "volume_down"] } }),
144            &["button"],
145        ),
146        tool(
147            "iph_launch",
148            "Bring an app to the front by bundle id (com.apple.Preferences) or by its name.",
149            json!({
150                "app": { "type": "string" },
151                "restart": { "type": "boolean", "description": "Quit the app first so it starts from its first screen." },
152                "arguments": {
153                    "type": "array", "items": { "type": "string" },
154                    "description": "Launch arguments for the app, e.g. [\"-AppleLanguages\", \"(ko)\"]. The app is restarted, since a running process takes none.",
155                },
156                "environment": {
157                    "type": "object", "additionalProperties": { "type": "string" },
158                    "description": "Environment variables for the app's process, e.g. {\"TZ\": \"UTC\"}. The app is restarted.",
159                },
160            }),
161            &["app"],
162        ),
163        tool("iph_apps", "List installed apps with their bundle ids.", json!({}), &[]),
164        tool(
165            "iph_dnd",
166            "Do Not Disturb on the phone, so a notification banner cannot take a tap. \"on\" before the work, \"off\" when it is done \
167             (puts back the Focus that was on).",
168            json!({ "action": { "type": "string", "enum": ["on", "off", "status"] } }),
169            &["action"],
170        ),
171        tool(
172            "iph_screenshot",
173            "Save a PNG screenshot to a file on this computer. Use iph_read for content; this is for looks.",
174            json!({ "path": { "type": "string", "description": "Absolute path of the PNG to write." } }),
175            &["path"],
176        ),
177        tool(
178            "iph_batch",
179            "Run several steps in one call, stopping at the first failure. Each step is a one-key object: \
180             {\"launch\": \"…\"}, {\"wait_for\": selector}, {\"tap\": selector}, {\"type\": {\"text\": \"…\"}}, {\"swipe\": \"up\"}, {\"pinch\": \"out\"}, \
181             {\"scroll_until\": selector}, {\"read_field\": {\"name\", \"selector\", \"take\"}}, {\"read_list\": {\"name\", \"row\", \"columns\"}}, \
182             {\"assert\": selector}, {\"button\": \"home\"}, {\"key\": \"return\"}, {\"sleep\": ms}.",
183            json!({ "steps": { "type": "array", "items": { "type": "object" } } }),
184            &["steps"],
185        ),
186        tool(
187            "iph_script",
188            SCRIPT_DOC,
189            json!({
190                "code": { "type": "string", "description": "JavaScript. The body of a function: `return` ends it and its value becomes `outputs`." },
191                "args": { "type": "object", "description": "What the script reads as `args`." },
192            }),
193            &["code"],
194        ),
195        tool(
196            "iph_run",
197            "Run a saved script by name (see iph_scripts) in one call and get its outputs. Same result as iph_script.",
198            json!({
199                "name": { "type": "string" },
200                "args": { "type": "object", "description": "What the script reads as `args`." },
201            }),
202            &["name"],
203        ),
204        tool("iph_scripts", "List saved scripts: name, what each does, and where it lives.", json!({}), &[]),
205        tool(
206            "iph_doctor",
207            "Check that the phone is ready. Each check says what the user must do when it fails.",
208            json!({
209                "no_touch_test": { "type": "boolean", "description": "Skip the check that scrolls Settings down and back." },
210                "fix_wifi": { "type": "boolean", "description": "Enable disabled Wi-Fi connection/debugging flags over USB and briefly check Network discovery." },
211            }),
212            &[],
213        ),
214    ]
215}
216
217async fn call(udid: &str, name: &str, args: Value) -> Value {
218    let started = std::time::Instant::now();
219    let args = if args.is_null() { json!({}) } else { args };
220    match name {
221        "iph_connect" => match serde_json::from_value::<connect::Args>(args) {
222            Ok(args) => envelope(connect::run(udid, args).await, started),
223            Err(e) => envelope(Err(Fail::invalid(e.to_string())), started),
224        },
225        "iph_scripts" => envelope(script::cmd_scripts(), started),
226        "iph_script" | "iph_run" => match script_request(name, &args) {
227            Ok(request) => client::request(udid, "script", request).await,
228            Err(f) => envelope(Err(f), started),
229        },
230        "iph_read" | "iph_find" | "iph_tap" | "iph_swipe" | "iph_pinch" | "iph_scroll" | "iph_type" | "iph_key"
231        | "iph_button" | "iph_launch" | "iph_apps" | "iph_screenshot" | "iph_batch" | "iph_doctor"
232        | "iph_dnd" => {
233            let cmd = name.trim_start_matches("iph_");
234            let args = if cmd == "apps" { Value::Null } else { args };
235            client::request(udid, cmd, args).await
236        }
237        other => envelope(Err(Fail::invalid(format!("unknown tool `{other}`"))), started),
238    }
239}
240
241/// What `iph_script` and `iph_run` send on: the code itself, or the saved
242/// script the name stands for.
243fn script_request(tool: &str, args: &Value) -> Result<Value, Fail> {
244    let empty = Map::new();
245    let given = match args.get("args") {
246        None | Some(Value::Null) => &empty,
247        Some(Value::Object(map)) => map,
248        Some(_) => return Err(Fail::invalid("`args` takes an object")),
249    };
250    let text = |key: &str| {
251        args.get(key)
252            .and_then(|v| v.as_str())
253            .ok_or_else(|| Fail::invalid(format!("{tool} needs `{key}`")))
254    };
255    if tool == "iph_script" {
256        // `script` is accepted for `code`: a model that goes by the tool's
257        // name rather than its schema sends that (Codex did, 2026-11-11).
258        let code = match args.get("script").and_then(|v| v.as_str()) {
259            Some(code) if args.get("code").is_none() => code,
260            _ => text("code")?,
261        };
262        return Ok(script::request(code, given, None));
263    }
264    let name = text("name")?;
265    let (code, source) = script::load(name)?;
266    Ok(script::request(&code, given, Some((name, &source))))
267}
268
269fn response(id: &Value, result: Value) -> Value {
270    json!({ "jsonrpc": "2.0", "id": id, "result": result })
271}
272
273fn error(id: &Value, code: i64, message: &str) -> Value {
274    json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": message } })
275}
276
277/// Tools this server offers: all of them, or the ones named with `--tools`.
278fn offered(only: &[String]) -> Vec<Value> {
279    tools()
280        .into_iter()
281        .filter(|t| {
282            let name = t["name"].as_str().unwrap_or("");
283            only.is_empty() || only.iter().any(|o| o == name || format!("iph_{o}") == name)
284        })
285        .collect()
286}
287
288async fn handle(udid: &str, only: &[String], msg: &Value) -> Option<Value> {
289    let method = msg.get("method").and_then(|m| m.as_str())?;
290    // Notifications carry no id and get no answer.
291    let id = msg.get("id")?;
292    let empty = Map::new();
293    let params = msg.get("params").and_then(|p| p.as_object()).unwrap_or(&empty);
294    Some(match method {
295        "initialize" => {
296            let version = params
297                .get("protocolVersion")
298                .and_then(|v| v.as_str())
299                .unwrap_or(PROTOCOL_VERSION);
300            response(
301                id,
302                json!({
303                    "protocolVersion": version,
304                    "capabilities": { "tools": {} },
305                    "serverInfo": { "name": "iph", "version": env!("CARGO_PKG_VERSION") },
306                    "instructions": "Operate a real iPhone through its accessibility tree. Read the screen with iph_read, act with iph_tap / iph_type / iph_scroll, \
307                                     and check `screen_changed` in each result. For typing also check value_changed and warning: verified only means read back. Never automatically retype on a warning. For a known sequence prefer one iph_script (or a saved one with iph_run): one call instead of one per step. \
308                                     Outcome \"unknown\" means input may have gone out: read the screen, never resend.",
309                }),
310            )
311        }
312        "ping" => response(id, json!({})),
313        "tools/list" => response(id, json!({ "tools": offered(only) })),
314        "tools/call" => {
315            let Some(name) = params.get("name").and_then(|n| n.as_str()) else {
316                return Some(error(id, -32602, "tools/call needs a tool name"));
317            };
318            if !offered(only).iter().any(|t| t["name"] == name) {
319                return Some(error(id, -32602, "this server does not offer that tool"));
320            }
321            let args = params.get("arguments").cloned().unwrap_or(Value::Null);
322            let result = call(udid, name, args).await;
323            let is_error = result["ok"] != true;
324            response(
325                id,
326                json!({
327                    "content": [{ "type": "text", "text": result.to_string() }],
328                    "isError": is_error,
329                }),
330            )
331        }
332        "resources/list" => response(id, json!({ "resources": [] })),
333        "prompts/list" => response(id, json!({ "prompts": [] })),
334        _ => error(id, -32601, "method not found"),
335    })
336}
337
338/// Serve MCP on stdio. `only` limits the tools on offer (empty = all).
339pub async fn serve(udid: String, only: Vec<String>) -> Result<()> {
340    let mut lines = BufReader::new(tokio::io::stdin()).lines();
341    let mut stdout = tokio::io::stdout();
342    while let Some(line) = lines.next_line().await? {
343        let line = line.trim();
344        if line.is_empty() {
345            continue;
346        }
347        let reply = match serde_json::from_str::<Value>(line) {
348            Ok(msg) => handle(&udid, &only, &msg).await,
349            Err(_) => Some(error(&Value::Null, -32700, "parse error")),
350        };
351        if let Some(reply) = reply {
352            stdout.write_all(format!("{reply}\n").as_bytes()).await?;
353            stdout.flush().await?;
354        }
355    }
356    Ok(())
357}
358
359#[cfg(test)]
360mod tests {
361    use super::*;
362
363    #[test]
364    fn launch_offers_arguments_and_environment() {
365        let launch = tools().into_iter().find(|t| t["name"] == "iph_launch").unwrap();
366        let props = &launch["inputSchema"]["properties"];
367        assert_eq!(props["arguments"]["items"]["type"], "string");
368        assert_eq!(props["environment"]["additionalProperties"]["type"], "string");
369    }
370
371    #[test]
372    fn tool_list_covers_the_spec() {
373        let names: Vec<String> = tools()
374            .iter()
375            .map(|t| t["name"].as_str().unwrap().to_string())
376            .collect();
377        for expected in [
378            "iph_read", "iph_find", "iph_tap", "iph_swipe", "iph_pinch", "iph_scroll", "iph_type", "iph_button",
379            "iph_launch", "iph_batch", "iph_script", "iph_run", "iph_scripts", "iph_doctor", "iph_connect",
380        ] {
381            assert!(names.contains(&expected.to_string()), "missing {expected}");
382        }
383        for t in tools() {
384            assert_eq!(t["inputSchema"]["type"], "object");
385            assert!(!t["description"].as_str().unwrap().is_empty());
386        }
387    }
388
389    #[test]
390    fn the_pinch_tool_offers_what_the_command_accepts() {
391        let tool = tools().into_iter().find(|t| t["name"] == "iph_pinch").unwrap();
392        let props = tool["inputSchema"]["properties"].as_object().unwrap();
393        let mut keys: Vec<&str> = props.keys().map(String::as_str).collect();
394        keys.sort();
395        assert_eq!(keys, ["at", "direction", "duration_ms", "rotate", "scale"]);
396        assert_eq!(props["direction"]["enum"], json!(["in", "out"]));
397        assert!(tool["inputSchema"].get("required").is_none_or(|r| r.as_array().is_some_and(|r| r.is_empty())));
398        for args in [
399            json!({"direction": "out", "rotate": 30, "at": "Map", "duration_ms": 300}),
400            json!({"scale": 0.5, "at": {"x": 100, "y": 200}}),
401        ] {
402            assert!(serde_json::from_value::<crate::actions::PinchArgs>(args).is_ok());
403        }
404        assert!(serde_json::from_value::<crate::actions::PinchArgs>(json!({"direction": "out", "zoom": 2})).is_err());
405    }
406
407    #[test]
408    fn a_script_request_takes_code_or_a_saved_name() {
409        let req = script_request("iph_script", &json!({"code": "return 1", "args": {"n": 2}})).unwrap();
410        assert_eq!(req, json!({"code": "return 1", "args": {"n": 2}}));
411        // The name a model guesses from the tool's name works too.
412        let req = script_request("iph_script", &json!({"script": "return 2"})).unwrap();
413        assert_eq!(req["code"], "return 2");
414        let req = script_request("iph_run", &json!({"name": "settings-version"})).unwrap();
415        assert_eq!(req["script"], "settings-version");
416        assert_eq!(req["source"], "builtin:settings-version");
417        assert!(req["code"].as_str().unwrap().contains("launch("));
418        for bad in [json!({}), json!({"code": 1}), json!({"code": "x", "args": [1]})] {
419            assert_eq!(script_request("iph_script", &bad).unwrap_err().code, "invalid_argument", "{bad}");
420        }
421        assert_eq!(script_request("iph_run", &json!({"name": "nope"})).unwrap_err().code, "script_not_found");
422    }
423
424    #[tokio::test]
425    async fn connect_rejects_missing_invalid_and_extra_arguments_before_contacting_a_phone() {
426        for args in [json!({}), json!({"ip": "not-an-ip"}),
427                     json!({"ip": "192.0.2.1", "submit": true})] {
428            let result = call("NO-PHONE", "iph_connect", args).await;
429            assert_eq!(result["ok"], false);
430            assert_eq!(result["outcome"], "not_sent");
431            assert_eq!(result["error"]["code"], "invalid_argument");
432        }
433    }
434
435    #[tokio::test]
436    async fn initialize_and_notifications() {
437        let init = json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{}}});
438        let r = handle("X", &[], &init).await.unwrap();
439        assert_eq!(r["result"]["protocolVersion"], "2024-11-05");
440        assert_eq!(r["result"]["serverInfo"]["name"], "iph");
441        let note = json!({"jsonrpc":"2.0","method":"notifications/initialized"});
442        assert!(handle("X", &[], &note).await.is_none());
443        let unknown = json!({"jsonrpc":"2.0","id":2,"method":"nope"});
444        assert_eq!(handle("X", &[], &unknown).await.unwrap()["error"]["code"], -32601);
445    }
446
447    #[tokio::test]
448    async fn tools_can_be_limited() {
449        let only = vec!["read".to_string(), "iph_find".to_string()];
450        let list = json!({"jsonrpc":"2.0","id":1,"method":"tools/list"});
451        let r = handle("X", &only, &list).await.unwrap();
452        let names: Vec<&str> = r["result"]["tools"]
453            .as_array()
454            .unwrap()
455            .iter()
456            .map(|t| t["name"].as_str().unwrap())
457            .collect();
458        assert_eq!(names, ["iph_read", "iph_find"]);
459        // A tool that is not on offer cannot be called either.
460        let call = json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"iph_tap","arguments":{"target":"x"}}});
461        assert_eq!(handle("X", &only, &call).await.unwrap()["error"]["code"], -32602);
462    }
463}