native-devtools-mcp 0.10.1

MCP server for computer use & browser automation — screenshot, OCR, click, type, find_text, Chrome/Electron CDP, template matching. macOS, Windows & Android.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use cocoa::base::{id, nil};
use core_foundation::array::CFArray;
use core_foundation::base::TCFType;
use core_foundation::dictionary::CFDictionary;
use core_foundation::number::CFNumber;
use core_graphics::window::{
    kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly,
    kCGWindowOwnerPID, CGWindowListCopyWindowInfo,
};
use objc::{class, msg_send, sel, sel_impl};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::ffi::c_void;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppInfo {
    pub name: String,
    pub bundle_id: Option<String>,
    pub pid: i32,
    pub is_active: bool,
    pub is_hidden: bool,
    /// Whether this is a regular user-facing app (appears in Dock).
    /// Background agents and accessory apps are not user-facing.
    #[serde(skip)]
    pub is_user_app: bool,
}

/// Returns NSRunningApplication handles by merging two sources:
/// 1. `NSWorkspace.runningApplications` — may contain stale entries and miss recently
///    launched apps (requires run loop events to update, which our server doesn't process)
/// 2. `CGWindowListCopyWindowInfo` — always fresh, but only includes apps with windows
///
/// Apps from source 1 are filtered by `is_app_alive` to remove stale entries.
/// PIDs from source 2 that aren't in source 1 are looked up via
/// `NSRunningApplication.runningApplicationWithProcessIdentifier:` and added.
unsafe fn get_running_apps() -> Vec<id> {
    let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
    let running_apps: id = msg_send![workspace, runningApplications];
    let count: usize = msg_send![running_apps, count];

    let mut apps: Vec<id> = Vec::new();
    let mut seen_pids: HashSet<i32> = HashSet::new();

    // Source 1: NSWorkspace (filter dead processes)
    for i in 0..count {
        let app: id = msg_send![running_apps, objectAtIndex: i];
        if is_app_alive(app) {
            let pid: i32 = msg_send![app, processIdentifier];
            seen_pids.insert(pid);
            apps.push(app);
        }
    }

    // Source 2: CGWindowList (catch apps NSWorkspace hasn't registered yet)
    for pid in get_window_owner_pids() {
        if pid > 0 && !seen_pids.contains(&pid) {
            let app: id = msg_send![
                class!(NSRunningApplication),
                runningApplicationWithProcessIdentifier: pid
            ];
            if app != nil {
                seen_pids.insert(pid);
                apps.push(app);
            }
        }
    }

    apps
}

/// Returns unique owner PIDs from all on-screen windows via CGWindowListCopyWindowInfo.
unsafe fn get_window_owner_pids() -> HashSet<i32> {
    let mut pids = HashSet::new();

    let options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
    let ptr = CGWindowListCopyWindowInfo(options, kCGNullWindowID);
    if ptr.is_null() {
        return pids;
    }

    let list: CFArray<*const c_void> = CFArray::wrap_under_create_rule(ptr);
    for i in 0..list.len() {
        let dict: CFDictionary<*const c_void, *const c_void> =
            CFDictionary::wrap_under_get_rule(*list.get_unchecked(i) as *const _);

        if let Some(val) = dict.find(kCGWindowOwnerPID as *const c_void) {
            let num: CFNumber =
                core_foundation::base::CFType::wrap_under_get_rule(*val as *const _)
                    .downcast_into()
                    .unwrap();
            if let Some(pid) = num.to_i32() {
                pids.insert(pid);
            }
        }
    }

    pids
}

/// Checks if the process behind an NSRunningApplication is still alive.
///
/// macOS keeps stale entries in `runningApplications` for several seconds after
/// termination. `NSRunningApplication.isTerminated` requires run loop events to
/// update, which our server doesn't process, so we check liveness via `kill(pid, 0)`.
unsafe fn is_app_alive(app: id) -> bool {
    extern "C" {
        fn kill(pid: i32, sig: i32) -> i32;
    }
    let pid: i32 = msg_send![app, processIdentifier];
    kill(pid, 0) == 0
}

