Cua Computer Server

0.3.32 · active · verified Sun Apr 12

The `cua-computer-server` is the server component for the Computer-Use Interface (CUI) framework, enabling AI agents to programmatically control a host desktop. It runs as a local HTTP service, exposing endpoints for screenshots, mouse/keyboard input, and other computer operations across macOS, Linux, and Windows. As of version 0.3.32, it forms a core part of the Cua ecosystem, facilitating direct machine interaction for agent development and testing.

Warnings

Install

Imports

Quickstart

To use `cua-computer-server`, first run the server as a Python module in a terminal. Then, connect to it from a Python script using the `cua` library's `Sandbox.create()` method with `use_host_computer_server=True`. The example demonstrates connecting and taking a screenshot.

import asyncio
import os
from cua import Sandbox

async def main():
    # Make sure to start the server first in a separate terminal:
    # python -m computer_server --port 8000 --log-level debug
    
    # For this example, we assume the server is running on localhost:8000
    # You might need to adjust api_host and api_port if running elsewhere.
    try:
        async with Sandbox.create(
            use_host_computer_server=True,
            api_host=os.environ.get('CUA_SERVER_HOST', 'localhost'),
            api_port=int(os.environ.get('CUA_SERVER_PORT', 8000))
        ) as computer:
            print("Connected to local computer server.")
            
            # Take a screenshot
            screenshot = await computer.interface.screenshot()
            print(f"Screenshot captured: {screenshot.format}")
            
            # Example: Left click at (500, 300) and type text
            # await computer.interface.left_click(500, 300)
            # await computer.interface.type_text("Hello from the agent!")
            # print("Performed click and typed text.")
            
    except Exception as e:
        print(f"Error connecting to computer server: {e}")
        print("Please ensure the computer server is running: 'python -m computer_server'")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →