get-current-activity
Retrieve the currently focused Android app or window using ADB's dumpsys command. Verify top activity in single-activity apps or identify active windows for debugging and automation tasks.
Instructions
Inspect current focused app/window via dumpsys window (mCurrentFocus/mFocusedApp). Useful even in single-activity apps to verify top window.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timeoutMs | No | Timeout per adb call in milliseconds |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"timeoutMs": {
"default": 5000,
"description": "Timeout per adb call in milliseconds",
"maximum": 15000,
"minimum": 1000,
"type": "integer"
}
},
"type": "object"
}
Implementation Reference
- src/tools/logcatTool.js:221-231 (handler)Handler function that executes adb shell dumpsys window, filters for current focus info (mCurrentFocus/mFocusedApp), trims to first 8 matching lines.async params => { const dump = await runAdbCommand(['shell', 'dumpsys', 'window'], params.timeoutMs); const lines = dump .split('\n') .filter(line => line.includes('mCurrentFocus') || line.includes('mFocusedApp')); const trimmed = lines.slice(0, 8).join('\n').trim(); if (!trimmed) { return { content: [{ type: 'text', text: 'No focus info found in dumpsys window.' }] }; } return { content: [{ type: 'text', text: trimmed }] }; }
- src/tools/logcatTool.js:59-67 (schema)Zod schema defining the input parameters for the get-current-activity tool (timeoutMs).const currentActivityInputSchema = z.object({ timeoutMs: z .number() .int() .min(1000) .max(15000) .default(5000) .describe('Timeout per adb call in milliseconds') });
- src/tools/logcatTool.js:213-232 (registration)Registration of the get-current-activity tool on the MCP server, specifying name, title, description, input schema, and inline handler function.server.registerTool( 'get-current-activity', { title: 'Get current activity/window focus', description: 'Inspect current focused app/window via dumpsys window (mCurrentFocus/mFocusedApp). Useful even in single-activity apps to verify top window.', inputSchema: currentActivityInputSchema }, async params => { const dump = await runAdbCommand(['shell', 'dumpsys', 'window'], params.timeoutMs); const lines = dump .split('\n') .filter(line => line.includes('mCurrentFocus') || line.includes('mFocusedApp')); const trimmed = lines.slice(0, 8).join('\n').trim(); if (!trimmed) { return { content: [{ type: 'text', text: 'No focus info found in dumpsys window.' }] }; } return { content: [{ type: 'text', text: trimmed }] }; } );
- src/tools/logcatTool.js:118-133 (helper)Utility function to run adb shell commands asynchronously with timeout, capturing stdout and handling errors appropriately.async function runAdbCommand(args, timeoutMs) { try { const { stdout } = await execFileAsync('adb', args, { timeout: timeoutMs, maxBuffer: 5 * 1024 * 1024 }); return stdout.trimEnd(); } catch (error) { const stderr = error && typeof error.stderr === 'string' ? error.stderr.trim() : ''; const message = [`adb ${args.join(' ')} failed`, error.message].filter(Boolean).join(': '); if (stderr) { throw new Error(`${message} | stderr: ${stderr}`); } throw new Error(message); } }