android_start_emulator
Start an Android emulator with configurable options for AVD name, GPU mode, console port, and UI window visibility to facilitate mobile app development and testing.
Instructions
Start an Android emulator
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| avdName | Yes | AVD name to start | |
| options | No |
Implementation Reference
- src/tools/android.ts:560-621 (handler)The main handler function that validates input using Zod schema, checks if the emulator is already running, constructs command-line arguments based on provided options (noWindow, port, gpu), spawns the 'emulator' process in detached mode, tracks the process ID in the global runningEmulators map, sets up an exit event listener for cleanup, and returns success status with PID.handler: async (args: any) => { const validation = AndroidEmulatorStartSchema.safeParse(args); if (!validation.success) { throw new Error(`Invalid request: ${validation.error.message}`); } const { avdName, options = {} } = validation.data; // Check if emulator is already running if (runningEmulators.has(avdName)) { return { success: true, data: { avdName, status: 'already_running', pid: runningEmulators.get(avdName), message: 'Emulator is already running', }, }; } const emulator_args = ['-avd', avdName]; if (options.noWindow) { emulator_args.push('-no-window'); } if (options.port) { emulator_args.push('-port', options.port.toString()); } if (options.gpu) { emulator_args.push('-gpu', options.gpu); } // Start emulator in background const emulatorProcess = spawn('emulator', emulator_args, { detached: true, stdio: 'ignore', }); // Track the process runningEmulators.set(avdName, emulatorProcess.pid!); // Handle process exit emulatorProcess.on('exit', () => { runningEmulators.delete(avdName); }); // Unref to allow the parent process to exit emulatorProcess.unref(); return { success: true, data: { avdName, pid: emulatorProcess.pid, status: 'started', options, }, }; }
- src/tools/android.ts:87-94 (schema)Zod schema for validating input parameters to the android_start_emulator tool: requires avdName (non-empty string), optional options object with noWindow (boolean), port (number 5554-5682), gpu (enum of acceleration modes).const AndroidEmulatorStartSchema = z.object({ avdName: z.string().min(1), options: z.object({ noWindow: z.boolean().optional(), port: z.number().min(5554).max(5682).optional(), gpu: z.enum(['auto', 'host', 'swiftshader_indirect', 'angle_indirect', 'guest']).optional(), }).optional(), });
- src/tools/android.ts:538-622 (registration)Tool registration within the createAndroidTools function: sets the tool configuration in the tools Map with name, description, JSON inputSchema (equivalent to Zod schema), and references the handler function.tools.set('android_start_emulator', { name: 'android_start_emulator', description: 'Start an Android emulator', inputSchema: { type: 'object', properties: { avdName: { type: 'string', minLength: 1, description: 'AVD name to start' }, options: { type: 'object', properties: { noWindow: { type: 'boolean', description: 'Run without UI window' }, port: { type: 'number', minimum: 5554, maximum: 5682, description: 'Console port number' }, gpu: { type: 'string', enum: ['auto', 'host', 'swiftshader_indirect', 'angle_indirect', 'guest'], description: 'GPU acceleration mode' } } } }, required: ['avdName'] }, handler: async (args: any) => { const validation = AndroidEmulatorStartSchema.safeParse(args); if (!validation.success) { throw new Error(`Invalid request: ${validation.error.message}`); } const { avdName, options = {} } = validation.data; // Check if emulator is already running if (runningEmulators.has(avdName)) { return { success: true, data: { avdName, status: 'already_running', pid: runningEmulators.get(avdName), message: 'Emulator is already running', }, }; } const emulator_args = ['-avd', avdName]; if (options.noWindow) { emulator_args.push('-no-window'); } if (options.port) { emulator_args.push('-port', options.port.toString()); } if (options.gpu) { emulator_args.push('-gpu', options.gpu); } // Start emulator in background const emulatorProcess = spawn('emulator', emulator_args, { detached: true, stdio: 'ignore', }); // Track the process runningEmulators.set(avdName, emulatorProcess.pid!); // Handle process exit emulatorProcess.on('exit', () => { runningEmulators.delete(avdName); }); // Unref to allow the parent process to exit emulatorProcess.unref(); return { success: true, data: { avdName, pid: emulatorProcess.pid, status: 'started', options, }, }; } });