mp_ensureConnection
Ensures the WeChat mini-program automation session is ready, automatically launching or reconnecting the developer tools if needed. Use this before any UI operation to guarantee a stable connection.
Instructions
确保小程序自动化会话就绪——这是连接链路的默认入口:在 mp_screenshot / page_* / element_* 之前先调它。它会自愈:会话未就绪时自动拉起微信开发者工具 / 重启 cli auto 并建立 automator 连接,不只是被动检查。
何时用:任何“先连上再操作”的场景,直接调本工具,不需要先 mp_diagnoseConnection(那是只读探测,可跳过)。 失败时:先读错误信息里的 Next step 引导,通常是 ① 带 reconnect=true 重试,或 ② 先 mp_listProjects 再带 projectSelection 重试——不要原样重试同一调用,也不要直接停下来找用户。
defaultProject 不在 recents 时,server 会用 defaultProject 重启 cli auto,第一次仍可能失败——此时按错误信息 retry 即可。
返回 JSON 含 mode / projectPath / defaultProjectPath / wsEndpoint / port / 内嵌 diagnosis / currentPage(已就绪可信,无需再调 mp_currentPage 校验)/ systemInfo。connect 模式无法确认 IDE 当前打开项目时 projectPath 会是 null,defaultProjectPath 仅表示持久化默认值,不会冒充当前项目。
参数:reconnect=true 强制丢弃现有会话重连(用于会话疑似失效/卡死);projectSelection 传 mp_listProjects 返回的 index / name / path 之一,用于在“需要选择项目”的提示后定向选中;所选路径会用于当前 ensure 并保存为默认项目,同名项目请用 index 或完整 path 消歧。connection 可选,覆盖默认连接参数。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| reconnect | No | ||
| connection | No | 可选连接覆盖(不传则用默认会话)。可用字段:mode(launch|connect)、cliPath、projectPath、wsEndpoint、timeout、port(【自动化端口】→ cli auto --auto-port,默认 9420;不是 IDE HTTP 服务端口,别把 IDE 服务端口传进来)、account、ticket、trustProject、args、cwd、autoClose、autoLaunch、launchTimeout、connectTimeout。 | |
| projectSelection | No |