Files
BrowserOS/apps/cli/output/printer.go
Nikhil b7e63a4a1f feat: add browseros-cli Go CLI for browser automation (#421)
* 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>
2026-03-05 14:49:45 -08:00

136 lines
2.7 KiB
Go

package output
import (
"encoding/json"
"fmt"
"os"
"browseros-cli/mcp"
"github.com/fatih/color"
)
var (
errColor = color.New(color.FgRed, color.Bold)
dimColor = color.New(color.Faint)
boldColor = color.New(color.Bold)
)
// JSON outputs raw JSON to stdout. If structuredContent exists, use it;
// otherwise fall back to serializing the full result.
func JSON(result *mcp.ToolResult) {
if result.StructuredContent != nil {
data, _ := json.Marshal(result.StructuredContent)
fmt.Println(string(data))
return
}
data, _ := json.Marshal(result)
fmt.Println(string(data))
}
// JSONRaw outputs any value as JSON.
func JSONRaw(v any) {
data, _ := json.Marshal(v)
fmt.Println(string(data))
}
// Text prints a text result to stdout.
func Text(result *mcp.ToolResult) {
fmt.Println(result.TextContent())
}
// Confirm prints a short confirmation message.
func Confirm(msg string) {
fmt.Println(msg)
}
// Error prints an error to stderr and exits with the given code.
func Error(msg string, code int) {
errColor.Fprintf(os.Stderr, "Error: %s\n", msg)
os.Exit(code)
}
// Errorf formats and prints an error to stderr and exits.
func Errorf(code int, format string, args ...any) {
errColor.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
os.Exit(code)
}
// PageList formats a list of pages for human display.
func PageList(result *mcp.ToolResult) {
if result.StructuredContent == nil {
Text(result)
return
}
pages, ok := result.StructuredContent["pages"].([]any)
if !ok {
Text(result)
return
}
if len(pages) == 0 {
fmt.Println("No pages open.")
return
}
for _, p := range pages {
page, ok := p.(map[string]any)
if !ok {
continue
}
pageID := intVal(page["pageId"])
tabID := intVal(page["tabId"])
title := strVal(page["title"])
url := strVal(page["url"])
active := boolVal(page["isActive"])
marker := ""
if active {
marker = " " + boldColor.Sprint("[ACTIVE]")
}
fmt.Printf(" %d. %s (tab %d)%s\n", pageID, title, tabID, marker)
fmt.Printf(" %s\n", dimColor.Sprint(url))
}
}
// ActivePage formats a single active page for human display.
func ActivePage(result *mcp.ToolResult) {
if result.StructuredContent == nil {
Text(result)
return
}
sc := result.StructuredContent
pageID := intVal(sc["pageId"])
tabID := intVal(sc["tabId"])
title := strVal(sc["title"])
url := strVal(sc["url"])
fmt.Printf("Active page: %d (tab %d)\n", pageID, tabID)
fmt.Println(title)
fmt.Println(dimColor.Sprint(url))
}
func intVal(v any) int {
if f, ok := v.(float64); ok {
return int(f)
}
return 0
}
func strVal(v any) string {
if s, ok := v.(string); ok {
return s
}
return ""
}
func boolVal(v any) bool {
if b, ok := v.(bool); ok {
return b
}
return false
}