/// List all running applications
pub fn list_apps() -> Vec<AppInfo> {
    let mut apps = Vec::new();

    unsafe {
        for app in get_running_apps() {
            if !is_app_alive(app) {
                continue;
            }

            // Get localized name
            let name_ns: id = msg_send![app, localizedName];
            let name = if name_ns != nil {
                nsstring_to_string(name_ns)
            } else {
                continue;
            };

            // Get bundle identifier
            let bundle_id_ns: id = msg_send![app, bundleIdentifier];
            let bundle_id = if bundle_id_ns != nil {
                Some(nsstring_to_string(bundle_id_ns))
            } else {
                None
            };

            // Get PID
            let pid: i32 = msg_send![app, processIdentifier];

            // Get active state
            let is_active: bool = msg_send![app, isActive];

            // Get hidden state
            let is_hidden: bool = msg_send![app, isHidden];

            // Get activation policy to distinguish user-facing apps from background agents
            // NSApplicationActivationPolicyRegular = 0
            let activation_policy: i64 = msg_send![app, activationPolicy];
            let is_user_app = activation_policy == 0;

            // Filter to user-facing apps (those with a name)
            if !name.is_empty() {
                apps.push(AppInfo {
                    name,
                    bundle_id,
                    pid,
                    is_active,
                    is_hidden,
                    is_user_app,
                });
            }
        }
    }

    apps
}

/// Activate (focus) an application by name
pub fn activate_app(app_name: &str) -> bool {
    unsafe {
        for app in get_running_apps() {
            let name_ns: id = msg_send![app, localizedName];

            if name_ns != nil {
                let name = nsstring_to_string(name_ns);
                if name.to_lowercase().contains(&app_name.to_lowercase()) {
                    let _: bool = msg_send![app, activateWithOptions: 1u64]; // NSApplicationActivateIgnoringOtherApps
                    return true;
                }
            }
        }
    }

    false
}

/// Activate an application by PID
pub fn activate_app_by_pid(pid: i32) -> bool {
    unsafe {
        let app: id = msg_send![
            class!(NSRunningApplication),
            runningApplicationWithProcessIdentifier: pid
        ];

        if app != nil {
            let _: bool = msg_send![app, activateWithOptions: 1u64];
            return true;
        }
    }

    false
}

/// Check if a user-facing application is currently running (case-insensitive name match).
///
/// Only considers apps with NSApplicationActivationPolicyRegular (i.e., apps that
/// appear in the Dock). Ignores background agents, helpers, and accessory processes.
pub fn is_app_running(app_name: &str) -> bool {
    unsafe {
        let needle = app_name.to_lowercase();
        for app in get_running_apps() {
            if !is_app_alive(app) {
                continue;
            }

            // Only consider user-facing apps (NSApplicationActivationPolicyRegular = 0)
            let activation_policy: i64 = msg_send![app, activationPolicy];
            if activation_policy != 0 {
                continue;
            }

            let name_ns: id = msg_send![app, localizedName];
            if name_ns != nil {
                let name = nsstring_to_string(name_ns);
                if name.to_lowercase().contains(&needle) {
                    return true;
                }
            }
        }
    }
    false
}

/// Build the `open` [`Command`] for launching an application.
///
/// Extracted so the argument list can be inspected in unit tests without
/// actually executing `open`. The caller is responsible for running the
/// returned command.
///
/// * `app_name`   — app to launch (passed as `open -a <app_name>`)
/// * `args`       — optional extra CLI args forwarded after `--args`
/// * `background` — when `true`, prepends `-g` so the app is launched
///   without stealing foreground focus (`open -g -a …`)
pub fn build_launch_command(
    app_name: &str,
    args: &[String],
    background: bool,
) -> std::process::Command {
    let mut cmd = std::process::Command::new("open");
    if background {
        cmd.arg("-g");
    }
    cmd.arg("-a").arg(app_name);
    if !args.is_empty() {
        cmd.arg("--args");
        cmd.args(args);
    }
    cmd
}

/// Launch an application by name using `open -a`.
///
/// Finds the app in standard locations (/Applications, ~/Applications, etc.)
/// and launches it. If args is non-empty, passes them via `--args`.
/// If the app is already running and no args are given, it is brought to the front.
///
/// When `background` is `true`, the `-g` flag is passed to `open` so the
/// app launches without stealing foreground focus. Recommended whenever the
/// next action will use CDP or AX dispatch, both of which are focus-preserving.
pub fn launch_app(app_name: &str, args: &[String], background: bool) -> Result<(), String> {
    let mut cmd = build_launch_command(app_name, args, background);

    let output = cmd
        .output()
        .map_err(|e| format!("Failed to run 'open' command: {}", e))?;

    if output.status.success() {
        Ok(())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        Err(format!(
            "Failed to launch '{}': {}",
            app_name,
            stderr.trim()
        ))
    }
}

