计算机控制服务器

提供鼠标、键盘控制、OCR等计算机控制功能的服务,适用于自动化测试和计算机操作辅助场景。

  • by AB498
  • Python
  • 本地部署
  • 172 星标
  • 更新于
在 GitHub 查看 AB498/computer-control-mcp

使用说明

计算机控制MCP

MCP服务器,使用PyAutoGUI、RapidOCR、ONNXRuntime提供计算机控制功能,如鼠标、键盘、OCR等。类似于Anthropic的“电脑使用”。零外部依赖。


快速使用(MCP设置使用 uvx)

注: 跑步 uvx computer-control-mcp@latest 首次将下载python依赖项(约70MB),这可能需要一些时间。建议在将其用作MCP之前在终端中运行此程序。后续运行将是即时的。

{
  "mcpServers": {
    "computer-control-mcp": {
      "command": "uvx",
      "args": ["computer-control-mcp@latest"]
    }
  }
}

或全局安装 pip:

pip install computer-control-mcp

然后使用以下命令运行服务器:

computer-control-mcp # instead of uvx computer-control-mcp, so you can use the latest version, also you can `uv cache clean` to clear the cache and `uvx` again to use latest version.

特性

  • 控制鼠标移动和点击
  • 在当前光标位置键入文本
  • 对整个屏幕或特定窗口进行截图,并可选择保存到下载目录
  • 使用OCR(光学字符识别)从屏幕截图中提取文本
  • 列出并激活窗口
  • 按键盘键
  • 拖放操作
  • GPU加速窗口的增强屏幕截图捕获(仅限windows)

关于GPU加速Windows的说明

GDI/PrintWindow等传统截图方法无法捕获GPU加速的窗口,导致黑屏。这会影响游戏、媒体播放器、Electron应用程序、具有GPU加速的浏览器、流媒体软件和CAD工具。通过以下方式使用WGC take_screenshot 工具标志或 ENV 变量

配置

自定义屏幕截图目录

默认情况下,屏幕截图保存到操作系统下载目录。您可以通过设置 COMPUTER_CONTROL_MCP_SCREENSHOT_DIR 环境变量:

{
  "mcpServers": {
    "computer-control-mcp": {
      "command": "uvx",
      "args": ["computer-control-mcp@latest"],
      "env": {
        "COMPUTER_CONTROL_MCP_SCREENSHOT_DIR": "C:\\Users\\YourName\\Pictures\\Screenshots"
      }
    }
  }
}

或者将其设置为系统范围:

# Windows (PowerShell)
$env:COMPUTER_CONTROL_MCP_SCREENSHOT_DIR = "C:\Users\YourName\Pictures\Screenshots"

# macOS/Linux
export COMPUTER_CONTROL_MCP_SCREENSHOT_DIR="/home/yourname/Pictures/Screenshots"

如果指定的目录不存在,服务器将回退到默认的下载目录。

特定窗口的自动WGC

您可以通过设置以下参数来配置系统,使其自动对特定窗口使用Windows图形捕获(WGC) COMPUTER_CONTROL_MCP_WGC_PATTERNS 环境变量。此变量应包含与窗口标题匹配的逗号分隔模式:

{
  "mcpServers": {
    "computer-control-mcp": {
      "command": "uvx",
      "args": ["computer-control-mcp@latest"],
      "env": {
        "COMPUTER_CONTROL_MCP_WGC_PATTERNS": "obs, discord, game, steam"
      }
    }
  }
}

或者将其设置为系统范围:

# Windows (PowerShell)
$env:COMPUTER_CONTROL_MCP_WGC_PATTERNS = "obs, discord, game, steam"

# macOS/Linux
export COMPUTER_CONTROL_MCP_WGC_PATTERNS="obs, discord, game, steam"

设置此变量后,标题包含任何指定模式的任何窗口都将自动使用WGC进行屏幕截图,从而消除GPU加速应用程序的黑屏。

可用工具

鼠标控制

  • click_screen(x: int, y: int):单击指定的屏幕坐标
  • move_mouse(x: int, y: int):将鼠标光标移动到指定坐标
  • drag_mouse(from_x: int, from_y: int, to_x: int, to_y: int, duration: float = 0.5):将鼠标从一个位置拖动到另一个位置
  • mouse_down(button: str = "left"):按住鼠标按钮(“左”、“右”、“中”)
  • mouse_up(button: str = "left"):松开鼠标按钮(“左”、“右”、“中”)

键盘控制

  • type_text(text: str):在当前光标位置键入指定文本
  • key_down(key: str):按住特定键盘键直到松开
  • key_up(key: str):释放特定键盘键
  • press_keys(keys: Union[str, List[Union[str, List[str]]]]):按键盘键(支持单键、序列和组合)

屏幕和窗口管理

  • take_screenshot(title_pattern: str = None, use_regex: bool = False, threshold: int = 10, scale_percent_for_ocr: int = None, save_to_downloads: bool = False, use_wgc: bool = False):捕获屏幕或窗口
  • take_screenshot_with_ocr(title_pattern: str = None, use_regex: bool = False, threshold: int = 10, scale_percent_for_ocr: int = None, save_to_downloads: bool = False):使用OCR从屏幕或窗口提取并返回带有坐标的文本
  • get_screen_size():获取当前屏幕分辨率
  • list_windows():列出所有打开的窗口
  • activate_window(title_pattern: str, use_regex: bool = False, threshold: int = 60):将指定窗口置于前台
  • wait_milliseconds(milliseconds: int):等待指定的毫秒数

使用以下工具测试MCP服务器 mcp-caller

您可以使用以下命令在没有MCP客户端的情况下快速测试MCP服务器工具 mcp呼叫者:

# List all available tools
npx mcp-caller --server-json '{"type":"stdio","command":"uvx","args":["computer-control-mcp@latest","server"]}' --list-tools

# Call a specific tool
npx mcp-caller --server-json '{"type":"stdio","command":"uvx","args":["computer-control-mcp@latest","server"]}' --tool list_windows --params-json '{}'

发展

建立发展环境

# Clone the repository
git clone https://github.com/AB498/computer-control-mcp.git
cd computer-control-mcp

# Build/Run:

# 1. Install in development mode | Meaning that your edits to source code will be reflected in the installed package.
pip install -e .

# Then Start server | This is equivalent to `uvx computer-control-mcp@latest` just the local code is used
computer-control-mcp

# -- OR --

# 2. Build after `pip install hatch` | This needs version increment in orer to reflect code changes
hatch build

# Windows
$latest = Get-ChildItem .\dist\*.whl | Sort-Object LastWriteTime -Descending | Select-Object -First 1
pip install $latest.FullName --upgrade 

# Non-windows
pip install dist/*.whl --upgrade

# Run
computer-control-mcp

运行测试

python -m pytest

API 参考

看 API 参考 有关可用函数和类的详细信息。

许可证

麻省理工学院

获取更多信息或帮助