|
| 1 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import type { AppiumSession } from '../session.js'; |
| 4 | +import { formatError } from '../errors.js'; |
| 5 | + |
| 6 | +const modifierKeys = z.array(z.enum(['shift', 'ctrl', 'alt', 'win'])).default([]); |
| 7 | + |
| 8 | +export function registerAdvancedTools(server: McpServer, session: AppiumSession): void { |
| 9 | + server.registerTool( |
| 10 | + 'advanced_click', |
| 11 | + { |
| 12 | + description: 'Perform an advanced click with modifier keys, multiple clicks, or custom duration. Use this for right-click, Ctrl+click, double-click, etc.', |
| 13 | + annotations: { destructiveHint: false }, |
| 14 | + inputSchema: { |
| 15 | + elementId: z.string().optional().describe('Element to click (its center). Provide either elementId or x+y.'), |
| 16 | + x: z.number().int().optional().describe('Absolute screen x coordinate'), |
| 17 | + y: z.number().int().optional().describe('Absolute screen y coordinate'), |
| 18 | + button: z.enum(['left', 'right', 'middle', 'back', 'forward']).default('left'), |
| 19 | + modifierKeys, |
| 20 | + durationMs: z.number().int().min(0).default(0).describe('Hold duration in ms (for long-press)'), |
| 21 | + times: z.number().int().min(1).default(1).describe('Number of clicks (2 = double-click)'), |
| 22 | + interClickDelayMs: z.number().int().min(0).default(100), |
| 23 | + }, |
| 24 | + }, |
| 25 | + async (args) => { |
| 26 | + try { |
| 27 | + const driver = session.getDriver(); |
| 28 | + await driver.executeScript('windows: click', [args]); |
| 29 | + return { content: [{ type: 'text' as const, text: 'clicked' }] }; |
| 30 | + } catch (err) { |
| 31 | + return { isError: true, content: [{ type: 'text' as const, text: formatError(err) }] }; |
| 32 | + } |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + server.registerTool( |
| 37 | + 'send_keys', |
| 38 | + { |
| 39 | + description: 'Send keyboard input. Each action can be a pause (ms delay), text to type, or a virtual key code press/release.', |
| 40 | + annotations: { destructiveHint: false }, |
| 41 | + inputSchema: { |
| 42 | + actions: z.array(z.object({ |
| 43 | + pause: z.number().int().optional().describe('Pause in milliseconds'), |
| 44 | + text: z.string().optional().describe('Text to type (unicode supported)'), |
| 45 | + virtualKeyCode: z.number().int().optional().describe('Windows Virtual Key code (e.g. 13 = Enter, 27 = Escape)'), |
| 46 | + down: z.boolean().optional().describe('true = key down only, false = key up only, omit = press and release'), |
| 47 | + })).describe('Sequence of keyboard actions to perform'), |
| 48 | + forceUnicode: z.boolean().default(false).describe('Use Unicode input method for special characters'), |
| 49 | + }, |
| 50 | + }, |
| 51 | + async (args) => { |
| 52 | + try { |
| 53 | + const driver = session.getDriver(); |
| 54 | + await driver.executeScript('windows: keys', [args]); |
| 55 | + return { content: [{ type: 'text' as const, text: 'keys sent' }] }; |
| 56 | + } catch (err) { |
| 57 | + return { isError: true, content: [{ type: 'text' as const, text: formatError(err) }] }; |
| 58 | + } |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + server.registerTool( |
| 63 | + 'hover', |
| 64 | + { |
| 65 | + description: 'Move the mouse pointer from one position to another, optionally with modifier keys held. Useful for hover effects and drag-without-click.', |
| 66 | + inputSchema: { |
| 67 | + startElementId: z.string().optional().describe('Element to start hover from (uses element center)'), |
| 68 | + startX: z.number().int().optional(), |
| 69 | + startY: z.number().int().optional(), |
| 70 | + endElementId: z.string().optional().describe('Element to hover to'), |
| 71 | + endX: z.number().int().optional(), |
| 72 | + endY: z.number().int().optional(), |
| 73 | + modifierKeys, |
| 74 | + durationMs: z.number().int().min(0).default(500).describe('Duration of the hover movement in ms'), |
| 75 | + }, |
| 76 | + }, |
| 77 | + async (args) => { |
| 78 | + try { |
| 79 | + const driver = session.getDriver(); |
| 80 | + await driver.executeScript('windows: hover', [args]); |
| 81 | + return { content: [{ type: 'text' as const, text: 'hovered' }] }; |
| 82 | + } catch (err) { |
| 83 | + return { isError: true, content: [{ type: 'text' as const, text: formatError(err) }] }; |
| 84 | + } |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + server.registerTool( |
| 89 | + 'scroll', |
| 90 | + { |
| 91 | + description: 'Scroll the mouse wheel at an element or screen coordinate.', |
| 92 | + inputSchema: { |
| 93 | + elementId: z.string().optional().describe('Element to scroll over (uses element center)'), |
| 94 | + x: z.number().int().optional().describe('Absolute screen x coordinate'), |
| 95 | + y: z.number().int().optional().describe('Absolute screen y coordinate'), |
| 96 | + deltaX: z.number().int().default(0).describe('Horizontal scroll amount (positive = right)'), |
| 97 | + deltaY: z.number().int().default(0).describe('Vertical scroll amount (positive = down)'), |
| 98 | + modifierKeys, |
| 99 | + }, |
| 100 | + }, |
| 101 | + async (args) => { |
| 102 | + try { |
| 103 | + const driver = session.getDriver(); |
| 104 | + await driver.executeScript('windows: scroll', [args]); |
| 105 | + return { content: [{ type: 'text' as const, text: 'scrolled' }] }; |
| 106 | + } catch (err) { |
| 107 | + return { isError: true, content: [{ type: 'text' as const, text: formatError(err) }] }; |
| 108 | + } |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + server.registerTool( |
| 113 | + 'click_and_drag', |
| 114 | + { |
| 115 | + description: 'Click and drag from one position to another. Useful for resizing, reordering, or moving elements.', |
| 116 | + inputSchema: { |
| 117 | + startElementId: z.string().optional().describe('Element to start drag from'), |
| 118 | + startX: z.number().int().optional(), |
| 119 | + startY: z.number().int().optional(), |
| 120 | + endElementId: z.string().optional().describe('Element to drag to'), |
| 121 | + endX: z.number().int().optional(), |
| 122 | + endY: z.number().int().optional(), |
| 123 | + modifierKeys, |
| 124 | + durationMs: z.number().int().min(0).default(500), |
| 125 | + button: z.enum(['left', 'right', 'middle']).default('left'), |
| 126 | + }, |
| 127 | + }, |
| 128 | + async (args) => { |
| 129 | + try { |
| 130 | + const driver = session.getDriver(); |
| 131 | + await driver.executeScript('windows: clickAndDrag', [args]); |
| 132 | + return { content: [{ type: 'text' as const, text: 'drag completed' }] }; |
| 133 | + } catch (err) { |
| 134 | + return { isError: true, content: [{ type: 'text' as const, text: formatError(err) }] }; |
| 135 | + } |
| 136 | + } |
| 137 | + ); |
| 138 | +} |
0 commit comments