/// Quit an application by name.
///
/// By default performs a graceful termination (NSRunningApplication.terminate).
/// If `force` is true, uses forceTerminate which kills immediately without cleanup.
/// Returns the number of app instances terminated.
pub fn quit_app(app_name: &str, force: bool) -> Result<u32, String> {
    let mut terminated = 0u32;

    unsafe {
        let needle = app_name.to_lowercase();
        for app in get_running_apps() {
            if !is_app_alive(app) {
                continue;
            }

            let name_ns: id = msg_send![app, localizedName];
            if name_ns != nil {
                let name = nsstring_to_string(name_ns);
                if name.to_lowercase().contains(&needle) {
                    let success: bool = if force {
                        msg_send![app, forceTerminate]
                    } else {
                        msg_send![app, terminate]
                    };
                    if success {
                        terminated += 1;
                    }
                }
            }
        }
    }

    if terminated > 0 {
        Ok(terminated)
    } else {
        Err(format!(
            "No running app found matching '{}'. Use list_apps to find the correct app name.",
            app_name
        ))
    }
}

/// Known Chrome-family bundle identifiers.
const CHROME_BUNDLE_IDS: &[&str] = &[
    "com.google.Chrome",
    "com.google.Chrome.canary",
    "com.brave.Browser",
    "com.microsoft.edgemac",
    "company.thebrowser.Browser",
    "org.chromium.Chromium",
];

/// Check if an app is a Chrome-family browser by its bundle ID.
pub fn is_chrome_browser(bundle_id: Option<&str>, _app_name: &str) -> bool {
    bundle_id.is_some_and(|bid| CHROME_BUNDLE_IDS.contains(&bid))
}

/// Check if a running app is an Electron app by inspecting its bundle for
/// `Contents/Frameworks/Electron Framework.framework`.
pub fn is_electron_app_by_pid(pid: i32) -> bool {
    bundle_path_for_pid(pid)
        .map(|p| is_electron_bundle(&p))
        .unwrap_or(false)
}

/// Check if a non-running app is Electron by searching standard application
/// directories for `<app_name>.app` and inspecting the bundle.
pub fn is_electron_app_by_name(app_name: &str) -> bool {
    find_app_bundle(app_name)
        .map(|p| is_electron_bundle(&p))
        .unwrap_or(false)
}

fn is_electron_bundle(bundle_path: &str) -> bool {
    std::path::Path::new(bundle_path)
        .join("Contents/Frameworks/Electron Framework.framework")
        .exists()
}

/// Get the `.app` bundle path for a running app by PID.
fn bundle_path_for_pid(pid: i32) -> Option<String> {
    unsafe {
        let app: id = msg_send![
            class!(NSRunningApplication),
            runningApplicationWithProcessIdentifier: pid
        ];
        if app == nil {
            return None;
        }
        let url: id = msg_send![app, bundleURL];
        if url == nil {
            return None;
        }
        let path: id = msg_send![url, path];
        if path == nil {
            return None;
        }
        Some(nsstring_to_string(path))
    }
}

/// Search standard application directories for `<app_name>.app`.
fn find_app_bundle(app_name: &str) -> Option<String> {
    let dirs = [
        "/Applications",
        "/System/Applications",
        "/System/Applications/Utilities",
    ];
    let home_apps = std::env::var("HOME")
        .ok()
        .map(|h| std::path::PathBuf::from(h).join("Applications"));

    for dir in dirs
        .iter()
        .map(std::path::PathBuf::from)
        .chain(home_apps.into_iter())
    {
        let candidate = dir.join(format!("{}.app", app_name));
        if candidate.exists() {
            return Some(candidate.to_string_lossy().into_owned());
        }
    }
    None
}

