Skip to main content

This is the Python SDK for Computer Use Tool Server, allowing you to easily control the computer desktop environment from your applications.

Project description

Tool Server SDK

A Python SDK for Tool Server that allows seamless control of computer desktop environments from your applications.

Installation

# Install using pip
pip install tool-server-client

Usage

Basic Usage

"""
Examples for Computer Use SDK
"""
import asyncio
from tool_server_client.client import new_computer_use_client
from tool_server_client.async_client import new_async_computer_use_client

# Run: PYTHONPATH=./src python -m tool_server_client.examples
def example_basic_operations():
    """
    Example of basic mouse and keyboard operations
    """
    # Initialize the client
    client = new_computer_use_client("http://localhost:8102", auth_key="1234567890")

    # Get screen size
    screen_size_response = client.get_screen_size()
    print(f"Screen size: {screen_size_response}")
    ret = client.move_mouse(100,100)
    print(f"MoveMouse response: {ret}")
    ret = client.click_mouse(100,120,"right")
    print(f"ClickMouse response: {ret}")
    client.type_text("Hello World")
    print(f"TypeText response: {client.type_text('Hello World')}")
    client.press_key("enter")
    print(f"PressKey response: {client.press_key('enter')}")
    client.click_mouse(100,100,"right")
    print(f"ClickMouse response: {client.click_mouse(100,100,"right")}")
    ret = client.get_cursor_position()
    print(f"Cursor position: {ret}")
    ret = client.take_screenshot()
    print(f"TakeScreenshot response: {ret.Result.screenshot[:100]}")


async def example_async_basic_operations():
    """
    Example of basic mouse and keyboard operations using async client
    """
    # Initialize the async client
    client = await new_async_computer_use_client("http://localhost:8102", auth_key="your-secret-api-key-here")
    try:
        # Get screen size
        screen_size_response = await client.get_screen_size()
        print(f"Screen size: {screen_size_response}")
        
        # Move mouse
        ret = await client.move_mouse(100, 100)
        print(f"MoveMouse response: {ret}")
        
        # Click mouse
        ret = await client.click_mouse(100, 120, "right")
        print(f"ClickMouse response: {ret}")
        
        # Type text
        ret = await client.type_text("Hello World")
        print(f"TypeText response: {ret}")
        
        # Press key
        ret = await client.press_key("enter")
        print(f"PressKey response: {ret}")
        
        # Click mouse again
        ret = await client.click_mouse(100, 100, "right")
        print(f"ClickMouse response: {ret}")
        
        # Get cursor position
        ret = await client.get_cursor_position()
        print(f"Cursor position: {ret}")
        
        # Take screenshot
        ret = await client.take_screenshot()
        print(f"TakeScreenshot response: {ret.Result.screenshot[:100]}")

    finally:
        await client.__aexit__(None, None, None)


async def example_async_concurrent_operations():
    """
    Example of concurrent operations using async client
    """
    client = await new_async_computer_use_client("http://localhost:8102", auth_key="your-secret-api-key-here")
    try:
        # Perform multiple operations concurrently
        tasks = [
            client.move_mouse(100, 100),
            client.type_text("Hello"),
            client.press_key("enter"),
            client.get_cursor_position(),
        ]
        
        # Wait for all operations to complete
        results = await asyncio.gather(*tasks)
        
        # Print results
        for i, result in enumerate(results):
            print(f"Operation {i+1} result: {result}")
    finally:
        await client.__aexit__(None, None, None)


def example_recording_and_file_operations():
    """
    Example of recording and file operations
    """
    import time
    client = new_computer_use_client("http://localhost:8102", auth_key="your-secret-api-key-here")
    ret = client.start_video_recording(quality="high", format="mp4", resolution="1920x1080", framerate=30, max_duration=10)
    print(f"StartVideoRecording response: {ret}")
    time.sleep(5)
    ret = client.stop_video_recording()
    print(f"StopVideoRecording response: {ret}")
    ret = client.file_operation(command="read", path=ret.Result.file_path, mode="binary")
    print(f"FileOperation response: {ret}")

if __name__ == "__main__":
    print("Running basic operations example:")
    example_basic_operations()
    
    print("\nRunning async basic operations example:")
    asyncio.run(example_async_basic_operations())
    
    print("\nRunning async concurrent operations example:")
    asyncio.run(example_async_concurrent_operations())

    print("\nRunning recording and file operations example:")
    example_recording_and_file_operations()

Features

The SDK provides the following operations:

Mouse Operations

  • move_mouse(x, y): Move mouse to specified coordinates
  • click_mouse(x, y, button="left", press=False, release=False): Click mouse at specified position
  • press_mouse(x, y, button="left"): Press mouse button at specified position
  • release_mouse(x, y, button="left"): Release mouse button at specified position
  • drag_mouse(source_x, source_y, target_x, target_y): Drag from source position to target position
  • scroll(x, y, scroll_direction="up", scroll_amount=1): Scroll mouse wheel at specified position

Keyboard Operations

  • press_key(key): Press specified key
  • type_text(text): Type specified text

Screen Operations

  • take_screenshot(): Take a screenshot
  • get_cursor_position(): Get current cursor position
  • get_screen_size(): Get screen size

System Operations

  • wait(duration): Wait for specified duration (milliseconds)
  • change_password(username, new_password): Change user password

Examples

For more usage examples, see examples.py.

Advanced Usage

Custom API Version

from tool_server_client.client import ComputerUseClient

client = ComputerUseClient(base_url="http://your-server.com", api_version="2020-04-01")

Handling Responses

API calls return a dictionary containing operation results that can be checked for success:

response = client.move_mouse(100, 100)
if response.success:
    print("Mouse moved successfully")
else:
    print(f"Error: {response.error}")

Error Handling

The SDK handles HTTP errors and raises exceptions when API calls fail:

try:
    client.move_mouse(100, 100)
except Exception as e:
    print(f"Operation failed: {e}")

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tool_server_client-1.0.0rc19.tar.gz (131.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tool_server_client-1.0.0rc19-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file tool_server_client-1.0.0rc19.tar.gz.

File metadata

  • Download URL: tool_server_client-1.0.0rc19.tar.gz
  • Upload date:
  • Size: 131.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for tool_server_client-1.0.0rc19.tar.gz
Algorithm Hash digest
SHA256 57d3ca2ab0bd6a96d488543c254ef0d9c0b45a1e06f56860cb54ff828be78135
MD5 86960d0b8dda3e2e9933fd9a2f6a1f45
BLAKE2b-256 5caacc4eedd12d2e7a8415e802e6ac2aea3348e687619f9e37e429528c1bd613

See more details on using hashes here.

File details

Details for the file tool_server_client-1.0.0rc19-py3-none-any.whl.

File metadata

File hashes

Hashes for tool_server_client-1.0.0rc19-py3-none-any.whl
Algorithm Hash digest
SHA256 4959bf021188772bb92cace94044f3ae8a55cddab8fc07131685853b8b6dcd8a
MD5 3a2dcdc16a214725696aba816f3e6990
BLAKE2b-256 65d82e93722659ee2c2a334ce9de141fc5af284132cd3bd04ccb8d933bba2e46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page