Files
supermarket/hermes/setup.sh
T
davidandClaude Opus 5.5 d039484a4a Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a
container as a CoreDesktop sibling:

- cmd/supermarket server with htmx web UI: dashboard, company profile,
  inventory (partners/competitors), AI and credential settings, and a
  Core & Hermes page
- SQLite storage and a secrets store under ~/.local/share/supermarket
- AI providers (Anthropic, OpenAI-compatible) behind one interface
- MCP server and a Hermes bridge to the Federated Core MCP hub
- Import of the old Coremarketing TUI's state
- install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the
  bin/supermarket launcher and hermes/setup.sh

Posting is not wired back up yet; that comes in Phase 1. See PLAN.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 08:38:09 -07:00

169 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
#
# Purpose : Connect CoreDesktop's Hermes agent to the Core's MCP hub and to
# Supermarket. Hermes is the only thing that talks to the hub: it
# carries out the CRM work Supermarket queues. Run by install.sh,
# and by `supermarket hermes` to set it up again. Safe to re-run.
#
# It adds two MCP servers to ~/.hermes/config.yaml, with no secret in that
# file: Hermes fills in ${...} from ~/.hermes/.env when it connects.
#
# mcp_servers:
# mcphub:
# url: "https://mcphub.<core domain>/sse?as_user=<user>"
# headers:
# Authorization: "Basic ${MCPHUB_AUTH}"
# supermarket:
# url: "http://127.0.0.1:8470/mcp"
# headers:
# Authorization: "Bearer ${SUPERMARKET_MCP_TOKEN}"
#
# Hermes runs in CoreDesktop's sandbox, which blocks both servers until
# they're added to ~/.config/coredesktop/sandbox/hermes.json, a file no
# sandbox can write; this script asks before adding them.
# ${...} in single quotes is meant literally: Hermes expands it, not bash.
# shellcheck disable=SC2016
set -e
SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
source "$SELF/../lib.sh"
HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"
HERMES_ENV="$HERMES_HOME/.env"
HERMES_CONFIG="$HERMES_HOME/config.yaml"
HERMES_SANDBOX_CONF="$HOME/.config/coredesktop/sandbox/hermes.json"
MCP_LOCAL="127.0.0.1:8470"
if [ ! -x "$HOME/.local/bin/hermes" ]; then
echo "Hermes isn't installed, so Supermarket can't reach your Core's CRM yet."
echo "Install CoreDesktop's agents, run \`configure hermes\`, then: supermarket hermes"
exit 0
fi
# CoreDesktop's `hermes` runs it sandboxed; settings are changed with the
# real command, as `configure hermes` does.
if [ -x "$COREDESKTOP/sandbox/bin/unsandboxed" ]; then
HERMES=("$COREDESKTOP/sandbox/bin/unsandboxed" hermes)
else
HERMES=("$HOME/.local/bin/hermes")
fi
# set_env KEY VALUE: set a line in Hermes' .env, readable only by you. The
# value never appears on a command line.
set_env() {
local tmp
mkdir -p "$HERMES_HOME"
touch "$HERMES_ENV" && chmod 600 "$HERMES_ENV"
tmp="$(mktemp "$HERMES_ENV.XXXXXX")"
grep -v "^$1=" "$HERMES_ENV" > "$tmp" || true
printf '%s=%s\n' "$1" "$2" >> "$tmp"
chmod 600 "$tmp" && mv -f "$tmp" "$HERMES_ENV"
}
# set_server NAME URL AUTH: add or update an MCP server in Hermes' config.
set_server() {
"${HERMES[@]}" config set "mcp_servers.$1.url" "$2" > /dev/null
"${HERMES[@]}" config set "mcp_servers.$1.headers.Authorization" "$3" > /dev/null
}
# server_configured NAME: true if Hermes' config has an MCP server NAME
server_configured() {
[ -f "$HERMES_CONFIG" ] && "${HERMES[@]}" config get "mcp_servers.$1.url" 2> /dev/null | grep -q '://'
}
allowed() {
[ -f "$HERMES_SANDBOX_CONF" ] && jq -e --arg host "$1" \
'(.network.allowedDomains // []) | index($host) != null' "$HERMES_SANDBOX_CONF" > /dev/null 2>&1
}
allow() {
local tmp
mkdir -p "$(dirname "$HERMES_SANDBOX_CONF")"
[ -s "$HERMES_SANDBOX_CONF" ] || echo '{}' > "$HERMES_SANDBOX_CONF"
tmp="$(mktemp "$HERMES_SANDBOX_CONF.XXXXXX")"
if jq --arg host "$1" '.network.allowedDomains = ((.network.allowedDomains // []) - [$host] + [$host])' \
"$HERMES_SANDBOX_CONF" > "$tmp"; then
mv -f "$tmp" "$HERMES_SANDBOX_CONF"
else
rm -f "$tmp"
echo "Couldn't change $HERMES_SANDBOX_CONF; is it valid JSON?"
return 1
fi
}
echo "Connecting Hermes to Supermarket and to your Core's MCP hub."
# --- Supermarket ---
set_env SUPERMARKET_MCP_TOKEN "$(tr -d '\n' < "$DATA/mcp-token")"
set_server supermarket "http://$MCP_LOCAL/mcp" 'Bearer ${SUPERMARKET_MCP_TOKEN}'
# --- The Core's MCP hub ---
hub_host=""
if [ -f "$CORE_CONF" ]; then
# shellcheck source=/dev/null
source "$CORE_CONF"
fi
if [ -z "$CORE_DOMAIN" ]; then
echo "No Federated Core is set up (CoreDesktop's \`configure core\`), so Hermes can't reach a CRM yet."
echo "Once it is, run: supermarket hermes"
elif server_configured mcphub && [ "${1:-}" != "--reconfigure" ]; then
hub_url="$("${HERMES[@]}" config get mcp_servers.mcphub.url 2> /dev/null | grep -o 'https\?://[^" ]*' | head -n 1)"
hub_host="$(sed -E 's#^https?://([^/:]+).*#\1#' <<< "$hub_url")"
echo "Hermes already connects to the MCP hub at $hub_url (to change it: supermarket hermes --reconfigure)."
else
user="${CORE_EMAIL%@*}"
default_url="https://mcphub.$CORE_DOMAIN/sse?as_user=$user"
echo
echo "Your Core's MCP hub lets Hermes use the Core's apps, such as EspoCRM."
read -rp "Its address [$default_url]: " hub_url
hub_url="${hub_url:-$default_url}"
read -rp "The hub's user name [$CORE_EMAIL]: " hub_user
hub_user="${hub_user:-$CORE_EMAIL}"
read -rsp "Its password (not shown): " hub_password
echo
if [ -z "$hub_password" ]; then
echo "No password given; skipping the MCP hub. Run \`supermarket hermes\` to try again."
else
set_env MCPHUB_AUTH "$(printf '%s:%s' "$hub_user" "$hub_password" | base64 -w0)"
unset hub_password
set_server mcphub "$hub_url" 'Basic ${MCPHUB_AUTH}'
hub_host="$(sed -E 's#^https?://([^/:]+).*#\1#' <<< "$hub_url")"
echo "Hermes will connect to $hub_url. The password is in $HERMES_ENV, readable only by you."
fi
fi
# Hermes must write ${...} into its config, not the secret itself.
if ! grep -qF '${SUPERMARKET_MCP_TOKEN}' "$HERMES_CONFIG" 2> /dev/null; then
echo "Warning: $HERMES_CONFIG doesn't hold the \${SUPERMARKET_MCP_TOKEN} reference as expected; check its mcp_servers section."
fi
# --- Hermes' sandbox ---
need=()
for host in "$MCP_LOCAL" $hub_host; do
allowed "$host" || need+=("$host")
done
if [ "${#need[@]}" -gt 0 ]; then
echo
echo "Hermes runs in CoreDesktop's sandbox, which only lets it reach servers you've allowed."
if confirm_yes "Let Hermes reach ${need[*]}?"; then
for host in "${need[@]}"; do allow "$host"; done
echo "Allowed in $HERMES_SANDBOX_CONF."
else
echo "Not allowed: Hermes can't use these servers until they're in network.allowedDomains in $HERMES_SANDBOX_CONF."
fi
fi
# The gateway only reads the config when it starts. It's usually started on
# demand (and exits when idle) rather than run as a service: stop it, and
# Hermes starts a fresh one the next time it's used.
if systemctl --user is-active --quiet hermes-gateway 2> /dev/null; then
systemctl --user restart hermes-gateway && echo "Restarted Hermes' gateway so it loads the servers."
elif "${HERMES[@]}" gateway stop > /dev/null 2>&1; then
echo "Stopped Hermes' gateway; the next time you use Hermes, it starts again with the new servers."
fi
echo
echo "To check that it works, open Supermarket's \"Core & Hermes\" page and ask Hermes the question shown there."