mirror of
https://github.com/browseros-ai/BrowserOS.git
synced 2026-05-20 12:30:04 +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>
196 lines
4.8 KiB
Go
196 lines
4.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"browseros-cli/config"
|
|
"browseros-cli/mcp"
|
|
"browseros-cli/output"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
serverURL string
|
|
pageFlag int
|
|
pageSet bool
|
|
jsonOut bool
|
|
debug bool
|
|
timeout time.Duration
|
|
version = "dev"
|
|
)
|
|
|
|
func SetVersion(v string) {
|
|
version = v
|
|
}
|
|
|
|
var (
|
|
helpHeaderColor = color.New(color.Bold, color.FgCyan)
|
|
helpCmdColor = color.New(color.FgHiGreen)
|
|
helpAliasColor = color.New(color.FgYellow)
|
|
helpHintColor = color.New(color.Faint)
|
|
)
|
|
|
|
func helpHeader(s string) string { return helpHeaderColor.Sprint(s) }
|
|
func helpCmdCol(s string) string { return helpCmdColor.Sprint(s) }
|
|
func helpHint(s string) string { return helpHintColor.Sprint(s) }
|
|
func helpAliases(aliases []string) string {
|
|
return helpAliasColor.Sprintf("(aliases: %s)", strings.Join(aliases, ", "))
|
|
}
|
|
|
|
var groupOrder = []string{
|
|
"Navigate:",
|
|
"Observe:",
|
|
"Input:",
|
|
"Resources:",
|
|
"Setup:",
|
|
}
|
|
|
|
func groupedHelp(cmd *cobra.Command) string {
|
|
groups := map[string][]*cobra.Command{}
|
|
for _, c := range cmd.Commands() {
|
|
if !c.IsAvailableCommand() && c.Name() != "help" {
|
|
continue
|
|
}
|
|
g := c.Annotations["group"]
|
|
if g == "" {
|
|
g = "Setup:"
|
|
}
|
|
groups[g] = append(groups[g], c)
|
|
}
|
|
|
|
var b strings.Builder
|
|
for _, name := range groupOrder {
|
|
cmds, ok := groups[name]
|
|
if !ok {
|
|
continue
|
|
}
|
|
b.WriteString("\n" + helpHeader(name) + "\n")
|
|
for _, c := range cmds {
|
|
line := " " + helpCmdCol(fmt.Sprintf("%-14s", c.Name())) + " " + c.Short
|
|
if len(c.Aliases) > 0 {
|
|
line += " " + helpAliases(c.Aliases)
|
|
}
|
|
b.WriteString(line + "\n")
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
const usageTemplate = `{{helpHeader "Usage:"}}{{if .Runnable}}
|
|
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
|
|
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
|
|
|
|
{{helpHeader "Aliases:"}}
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
|
|
|
{{helpHeader "Examples:"}}
|
|
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
|
{{groupedHelp .}}{{end}}{{if .HasAvailableLocalFlags}}
|
|
|
|
{{helpHeader "Flags:"}}
|
|
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
|
|
|
|
{{helpHeader "Global Flags:"}}
|
|
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
|
|
|
|
{{helpHint (printf "Use \"%s [command] --help\" for more information." .CommandPath)}}{{end}}
|
|
`
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "browseros-cli",
|
|
Short: "Browser control CLI for BrowserOS",
|
|
Long: "browseros-cli — command-line interface for controlling BrowserOS via MCP",
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
}
|
|
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
cobra.AddTemplateFunc("helpHeader", helpHeader)
|
|
cobra.AddTemplateFunc("helpCmdCol", helpCmdCol)
|
|
cobra.AddTemplateFunc("helpAliases", helpAliases)
|
|
cobra.AddTemplateFunc("helpHint", helpHint)
|
|
cobra.AddTemplateFunc("groupedHelp", groupedHelp)
|
|
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&serverURL, "server", "s", defaultServerURL(), "BrowserOS server URL")
|
|
rootCmd.PersistentFlags().IntVarP(&pageFlag, "page", "p", 0, "Target page ID (default: active page)")
|
|
rootCmd.PersistentFlags().BoolVar(&jsonOut, "json", envBool("BOS_JSON"), "JSON output")
|
|
rootCmd.PersistentFlags().BoolVar(&debug, "debug", envBool("BOS_DEBUG"), "Debug output")
|
|
rootCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", 120*time.Second, "Request timeout")
|
|
|
|
rootCmd.Version = version
|
|
}
|
|
|
|
func newClient() *mcp.Client {
|
|
baseURL, err := validateServerURL(serverURL)
|
|
if err != nil {
|
|
output.Error(err.Error(), 1)
|
|
}
|
|
|
|
c := mcp.NewClient(baseURL, version, timeout)
|
|
c.Debug = debug
|
|
return c
|
|
}
|
|
|
|
func resolvePageID(c *mcp.Client) (int, error) {
|
|
if rootCmd.PersistentFlags().Changed("page") {
|
|
return pageFlag, nil
|
|
}
|
|
|
|
if env := os.Getenv("BROWSEROS_PAGE"); env != "" {
|
|
if v, err := strconv.Atoi(env); err == nil {
|
|
return v, nil
|
|
}
|
|
}
|
|
|
|
return c.ResolvePageID(nil)
|
|
}
|
|
|
|
func envBool(key string) bool {
|
|
v := os.Getenv(key)
|
|
return v == "1" || v == "true"
|
|
}
|
|
|
|
func defaultServerURL() string {
|
|
if env := normalizeServerURL(os.Getenv("BROWSEROS_URL")); env != "" {
|
|
return env
|
|
}
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return normalizeServerURL(cfg.ServerURL)
|
|
}
|
|
|
|
func normalizeServerURL(raw string) string {
|
|
normalized := strings.TrimSpace(raw)
|
|
normalized = strings.TrimSuffix(normalized, "/mcp")
|
|
return strings.TrimSuffix(normalized, "/")
|
|
}
|
|
|
|
func validateServerURL(raw string) (string, error) {
|
|
baseURL := normalizeServerURL(raw)
|
|
if baseURL != "" {
|
|
return baseURL, nil
|
|
}
|
|
|
|
return "", fmt.Errorf(
|
|
"BrowserOS server URL is not configured.\n Open BrowserOS -> Settings -> BrowserOS MCP and copy the Server URL.\n Then run: browseros-cli init",
|
|
)
|
|
}
|