|
| 1 | +import { z } from 'zod' |
| 2 | +import { getOSType } from '@/utils' |
| 3 | +import { exec } from 'child_process' |
| 4 | +import { promisify } from 'util' |
| 5 | +import { setTimeout as sleep } from 'timers/promises' |
| 6 | +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' |
| 7 | +import type { OptionsType } from '@/types' |
| 8 | + |
| 9 | +const execPromise = promisify(exec) |
| 10 | + |
| 11 | +export default function register(server: McpServer, options: OptionsType) { |
| 12 | + const openMacOsAppTool = server.registerTool( |
| 13 | + 'MacOs Open App', |
| 14 | + { |
| 15 | + title: 'MacOs Open App', |
| 16 | + description: 'MacOs Open App', |
| 17 | + inputSchema: { |
| 18 | + appName: z.string().describe('app name'), |
| 19 | + }, |
| 20 | + }, |
| 21 | + async ({ appName }) => { |
| 22 | + const { success, data, message } = await openMacOsApp(appName, options) |
| 23 | + await sleep(2000) |
| 24 | + return { |
| 25 | + content: [ |
| 26 | + { |
| 27 | + type: 'text', |
| 28 | + text: success ? data! : message!, |
| 29 | + }, |
| 30 | + ], |
| 31 | + } |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + const findInAppTool = server.registerTool( |
| 36 | + 'Find In App', |
| 37 | + { |
| 38 | + title: 'Find In App', |
| 39 | + description: 'Find In App', |
| 40 | + inputSchema: { |
| 41 | + appPath: z.string().describe('app path'), |
| 42 | + keyword: z.string().describe('search keyword'), |
| 43 | + }, |
| 44 | + }, |
| 45 | + async ({ appPath, keyword }) => { |
| 46 | + await findInApp(appPath, keyword, options) |
| 47 | + await sleep(2000) |
| 48 | + return { |
| 49 | + content: [ |
| 50 | + { |
| 51 | + type: 'text', |
| 52 | + text: `find ${keyword} in ${appPath}`, |
| 53 | + }, |
| 54 | + ], |
| 55 | + } |
| 56 | + }, |
| 57 | + ) |
| 58 | + |
| 59 | + const inputMessageTool = server.registerTool( |
| 60 | + 'Input Message', |
| 61 | + { |
| 62 | + title: 'Input Message', |
| 63 | + description: 'Input Message', |
| 64 | + inputSchema: { |
| 65 | + appPath: z.string().describe('app path'), |
| 66 | + message: z.string().describe('input message'), |
| 67 | + }, |
| 68 | + }, |
| 69 | + async ({ appPath, message }) => { |
| 70 | + await inputMessage(appPath, message, options) |
| 71 | + await sleep(2000) |
| 72 | + return { |
| 73 | + content: [ |
| 74 | + { |
| 75 | + type: 'text', |
| 76 | + text: `input ${message} in ${appPath}`, |
| 77 | + }, |
| 78 | + ], |
| 79 | + } |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + if (getOSType() !== 'macOS') { |
| 84 | + openMacOsAppTool.disable() |
| 85 | + findInAppTool.disable() |
| 86 | + inputMessageTool.disable() |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export const openMacOsApp = async (appName: string, options: OptionsType) => { |
| 91 | + const app = options.apps.find(item => item.name === appName) |
| 92 | + if (!app) { |
| 93 | + return { |
| 94 | + success: false, |
| 95 | + message: `app ${appName} not found`, |
| 96 | + } |
| 97 | + } |
| 98 | + try { |
| 99 | + await execPromise(`open "${app.path}"`) |
| 100 | + } catch (error) { |
| 101 | + return { |
| 102 | + success: false, |
| 103 | + message: `open app ${app.name} failed: ${(error as Error).message}`, |
| 104 | + } |
| 105 | + } |
| 106 | + return { |
| 107 | + success: true, |
| 108 | + data: `appName: ${app.name}; appVersion: ${app.version}; appPath: ${app.path};`, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export const findInApp = async (appPath: string, keyword: string, options: OptionsType) => { |
| 113 | + await execPromise(`osascript -e 'set originalClipboard to the clipboard |
| 114 | +set the clipboard to "${keyword}" |
| 115 | +tell application "${appPath}" |
| 116 | + activate |
| 117 | + delay 1 |
| 118 | + tell application "System Events" |
| 119 | + key code 3 using command down |
| 120 | + delay 1 |
| 121 | + key code 0 using command down |
| 122 | + delay 0.2 |
| 123 | + key code 9 using command down |
| 124 | + delay 1 |
| 125 | + key code 36 |
| 126 | + end tell |
| 127 | +end tell |
| 128 | +set the clipboard to originalClipboard'`) |
| 129 | +} |
| 130 | + |
| 131 | +export const inputMessage = async (appPath: string, message: string, options: OptionsType) => { |
| 132 | + await execPromise(`echo '${message}' | pbcopy`) |
| 133 | + await execPromise(`osascript -e 'set originalClipboard to the clipboard |
| 134 | +set the clipboard to "${message}" |
| 135 | +tell application "${appPath}" |
| 136 | + activate |
| 137 | + tell application "System Events" |
| 138 | + key code 53 |
| 139 | + delay 0.3 |
| 140 | + key code 0 using command down |
| 141 | + delay 0.2 |
| 142 | + key code 9 using command down |
| 143 | + delay 1 |
| 144 | + key code 36 |
| 145 | + end tell |
| 146 | +end tell |
| 147 | +set the clipboard to originalClipboard'`) |
| 148 | +} |
0 commit comments