ask_chatgpt_tool
Send a prompt to ChatGPT and receive the AI-generated reply. This tool lets MCP-compatible assistants access ChatGPT.
Instructions
Send a prompt to ChatGPT and return the response.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes |
Implementation Reference
- chatgpt_mcp/mcp_tools.py:91-116 (handler)Core logic handler for 'ask_chatgpt_tool'. Cleans the prompt, activates ChatGPT, sends the message via keystroke, and awaits the response.
async def ask_chatgpt(prompt: str) -> str: """Send a prompt to ChatGPT and return the response. Args: prompt: The text to send to ChatGPT Returns: ChatGPT's response """ await check_chatgpt_access() try: # 프롬프트에서 개행 문자 제거 및 더블쿼츠를 싱글쿼츠로 변경 cleaned_prompt = prompt.replace('\n', ' ').replace('\r', ' ').replace('"', "'").strip() # Activate ChatGPT and send message using keystroke automation = ChatGPTAutomation() automation.activate_chatgpt() automation.send_message_with_keystroke(cleaned_prompt) # Get the response response = await get_chatgpt_response() return response except Exception as e: raise Exception(f"Failed to send message to ChatGPT: {str(e)}") - chatgpt_mcp/mcp_tools.py:152-154 (handler)The MCP tool function 'ask_chatgpt_tool' defined as an async function that delegates to ask_chatgpt() with the provided prompt string.
async def ask_chatgpt_tool(prompt: str) -> str: """Send a prompt to ChatGPT and return the response.""" return await ask_chatgpt(prompt) - chatgpt_mcp/mcp_tools.py:148-154 (registration)Registration point: @mcp.tool() decorator registers 'ask_chatgpt_tool' within setup_mcp_tools() called from the main entry point.
def setup_mcp_tools(mcp: FastMCP): """MCP 도구들을 설정""" @mcp.tool() async def ask_chatgpt_tool(prompt: str) -> str: """Send a prompt to ChatGPT and return the response.""" return await ask_chatgpt(prompt) - Helper function send_message_with_keystroke that types the prompt into the ChatGPT app using AppleScript System Events keystroke.
def send_message_with_keystroke(self, message): """AppleScript를 사용해서 직접 키스트로크로 메시지 전송""" time.sleep(0.5) self._type_with_applescript(message) def _type_with_applescript(self, text): """AppleScript를 사용해서 텍스트 입력""" escaped_text = text.replace('"', '\\"').replace("\\", "\\\\") script = f''' tell application "System Events" tell process "ChatGPT" -- 먼저 백스페이스 key code 51 delay 0.1 -- 텍스트 입력 (각 문자를 개별적으로) set textToType to "{escaped_text}" repeat with i from 1 to length of textToType set currentChar to character i of textToType keystroke currentChar delay 0.01 end repeat -- Enter 키 입력 key code 36 end tell end tell ''' subprocess.run(['osascript', '-e', script], capture_output=True, text=True) - Helper function activate_chatgpt used to focus the ChatGPT desktop app before sending a message.
def activate_chatgpt(self): """ChatGPT Desktop 앱 활성화""" subprocess.run(['osascript', '-e', 'tell application "ChatGPT" to activate']) time.sleep(1) def new_chat(self): """새 ChatGPT 채팅창 열기""" # ChatGPT 앱을 활성화 self.activate_chatgpt() # 메뉴를 통해 새 채팅 열기 시도 script = ''' tell application "System Events" tell process "ChatGPT" try -- 메뉴바에서 File > New Chat 클릭 click menu item "New Chat" of menu "File" of menu bar 1 return "success_menu"