#!/usr/bin/env bash set -euo pipefail URL="${BRAVE_MCP_URL:-http://192.168.153.113:18802/mcp}" if [[ $# -lt 1 ]]; then echo 'Usage: scripts/search.sh "query" '\''{"count":5}'\''' >&2 exit 1 fi QUERY="$1" OPTS_JSON_RAW="${2:-{}}" OPTS_JSON="$(printf '%s' "$OPTS_JSON_RAW" | jq -c '. // {}')" ARGS_JSON="$(jq -cn --arg q "$QUERY" --arg opts "$OPTS_JSON" ' ($opts | fromjson) as $o | { query: $q, country: ($o.country // "US"), search_lang: ($o.search_lang // "en"), ui_lang: ($o.ui_lang // "en-US"), count: ($o.count // 5), freshness: ($o.freshness // empty), safesearch: ($o.safesearch // empty) } ')" RAW="$(mcporter call --allow-http "$URL.brave_web_search" --args "$ARGS_JSON" --output json)" printf '%s\n' "$RAW" | jq -r ' def results: if type=="array" then . elif .content? then ([.content[]? | select(.type=="text") | .text | fromjson?] | map(select(. != null)) | .[0]) else . end; results as $r | if ($r|type) == "array" then ("Brave MCP search results (" + (($r|length)|tostring) + ")"), "", ($r[] | "- " + (.title // "(untitled)") + "\n " + (.url // "") + (if .description then "\n " + .description else "" end) + "\n") else $r end '