unsafe fn nsstring_to_string(ns_string: id) -> String {
    let utf8_ptr: *const i8 = msg_send![ns_string, UTF8String];
    if utf8_ptr.is_null() {
        String::new()
    } else {
        std::ffi::CStr::from_ptr(utf8_ptr)
            .to_string_lossy()
            .into_owned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Tests that quit_app + list_apps doesn't return stale entries.
    ///
    /// Requires Calculator.app — run with: cargo test test_no_stale_entries -- --ignored --nocapture
    #[test]
    #[ignore]
    fn test_no_stale_entries_after_quit() {
        let app = "Calculator";

        // Ensure Calculator is running
        launch_app(app, &[], false).ok();
        std::thread::sleep(std::time::Duration::from_secs(2));

        let apps = list_apps();
        let found = apps.iter().find(|a| a.name == app);
        assert!(
            found.is_some(),
            "Calculator should appear in list_apps after launch"
        );
        let old_pid = found.unwrap().pid;
        println!("Calculator running at PID {}", old_pid);

        // Force quit to ensure immediate termination
        let result = quit_app(app, true);
        assert!(result.is_ok(), "quit_app should succeed");
        std::thread::sleep(std::time::Duration::from_millis(500));

        // Stale entry should be filtered by is_app_alive
        let apps = list_apps();
        let stale = apps.iter().find(|a| a.name == app && a.pid == old_pid);
        assert!(
            stale.is_none(),
            "Stale Calculator entry (PID {}) should be filtered from list_apps",
            old_pid
        );

        // is_app_running should return false
        assert!(
            !is_app_running(app),
            "is_app_running should return false after quit"
        );

        // quit_app on an already-dead app should return an error
        let result = quit_app(app, false);
        assert!(
            result.is_err(),
            "quit_app should error when app is not running"
        );

        // Relaunch and verify it appears (via CGWindowList supplement)
        launch_app(app, &[], false).ok();
        std::thread::sleep(std::time::Duration::from_secs(2));

        let apps = list_apps();
        let new = apps.iter().find(|a| a.name == app);
        assert!(
            new.is_some(),
            "Calculator should appear in list_apps after relaunch"
        );
        let new_pid = new.unwrap().pid;
        assert_ne!(
            old_pid, new_pid,
            "Relaunched Calculator should have a new PID"
        );
        println!("Calculator relaunched at PID {}", new_pid);

        // Cleanup
        quit_app(app, true).ok();
    }

    // MARK: - is_chrome_browser tests

    #[test]
    fn test_chrome_bundle_ids_detected() {
        for bid in CHROME_BUNDLE_IDS {
            assert!(
                is_chrome_browser(Some(bid), "irrelevant"),
                "Expected ChromeBrowser for bundle_id={}",
                bid,
            );
        }
    }

    #[test]
    fn test_non_chrome_bundle_id() {
        assert!(!is_chrome_browser(Some("com.apple.Safari"), "Safari"));
    }

    #[test]
    fn test_no_bundle_id() {
        assert!(!is_chrome_browser(None, "anything"));
    }

    // MARK: - is_electron_bundle tests

    #[test]
    fn test_electron_bundle_detected() {
        let dir = tempfile::tempdir().unwrap();
        let framework_path = dir
            .path()
            .join("Contents/Frameworks/Electron Framework.framework");
        std::fs::create_dir_all(&framework_path).unwrap();

        assert!(is_electron_bundle(dir.path().to_str().unwrap()));
    }

    #[test]
    fn test_non_electron_bundle() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!is_electron_bundle(dir.path().to_str().unwrap()));
    }

    #[test]
    fn test_nonexistent_path_not_electron() {
        assert!(!is_electron_bundle("/nonexistent/path"));
    }

    // MARK: - build_launch_command tests

    fn cmd_args(cmd: &std::process::Command) -> Vec<&std::ffi::OsStr> {
        cmd.get_args().collect()
    }

    #[test]
    fn build_launch_command_foreground_has_no_dash_g() {
        let cmd = build_launch_command("Calculator", &[], false);
        let args = cmd_args(&cmd);
        assert!(
            !args.contains(&std::ffi::OsStr::new("-g")),
            "Expected no -g flag when background=false, got: {:?}",
            args
        );
        assert!(args.contains(&std::ffi::OsStr::new("-a")));
        assert!(args.contains(&std::ffi::OsStr::new("Calculator")));
    }

    #[test]
    fn build_launch_command_background_includes_dash_g() {
        let cmd = build_launch_command("Calculator", &[], true);
        let args = cmd_args(&cmd);
        assert!(
            args.contains(&std::ffi::OsStr::new("-g")),
            "Expected -g flag when background=true, got: {:?}",
            args
        );
        assert!(args.contains(&std::ffi::OsStr::new("-a")));
        assert!(args.contains(&std::ffi::OsStr::new("Calculator")));
    }

    #[test]
    fn build_launch_command_background_dash_g_precedes_dash_a() {
        let cmd = build_launch_command("MyApp", &[], true);
        let args = cmd_args(&cmd);
        let g_pos = args
            .iter()
            .position(|a| *a == std::ffi::OsStr::new("-g"))
            .expect("-g must be present when background=true");
        let a_pos = args
            .iter()
            .position(|a| *a == std::ffi::OsStr::new("-a"))
            .expect("-a must always be present");
        assert!(
            g_pos < a_pos,
            "-g ({}) must come before -a ({}) in open invocation",
            g_pos,
            a_pos
        );
    }

    #[test]
    fn build_launch_command_background_with_extra_args() {
        let extra = vec!["--remote-debugging-port=9222".to_string()];
        let cmd = build_launch_command("Electron App", &extra, true);
        let args = cmd_args(&cmd);
        assert!(args.contains(&std::ffi::OsStr::new("-g")));
        assert!(args.contains(&std::ffi::OsStr::new("--args")));
        assert!(args.contains(&std::ffi::OsStr::new("--remote-debugging-port=9222")));
    }
}