1
1
import subprocess
2
+ import os
2
3
from typing import Optional , Dict , Any
3
4
from mcp .server .fastmcp import FastMCP
4
5
5
6
mcp = FastMCP ("ChatGPT" )
6
7
7
- def run_applescript (script : str ) -> Dict [str , Any ]:
8
- """Run an AppleScript and return its output and status."""
8
+ def run_applescript (script : str , wait_for_output : bool = True ) -> Dict [str , Any ]:
9
+ """
10
+ Run an AppleScript and return its output and status.
11
+
12
+ Args:
13
+ script: The AppleScript to run
14
+ wait_for_output: Whether to wait for and capture output
15
+ """
9
16
try :
10
- result = subprocess .run (
11
- ['osascript' , '-e' , script ],
12
- capture_output = True ,
13
- text = True ,
14
- check = True
15
- )
16
- return {
17
- "success" : True ,
18
- "output" : result .stdout .strip ()
19
- }
20
- except subprocess .CalledProcessError as e :
21
- error_msg = e .stderr .strip ()
22
- help_msg = ""
23
-
24
- if "is not allowed to send keystrokes" in error_msg :
25
- help_msg = (
26
- "Permission Error: The script needs accessibility permissions.\n "
27
- "1. Open System Settings > Privacy & Security > Accessibility\n "
28
- "2. Add and enable your terminal application\n "
29
- "3. Also check System Settings > Privacy & Security > Automation"
17
+ if wait_for_output :
18
+ # Run synchronously and capture output
19
+ result = subprocess .run (
20
+ ['osascript' , '-e' , script ],
21
+ capture_output = True ,
22
+ text = True ,
23
+ check = True
30
24
)
31
- elif "not allowed to send apple events to" in error_msg .lower ():
32
- help_msg = (
33
- "Permission Error: The script needs automation permissions.\n "
34
- "1. Open System Settings > Privacy & Security > Automation\n "
35
- "2. Enable permissions for your terminal to control 'ChatGPT' and 'System Events'"
25
+ return {
26
+ "success" : True ,
27
+ "output" : result .stdout .strip ()
28
+ }
29
+ else :
30
+ # Use Popen for non-blocking execution
31
+ subprocess .Popen (
32
+ ['osascript' , '-e' , script ],
33
+ stdout = subprocess .DEVNULL ,
34
+ stderr = subprocess .DEVNULL ,
35
+ start_new_session = True
36
36
)
37
-
37
+ return {
38
+ "success" : True ,
39
+ "output" : ""
40
+ }
41
+ except Exception as e :
42
+ error_msg = str (e )
38
43
return {
39
44
"success" : False ,
40
45
"error" : error_msg ,
41
- "help" : help_msg
42
46
}
43
47
44
48
@mcp .tool ()
45
- def ask_chatgpt (prompt : str ) -> Dict [str , Any ]:
49
+ def ask_chatgpt (prompt : str , wait_for_output : bool = False ) -> Dict [str , Any ]:
46
50
"""
47
- Send a prompt to ChatGPT macOS app and optionally wait for a response .
51
+ Send a prompt to ChatGPT macOS app using Shortcuts .
48
52
49
53
Args:
50
54
prompt: The text to send to ChatGPT
51
-
55
+ wait_for_output: Whether to wait for ChatGPT to respond
52
56
Returns:
53
57
Dict containing operation status
54
58
"""
59
+ # Escape double quotes in the prompt for AppleScript
60
+ escaped_prompt = prompt .replace ('"' , '\\ "' )
61
+
55
62
script = f'''
56
- tell application "ChatGPT"
57
- activate
58
- delay 1
59
-
60
- tell application "System Events"
61
- tell process "ChatGPT"
62
- keystroke "{ prompt } "
63
- delay 0.5
64
- keystroke return
65
- end tell
66
- end tell
63
+ set shortcutName to "Ask ChatGPT on Mac"
64
+ set shortcutInput to "{ escaped_prompt } "
65
+
66
+ tell application "Shortcuts Events"
67
+ run shortcut shortcutName with input shortcutInput
67
68
end tell
68
69
'''
69
70
70
- result = run_applescript (script )
71
+ result = run_applescript (script , wait_for_output = wait_for_output )
71
72
72
73
if result ["success" ]:
73
74
return {
@@ -80,7 +81,6 @@ def ask_chatgpt(prompt: str) -> Dict[str, Any]:
80
81
"operation" : "ask_chatgpt" ,
81
82
"status" : "error" ,
82
83
"message" : result ["error" ],
83
- "help" : result ["help" ]
84
84
}
85
85
86
86
def main ():
0 commit comments