mirror of
https://github.com/browseros-ai/BrowserOS.git
synced 2026-05-19 11:31:03 +00:00
* feat: add browseros-cli Go CLI for browser automation Implements a full-featured CLI that communicates with the BrowserOS MCP server over JSON-RPC 2.0 / StreamableHTTP. Covers all 54 MCP tools across 10 categories with a hybrid command structure (flat verbs for hot-path commands, grouped noun-verb for resource management). - MCP client with initialize + tools/call pattern, thread-safe request IDs - Dual output: human-readable default, --json for structured/piped usage - Implicit active page resolution with --page override - 21 command files: open, nav, snap, click, fill, scroll, eval, ss, pdf, dom, wait, dialog, pages, window, bookmark, history, group, health, info - Cobra CLI framework with fatih/color for terminal formatting Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add end-to-end integration tests for browseros-cli Go integration tests gated by `//go:build integration` that exercise the CLI binary against a running BrowserOS server. Tests build the binary, run commands via exec.Command, and verify JSON output. Covers: health, version, page lifecycle (open → text → snap → eval → screenshot → nav → reload → close), active page, info, error handling, and invalid page ID rejection. Skips gracefully when no server is running. Run with: go test -tags integration -v ./... Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add init command and fix MCP client bugs - Add `browseros-cli init` command that prompts for the server URL, verifies connectivity, and saves to ~/.config/browseros-cli/config.json - Config priority: --server flag > BROWSEROS_URL env > config file > default - Fix Accept header: include text/event-stream (required by StreamableHTTPTransport) - Fix nil args: send empty object {} instead of null for tools with no params - Update error messages to suggest `browseros-cli init` on connection failure Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: add README for browseros-cli with setup, usage, and testing guide Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: always send arguments object in MCP tools/call Go's json omitempty omits empty maps, causing the arguments field to be missing from tools/call requests. The MCP SDK requires arguments to be an object (even empty {}), not undefined. Remove omitempty from the tag. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: update help menu to be have groups * refactor: replace hand-rolled MCP client with official Go SDK Switch from custom JSON-RPC implementation to the official github.com/modelcontextprotocol/go-sdk. This removes all hand-rolled protocol types (jsonrpcRequest, jsonrpcResponse, RPCError, etc.) and uses the SDK's StreamableClientTransport with DisableStandaloneSSE for clean CLI process lifecycle. Also adds URL normalization/validation, config command, and updates init/README to reference YAML config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
176 lines
4.3 KiB
Go
176 lines
4.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"browseros-cli/output"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func init() {
|
|
hoverCmd := &cobra.Command{
|
|
Use: "hover <element>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Hover over an element",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: elementAction("hover"),
|
|
}
|
|
|
|
focusCmd := &cobra.Command{
|
|
Use: "focus <element>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Focus an element",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: elementAction("focus"),
|
|
}
|
|
|
|
checkCmd := &cobra.Command{
|
|
Use: "check <element>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Check a checkbox or radio button",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: elementAction("check"),
|
|
}
|
|
|
|
uncheckCmd := &cobra.Command{
|
|
Use: "uncheck <element>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Uncheck a checkbox",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: elementAction("uncheck"),
|
|
}
|
|
|
|
selectCmd := &cobra.Command{
|
|
Use: "select <element> <value>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Select a dropdown option",
|
|
Args: cobra.MinimumNArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var element int
|
|
if _, err := fmt.Sscanf(args[0], "%d", &element); err != nil {
|
|
output.Errorf(3, "invalid element ID: %s", args[0])
|
|
}
|
|
value := strings.Join(args[1:], " ")
|
|
|
|
c := newClient()
|
|
pageID, err := resolvePageID(c)
|
|
if err != nil {
|
|
output.Error(err.Error(), 2)
|
|
}
|
|
result, err := c.CallTool("select_option", map[string]any{
|
|
"page": pageID,
|
|
"element": element,
|
|
"value": value,
|
|
})
|
|
if err != nil {
|
|
output.Error(err.Error(), 1)
|
|
}
|
|
if jsonOut {
|
|
output.JSON(result)
|
|
} else {
|
|
output.Confirm(result.TextContent())
|
|
}
|
|
},
|
|
}
|
|
|
|
dragCmd := &cobra.Command{
|
|
Use: "drag <source> --to <target>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Drag from one element to another",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var source int
|
|
if _, err := fmt.Sscanf(args[0], "%d", &source); err != nil {
|
|
output.Errorf(3, "invalid source element: %s", args[0])
|
|
}
|
|
target, _ := cmd.Flags().GetInt("to")
|
|
|
|
c := newClient()
|
|
pageID, err := resolvePageID(c)
|
|
if err != nil {
|
|
output.Error(err.Error(), 2)
|
|
}
|
|
result, err := c.CallTool("drag", map[string]any{
|
|
"page": pageID,
|
|
"sourceElement": source,
|
|
"targetElement": target,
|
|
})
|
|
if err != nil {
|
|
output.Error(err.Error(), 1)
|
|
}
|
|
if jsonOut {
|
|
output.JSON(result)
|
|
} else {
|
|
output.Confirm(result.TextContent())
|
|
}
|
|
},
|
|
}
|
|
dragCmd.Flags().Int("to", 0, "Target element ID")
|
|
_ = dragCmd.MarkFlagRequired("to")
|
|
|
|
uploadCmd := &cobra.Command{
|
|
Use: "upload <element> <file...>",
|
|
Annotations: map[string]string{"group": "Input:"},
|
|
Short: "Upload files to a file input",
|
|
Args: cobra.MinimumNArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var element int
|
|
if _, err := fmt.Sscanf(args[0], "%d", &element); err != nil {
|
|
output.Errorf(3, "invalid element ID: %s", args[0])
|
|
}
|
|
|
|
c := newClient()
|
|
pageID, err := resolvePageID(c)
|
|
if err != nil {
|
|
output.Error(err.Error(), 2)
|
|
}
|
|
result, err := c.CallTool("upload_file", map[string]any{
|
|
"page": pageID,
|
|
"element": element,
|
|
"files": args[1:],
|
|
})
|
|
if err != nil {
|
|
output.Error(err.Error(), 1)
|
|
}
|
|
if jsonOut {
|
|
output.JSON(result)
|
|
} else {
|
|
output.Confirm(result.TextContent())
|
|
}
|
|
},
|
|
}
|
|
|
|
rootCmd.AddCommand(hoverCmd, focusCmd, checkCmd, uncheckCmd, selectCmd, dragCmd, uploadCmd)
|
|
}
|
|
|
|
// elementAction creates a simple element-based tool command.
|
|
func elementAction(toolName string) func(*cobra.Command, []string) {
|
|
return func(cmd *cobra.Command, args []string) {
|
|
var element int
|
|
if _, err := fmt.Sscanf(args[0], "%d", &element); err != nil {
|
|
output.Errorf(3, "invalid element ID: %s", args[0])
|
|
}
|
|
|
|
c := newClient()
|
|
pageID, err := resolvePageID(c)
|
|
if err != nil {
|
|
output.Error(err.Error(), 2)
|
|
}
|
|
|
|
result, err := c.CallTool(toolName, map[string]any{
|
|
"page": pageID,
|
|
"element": element,
|
|
})
|
|
if err != nil {
|
|
output.Error(err.Error(), 1)
|
|
}
|
|
if jsonOut {
|
|
output.JSON(result)
|
|
} else {
|
|
output.Confirm(result.TextContent())
|
|
}
|
|
}
|
|
}
|