Serve six computer-use tools on the mcp channel and add the guest, restore point, clone and test tools on a new mcp-dev channel. - vm_power replaces vm_start, vm_shutdown, vm_suspend and vm_resume and adds force_stop. - screen replaces the screenshot and zoom actions and wait_for_screen: it can wait for a change or a stable frame (up to 180 s while booting) and return one region at up to source resolution. - computer_input replaces computer and computer_batch: it checks every step before sending input, holds modifiers around pointer actions, stops on a wait_screen timeout, and releases held keys and buttons on success, error or cancellation. - Coordinates follow the last full image returned, so scaled screenshots no longer misplace clicks; results report the image size and a short sha256 as compact JSON and structuredContent. - Calls on one VM run in order instead of all tools sharing one lock; request ids and cancellation are scoped per client connection, and a closed connection cancels its calls. - guest_wait_ready returns the plugin public key; logs_read pages guest and test logs; test_wait returns the result once finished; sessions and test runs always use a clone. Removed the screen resource. - Tools carry annotations and one-sentence descriptions, and arguments the schema does not name are refused. Requires a Reims-Go host with region screenshots, held-key input, host/console.release and forced shutdown. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
202 lines
8.3 KiB
Go
202 lines
8.3 KiB
Go
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"image/color"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// inputServer returns a server whose VM last showed a 640x360 image.
|
|
func inputServer(t *testing.T) (*Server, *scriptedHost) {
|
|
t.Helper()
|
|
host := &scriptedHost{screens: []scriptedScreen{
|
|
{data: makeFrame(t, 64, 36, color.RGBA{R: 30, A: 255}, false), w: 640, h: 360},
|
|
}}
|
|
server := New()
|
|
server.BindHost(host)
|
|
callTool(t, server, "screen", `{"vm":"macos1","scale":0.5}`)
|
|
return server, host
|
|
}
|
|
|
|
// inputTrace lists the input calls after the initial screen call, one line
|
|
// each, in a form the tests can compare.
|
|
func inputTrace(host *scriptedHost) []string {
|
|
host.mu.Lock()
|
|
defer host.mu.Unlock()
|
|
var trace []string
|
|
for _, call := range host.calls[1:] {
|
|
args, _ := call.params.(map[string]any)
|
|
switch call.method {
|
|
case "host/console.pointer":
|
|
switch args["action"] {
|
|
case "move":
|
|
trace = append(trace, fmt.Sprintf("move %v,%v in %vx%v", args["x"], args["y"], args["width"], args["height"]))
|
|
case "button":
|
|
trace = append(trace, fmt.Sprintf("button %v %v", args["button"], args["down"]))
|
|
case "click":
|
|
trace = append(trace, fmt.Sprintf("click %v", args["button"]))
|
|
case "scroll":
|
|
trace = append(trace, fmt.Sprintf("scroll %v %v", args["delta"], args["horizontal"]))
|
|
}
|
|
case "host/console.key":
|
|
if down, ok := args["down"]; ok {
|
|
trace = append(trace, fmt.Sprintf("key %v %v", args["key"], down))
|
|
} else {
|
|
trace = append(trace, fmt.Sprintf("key %v %v", args["key"], args["modifiers"]))
|
|
}
|
|
case "host/console.type":
|
|
trace = append(trace, "type "+args["text"].(string))
|
|
case "host/console.release":
|
|
trace = append(trace, fmt.Sprintf("release %v %v", args["keys"], args["buttons"]))
|
|
case "host/console.screenshot":
|
|
trace = append(trace, "screenshot")
|
|
}
|
|
}
|
|
return trace
|
|
}
|
|
|
|
func expectTrace(t *testing.T, host *scriptedHost, want ...string) {
|
|
t.Helper()
|
|
got := inputTrace(host)
|
|
if strings.Join(got, "\n") != strings.Join(want, "\n") {
|
|
t.Fatalf("input calls:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
|
|
}
|
|
}
|
|
|
|
func TestComputerInputUsesTheLastImageAsCoordinateSpace(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
result := callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"click","x":100,"y":200},{"action":"type","text":"hi"},{"action":"key","key":"cmd+c"}]}`)
|
|
if result["isError"] == true {
|
|
t.Fatalf("computer_input = %v", result)
|
|
}
|
|
expectTrace(t, host,
|
|
"move 100,200 in 640x360", "click left", "type hi", "key c [cmd]", "screenshot")
|
|
if info := structured(t, result); info["executed"] != float64(3) || info["width"] != float64(640) {
|
|
t.Fatalf("result info = %v", info)
|
|
}
|
|
}
|
|
|
|
func TestComputerInputChecksEveryStepBeforeSendingAny(t *testing.T) {
|
|
cases := map[string]string{
|
|
"field of another action": `[{"action":"click","x":1,"y":1},{"action":"type","text":"a","x":3}]`,
|
|
"outside the image": `[{"action":"click","x":1,"y":1},{"action":"click","x":640,"y":10}]`,
|
|
"half a coordinate": `[{"action":"click","x":1}]`,
|
|
"unknown action": `[{"action":"left_click","x":1,"y":1}]`,
|
|
"drag without an end": `[{"action":"drag","x":1,"y":1}]`,
|
|
"scroll without a way": `[{"action":"scroll"}]`,
|
|
"empty text": `[{"action":"type","text":""}]`,
|
|
}
|
|
for name, actions := range cases {
|
|
server, host := inputServer(t)
|
|
result := callTool(t, server, "computer_input", `{"vm":"macos1","actions":`+actions+`}`)
|
|
if result["isError"] != true {
|
|
t.Errorf("%s: expected an error, got %v", name, result)
|
|
}
|
|
if trace := inputTrace(host); len(trace) != 0 {
|
|
t.Errorf("%s: input was sent before the sequence was checked: %v", name, trace)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestComputerInputHoldsModifiersAroundPointerActions(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"click","x":5,"y":6,"modifiers":["cmd","shift"]},{"action":"scroll","direction":"up"},{"action":"scroll","direction":"right","amount":5}]}`)
|
|
expectTrace(t, host,
|
|
"move 5,6 in 640x360", "key cmd true", "key shift true", "click left", "key shift false", "key cmd false",
|
|
"scroll -3 false", "scroll 5 true", "screenshot")
|
|
}
|
|
|
|
func TestComputerInputDragAndHeldInputAreReleased(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"drag","x":1,"y":2,"x2":30,"y2":40},{"action":"mouse_down","button":"right"}]}`)
|
|
expectTrace(t, host,
|
|
"move 1,2 in 640x360", "button left true", "move 30,40 in 640x360", "button left false",
|
|
"button right true", "release [] [right]", "screenshot")
|
|
}
|
|
|
|
func TestComputerInputReleasesHeldInputWhenAStepFails(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
host.failOn = func(method string, args map[string]any) error {
|
|
if method == "host/console.pointer" && args["action"] == "click" {
|
|
return errors.New("console busy")
|
|
}
|
|
return nil
|
|
}
|
|
result := callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"key","key":"a"},{"action":"click","x":1,"y":1,"modifiers":["alt"]},{"action":"type","text":"never"}]}`)
|
|
info := structured(t, result)
|
|
if result["isError"] != true || info["failed_step"] != float64(1) || info["executed"] != float64(1) || !strings.Contains(info["error"].(string), "console busy") {
|
|
t.Fatalf("failure info = %v", info)
|
|
}
|
|
expectTrace(t, host, "key a <nil>", "move 1,1 in 640x360", "key alt true", "click left", "release [alt] []", "screenshot")
|
|
}
|
|
|
|
func TestComputerInputWaitScreenTimeoutStopsTheSequence(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
result := callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"wait_screen","until":"change","timeout_ms":300},{"action":"key","key":"return"}]}`)
|
|
info := structured(t, result)
|
|
if result["isError"] != true || info["timed_out"] != true || info["failed_step"] != float64(0) {
|
|
t.Fatalf("a wait that times out mid-sequence should stop it: %v", info)
|
|
}
|
|
for _, line := range inputTrace(host) {
|
|
if strings.HasPrefix(line, "key") {
|
|
t.Fatal("steps after a timed-out wait must not run")
|
|
}
|
|
}
|
|
// On the last step a timeout is reported, not treated as an error.
|
|
result = callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"key","key":"a"},{"action":"wait_screen","until":"change","timeout_ms":300}]}`)
|
|
if info := structured(t, result); result["isError"] == true || info["timed_out"] != true || info["executed"] != float64(1) {
|
|
t.Fatalf("a final wait timeout should only be reported: %v", info)
|
|
}
|
|
}
|
|
|
|
func TestComputerInputWaitScreenEndsOnTheChangedFrame(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
host.mu.Lock()
|
|
host.screens = append(host.screens, scriptedScreen{data: makeFrame(t, 64, 36, color.RGBA{B: 220, A: 255}, false), w: 640, h: 360})
|
|
host.mu.Unlock()
|
|
result := callTool(t, server, "computer_input", `{"vm":"macos1","actions":[{"action":"key","key":"return"},{"action":"wait_screen","stable_ms":0}]}`)
|
|
if info := structured(t, result); result["isError"] == true || info["timed_out"] != nil {
|
|
t.Fatalf("wait_screen should see the change: %v", info)
|
|
}
|
|
// The frame from the wait is returned without another capture.
|
|
trace := inputTrace(host)
|
|
if trace[len(trace)-1] != "screenshot" || len(trace) != 2 {
|
|
t.Fatalf("input calls = %v", trace)
|
|
}
|
|
}
|
|
|
|
func TestCancelledInputReleasesWhatItHolds(t *testing.T) {
|
|
server, host := inputServer(t)
|
|
result := make(chan json.RawMessage, 1)
|
|
go func() {
|
|
raw, _ := server.Handle(context.Background(), ChannelCore, "client-a", json.RawMessage(
|
|
`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"computer_input","arguments":{"vm":"macos1","actions":[{"action":"mouse_down","x":3,"y":4},{"action":"wait","duration_ms":10000}]}}}`))
|
|
result <- raw
|
|
}()
|
|
deadline := time.Now().Add(time.Second)
|
|
for len(host.callsTo("host/console.pointer")) < 2 {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the button was never pressed")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
server.SessionClosed(ChannelCore, "client-a")
|
|
select {
|
|
case raw := <-result:
|
|
if failure, _ := parseResponse(t, raw)["error"].(map[string]any); failure["code"] != float64(-32800) {
|
|
t.Fatalf("response = %s", raw)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("closing the session did not stop the input")
|
|
}
|
|
releases := host.callsTo("host/console.release")
|
|
if len(releases) != 1 || fmt.Sprint(releases[0]["buttons"]) != "[left]" {
|
|
t.Fatalf("the held button was not released: %v", releases)
|
|
}
|
|
}
|