-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1359 lines (1106 loc) · 48 KB
/
Copy pathserver.py
File metadata and controls
1359 lines (1106 loc) · 48 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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Windows Automation MCP Server
Provides tools for controlling Windows applications via MCP protocol.
"""
import ctypes
import ctypes.wintypes
import json
import os
import subprocess
import sys
import time
import threading
from typing import Any, Optional
# Set DPI awareness before any GUI operations
try:
ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception:
try:
ctypes.windll.user32.SetProcessDPIAware()
except Exception:
pass
import pyautogui
from mcp.server.fastmcp import FastMCP, Context
from mcp.server.fastmcp.utilities.types import Image
# Disable pyautogui failsafe
pyautogui.FAILSAFE = False
# Create server
server = FastMCP(
name="win-automation",
instructions="Windows desktop automation tools for controlling applications, capturing screenshots, and simulating user input."
)
# Global element index storage
_element_indices: dict[int, dict[int, Any]] = {}
_element_index_lock = threading.Lock()
# State file path for persistent synchronization with tools.py
STATE_FILE = os.path.join(os.path.expanduser("~"), ".win-auto-state.json")
def _load_state() -> dict:
"""Load persistent state from disk."""
try:
if os.path.exists(STATE_FILE):
with open(STATE_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
pass
return {}
def _save_state(state: dict) -> None:
"""Save persistent state to disk."""
try:
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump(state, f, ensure_ascii=False, indent=2)
except Exception:
pass
def _resolve_target(hwnd: Optional[int]) -> int:
"""Resolve hwnd, falling back to state target_hwnd if None."""
if hwnd is not None:
state = _load_state()
state["target_hwnd"] = hwnd
_save_state(state)
return hwnd
state = _load_state()
target = state.get("target_hwnd")
if target and user32.IsWindow(target):
return target
raise ValueError(
"No hwnd provided and no valid target_hwnd in state. "
"Please find the window using list_windows first, then pass its hwnd."
)
def _get_dpi_scale(hwnd: int) -> float:
"""Return DPI scale factor relative to 96 DPI (1.0 = no scaling)."""
try:
if hasattr(user32, "GetDpiForWindow"):
user32.GetDpiForWindow.argtypes = [ctypes.c_void_p]
user32.GetDpiForWindow.restype = ctypes.c_uint
dpi = user32.GetDpiForWindow(hwnd)
if dpi > 0:
return dpi / 96.0
except Exception:
pass
# Fallback: Get DC and query LOGPIXELSX
try:
hdc = user32.GetDC(hwnd)
if hdc:
gdi32 = ctypes.windll.gdi32
gdi32.GetDeviceCaps.argtypes = [ctypes.c_void_p, ctypes.c_int]
gdi32.GetDeviceCaps.restype = ctypes.c_int
dpi = gdi32.GetDeviceCaps(hdc, 88) # LOGPIXELSX = 88
user32.ReleaseDC(hwnd, hdc)
if dpi > 0:
return dpi / 96.0
except Exception:
pass
return 1.0
def _get_window_rect(hwnd: int) -> ctypes.wintypes.RECT:
"""Get the visible window rect, excluding invisible shadow borders using DWM API."""
rect = ctypes.wintypes.RECT()
try:
dwmapi = ctypes.windll.dwmapi
DWMWA_EXTENDED_FRAME_BOUNDS = 9
hr = dwmapi.DwmGetWindowAttribute(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
ctypes.byref(rect),
ctypes.sizeof(rect)
)
if hr == 0:
return rect
except Exception:
pass
# Fallback to standard GetWindowRect
user32.GetWindowRect(hwnd, ctypes.byref(rect))
return rect
# Dangerous actions classification for safety checking
_DANGEROUS_ACTIONS = {
"delete": {"category": "Delete data", "description": "This action deletes files, messages, or data"},
"remove": {"category": "Delete data", "description": "This action deletes files, messages, or data"},
"uninstall": {"category": "Delete data", "description": "This action uninstalls software"},
"install": {"category": "Install software", "description": "This action installs new software"},
"run_setup": {"category": "Install software", "description": "This action runs an installer"},
"pay": {"category": "Financial", "description": "This action involves monetary transactions"},
"purchase": {"category": "Financial", "description": "This action involves monetary transactions"},
"subscribe": {"category": "Financial", "description": "This action creates a subscription"},
"transfer": {"category": "Financial", "description": "This action transfers funds"},
"create_account": {"category": "Account creation", "description": "This action creates a new account"},
"sign_up": {"category": "Account creation", "description": "This action signs up for a new account"},
"send_message": {"category": "Send messages", "description": "This action sends a message to others"},
"post_comment": {"category": "Send messages", "description": "This action posts a public comment"},
"reply": {"category": "Send messages", "description": "This action sends a reply"},
"change_password": {"category": "System settings", "description": "This action changes security settings"},
"modify_security": {"category": "System settings", "description": "This action changes security settings"},
"update_permissions": {"category": "System settings", "description": "This action changes permissions"},
}
def _check_safety(action: str) -> dict:
"""Check if an action requires user confirmation before proceeding."""
action_lower = action.lower().replace(" ", "_").replace("-", "_")
# Check direct match
if action_lower in _DANGEROUS_ACTIONS:
info = _DANGEROUS_ACTIONS[action_lower]
return {
"needs_confirmation": True,
"category": info["category"],
"description": info["description"],
"action": action,
}
# Check partial matches
for keyword, info in _DANGEROUS_ACTIONS.items():
if keyword in action_lower:
return {
"needs_confirmation": True,
"category": info["category"],
"description": info["description"],
"action": action,
}
return {"needs_confirmation": False, "action": action}
# Windows API constants
GWL_STYLE = -16
GWL_EXSTYLE = -20
WS_VISIBLE = 0x10000000
WS_EX_TOOLWINDOW = 0x00000080
WS_EX_APPWINDOW = 0x00040000
SW_RESTORE = 9
SW_SHOW = 5
SW_SHOWNORMAL = 1
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
MAX_PATH = 265
PW_RENDERFULLCONTENT = 0x00000002
# Windows API functions
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
shell32 = ctypes.windll.shell32
# Function prototypes
user32.EnumWindows.argtypes = [ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_void_p), ctypes.c_void_p]
user32.EnumWindows.restype = ctypes.c_bool
user32.IsWindowVisible.argtypes = [ctypes.c_void_p]
user32.IsWindowVisible.restype = ctypes.c_bool
user32.GetWindowTextW.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_int]
user32.GetWindowTextW.restype = ctypes.c_int
user32.GetWindowThreadProcessId.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_ulong)]
user32.GetWindowThreadProcessId.restype = ctypes.c_ulong
user32.ShowWindow.argtypes = [ctypes.c_void_p, ctypes.c_int]
user32.ShowWindow.restype = ctypes.c_bool
user32.SetForegroundWindow.argtypes = [ctypes.c_void_p]
user32.SetForegroundWindow.restype = ctypes.c_bool
user32.GetWindowRect.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.wintypes.RECT)]
user32.GetWindowRect.restype = ctypes.c_bool
user32.GetClientRect.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.wintypes.RECT)]
user32.GetClientRect.restype = ctypes.c_bool
user32.ClientToScreen.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.wintypes.POINT)]
user32.ClientToScreen.restype = ctypes.c_bool
user32.GetWindowLongW.argtypes = [ctypes.c_void_p, ctypes.c_int]
user32.GetWindowLongW.restype = ctypes.c_long
user32.PrintWindow.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint]
user32.PrintWindow.restype = ctypes.c_bool
user32.GetDC.argtypes = [ctypes.c_void_p]
user32.GetDC.restype = ctypes.c_void_p
user32.ReleaseDC.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
user32.ReleaseDC.restype = ctypes.c_int
# DWM Frame Bounds API
try:
dwmapi = ctypes.windll.dwmapi
dwmapi.DwmGetWindowAttribute.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p, ctypes.c_ulong]
dwmapi.DwmGetWindowAttribute.restype = ctypes.c_long
except Exception:
pass
kernel32.OpenProcess.argtypes = [ctypes.c_ulong, ctypes.c_bool, ctypes.c_ulong]
kernel32.OpenProcess.restype = ctypes.c_void_p
kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
kernel32.CloseHandle.restype = ctypes.c_bool
kernel32.QueryFullProcessImageNameW.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_wchar_p, ctypes.POINTER(ctypes.c_ulong)]
kernel32.QueryFullProcessImageNameW.restype = ctypes.c_bool
gdi32 = ctypes.windll.gdi32
gdi32.CreateCompatibleDC.argtypes = [ctypes.c_void_p]
gdi32.CreateCompatibleDC.restype = ctypes.c_void_p
gdi32.CreateCompatibleBitmap.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
gdi32.CreateCompatibleBitmap.restype = ctypes.c_void_p
gdi32.SelectObject.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
gdi32.SelectObject.restype = ctypes.c_void_p
gdi32.DeleteObject.argtypes = [ctypes.c_void_p]
gdi32.DeleteObject.restype = ctypes.c_bool
gdi32.DeleteDC.argtypes = [ctypes.c_void_p]
gdi32.DeleteDC.restype = ctypes.c_bool
gdi32.GetDIBits.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint]
gdi32.GetDIBits.restype = ctypes.c_int
gdi32.BitBlt.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_uint32]
gdi32.BitBlt.restype = ctypes.c_bool
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [
("biSize", ctypes.c_uint32),
("biWidth", ctypes.c_int32),
("biHeight", ctypes.c_int32),
("biPlanes", ctypes.c_uint16),
("biBitCount", ctypes.c_uint16),
("biCompression", ctypes.c_uint32),
("biSizeImage", ctypes.c_uint32),
("biXPelsPerMeter", ctypes.c_int32),
("biYPelsPerMeter", ctypes.c_int32),
("biClrUsed", ctypes.c_uint32),
("biClrImportant", ctypes.c_uint32),
]
class BITMAPINFO(ctypes.Structure):
_fields_ = [
("bmiHeader", BITMAPINFOHEADER),
("bmiColors", ctypes.c_uint32 * 3),
]
def _get_process_name(pid: int) -> str:
"""Get process executable path from PID."""
try:
h = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not h:
return ""
buf = ctypes.create_unicode_buffer(MAX_PATH)
size = ctypes.c_ulong(MAX_PATH)
if kernel32.QueryFullProcessImageNameW(h, 0, buf, ctypes.byref(size)):
kernel32.CloseHandle(h)
return buf.value
kernel32.CloseHandle(h)
except Exception:
pass
return ""
def _enum_windows() -> list[dict[str, Any]]:
"""Enumerate all visible windows."""
results = []
@ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_void_p)
def callback(hwnd, _):
try:
if not user32.IsWindowVisible(hwnd):
return True
style = user32.GetWindowLongW(hwnd, GWL_STYLE)
ex_style = user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
# Skip tool windows unless they have app window style
if (ex_style & WS_EX_TOOLWINDOW) and not (ex_style & WS_EX_APPWINDOW):
return True
# Get title
buf = ctypes.create_unicode_buffer(512)
user32.GetWindowTextW(hwnd, buf, 512)
title = buf.value.strip()
if not title:
return True
# Get PID
pid = ctypes.c_ulong()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
# Get process path
proc_path = _get_process_name(pid.value)
results.append({
"hwnd": hwnd,
"title": title,
"pid": pid.value,
"process_path": proc_path,
"process_name": os.path.basename(proc_path) if proc_path else "",
})
except Exception:
pass
return True
user32.EnumWindows(callback, None)
return results
def _capture_window_screenshot(hwnd: int, max_width: int = 1280, format: str = "jpeg") -> bytes:
"""Capture window screenshot using PrintWindow or BitBlt fallback. Returns image bytes."""
from PIL import Image as PILImage
import io
# Physical bounds (visible bounds via DWM)
rect = _get_window_rect(hwnd)
win_w = rect.right - rect.left
win_h = rect.bottom - rect.top
if win_w <= 0 or win_h <= 0:
raise ValueError(f"Invalid window dimensions: {win_w}x{win_h}")
# Logical bounds (DPI virtualized bounds via GetWindowRect)
logical_rect = ctypes.wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(logical_rect))
log_w = logical_rect.right - logical_rect.left
log_h = logical_rect.bottom - logical_rect.top
# Create device context and bitmap
hdc = user32.GetDC(hwnd)
hdc_mem = gdi32.CreateCompatibleDC(hdc)
# Use logical size for PrintWindow to prevent black/empty borders
hbitmap = gdi32.CreateCompatibleBitmap(hdc, log_w, log_h)
old_bmp = gdi32.SelectObject(hdc_mem, hbitmap)
# Capture window
captured = user32.PrintWindow(hwnd, hdc_mem, PW_RENDERFULLCONTENT)
if not captured:
# Fallback: try without PW_RENDERFULLCONTENT
captured = user32.PrintWindow(hwnd, hdc_mem, 0)
if captured:
width = log_w
height = log_h
else:
# Fallback: BitBlt from screen DC (physical size)
gdi32.SelectObject(hdc_mem, old_bmp)
gdi32.DeleteObject(hbitmap)
gdi32.DeleteDC(hdc_mem)
user32.ReleaseDC(hwnd, hdc)
hdc_screen = user32.GetDC(0)
hdc_mem = gdi32.CreateCompatibleDC(hdc_screen)
hbitmap = gdi32.CreateCompatibleBitmap(hdc_screen, win_w, win_h)
old_bmp = gdi32.SelectObject(hdc_mem, hbitmap)
gdi32.BitBlt(hdc_mem, 0, 0, win_w, win_h,
hdc_screen, rect.left, rect.top, 0x00CC0020)
user32.ReleaseDC(0, hdc_screen)
width = win_w
height = win_h
# Prepare bitmap info
bmi = BITMAPINFO()
bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
bmi.bmiHeader.biWidth = width
bmi.bmiHeader.biHeight = -height # Top-down
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 32
bmi.bmiHeader.biCompression = 0 # BI_RGB
# Get pixel data
buf_size = width * height * 4
buf = ctypes.create_string_buffer(buf_size)
gdi32.GetDIBits(hdc_mem, hbitmap, 0, height, buf, ctypes.byref(bmi), 0)
# Convert to PIL Image
img = PILImage.frombuffer("RGBA", (width, height), buf, "raw", "BGRA", 0, 1)
img = img.convert("RGB")
# Scale down if needed
if width > max_width:
ratio = max_width / width
new_height = int(height * ratio)
img = img.resize((max_width, new_height), PILImage.LANCZOS)
# Convert to bytes (JPEG is 10x smaller and more compatible, fallback to PNG)
output = io.BytesIO()
if format.lower() in ("jpeg", "jpg"):
img.save(output, format="JPEG", quality=85)
else:
img.save(output, format="PNG", optimize=True)
img_data = output.getvalue()
# Cleanup
gdi32.SelectObject(hdc_mem, old_bmp)
gdi32.DeleteObject(hbitmap)
gdi32.DeleteDC(hdc_mem)
user32.ReleaseDC(hwnd, hdc)
return img_data
def _activate_window(hwnd: int) -> bool:
"""Bring window to foreground."""
try:
# Restore if minimized
user32.ShowWindow(hwnd, SW_RESTORE)
time.sleep(0.1)
return user32.SetForegroundWindow(hwnd)
except Exception:
return False
def _get_client_offset(hwnd: int) -> tuple[int, int]:
"""Get client area offset from window rect."""
window_rect = ctypes.wintypes.RECT()
client_rect = ctypes.wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(window_rect))
user32.GetClientRect(hwnd, ctypes.byref(client_rect))
point = ctypes.wintypes.POINT(0, 0)
user32.ClientToScreen(hwnd, ctypes.byref(point))
return (point.x - window_rect.left, point.y - window_rect.top)
def _build_accessibility_tree(hwnd: int, max_depth: int = 10, max_elements: int = 500) -> tuple[str, dict[int, Any], str, str]:
"""Build accessibility tree using UI Automation. Returns (tree_text, index_map, focused_element, selected_text)."""
try:
import comtypes
import comtypes.client
import comtypes.gen.UIAutomationClient as UIAClient
# Ensure COM is initialized for the current thread
try:
comtypes.CoInitialize()
except Exception:
pass
# Create UI Automation instance
uia = comtypes.client.CreateObject(
'{ff48dba4-60ef-4201-aa87-54103eef594e}',
interface=UIAClient.IUIAutomation
)
# Get element from handle
element = uia.ElementFromHandle(hwnd)
if not element:
return "No accessible content", {}, "None", ""
index_map = {}
lines = []
current_index = [0] # Mutable counter
def walk(elem, depth, parent_info=""):
if depth > max_depth or current_index[0] >= max_elements:
return
idx = current_index[0]
current_index[0] += 1
index_map[idx] = elem
try:
name = elem.CurrentName or ""
control_type = elem.CurrentLocalizedControlType or ""
class_name = elem.CurrentClassName or ""
# Get patterns
patterns = []
try:
# Check for Value pattern
vp = elem.GetCurrentPattern(10002) # UIA_ValuePatternId
if vp:
patterns.append("Value")
except Exception:
pass
try:
# Check for Invoke pattern
ip = elem.GetCurrentPattern(10000) # UIA_InvokePatternId
if ip:
patterns.append("Invoke")
except Exception:
pass
try:
# Check for Toggle pattern
tp = elem.GetCurrentPattern(10001) # UIA_TogglePatternId
if tp:
patterns.append("Toggle")
except Exception:
pass
try:
# Check for SelectionItem pattern
sp = elem.GetCurrentPattern(10010) # UIA_SelectionItemPatternId
if sp:
patterns.append("SelectionItem")
except Exception:
pass
# Build line
indent = " " * depth
pattern_str = f" [{', '.join(patterns)}]" if patterns else ""
value_str = ""
if "Value" in patterns:
try:
vp = elem.GetCurrentPattern(10002)
value_str = f' value="{vp.CurrentValue}"'
except Exception:
pass
name_display = f'"{name}"' if name else '""'
line = f"{indent}[{idx}] {name_display} ({control_type}){pattern_str}{value_str}"
lines.append(line)
# Walk children
walker = uia.CreateTreeWalker(uia.RawViewCondition)
try:
child = walker.GetFirstChildElement(elem)
while child:
walk(child, depth + 1)
next_child = walker.GetNextSiblingElement(child)
child = next_child
except Exception:
pass
except Exception:
pass
walk(element, 0)
tree_text = "\n".join(lines) if lines else "No accessible elements found"
# --- Extract focused element and selected text ---
focused_element_str = "None"
selected_text_str = ""
try:
focused = uia.GetFocusedElement()
if focused:
f_name = focused.CurrentName or ""
f_type = focused.CurrentLocalizedControlType or ""
f_class = focused.CurrentClassName or ""
focused_element_str = f'"{f_name}" ({f_type}) Class={f_class}'
# Try TextPattern first for selection
try:
tp = focused.GetCurrentPattern(10014) # UIA_TextPatternId
if tp:
selection = tp.GetSelection()
if selection and selection.Length > 0:
selected_ranges = []
for idx_sel in range(selection.Length):
r = selection.GetElement(idx_sel)
selected_ranges.append(r.GetText(-1))
selected_text_str = "\n".join(selected_ranges)
except Exception:
pass
# Fallback to ValuePattern if no selection found
if not selected_text_str:
try:
vp = focused.GetCurrentPattern(10002) # UIA_ValuePatternId
if vp:
selected_text_str = vp.CurrentValue or ""
except Exception:
pass
except Exception:
pass
return tree_text, index_map, focused_element_str, selected_text_str
except ImportError:
return "comtypes not installed - accessibility tree unavailable", {}, "None", ""
except Exception as e:
return f"Error building accessibility tree: {e}", {}, "None", ""
def _get_element_by_index(hwnd: int, index: int) -> Any:
"""Get element from index map."""
with _element_index_lock:
hwnd_map = _element_indices.get(hwnd, {})
return hwnd_map.get(index)
def _set_clipboard_text(text: str) -> None:
"""Set clipboard text using Windows API."""
CF_UNICODETEXT = 13
user32.OpenClipboard(0)
user32.EmptyClipboard()
# Allocate memory for text
text_bytes = text.encode("utf-16-le") + b"\x00\x00"
h_mem = kernel32.GlobalAlloc(0x0042, len(text_bytes)) # GMEM_MOVEABLE | GMEM_ZEROINIT
p_mem = kernel32.GlobalLock(h_mem)
ctypes.memmove(p_mem, text_bytes, len(text_bytes))
kernel32.GlobalUnlock(h_mem)
user32.SetClipboardData(CF_UNICODETEXT, h_mem)
user32.CloseClipboard()
def _paste_text(text: str) -> None:
"""Paste text via clipboard."""
_set_clipboard_text(text)
time.sleep(0.05)
pyautogui.hotkey("ctrl", "v")
time.sleep(0.05)
def _parse_key_string(key_str: str) -> list[str]:
"""Parse key string like 'Control_L+c' into pyautogui key list."""
# Map X11 keysym names to pyautogui names
KEY_MAP = {
"Control_L": "ctrl",
"Control_R": "ctrl",
"Shift_L": "shift",
"Shift_R": "shift",
"Alt_L": "alt",
"Alt_R": "alt",
"Return": "enter",
"KP_Enter": "enter",
"Escape": "escape",
"Tab": "tab",
"BackSpace": "backspace",
"Delete": "delete",
"space": "space",
"Up": "up",
"Down": "down",
"Left": "left",
"Right": "right",
"Home": "home",
"End": "end",
"Page_Up": "pageup",
"Page_Down": "pagedown",
"F1": "f1",
"F2": "f2",
"F3": "f3",
"F4": "f4",
"F5": "f5",
"F6": "f6",
"F7": "f7",
"F8": "f8",
"F9": "f9",
"F10": "f10",
"F11": "f11",
"F12": "f12",
"period": ".",
"greater": ">",
"less": "<",
"comma": ",",
"slash": "/",
"question": "?",
}
parts = key_str.replace(" ", "").split("+")
result = []
for part in parts:
result.append(KEY_MAP.get(part, part.lower()))
return result
# ========== MCP Tools ==========
@server.tool()
async def list_apps() -> str:
"""List running applications with their visible windows.
Returns a formatted list showing each application's windows with HWND, title, and PID.
Use the HWND values for other tools like get_window_state, click, etc.
"""
try:
windows = _enum_windows()
# Group by process
by_process: dict[str, list[dict]] = {}
for w in windows:
key = w.get("process_name") or "(unknown)"
by_process.setdefault(key, []).append(w)
lines = []
for proc_name, wins in sorted(by_process.items()):
lines.append(f"\nApplication: {proc_name}")
for w in wins:
lines.append(f" [{w['hwnd']}] {w['title']} (PID={w['pid']})")
return "\n".join(lines) if lines else "No visible windows found"
except Exception as e:
return f"Error listing apps: {e}"
@server.tool()
async def list_windows() -> str:
"""List all open visible windows.
Returns a flat list of all visible windows with HWND, title, and process info.
"""
try:
windows = _enum_windows()
lines = []
for w in windows:
lines.append(f"[{w['hwnd']}] {w['title']} - {w.get('process_name', 'unknown')} (PID={w['pid']})")
return "\n".join(lines) if lines else "No visible windows found"
except Exception as e:
return f"Error listing windows: {e}"
@server.tool()
async def get_window(hwnd: Optional[int] = None) -> str:
"""Get information about a specific window by HWND.
Args:
hwnd: Window handle from list_apps or list_windows. If not provided, falls back to the active target.
Returns window title, position, size, and state.
"""
try:
hwnd = _resolve_target(hwnd)
if not user32.IsWindow(hwnd):
return f"Window {hwnd} no longer exists"
# Get title
buf = ctypes.create_unicode_buffer(512)
user32.GetWindowTextW(hwnd, buf, 512)
title = buf.value
# Get rect
rect = ctypes.wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rect))
# Get PID
pid = ctypes.c_ulong()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
proc_path = _get_process_name(pid.value)
return json.dumps({
"hwnd": hwnd,
"title": title,
"pid": pid.value,
"process_path": proc_path,
"rect": {
"left": rect.left,
"top": rect.top,
"right": rect.right,
"bottom": rect.bottom,
"width": rect.right - rect.left,
"height": rect.bottom - rect.top,
}
}, indent=2, ensure_ascii=False)
except Exception as e:
return f"Error getting window: {e}"
@server.tool()
async def launch_app(path_or_name: str) -> str:
"""Launch an application by name or path.
Args:
path_or_name: Application name (e.g. "notepad") or full path (e.g. "C:\\Windows\\notepad.exe")
Returns the HWND of the launched application's main window, or error message.
"""
try:
# Get current windows before launch
before = {w["hwnd"] for w in _enum_windows()}
# Launch via ShellExecute
result = shell32.ShellExecuteW(None, "open", path_or_name, None, None, SW_SHOWNORMAL)
if result <= 32:
return f"Failed to launch '{path_or_name}' (error code: {result})"
# Wait for window to appear
time.sleep(1.0)
# Find new windows
after = _enum_windows()
new_windows = [w for w in after if w["hwnd"] not in before]
if new_windows:
w = new_windows[0]
return f"Launched '{path_or_name}' -> [{w['hwnd']}] {w['title']}"
else:
return f"Launched '{path_or_name}' but no new window detected. Check list_windows."
except Exception as e:
return f"Error launching app: {e}"
@server.tool()
async def get_window_state(hwnd: Optional[int] = None, include_screenshot: bool = True, include_accessibility: bool = False, max_screenshot_width: int = 1280) -> list:
"""Capture the current state of a window.
Args:
hwnd: Window handle. If not provided, falls back to the active target.
include_screenshot: Whether to capture a screenshot (default True)
include_accessibility: Whether to build accessibility tree with element indexes (default False)
max_screenshot_width: Maximum screenshot width in pixels (default 1280)
Returns screenshot image and/or accessibility tree with element indexes, focused element, and selected text.
Element indexes are ephemeral - they're only valid until the next get_window_state call.
"""
try:
hwnd = _resolve_target(hwnd)
if not user32.IsWindow(hwnd):
return [f"Window {hwnd} no longer exists"]
result = []
# Screenshot
if include_screenshot:
try:
img_data = _capture_window_screenshot(hwnd, max_screenshot_width, format="jpeg")
img = Image(data=img_data, format="jpeg")
# Add text descriptor to prevent 400 "text is not set" in strict clients/models like MiMo
result.append("Captured screenshot of target window state:")
result.append(img)
except Exception as e:
result.append(f"Screenshot error: {e}")
# Accessibility tree
if include_accessibility:
try:
tree_text, index_map, focused_element, selected_text = _build_accessibility_tree(hwnd)
# Store index map
with _element_index_lock:
_element_indices[hwnd] = index_map
accessibility_summary = [
f"Accessibility Tree (indexes refreshed, {len(index_map)} elements):",
f"Focused UI Element: {focused_element}"
]
if selected_text:
accessibility_summary.append(f"Selected/Value Text: \"{selected_text}\"")
accessibility_summary.append("\n" + tree_text)
result.append("\n".join(accessibility_summary))
except Exception as e:
result.append(f"Accessibility tree error: {e}")
if not result:
return ["No data captured. Set include_screenshot or include_accessibility to True."]
return result
except Exception as e:
return [f"Error capturing window state: {e}"]
@server.tool()
async def click(hwnd: Optional[int] = None, x: Optional[int] = None, y: Optional[int] = None, index: Optional[int] = None, button: str = "left", clicks: int = 1, screenshot_width: Optional[int] = None, screenshot_height: Optional[int] = None) -> str:
"""Click in a window at coordinates or on an element by index.
Args:
hwnd: Window handle. If not provided, falls back to the active target.
x: X coordinate in window (client area). Required if index not provided.
y: Y coordinate in window (client area). Required if index not provided.
index: Element index from get_window_state accessibility tree. Takes priority over x/y.
button: Mouse button - "left", "right", or "middle" (default "left")
clicks: Number of clicks (default 1)
screenshot_width: Width of screenshot clicked on, for dynamic coordinate scaling.
screenshot_height: Height of screenshot clicked on, for dynamic coordinate scaling.
Coordinates are relative to the window's client area.
Element indexes are ephemeral - refresh with get_window_state if stale.
"""
try:
hwnd = _resolve_target(hwnd)
if not user32.IsWindow(hwnd):
return f"Window {hwnd} no longer exists"
_activate_window(hwnd)
time.sleep(0.1)
if index is not None:
# Click by element index
elem = _get_element_by_index(hwnd, index)
if not elem:
return f"Element index {index} not found. Call get_window_state to refresh indexes."
try:
rect = elem.CurrentBoundingRectangle
cx = (rect.left + rect.right) // 2
cy = (rect.top + rect.bottom) // 2
# In DPI-aware process, pyautogui click expects physical screen coordinates
pyautogui.click(cx, cy, button=button, clicks=clicks)
return f"Clicked element [{index}] at screen ({cx}, {cy})"
except Exception as e:
return f"Error clicking element [{index}]: {e}"
elif x is not None and y is not None:
# Click by coordinates - scale from screenshot space to logical window, then add DWM offset
rect = _get_window_rect(hwnd)
logical_rect = ctypes.wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(logical_rect))
log_w = logical_rect.right - logical_rect.left
log_h = logical_rect.bottom - logical_rect.top
ss_w = screenshot_width
ss_h = screenshot_height
if not ss_w:
ss_w = 1280 if log_w > 1280 else log_w
if not ss_h:
ss_h = int(log_h * 1280 / log_w) if log_w > 1280 else log_h
real_x = int(x * log_w / ss_w)
real_y = int(y * log_h / ss_h)
screen_x = rect.left + real_x
screen_y = rect.top + real_y
# In DPI-aware process, pyautogui click expects physical screen coordinates
pyautogui.click(screen_x, screen_y, button=button, clicks=clicks)
return f"Clicked at window logical ({real_x}, {real_y}) -> screen physical ({screen_x}, {screen_y}) [scaled from screenshot ({x}, {y})]"
else:
return "Must provide either (x, y) coordinates or element index"
except Exception as e:
return f"Error clicking: {e}"
@server.tool()
async def type_text(text: str, hwnd: Optional[int] = None) -> str:
"""Type text into a window.
Args:
text: Text to type (supports Unicode/Chinese characters)
hwnd: Window handle. If not provided, falls back to the active target.
Activates the window first, then pastes text via clipboard.
"""
try:
hwnd = _resolve_target(hwnd)
if not user32.IsWindow(hwnd):
return f"Window {hwnd} no longer exists"
_activate_window(hwnd)
time.sleep(0.1)
_paste_text(text)
return f"Typed {len(text)} characters"
except Exception as e:
return f"Error typing text: {e}"
@server.tool()
async def press_key(keys: str, hwnd: Optional[int] = None) -> str:
"""Press a key or keyboard shortcut.
Args:
keys: Key combo e.g. "Control_L+c"
hwnd: Window handle. If not provided, falls back to the active target.
"""
try:
hwnd = _resolve_target(hwnd)
if not user32.IsWindow(hwnd):
return f"Window {hwnd} no longer exists"
_activate_window(hwnd)
time.sleep(0.1)
key_list = _parse_key_string(keys)
if len(key_list) == 1: