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>
140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package sidecar
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestReadFrameRejectsInvalidAndOversizedHeaders(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "duplicate length",
|
|
input: "Content-Length: 2\r\nContent-Length: 2\r\n\r\n{}",
|
|
wantErr: "duplicate",
|
|
},
|
|
{
|
|
name: "oversized header",
|
|
input: strings.Repeat("X", maxHeaderBytes+1) + "\n\n",
|
|
wantErr: "exceeds limit",
|
|
},
|
|
{
|
|
name: "oversized body",
|
|
input: "Content-Length: " + strconv.Itoa(maxFrameBytes+1) + "\r\n\r\n",
|
|
wantErr: "invalid",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
_, err := readFrame(bufio.NewReaderSize(strings.NewReader(test.input), 64*1024))
|
|
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
|
|
t.Fatalf("readFrame error = %v, want text %q", err, test.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWriteFrameCompletesShortWrites(t *testing.T) {
|
|
writer := &shortWriter{maxChunk: 3}
|
|
server := NewWithIO(strings.NewReader(""), writer, nil)
|
|
if err := server.writeFrame(envelope{JSONRPC: "2.0", ID: json.RawMessage("1"), Result: json.RawMessage(`{"ok":true}`)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reader := bufio.NewReader(bytes.NewReader(writer.Bytes()))
|
|
frame, err := readFrame(reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var response envelope
|
|
if err := json.Unmarshal(frame, &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(response.Result) != `{"ok":true}` {
|
|
t.Fatalf("response result = %s", response.Result)
|
|
}
|
|
}
|
|
|
|
func TestWriteFullStopsOnZeroProgress(t *testing.T) {
|
|
if err := writeFull(zeroWriter{}, []byte("payload")); !errors.Is(err, io.ErrShortWrite) {
|
|
t.Fatalf("writeFull error = %v, want io.ErrShortWrite", err)
|
|
}
|
|
}
|
|
|
|
type recordingHandler struct {
|
|
handled []string
|
|
closed []string
|
|
}
|
|
|
|
func (h *recordingHandler) Handle(_ context.Context, channel, session string, _ json.RawMessage) (json.RawMessage, error) {
|
|
h.handled = append(h.handled, channel+"/"+session)
|
|
return json.RawMessage(`{"ok":true}`), nil
|
|
}
|
|
|
|
func (h *recordingHandler) SessionClosed(channel, session string) {
|
|
h.closed = append(h.closed, channel+"/"+session)
|
|
}
|
|
|
|
func TestDispatchRoutesBothChannelsWithSessions(t *testing.T) {
|
|
handler := &recordingHandler{}
|
|
var out bytes.Buffer
|
|
server := NewWithIO(strings.NewReader(""), &out, handler)
|
|
ctx := context.Background()
|
|
server.dispatch(ctx, envelope{JSONRPC: "2.0", ID: json.RawMessage("1"), Method: "plugin/initialize"})
|
|
server.dispatch(ctx, envelope{JSONRPC: "2.0", ID: json.RawMessage("2"), Method: "channel/mcp/message", Params: json.RawMessage(`{"message":{},"session":"a"}`)})
|
|
server.dispatch(ctx, envelope{JSONRPC: "2.0", ID: json.RawMessage("3"), Method: "channel/mcp-dev/message", Params: json.RawMessage(`{"message":{}}`)})
|
|
server.dispatch(ctx, envelope{JSONRPC: "2.0", Method: "channel/mcp/closed", Params: json.RawMessage(`{"session":"a"}`)})
|
|
server.dispatch(ctx, envelope{JSONRPC: "2.0", ID: json.RawMessage("4"), Method: "channel/other/message", Params: json.RawMessage(`{"message":{}}`)})
|
|
|
|
if strings.Join(handler.handled, ",") != "mcp/a,mcp-dev/" || strings.Join(handler.closed, ",") != "mcp/a" {
|
|
t.Fatalf("handled=%v closed=%v", handler.handled, handler.closed)
|
|
}
|
|
reader := bufio.NewReader(&out)
|
|
var responses []envelope
|
|
for {
|
|
frame, err := readFrame(reader)
|
|
if err != nil {
|
|
break
|
|
}
|
|
var response envelope
|
|
if err := json.Unmarshal(frame, &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
responses = append(responses, response)
|
|
}
|
|
if len(responses) != 4 {
|
|
t.Fatalf("got %d responses, want 4 (the closed notification has none)", len(responses))
|
|
}
|
|
if !strings.Contains(string(responses[0].Result), `"channels":["mcp","mcp-dev"]`) {
|
|
t.Fatalf("initialize = %s", responses[0].Result)
|
|
}
|
|
if responses[3].Error == nil {
|
|
t.Fatal("a channel this sidecar does not serve must be refused")
|
|
}
|
|
}
|
|
|
|
type shortWriter struct {
|
|
bytes.Buffer
|
|
maxChunk int
|
|
}
|
|
|
|
func (w *shortWriter) Write(data []byte) (int, error) {
|
|
if len(data) > w.maxChunk {
|
|
data = data[:w.maxChunk]
|
|
}
|
|
return w.Buffer.Write(data)
|
|
}
|
|
|
|
type zeroWriter struct{}
|
|
|
|
func (zeroWriter) Write([]byte) (int, error) { return 0, nil }
|