get_current_activity
Retrieve the currently displayed Android app and activity for device control and automation tasks.
Instructions
Get the currently displayed app and activity
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| device_id | No | Device ID (optional if only one device) |
Implementation Reference
- src/adb.ts:347-357 (handler)The implementation of the `getCurrentActivity` method which queries the device using `dumpsys` and parses the result.
async getCurrentActivity(deviceId?: string): Promise<{ packageName: string; activity: string }> { const output = await this.exec(["shell", "dumpsys", "activity", "activities"], deviceId); const patterns = [/topResumedActivity=.*?\{[^ ]* [^ ]* ([^\s/}]+)\/([^\s}]+)/, /mResumedActivity=.*?\{[^ ]* [^ ]* ([^\s/}]+)\/([^\s}]+)/]; for (const pattern of patterns) { const match = output.match(pattern); if (match) { return { packageName: match[1], activity: match[2] }; } } throw new Error("Could not determine current activity from dumpsys output"); } - src/index.ts:486-498 (registration)Registration of the "get_current_activity" MCP tool and its handler in `src/index.ts`.
server.tool( "get_current_activity", "Get the currently displayed app and activity", { device_id: z.string().optional().describe("Device ID (optional if only one device)") }, async ({ device_id }) => { const info = await adb.getCurrentActivity(device_id); return { content: [ { type: "text", text: `Package: ${info.packageName}\nActivity: ${info.activity}` }, ], }; }, );