Desktop Computer Automation
CRITICAL RULES:
- Never run midscene commands in the background. Each command must run synchronously so you can read its output (especially screenshots) before deciding the next action.
- Run only one midscene command at a time. Wait for the previous command to finish, read the screenshot, then decide the next action.
- Allow enough time for each command to complete. Midscene commands involve AI inference and screen interaction, which can take longer than typical shell commands.
- Always report task results before finishing.
Control your desktop (macOS, Windows, Linux) using npx @midscene/computer@1. Each CLI command maps directly to an MCP tool -- you (the AI agent) act as the brain, deciding which actions to take based on screenshots.
Prerequisites
Midscene requires models with strong visual grounding capabilities. Configure these environment variables:
MIDSCENE_MODEL_API_KEY="your-api-key"
MIDSCENE_MODEL_NAME="model-name"
MIDSCENE_MODEL_BASE_URL="https://..."
MIDSCENE_MODEL_FAMILY="family-identifier"
Commands
Connect to Desktop
npx @midscene/computer@1 connect
npx @midscene/computer@1 connect --displayId <id>
List Displays
npx @midscene/computer@1 list_displays
Take Screenshot
npx @midscene/computer@1 take_screenshot
Perform Action
Use act to interact with the computer. Describe what you want to do in natural language:
npx @midscene/computer@1 act --prompt "type hello world in the search field and press Enter"
npx @midscene/computer@1 act --prompt "drag the file icon to the Trash"
npx @midscene/computer@1 act --prompt "search for the weather in Shanghai using the Chrome browser, tell me the result"
Disconnect
npx @midscene/computer@1 disconnect
Workflow Pattern
- Connect to establish a session
- Health check -- take a screenshot and verify it succeeds, then move the mouse to a random position
- Launch the target app and take screenshot to see the current state
- Execute action using
actto perform the desired action - Disconnect when done
- Report results -- summarize what was accomplished
Best Practices
- Always run a health check first after connecting
- Bring the target app to the foreground before using this skill (e.g.,
open -a <AppName>on macOS) - Be specific about UI elements: Say "the red close button in the top-left corner" instead of "the close button"
- Describe locations when possible: "the icon in the top-right corner of the menu bar"
- Never run in background: Every midscene command must run synchronously
- Check for multiple displays: Use
list_displaysif an app is not visible - Batch related operations into a single
actcommand when possible - Set up PATH before running (macOS):
export PATH="/usr/sbin:/usr/bin:/bin:/sbin:$PATH"
Troubleshooting
macOS: Accessibility Permission Denied
Open System Settings > Privacy & Security > Accessibility and add your terminal app.
macOS: Xcode Command Line Tools Not Found
xcode-select --install
API Key Not Set
Check .env file contains MIDSCENE_MODEL_API_KEY=<your-key>.
AI Cannot Find the Element
- Take a screenshot to verify the element is actually visible
- Use more specific descriptions (include color, position, surrounding text)
- Ensure the element is not hidden behind another window