WeniVooks

검색

MCP 베이스캠프

키보드 컨트롤

1. 직접 컨트롤 하게 하기

앞서 말씀드린 것처럼 MCP로 단지 내가 만든 함수를 동작하게 하는 것이 아니라 컴퓨터를 제어하게 하는 것이 가능합니다. 마우스와 키보드를 컨트롤하게 하고 그 결과값을 지속적으로 받을 수 있게 하면 됩니다. 클로드는 해당 함수를 하나의 프롬프트에서 여러번 실행시킬 수 있습니다.

2. 파일 생성

코드는 아래와 같습니다. pip install pyautogui를 통해 pyautogui를 설치해야 합니다. 이번 챕터에서는 Python 코드만 제공합니다.

# 키보드 컨트롤 해서 메모장에 'hello world' 입력하는 과제
from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="autokeyboard", host="127.0.0.1", port=5010, timeout=30)
 
 
@mcp.tool()
def control_keyboard(message: str) -> str:
    """
    메모장에 'hello world' 입력하기
    """
    import pyautogui
    import time
 
    # 메모장 실행
    pyautogui.hotkey("win", "r")
    time.sleep(1)
    pyautogui.typewrite("notepad\n", interval=0.1)
    time.sleep(1)
 
    # 메시지 입력
    pyautogui.typewrite(message, interval=0.1)
 
    return f"'{message}' 가 메모장에 입력되었습니다."
 
 
mcp.run()
# 키보드 컨트롤 해서 메모장에 'hello world' 입력하는 과제
from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="autokeyboard", host="127.0.0.1", port=5010, timeout=30)
 
 
@mcp.tool()
def control_keyboard(message: str) -> str:
    """
    메모장에 'hello world' 입력하기
    """
    import pyautogui
    import time
 
    # 메모장 실행
    pyautogui.hotkey("win", "r")
    time.sleep(1)
    pyautogui.typewrite("notepad\n", interval=0.1)
    time.sleep(1)
 
    # 메시지 입력
    pyautogui.typewrite(message, interval=0.1)
 
    return f"'{message}' 가 메모장에 입력되었습니다."
 
 
mcp.run()

3. 결과

함수가 실행되면 실행창이 열리고 notepad를 입력하여, 메모장이 실행됩니다. 이후 hello world가 입력됩니다. 아래 이미지를 참고해주세요.

4.1 macOS 사용자 가이드4.3 클로드로 한글 문서 읽기