axterminator 0.1.0

World's most superior macOS GUI testing framework with background testing
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
#!/usr/bin/env python3
"""AXTerminator MCP Server - World's First Background macOS GUI Testing.

WORLD FIRST: Test macOS apps without stealing focus - true background testing.

Performance:
    - Element access: 242µs (vs 500ms-2s competitors)
    - Full login test: 103ms (vs 6.6s Appium)
    - 60-100x faster than XCUITest/Appium

Features:
    - Background testing (no focus stealing!)
    - 7-strategy self-healing locators
    - Cross-app testing support
    - Works with any macOS app (SwiftUI, AppKit, Electron, WebView)

Created: 2026-01-21
"""

import asyncio
import base64
import logging
from typing import Any

from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import TextContent, Tool

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("axterminator")

# Try to import axterminator
try:
    import axterminator as ax

    AXTERMINATOR_AVAILABLE = True
except ImportError:
    ax = None  # type: ignore
    AXTERMINATOR_AVAILABLE = False
    logger.warning("axterminator not installed - install with: pip install axterminator")

# Store connected apps
_connected_apps: dict[str, Any] = {}

# Initialize MCP server
server = Server("axterminator")


def _get_app(app_name: str) -> Any:
    """Get a connected app by name."""
    if app_name not in _connected_apps:
        raise ValueError(f"App '{app_name}' not connected. Use ax_connect first.")
    return _connected_apps[app_name]


# ============================================================================
# TOOLS
# ============================================================================


