Files
reims-agent/internal/mcp/tools.go
T
千川白浪andClaude Opus 5.5 d682620fe5 feat(mcp)!: 工具设计范式更新到 0.2.0 (#6)
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>
2026-12-04 23:52:45 -07:00

240 lines
11 KiB
Go

package mcp
// tool describes one MCP tool. Core tools are served on both channels; dev
// tools only on ChannelDev.
type tool struct {
name string
description string
schema map[string]any
dev bool
// locksVM serializes the tool with other display and input work on the
// same VM.
locksVM bool
readOnly bool
destructive bool
idempotent bool
}
func (t tool) listing() map[string]any {
annotations := map[string]any{"readOnlyHint": t.readOnly, "idempotentHint": t.idempotent}
if !t.readOnly {
annotations["destructiveHint"] = t.destructive
}
return map[string]any{"name": t.name, "description": t.description, "inputSchema": t.schema, "annotations": annotations}
}
func toolList(dev bool) []map[string]any {
list := make([]map[string]any, 0, len(tools))
for _, t := range tools {
if dev || !t.dev {
list = append(list, t.listing())
}
}
return list
}
func lookupTool(name string, dev bool) (tool, bool) {
for _, t := range tools {
if t.name == name && (dev || !t.dev) {
return t, true
}
}
return tool{}, false
}
func objectSchema(properties map[string]any, required ...string) map[string]any {
schema := map[string]any{"type": "object", "properties": properties, "additionalProperties": false}
if len(required) > 0 {
schema["required"] = required
}
return schema
}
func intRange(lo, hi int) map[string]any {
return map[string]any{"type": "integer", "minimum": lo, "maximum": hi}
}
func described(schema map[string]any, description string) map[string]any {
schema["description"] = description
return schema
}
var (
vmProp = map[string]any{"type": "string", "description": "VM name or reference from vm_list."}
stringProp = map[string]any{"type": "string"}
coordProp = map[string]any{"type": "integer", "minimum": 0}
scaleProp = map[string]any{"type": "number", "minimum": 0.25, "maximum": 1, "description": "Result image width as a share of 1280 px; default 1."}
imageProp = map[string]any{"type": "boolean", "description": "Attach the image; default true."}
untilProp = map[string]any{"type": "string", "enum": []string{"change", "stable", "change_then_stable"}, "description": "Default change_then_stable."}
)
var tools = []tool{
{
name: "vm_list", description: "List the virtual machines this plugin may use.",
schema: objectSchema(map[string]any{}),
readOnly: true, idempotent: true,
},
{
name: "vm_get", description: "Read one virtual machine's state, display and SSH settings.",
schema: objectSchema(map[string]any{"vm": vmProp}, "vm"),
readOnly: true, idempotent: true,
},
{
name: "vm_power", description: "Start, shut down, force-stop, suspend or resume a virtual machine; follow a boot with screen and wait.",
schema: objectSchema(map[string]any{
"vm": vmProp,
"action": map[string]any{"type": "string", "enum": []string{"start", "shutdown", "force_stop", "suspend", "resume"}},
}, "vm", "action"),
locksVM: true, destructive: true,
},
{
name: "vm_unlock", description: "Type the saved login password, press Return and return the screen once it settles.",
schema: objectSchema(map[string]any{"vm": vmProp}, "vm"),
locksVM: true,
},
{
name: "screen", description: "Return the display, optionally after waiting for it to change or settle, or one region of it in more detail; x/y everywhere are pixels of the last full screen image.",
schema: objectSchema(map[string]any{
"vm": vmProp,
"region": described(objectSchema(map[string]any{
"x": coordProp, "y": coordProp, "width": intRange(1, 10000), "height": intRange(1, 10000),
}, "x", "y", "width", "height"), "Area of the last full image to return at up to source resolution."),
"wait": objectSchema(map[string]any{
"until": untilProp,
"baseline": described(map[string]any{"type": "string"}, "sha256 to measure change from; default the last image."),
"stable_ms": described(intRange(0, maxScreenWaitMS), "Default 1000."),
"timeout_ms": described(intRange(1, maxScreenWaitMS), "Default 10000; a timeout returns the last frame with timed_out."),
"accept_blank": described(map[string]any{"type": "boolean"}, "Let a blank frame end the wait."),
}),
"scale": scaleProp,
"return_image": imageProp,
}, "vm"),
locksVM: true, readOnly: true,
},
{
name: "computer_input", description: "Send mouse and keyboard actions in order and return the screen after the last one; end with wait_screen when the UI needs time to respond.",
schema: objectSchema(map[string]any{
"vm": vmProp,
"actions": map[string]any{
"type": "array", "minItems": 1, "maxItems": maxInputActions,
"items": objectSchema(map[string]any{
"action": map[string]any{"type": "string", "enum": actionNames},
"x": coordProp,
"y": coordProp,
"x2": described(map[string]any{"type": "integer", "minimum": 0}, "Drag end x."),
"y2": described(map[string]any{"type": "integer", "minimum": 0}, "Drag end y."),
"button": map[string]any{"type": "string", "enum": []string{"left", "right", "middle"}},
"modifiers": map[string]any{"type": "array", "items": map[string]any{"type": "string", "enum": []string{"cmd", "ctrl", "alt", "shift"}}},
"key": described(map[string]any{"type": "string"}, "Key name such as return, esc, tab, space, backspace, delete, up, f5 or a; combos like cmd+c."),
"text": described(map[string]any{"type": "string"}, "Text to type; non-ASCII needs Guest Tools in the guest."),
"direction": map[string]any{"type": "string", "enum": []string{"up", "down", "left", "right"}},
"amount": described(intRange(1, maxScrollAmount), "Scroll steps; default 3."),
"duration_ms": described(intRange(1, maxStepWaitMS), "For wait and hold_key."),
"until": untilProp,
"stable_ms": intRange(0, maxStepWaitMS),
"timeout_ms": described(intRange(1, maxStepWaitMS), "Default 10000; a timeout stops the sequence."),
}, "action"),
},
"scale": scaleProp,
"return_image": imageProp,
}, "vm", "actions"),
locksVM: true, destructive: true,
},
{
name: "guest_wait_ready", description: "Wait for guest SSH and return the host fingerprint to pin and the plugin public key to authorize.",
schema: objectSchema(map[string]any{"vm": vmProp, "timeout_ms": described(intRange(1, 30000), "Default 10000.")}, "vm"),
dev: true, readOnly: true,
},
{
name: "guest_trust_host", description: "Pin the guest SSH host fingerprint after checking it against the live connection.",
schema: objectSchema(map[string]any{"vm": vmProp, "fingerprint": stringProp}, "vm", "fingerprint"),
dev: true, idempotent: true,
},
{
name: "guest_exec", description: "Run a shell command in the guest over SSH and return its exit code, bounded output and log_id.",
schema: objectSchema(map[string]any{
"vm": vmProp, "command": stringProp, "cwd": stringProp,
"env": map[string]any{"type": "object", "additionalProperties": stringProp},
"timeout_ms": described(intRange(1, 1800000), "Default 900000."),
}, "vm", "command"),
dev: true, destructive: true,
},
{
name: "guest_put", description: "Upload a host file to a path in the guest user's home directory.",
schema: objectSchema(map[string]any{"vm": vmProp, "local_path": stringProp, "remote_path": stringProp}, "vm", "local_path", "remote_path"),
dev: true, destructive: true, idempotent: true,
},
{
name: "guest_get", description: "Download a file from the guest user's home directory to a host path.",
schema: objectSchema(map[string]any{"vm": vmProp, "remote_path": stringProp, "local_path": stringProp}, "vm", "remote_path", "local_path"),
dev: true, destructive: true, idempotent: true,
},
{
name: "snapshot_list", description: "List a virtual machine's restore points.",
schema: objectSchema(map[string]any{"vm": vmProp}, "vm"),
dev: true, readOnly: true, idempotent: true,
},
{
name: "snapshot_create", description: "Create a restore point.",
schema: objectSchema(map[string]any{"vm": vmProp, "name": stringProp}, "vm", "name"),
dev: true, locksVM: true,
},
{
name: "snapshot_restore", description: "Restore a restore point on a temporary clone made by session_create.",
schema: objectSchema(map[string]any{"vm": vmProp, "name": stringProp}, "vm", "name"),
dev: true, locksVM: true, destructive: true,
},
{
name: "session_create", description: "Clone a virtual machine into a temporary copy owned by this plugin.",
schema: objectSchema(map[string]any{"vm": vmProp}, "vm"),
dev: true,
},
{
name: "session_dispose", description: "Stop and delete a temporary copy made by session_create.",
schema: objectSchema(map[string]any{"session_id": stringProp}, "session_id"),
dev: true, destructive: true, idempotent: true,
},
{
name: "test_run", description: "Start a test run on a temporary clone and return its run_id.",
schema: objectSchema(map[string]any{
"config": objectSchema(map[string]any{
"vm": stringProp,
"workspace": described(map[string]any{"type": "string"}, "Host folder copied into the guest before the steps."),
"boot_timeout": described(map[string]any{"type": "string"}, "Go duration to wait for guest SSH; default 4m."),
"sync": objectSchema(map[string]any{
"dest": stringProp,
"exclude": map[string]any{"type": "array", "items": stringProp},
}),
"steps": map[string]any{"type": "array", "minItems": 1, "items": objectSchema(map[string]any{
"name": stringProp, "run": stringProp, "cwd": stringProp,
"timeout": described(map[string]any{"type": "string"}, "Go duration; default 15m."),
}, "run")},
"junit": map[string]any{"type": "array", "items": stringProp},
"artifacts": map[string]any{"type": "array", "items": stringProp},
"keep_on_failure": map[string]any{"type": "boolean"},
}, "vm", "steps"),
}, "config"),
dev: true,
},
{
name: "test_wait", description: "Wait for a test run and return its status, with the result once it has finished.",
schema: objectSchema(map[string]any{"run_id": stringProp, "timeout_ms": described(intRange(0, 300000), "Default 30000; 0 returns at once.")}, "run_id"),
dev: true, readOnly: true,
},
{
name: "test_cancel", description: "Cancel a test run and delete its temporary clone.",
schema: objectSchema(map[string]any{"run_id": stringProp}, "run_id"),
dev: true, destructive: true, idempotent: true,
},
{
name: "logs_read", description: "Read a page of a guest_exec log_id or a test run_id log.",
schema: objectSchema(map[string]any{
"id": stringProp,
"offset": map[string]any{"type": "integer", "minimum": 0},
"limit": described(intRange(1, 65536), "Default 8192."),
}, "id"),
dev: true, readOnly: true, idempotent: true,
},
}