Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ Before drawing any conclusion from a tool call, verify:

### MaaMCP tool behavior notes (project-specific)

- __`ocr` / `screencap` accept `region=(x,y,w,h)`__ — small-region OCR is 4–8× faster, coords auto-compensated to original screen via maafw `JOCR(roi=..., roi_offset=...)` (no manual Python crop+offset needed). See commit `ea4b4bd` for the perf data.
- __`ocr` / `screencap` accept `region=(x,y,w,h)`__: regions use full controller screenshot coordinates. OCR uses `JOCR(roi=...)`; the framework restores the origin automatically. Do not repeat roi in roi_offset.
- __Shared coordinate basis__: Win32/ADB default to a 720-pixel short side. Connection parameter `target_short_side` accepts another positive integer or `None` (native size). `screencap` defaults to `resolution=None`, preserving controller image dimensions. OCR, actions, and Pipelines use that controller's coordinates. Non-720 modes do not automatically adapt existing 720p templates or ROIs.
- __Locating targets after cropping/resizing__: `screencap(..., include_metadata=True)` returns `path`, `coordinate_size`, `image_size`, and `image_to_controller`. Map image point `(u,v)` to `(u*sx+ox,v*sy+oy)` and round before acting. The default return remains a path string; metadata's `path` can be passed to `save_captured_image`.
- __Win32 `click` on Chromium/Electron windows can silent-fail__ — PostMessage mouse events are dropped, API returns True but no visual change. Keyboard events (`post_input_text`, `post_key_*`) still work because Chrome's keyboard handler is more lenient. Tool description has the full caveat.
- __`input_text` requires target focus__ — it sends keyboard events to the focused element. If `click` failed to focus, `input_text` lands elsewhere. Sequence: `click(target)` → `input_text(text)` in __immediate succession__, no `screencap/ocr` between.
- __Controller has a lifecycle__ — system cross-day, Chrome restart, or sleep/wake invalidates `controller_id`. Symptom: all operations return `None`/`False` silently. Fix: re-call `find_window_list()` + `connect_window()`.
Expand Down
4 changes: 3 additions & 1 deletion CLAUDE_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ OCR 模型和截图存储在平台特定的目录中:

### MaaMCP 工具行为注记(项目特定)

