-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
317 lines (260 loc) · 7.96 KB
/
Copy pathserver.py
File metadata and controls
317 lines (260 loc) · 7.96 KB
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
from pathlib import Path
import base64
from io import BytesIO
import time
from PIL import Image
from mcp.server.fastmcp import FastMCP
from appium_driver import (
start_session,
get_driver,
stop_session
)
from utils import (
resize_image,
get_current_timestamp,
scale_bounds,
parse_ios_xml,
get_focused_element_id
)
from waiter import wait_for_element
from actions import execute_with_retry
mcp = FastMCP("appium-stdio-mcp")
def collect_artifacts(driver):
png = driver.get_screenshot_as_png()
xml = driver.page_source
img = Image.open(BytesIO(png))
buf = BytesIO()
img.save(buf, format="PNG")
return {
"screenshot_base64": base64.b64encode(buf.getvalue()).decode(),
"page_source_xml": xml
}
def get_bounds_by_element_id(element_id):
if not LAST_UI_TREE:
return None
for el in LAST_UI_TREE["elements"]:
if el["id"] == element_id:
return el["bounds"]
return None
@mcp.tool()
def start_default_ios_appium_session():
_platform = "iOS"
_server_url = "http://10.160.13.112:8080/grid"
_capabilities = {
"udid": "f67d7ce40691d9ab546d7362a4cc7a6182870de2",
"gads:clientSecret": "YmLZZlF6PnduXxSZvF3sTEeHIjT2XKKuA2UBoaqT4E0=",
"platformName": "iOS",
"automationName": "XCUITest"
}
start_session(_platform, _server_url, _capabilities)
return "default ios appium session started"
@mcp.tool()
def start_default_android_appium_session():
_platform = "Android"
_server_url = "http://10.160.13.112:8080/grid"
_capabilities = {
"udid": "8BEX18XP6",
"gads:clientSecret": "YmLZZlF6PnduXxSZvF3sTEeHIjT2XKKuA2UBoaqT4E0=",
"platformName": "Android",
"automationName": "UiAutomator2"
}
start_session(_platform, _server_url, _capabilities)
return "default android appium session started"
@mcp.tool()
def start_appium_session(platform: str, server_url: str, capabilities: dict):
start_session(platform, server_url, capabilities)
return "session started"
@mcp.tool()
def stop_appium_session():
stop_session()
return "session stopped"
@mcp.tool()
def get_screenshot(region: str = "full"):
driver = get_driver()
ts = get_current_timestamp()
base_dir = Path(__file__).resolve().parent
out_dir = base_dir / "screenshots"
out_dir.mkdir(exist_ok=True)
png_bytes = driver.get_screenshot_as_png()
img = Image.open(BytesIO(png_bytes))
if region == "focused":
focused_id = LAST_UI_TREE.get("focused") if LAST_UI_TREE else None
bounds = get_bounds_by_element_id(focused_id)
if bounds:
bounds = scale_bounds(
bounds,
img.size,
(LAST_UI_TREE["ui_width"], LAST_UI_TREE["ui_height"])
)
img = img.crop(bounds)
else:
region = "full" # fallback,永不炸
img = resize_image(img, max_long=768, max_short=384)
path = out_dir / f"{region}_{ts}.jpg"
img.save(path, "JPEG", quality=60, optimize=True)
return {
"type": "image_ref",
"region": region,
"path": str(path),
"width": img.width,
"height": img.height,
"focused_element_id": focused_id
}
@mcp.tool()
def get_ui_tree():
global LAST_UI_TREE
driver = get_driver()
source = driver.page_source # XML
elements, ui_w, ui_h = parse_ios_xml(source) # 你可以先只抽 button / textfield
focused = get_focused_element_id(elements)
LAST_UI_TREE = {
"focused": focused,
"elements": elements,
"ui_width": ui_w,
"ui_height": ui_h,
}
return {
"platform": "ios",
"focused_element_id": focused,
"elements": elements
}
@mcp.tool()
def tap(by: str, value: str):
d = get_driver()
def action():
el = wait_for_element(d, by, value)
el.click()
meta = execute_with_retry(d, action)
return {
"action": "tap",
"locator": f"{by}={value}",
"meta": meta,
**collect_artifacts(d)
}
@mcp.tool()
def tap_element(element_id: str):
bounds = get_bounds_by_element_id(element_id)
if not bounds:
return {"status": "failed", "reason": "element not found"}
x1, y1, x2, y2 = bounds
cx = (x1 + x2) // 2
cy = (y1 + y2) // 2
driver = get_driver()
driver.tap([(cx, cy)])
return {
"status": "ok",
"element_id": element_id,
"tap_point": [cx, cy]
}
@mcp.tool()
def launch_app(
platform: str,
bundle_id: str | None = None,
app_package: str | None = None,
activity: str | None = None,
):
"""
Launch an app by bundleId (iOS) or package/activity (Android)
"""
driver = get_driver()
if platform == "ios":
if not bundle_id:
return {"status": "failed", "reason": "bundle_id required for iOS"}
driver.activate_app(bundle_id)
elif platform == "android":
if not app_package:
return {"status": "failed", "reason": "app_package required for Android"}
if activity:
driver.start_activity(app_package, activity)
else:
driver.activate_app(app_package)
else:
return {"status": "failed", "reason": "unknown platform"}
return {
"status": "ok",
"platform": platform,
"bundle_id": bundle_id,
"app_package": app_package,
}
@mcp.tool()
def terminate_app(platform: str, bundle_id: str = None, app_package: str = None):
driver = get_driver()
if platform == "ios" and bundle_id:
driver.terminate_app(bundle_id)
elif platform == "android" and app_package:
driver.terminate_app(app_package)
else:
return {"status": "failed", "reason": "missing app id"}
return {"status": "ok"}
@mcp.tool()
def type_text(text: str, clear: bool = True):
driver = get_driver()
try:
el = driver.switch_to.active_element
if clear:
el.clear()
el.send_keys(text)
except Exception:
return {"status": "failed", "reason": "no active input"}
return {
"status": "ok",
"text": text,
}
@mcp.tool()
def swipe(direction: str, distance: float = 0.6):
"""
direction: up | down | left | right
distance: percentage of screen
"""
driver = get_driver()
size = driver.get_window_size()
w, h = size["width"], size["height"]
cx, cy = w // 2, h // 2
dx = dy = 0
if direction == "up":
dy = -int(h * distance)
elif direction == "down":
dy = int(h * distance)
elif direction == "left":
dx = -int(w * distance)
elif direction == "right":
dx = int(w * distance)
else:
return {"status": "failed", "reason": "invalid direction"}
driver.swipe(cx, cy, cx + dx, cy + dy, 300)
return {
"status": "ok",
"direction": direction,
"distance": distance,
}
@mcp.tool()
def scroll_until_text(text: str, max_swipes: int = 5):
for _ in range(max_swipes):
tree = get_ui_tree()
for el in tree["elements"]:
if text.lower() in el["label"].lower():
return {
"status": "found",
"element_id": el["id"],
}
swipe("up", 0.5)
return {"status": "not_found", "text": text}
@mcp.tool()
def assert_element_visible(label: str):
tree = get_ui_tree()
for el in tree["elements"]:
if label.lower() in el["label"].lower() and el["visible"]:
return {"status": "ok", "element_id": el["id"]}
return {"status": "failed", "reason": "not visible"}
@mcp.tool()
def wait_for_text(text: str, timeout: int = 10):
start = time.time()
while time.time() - start < timeout:
tree = get_ui_tree()
for el in tree["elements"]:
if text.lower() in el["label"].lower():
return {"status": "ok", "element_id": el["id"]}
time.sleep(1)
return {"status": "timeout", "text": text}
if __name__ == "__main__":
mcp.run()