mp_ensureConnection
Verifies and maintains the automation session connection for WeChat Mini Program development, allowing optional configuration overrides or forced reconnection when needed.
Instructions
检查小程序自动化会话是否就绪。可选择覆盖连接设置或强制重连。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connection | No | ||
| reconnect | No |
Implementation Reference
- src/tools/application.ts:71-105 (handler)The main handler function (execute) that implements the tool logic: parses input parameters, uses manager.withMiniProgram to ensure connection (with optional override and reconnect), fetches current page and system info, formats and returns the connection details as text result.execute: async (rawArgs, context: ToolContext) => { const args = ensureConnectionParameters.parse(rawArgs ?? {}); const result = await manager.withMiniProgram<ContentResult>( context.log, { overrides: args.connection, reconnect: args.reconnect ?? false, }, async (miniProgram, config) => { const page = await miniProgram.currentPage(); let systemInfo: unknown; try { systemInfo = await miniProgram.systemInfo(); } catch { systemInfo = null; } return toTextResult( formatJson({ mode: config.mode, projectPath: config.projectPath, wsEndpoint: config.wsEndpoint, port: config.port, autoClose: config.autoClose ?? false, currentPage: page ? { path: page.path, query: page.query } : null, systemInfo, }) ); } ); return result; },
- src/tools/common.ts:21-24 (schema)Zod schema for the tool's input parameters, extending connectionContainerSchema with an optional 'reconnect' boolean flag.export const ensureConnectionParameters = connectionContainerSchema .extend({ reconnect: z.coerce.boolean().optional().default(false), });
- src/tools/application.ts:65-107 (registration)Factory function that defines and returns the tool object, including name, description, parameters schema, and execute handler. This tool is included in createApplicationTools.function createEnsureConnectionTool(manager: WeappAutomatorManager): AnyTool { return { name: "mp_ensureConnection", description: "检查小程序自动化会话是否就绪。可选择覆盖连接设置或强制重连。", parameters: ensureConnectionParameters, execute: async (rawArgs, context: ToolContext) => { const args = ensureConnectionParameters.parse(rawArgs ?? {}); const result = await manager.withMiniProgram<ContentResult>( context.log, { overrides: args.connection, reconnect: args.reconnect ?? false, }, async (miniProgram, config) => { const page = await miniProgram.currentPage(); let systemInfo: unknown; try { systemInfo = await miniProgram.systemInfo(); } catch { systemInfo = null; } return toTextResult( formatJson({ mode: config.mode, projectPath: config.projectPath, wsEndpoint: config.wsEndpoint, port: config.port, autoClose: config.autoClose ?? false, currentPage: page ? { path: page.path, query: page.query } : null, systemInfo, }) ); } ); return result; }, }; }
- src/tools/application.ts:53-63 (registration)Function that creates the array of application tools, including mp_ensureConnection via createEnsureConnectionTool.export function createApplicationTools( manager: WeappAutomatorManager ): AnyTool[] { return [ createEnsureConnectionTool(manager), createNavigateTool(manager), createScreenshotTool(manager), createCallWxMethodTool(manager), createGetConsoleLogsTool(manager), ]; }
- src/tools.ts:7-13 (registration)Top-level function that aggregates all tools, including application tools containing mp_ensureConnection.export function createTools(manager: WeappAutomatorManager): AnyTool[] { return [ ...createApplicationTools(manager), ...createPageTools(manager), ...createElementTools(manager), ]; }