take_screenshot
Capture screen images and save them in PNG or JPG format to automate screenshot tasks on Windows systems.
Instructions
截取屏幕截图
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | 保存路径(可选,默认桌面) | |
| format | No | 图片格式(可选,默认 png) |
Input Schema (JSON Schema)
{
"properties": {
"format": {
"description": "图片格式(可选,默认 png)",
"enum": [
"png",
"jpg"
],
"type": "string"
},
"path": {
"description": "保存路径(可选,默认桌面)",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/tools/screen.js:59-95 (handler)Core handler function that executes the take_screenshot tool: captures the primary screen using PowerShell, generates filename with timestamp, saves to specified or default desktop path, and returns success/error info.async takeScreenshot(savePath, format = 'png') { try { // 生成文件名 const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const fileName = `screenshot-${timestamp}.${format}`; const defaultPath = path.join(process.env.USERPROFILE, 'Desktop', fileName); const finalPath = savePath || defaultPath; // 使用 PowerShell 截图 const script = ` Add-Type -AssemblyName System.Windows.Forms,System.Drawing $bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds $bitmap = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height $graphics = [System.Drawing.Graphics]::FromImage($bitmap) $graphics.CopyFromScreen($bounds.Location, [System.Drawing.Point]::Empty, $bounds.Size) $bitmap.Save("${finalPath}", [System.Drawing.Imaging.ImageFormat]::${format === 'jpg' ? 'Jpeg' : 'Png'}) $bitmap.Dispose() $graphics.Dispose() `; await execAsync(`powershell -Command "${script.replace(/"/g, '\\"')}"`, { shell: 'powershell.exe' }); // 验证文件是否创建 await fs.access(finalPath); return { success: true, path: finalPath, format, message: '截图已保存' }; } catch (error) { return { success: false, error: error.message }; } }
- src/tools/screen.js:14-24 (schema)Schema definition for the take_screenshot tool, specifying optional path and format (png/jpg) parameters.{ name: 'take_screenshot', description: '截取屏幕截图', inputSchema: { type: 'object', properties: { path: { type: 'string', description: '保存路径(可选,默认桌面)' }, format: { type: 'string', enum: ['png', 'jpg'], description: '图片格式(可选,默认 png)' }, }, }, },
- src/tools/screen.js:48-57 (handler)Tool dispatcher function that handles the 'take_screenshot' case by calling the specific takeScreenshot handler.async executeTool(name, args) { switch (name) { case 'take_screenshot': return await this.takeScreenshot(args.path, args.format); case 'take_screenshot_region': return await this.takeScreenshotRegion(args.x, args.y, args.width, args.height, args.path); default: throw new Error(`未知工具: ${name}`); } }
- src/server.js:43-52 (registration)Registers the ScreenTools instance in the server's tools map, allowing the MCP server to discover and route calls to take_screenshot via getToolDefinitions() and canHandle().this.tools = { filesystem: new FileSystemTools(), process: new ProcessTools(), mouseKeyboard: new MouseKeyboardTools(), window: new WindowTools(), screen: new ScreenTools(), clipboard: new ClipboardTools(), powershell: new PowerShellTools(), browser: new BrowserTools(), };
- src/tools/screen.js:12-41 (registration)Defines and returns the tool specifications for registration with the MCP server, including take_screenshot.getToolDefinitions() { return [ { name: 'take_screenshot', description: '截取屏幕截图', inputSchema: { type: 'object', properties: { path: { type: 'string', description: '保存路径(可选,默认桌面)' }, format: { type: 'string', enum: ['png', 'jpg'], description: '图片格式(可选,默认 png)' }, }, }, }, { name: 'take_screenshot_region', description: '截取屏幕区域', inputSchema: { type: 'object', properties: { x: { type: 'number', description: 'X 坐标' }, y: { type: 'number', description: 'Y 坐标' }, width: { type: 'number', description: '宽度' }, height: { type: 'number', description: '高度' }, path: { type: 'string', description: '保存路径(可选)' }, }, required: ['x', 'y', 'width', 'height'], }, }, ]; }