@server.list_tools()
async def list_tools() -> list[Tool]:
    """List available tools."""
    return [
        Tool(
            name="ax_connect",
            description="""Connect to a macOS application for GUI testing.

Connect by bundle ID (recommended), name, or PID.
The app must be running and accessibility must be enabled.

Returns: Connection status and app info.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App identifier: bundle ID (com.apple.Safari), name (Safari), or PID (12345)",
                    },
                    "alias": {
                        "type": "string",
                        "description": "Optional alias to reference this app later (defaults to app name)",
                    },
                },
                "required": ["app"],
            },
        ),
        Tool(
            name="ax_find",
            description="""Find UI elements in a connected app.

Search by text, role, or attributes. Uses 7-strategy self-healing locators.
Returns element info including role, title, position, and available actions.

Query syntax:
- Simple text: "Save" (finds element with text "Save")
- By role: "role:AXButton" (finds buttons)
- Combined: "role:AXButton title:Save" (finds Save button)
- XPath-like: "//AXButton[@AXTitle='OK']" """,
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Element query (text, role:X, title:X, or XPath-like)",
                    },
                    "timeout_ms": {
                        "type": "integer",
                        "description": "Max time to wait for element (default: 5000ms)",
                        "default": 5000,
                    },
                },
                "required": ["app", "query"],
            },
        ),
        Tool(
            name="ax_click",
            description="""Click a UI element - IN BACKGROUND (no focus stealing!).

WORLD FIRST: Click without stealing focus from user's active work.
Use mode=focus only if the element requires it (rare).

Returns: Click result and element state after click.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Element to click (text, role:X, or XPath-like)",
                    },
                    "mode": {
                        "type": "string",
                        "description": "Action mode: 'background' (default, no focus steal) or 'focus'",
                        "enum": ["background", "focus"],
                        "default": "background",
                    },
                    "click_type": {
                        "type": "string",
                        "description": "Click type: 'single' (default), 'double', or 'right'",
                        "enum": ["single", "double", "right"],
                        "default": "single",
                    },
                },
                "required": ["app", "query"],
            },
        ),
        Tool(
            name="ax_type",
            description="""Type text into an element.

Note: Text input typically requires focus mode.
For setting values directly without typing, use ax_set_value instead.

Returns: Typing result.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Element to type into (text field query)",
                    },
                    "text": {
                        "type": "string",
                        "description": "Text to type",
                    },
                    "mode": {
                        "type": "string",
                        "description": "Action mode: 'focus' (default for typing) or 'background'",
                        "enum": ["background", "focus"],
                        "default": "focus",
                    },
                },
                "required": ["app", "query", "text"],
            },
        ),
        Tool(
            name="ax_set_value",
            description="""Set the value of an element directly (no keystroke simulation).

Faster than ax_type and works in background mode.
Use for text fields, sliders, etc.

Returns: Set result and new value.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Element to set value on",
                    },
                    "value": {
                        "type": "string",
                        "description": "Value to set",
                    },
                },
                "required": ["app", "query", "value"],
            },
        ),
        Tool(
            name="ax_get_value",
            description="""Get the current value of an element.

Works for text fields, labels, checkboxes, sliders, etc.
Returns the accessibility value attribute.

Returns: Element value and additional attributes.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Element to get value from",
                    },
                },
                "required": ["app", "query"],
            },
        ),
        Tool(
            name="ax_list_windows",
            description="""List all windows of a connected app.

Returns window titles, positions, sizes, and states.

Returns: List of window information.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                },
                "required": ["app"],
            },
        ),
        Tool(
            name="ax_screenshot",
            description="""Take a screenshot of an app or element.

Captures without stealing focus.
Returns base64-encoded PNG image.

Returns: Base64 screenshot data.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "query": {
                        "type": "string",
                        "description": "Optional: element to screenshot (entire app if not specified)",
                    },
                },
                "required": ["app"],
            },
        ),
        Tool(
            name="ax_wait_idle",
            description="""Wait for an app to become idle (no pending UI updates).

Useful before asserting state or taking screenshots.

Returns: Idle status and wait time.""",
            inputSchema={
                "type": "object",
                "properties": {
                    "app": {
                        "type": "string",
                        "description": "App name/alias from ax_connect",
                    },
                    "timeout_ms": {
                        "type": "integer",
                        "description": "Max time to wait (default: 5000ms)",
                        "default": 5000,
                    },
                },
                "required": ["app"],
            },
        ),
        Tool(
            name="ax_is_accessible",
            description="""Check if accessibility is enabled and working.

Returns: Accessibility status and any setup instructions needed.""",
            inputSchema={
                "type": "object",
                "properties": {},
            },
        ),
    ]


@server.call_tool()
async def call_tool(name: str, arguments: dict[str, Any]) -> list[TextContent]:
    """Handle tool calls."""
    if not AXTERMINATOR_AVAILABLE:
        return [
            TextContent(
                type="text",
                text="Error: axterminator not installed. Install with: pip install axterminator",
            )
        ]

    try:
        if name == "ax_is_accessible":
            is_enabled = ax.is_accessibility_enabled()
            if is_enabled:
                return [
                    TextContent(
                        type="text",
                        text="✅ Accessibility is enabled and working.",
                    )
                ]
            else:
                return [
                    TextContent(
                        type="text",
                        text="❌ Accessibility not enabled.\n\nTo enable:\n1. Open System Settings > Privacy & Security > Accessibility\n2. Add and enable the terminal app (Terminal, iTerm2, etc.)\n3. Restart the terminal",
                    )
                ]

        elif name == "ax_connect":
            app_id = arguments["app"]
            alias = arguments.get("alias")

            # Determine connection method
            if app_id.isdigit():
                app_obj = ax.app(pid=int(app_id))
            elif "." in app_id and app_id.count(".") >= 2:
                app_obj = ax.app(bundle_id=app_id)
            else:
                app_obj = ax.app(name=app_id)

            # Store with alias or app name
            key = alias or app_id
            _connected_apps[key] = app_obj

            return [
                TextContent(
                    type="text",
                    text=f"✅ Connected to '{key}'\nReady for GUI testing (background mode by default).",
                )
            ]

        elif name == "ax_find":
            app = _get_app(arguments["app"])
            query = arguments["query"]
            timeout = arguments.get("timeout_ms", 5000)

            element = app.wait_for_element(query, timeout_ms=timeout)

            if element:
                info = {
                    "found": True,
                    "role": getattr(element, "role", "unknown"),
                    "title": getattr(element, "title", ""),
                    "value": getattr(element, "value", ""),
                    "enabled": getattr(element, "enabled", True),
                    "position": getattr(element, "position", None),
                    "size": getattr(element, "size", None),
                }
                return [
                    TextContent(
                        type="text",
                        text=f"✅ Found element:\n{info}",
                    )
                ]
            else:
                return [
                    TextContent(
                        type="text",
                        text=f"❌ Element not found: '{query}' (timeout: {timeout}ms)",
                    )
                ]

        elif name == "ax_click":
            app = _get_app(arguments["app"])
            query = arguments["query"]
            mode_str = arguments.get("mode", "background")
            click_type = arguments.get("click_type", "single")

            mode = ax.BACKGROUND if mode_str == "background" else ax.FOCUS
            element = app.find(query)

            if not element:
                return [TextContent(type="text", text=f"❌ Element not found: '{query}'")]

            if click_type == "double":
                element.double_click(mode=mode)
            elif click_type == "right":
                element.right_click(mode=mode)
            else:
                element.click(mode=mode)

            return [
                TextContent(
                    type="text",
                    text=f"✅ Clicked '{query}' ({click_type}, {mode_str} mode)",
                )
            ]

        elif name == "ax_type":
            app = _get_app(arguments["app"])
            query = arguments["query"]
            text = arguments["text"]
            mode_str = arguments.get("mode", "focus")

            mode = ax.BACKGROUND if mode_str == "background" else ax.FOCUS
            element = app.find(query)

            if not element:
                return [TextContent(type="text", text=f"❌ Element not found: '{query}'")]

            element.type_text(text, mode=mode)

            return [
                TextContent(
                    type="text",
                    text=f"✅ Typed {len(text)} chars into '{query}' ({mode_str} mode)",
                )
            ]

        elif name == "ax_set_value":
            app = _get_app(arguments["app"])
            query = arguments["query"]
            value = arguments["value"]

            element = app.find(query)
            if not element:
                return [TextContent(type="text", text=f"❌ Element not found: '{query}'")]

            element.set_value(value)

            return [
                TextContent(
                    type="text",
                    text=f"✅ Set value on '{query}' to: {value}",
                )
            ]

        elif name == "ax_get_value":
            app = _get_app(arguments["app"])
            query = arguments["query"]

            element = app.find(query)
            if not element:
                return [TextContent(type="text", text=f"❌ Element not found: '{query}'")]

            value = getattr(element, "value", None)

            return [
                TextContent(
                    type="text",
                    text=f"Value of '{query}': {value}",
                )
            ]

        elif name == "ax_list_windows":
            app = _get_app(arguments["app"])
            windows = app.windows() if hasattr(app, "windows") else []

            if not windows:
                return [TextContent(type="text", text="No windows found")]

            result = "Windows:\n"
            for i, w in enumerate(windows):
                title = getattr(w, "title", f"Window {i}")
                result += f"  {i + 1}. {title}\n"

            return [TextContent(type="text", text=result)]

        elif name == "ax_screenshot":
            app = _get_app(arguments["app"])
            query = arguments.get("query")

            if query:
                element = app.find(query)
                if not element:
                    return [TextContent(type="text", text=f"❌ Element not found: '{query}'")]
                screenshot_data = element.screenshot()
            else:
                screenshot_data = app.screenshot()

            if screenshot_data:
                b64 = base64.b64encode(screenshot_data).decode("utf-8")
                return [
                    TextContent(
                        type="text",
                        text=f"Screenshot captured ({len(screenshot_data)} bytes)\nBase64: {b64[:100]}...",
                    )
                ]
            else:
                return [TextContent(type="text", text="❌ Failed to capture screenshot")]

        elif name == "ax_wait_idle":
            app = _get_app(arguments["app"])
            timeout = arguments.get("timeout_ms", 5000)

            # Wait for idle - implementation depends on axterminator API
            if hasattr(app, "wait_idle"):
                app.wait_idle(timeout_ms=timeout)
                return [TextContent(type="text", text="✅ App is idle")]
            else:
                await asyncio.sleep(0.1)  # Brief pause
                return [
                    TextContent(type="text", text="✅ Wait complete (no native idle detection)")
                ]

        else:
            return [TextContent(type="text", text=f"Unknown tool: {name}")]

    except Exception as e:
        logger.exception(f"Error in {name}")
        return [TextContent(type="text", text=f"❌ Error: {e}")]


async def main():
    """Run the MCP server."""
    async with stdio_server() as (read_stream, write_stream):
        await server.run(read_stream, write_stream, server.create_initialization_options())


if __name__ == "__main__":
    asyncio.run(main())