- __`ocr` / `screencap` 支持 `region=(x,y,w,h)`__——小区域 OCR 提速 4–8×,坐标由 maafw `JOCR(roi=..., roi_offset=...)` 自动补偿(不用 Python 侧手写 crop+offset)。性能数据见 commit `ea4b4bd`。
- __`ocr` / `screencap` 支持 `region=(x,y,w,h)`__:region 使用控制器完整截图坐标。OCR 使用 `JOCR(roi=...)`,框架自动补回原点;不要将 roi 重复赋给 roi_offset。
- __统一坐标基准__:Win32/ADB 默认短边 720,连接参数 `target_short_side` 可选其他正整数或 `None`(原尺寸)。`screencap` 默认 `resolution=None`,保留控制器截图尺寸;OCR、动作和 Pipeline 坐标均服从该控制器。非 720 模式不会自动适配既有 720p 模板及 ROI。
- __裁图/缩放后的定位__:`screencap(..., include_metadata=True)` 返回 `path`、`coordinate_size`、`image_size` 和 `image_to_controller`。图中 `(u,v)` 换算为 `(u*sx+ox,v*sy+oy)` 后取整再操作。默认仍返回路径字符串;元数据中的 `path` 可直接传给 `save_captured_image`。
- __Win32 `click` 对 Chromium/Electron 窗口可能静默失效__——PostMessage 鼠标消息被 Chromium 丢,API 返回 True 但界面无变化。键盘消息(`post_input_text` / `post_key_*`)仍能正常投递(Chrome 内部对键盘消息更宽松)。完整说明在工具描述里。
- __`input_text` 需要目标已聚焦__——它把字符投到当前焦点元素。`click` 失败没建立焦点的话,`input_text` 会投到错误位置。正确顺序:`click(target)` → `input_text(text)` __紧接__,中间不夹 screencap/ocr。
- __Controller 有生命周期__——系统跨天 / Chrome 重启 / 休眠唤醒后,`controller_id` 失效。症状:所有操作静默返回 None/False。处理:重新 `find_window_list()` + `connect_window()`。
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ Talk is cheap, 请看: **[🎞️ Bilibili 视频演示](https://www.bilibili.co
- `ocr` - 光学字符识别(高效,推荐优先使用)
- `screencap` - 屏幕截图(按需使用,token 开销大)

#### 截图与坐标约定

Win32 和 ADB 连接默认使用 `target_short_side=720`,保持画面比例。OCR 的 `region`、
返回框、点击、滑动和 Pipeline 均使用该控制器的完整截图坐标,不一定是设备物理坐标。
`screencap` 默认 `resolution=None`,不再额外缩放,整图坐标可直接用于操作。

连接时可传 `target_short_side=1080` 等正整数,或传 `None` 使用原尺寸。该选项会改变
整个控制器的坐标基准,模板和 Pipeline ROI 必须使用匹配的尺度;现有 720p 资源应保留
默认连接设置。依赖旧版 Win32 1080p 坐标的调用方可显式选择 `1080`。

`screencap` 默认仍返回路径字符串。裁图或显式设置 `resolution` 后需要定位操作时,
传入 `include_metadata=True`,返回以下信息:

| 字段 | 含义 |
| --- | --- |
| `path` | 图像绝对路径,也可传给 `save_captured_image` |
| `controller_id` / `coordinate_system` | 所属控制器及坐标空间(`controller`) |
| `coordinate_size` | 控制器完整截图 `[宽, 高]` |
| `image_size` | 保存图像 `[宽, 高]` |
| `image_to_controller.scale` | 图像到控制器的 `[sx, sy]` 比例 |
| `image_to_controller.offset` | 实际裁剪原点在控制器空间中的 `[ox, oy]` |

图中点 `(u, v)` 应换算为 `(round(u*sx+ox), round(v*sy+oy))` 再用于点击。
变换包含实际缩放尺寸、取整和裁剪边界修正,只对应本次截图;窗口大小或布局变化后应重新截图。
显式 `resolution` 只影响保存图像,不改变控制器坐标,也不能恢复之前下采样丢失的细节。

### 🎮 设备控制

- `click` - 点击指定坐标(支持多触点/鼠标按键选择、长按)
Expand Down
30 changes: 30 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ Talk is cheap, see: **[🎞️ Bilibili Video Demo](https://www.bilibili.com/vid
- `ocr` - Optical Character Recognition (efficient, recommended for priority use, OCR model auto-downloads on first use)
- `screencap` - Screenshot capture (use as needed, high token cost)

#### Screenshot Coordinates

Win32 and ADB connections default to `target_short_side=720`, preserving aspect ratio.
OCR regions and boxes, clicks, swipes, and Pipelines all use the controller's full screenshot
coordinates, which may differ from physical device coordinates. `screencap` now defaults to
`resolution=None`, preserving that image size so full-image coordinates can be used directly.

Pass a positive `target_short_side`, such as `1080`, when connecting, or `None` for native size.
This changes the coordinate basis of the entire controller: templates and Pipeline ROIs must
use the corresponding scale. Keep the default for existing 720p resources. Callers that rely
on the previous Win32 1080p coordinates can explicitly select `1080`.

`screencap` still returns a path string by default. When locating targets in cropped or explicitly
resized images, pass `include_metadata=True` to receive:

| Field | Meaning |
| --- | --- |
| `path` | Absolute image path, also accepted by `save_captured_image` |
| `controller_id` / `coordinate_system` | Owning controller and coordinate space (`controller`) |
| `coordinate_size` | Full controller screenshot `[width, height]` |
| `image_size` | Saved image `[width, height]` |
| `image_to_controller.scale` | Image-to-controller scale `[sx, sy]` |
| `image_to_controller.offset` | Actual crop origin in controller coordinates `[ox, oy]` |

Map an image point `(u, v)` to `(round(u*sx+ox), round(v*sy+oy))` before clicking.
The transform includes actual resized dimensions, rounding, and crop boundary clamping. It only
describes this capture; take another screenshot after the window size or layout changes.
Explicit `resolution` only affects the saved image, not controller coordinates, and cannot
restore detail lost in an earlier downsampling step.

### 🎮 Device Control

- `click` - Click at coordinates (supports multi-touch/mouse button selection, long press)
Expand Down
21 changes: 17 additions & 4 deletions maa_mcp/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def find_adb_device_list() -> list[str]:

参数:
- device_name: 目标设备名称,需通过 find_adb_device_list() 获取
- target_short_side: 控制器截图短边,默认 720;可设为其他正整数。
传 None 使用原始尺寸。OCR、动作和 Pipeline 均使用该控制器的截图坐标,
非 720 模式需要匹配的模板及 ROI。

返回值:
- 成功:返回控制器 ID(字符串),用于后续所有设备操作
Expand All @@ -49,7 +52,13 @@ def find_adb_device_list() -> list[str]:
控制器 ID 将用于后续的点击、滑动、截图等操作,请妥善保存。
""",
)
def connect_adb_device(device_name: str) -> Optional[str]:
def connect_adb_device(
device_name: str, target_short_side: Optional[int] = 720
) -> Optional[str]:
if target_short_side is not None and (
type(target_short_side) is not int or target_short_side <= 0
):
raise ValueError("target_short_side must be a positive integer or None")
device = object_registry.get(device_name)
if not device:
return None
Expand All @@ -61,9 +70,12 @@ def connect_adb_device(device_name: str) -> Optional[str]:
device.input_methods,
device.config,
)
# 设置默认截图短边为 720p
# 手机上文字/图标通常较大,不需要太高清
adb_controller.set_screenshot_target_short_side(720)
if target_short_side is None:
configured = adb_controller.set_screenshot_use_raw_size(True)
else:
configured = adb_controller.set_screenshot_target_short_side(target_short_side)
if not configured:
return None

if not adb_controller.post_connection().wait().succeeded:
return None
Expand All @@ -75,6 +87,7 @@ def connect_adb_device(device_name: str) -> Optional[str]:
"screencap_methods": device.screencap_methods,
"input_methods": device.input_methods,
"config": device.config,
"target_short_side": target_short_side,
}

controller_info_registry[controller_id] = ControllerInfo(
Expand Down
8 changes: 5 additions & 3 deletions maa_mcp/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
- 失败:返回 False

说明:
坐标系统以屏幕左上角为原点 (0, 0),X 轴向右,Y 轴向下。
坐标使用控制器完整截图空间,以左上角为原点 (0, 0),X 轴向右,Y 轴向下。
默认全屏 screencap 和 OCR 返回框可直接用于点击。显式缩放或裁剪的截图
坐标需先换算回完整截图空间;设备物理分辨率可能与该空间不同。

重要提示:
- 返回 True 不代表"视觉上生效"。Win32 控制器对 Chromium/Electron
Expand Down Expand Up @@ -67,7 +69,7 @@ def click(
- 失败:返回 False

说明:
坐标系统以屏幕左上角为原点 (0, 0),X 轴向右,Y 轴向下。
坐标使用控制器完整截图空间,与默认全屏 screencap、OCR 和 click 一致。
""",
)
def double_click(
Expand Down Expand Up @@ -118,7 +120,7 @@ def double_click(
- 失败:返回 False

说明:
- 坐标系统以屏幕左上角为原点 (0, 0)
- 坐标使用控制器完整截图空间,与默认全屏 screencap、OCR 和 click 一致
- duration 参数控制滑动速度,数值越大滑动越慢
- 起点、终点坐标由 AI 根据当前 OCR 识别结果和场景自行计算决定

Expand Down
6 changes: 6 additions & 0 deletions maa_mcp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class ControllerInfo:
- 使用 connect_adb_device(device_name) 或 connect_window(window_name) 建立连接
- 可连接多个设备/窗口,每个连接返回独立的控制器 ID

坐标约定:连接默认 target_short_side=720,可指定其他正整数或 None(原尺寸)。
OCR、动作和 Pipeline 都使用控制器完整截图坐标,不一定等于设备物理坐标。
screencap 默认不额外缩放;裁图或显式缩放后定位,请设 include_metadata=True,
按 image_to_controller 的 scale 和 offset 换算后再操作。
非 720 连接模式需要对应尺度的 Pipeline 模板和 ROI。

2. 串行自动化执行循环(流程 1 之后选择此流程进入串行模式)
⭐ 标准工作循环:截图(screencap) → OCR识别(ocr) → 分析内容 → 执行操作(click/swipe等) → 重复直到完成
- 调用 ocr(controller_id) 对指定设备进行屏幕截图和 OCR 识别
Expand Down
Loading
Loading