屏幕截图服务

一个用于macOS的屏幕截图服务,提供远程应用程序通过RESTful API和WebSocket接口获取特定窗口或全屏截图的功能。

  • by gaurav-kispotta
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 gaurav-kispotta/screenshot-mcp-server

使用说明

MCP服务器截图

一种媒体控制协议(MCP)服务器,用于捕获macOS中窗口的屏幕截图,并通过网络接口提供。此工具允许远程应用程序请求和接收特定窗口或整个屏幕的屏幕截图。

特性

  • 捕获全屏屏幕截图
  • 按ID或标题捕获特定窗口的屏幕截图
  • 捕获特定屏幕区域的屏幕截图
  • 列出所有可用窗口
  • 获取窗口属性(位置、大小、标题、进程ID)
  • 跟踪窗口焦点更改
  • 支持不同的图像格式(PNG、JPEG等)
  • RESTful API和WebSocket接口
  • 实时窗口事件通知

需求

  • macOS 12+(蒙特利或更高版本)
  • Node.js 18+
  • 屏幕录制权限
  • 无障碍权限

安装

# Clone the repository
git clone https://github.com/yourusername/screenshot-mcp-server.git
cd screenshot-mcp-server

# Install dependencies
npm install

# Build the project
npm run build

用法

启动服务器

# Start the server on default port 3000
npm start

# Start the server on a custom port
npm start -- --port 8080

API终点

窗口管理

  • GET /api/v1/windows -列出所有窗口
  • GET /api/v1/windows/active -获取活动窗口
  • GET /api/v1/windows/:id -按ID获取窗口
  • GET /api/v1/applications/:appName/windows -按应用程序名称获取窗口

截图

  • GET /api/v1/screenshots/screen -捕获全屏
  • GET /api/v1/screenshots/window/:id -按ID捕获窗口
  • GET /api/v1/screenshots/region -捕获特定屏幕区域

WebSocket API

连接到 ws://localhost:3000 并使用以下事件:

  • windows:list -获取所有窗口的列表
  • windows:active -获取活动窗口
  • screenshot:capture -截图
  • subscribe:window_events -订阅窗口事件

示例

// Connect to the WebSocket API
const socket = io('http://localhost:3000');

// Request a screenshot of the active window
socket.emit('windows:active', {}, (response) => {
  if (response.success && response.window) {
    socket.emit('screenshot:capture', {
      target: 'window',
      windowId: response.window.id,
      options: {
        format: 'png',
        quality: 90
      }
    }, (result) => {
      if (result.success) {
        // result.image contains the base64-encoded screenshot
        const imageElement = document.createElement('img');
        imageElement.src = `data:image/${result.metadata.format};base64,${result.image}`;
        document.body.appendChild(imageElement);
      }
    });
  }
});

// Subscribe to window events
socket.emit('subscribe:window_events');
socket.on('window_event', (event) => {
  console.log(`Window event: ${event.type}`, event.window);
});

权限

此应用程序需要:

  1. 屏幕录制 权限-需要 screencapture 实用功能
  2. 无障碍 权限-AppleScript访问窗口信息所必需的

首次运行应用程序时,系统将提示您授予这些权限。您还可以在“系统首选项”>“安全与隐私”>“隐私”中管理权限。

发展

# Run in development mode with auto-reload
npm run dev

# Run tests
npm test

# Lint code
npm run lint

# Format code
npm run format

许可证

麻省理工学院