#!/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./sse?as_user=" # transport: sse # headers: # Authorization: "Basic ${MCPHUB_AUTH}" # supermarket: # command: socat # args: ["STDIO", "UNIX-CONNECT:/mcp.sock"] # # Hermes runs in CoreDesktop's sandbox, which has its own network: there, # 127.0.0.1:8470 isn't this computer's, and the sandbox's proxy won't carry # loopback traffic. So Hermes reaches Supermarket through the socket the app # keeps in its data folder (Unix sockets are allowed in Hermes' sandbox). The # hub is blocked until it's added to ~/.config/coredesktop/sandbox/hermes.json, # a file no sandbox can write; this script asks before adding it. # ${...} 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_SOCKET="$DATA/mcp.sock" # What earlier versions of this script set up, before the socket OLD_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" } # unset_env KEY: remove a line from Hermes' .env unset_env() { local tmp [ -f "$HERMES_ENV" ] && grep -q "^$1=" "$HERMES_ENV" || return 0 tmp="$(mktemp "$HERMES_ENV.XXXXXX")" grep -v "^$1=" "$HERMES_ENV" > "$tmp" || true 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 set_transport "$1" "$2" } # set_transport NAME URL: a hub address ending in /sse speaks only the older # SSE transport. Hermes tries Streamable HTTP first and falls back to SSE, # but the failed first try can use up its 60-second connect time, and then # the hub's tools never load. So name the transport outright. set_transport() { if [[ "$2" =~ /sse/?(\?|$) ]]; then "${HERMES[@]}" config set "mcp_servers.$1.transport" sse > /dev/null else "${HERMES[@]}" config unset "mcp_servers.$1.transport" > /dev/null 2>&1 || true fi } # 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 '://' } disallow() { local tmp allowed "$1" || return 0 tmp="$(mktemp "$HERMES_SANDBOX_CONF.XXXXXX")" if jq --arg host "$1" '.network.allowedDomains -= [$host]' "$HERMES_SANDBOX_CONF" > "$tmp"; then mv -f "$tmp" "$HERMES_SANDBOX_CONF" else rm -f "$tmp" fi } 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 --- if ! command -v socat > /dev/null; then echo "Hermes reaches Supermarket with socat, which isn't installed: sudo pacman -S socat, then: supermarket hermes" exit 1 fi # Drop the HTTP settings earlier versions used; Hermes picks the transport # from whichever of url and command is there. "${HERMES[@]}" config unset mcp_servers.supermarket.url > /dev/null 2>&1 || true "${HERMES[@]}" config unset mcp_servers.supermarket.headers > /dev/null 2>&1 || true "${HERMES[@]}" config set mcp_servers.supermarket.command socat > /dev/null "${HERMES[@]}" config set mcp_servers.supermarket.args "[\"STDIO\", \"UNIX-CONNECT:$MCP_SOCKET\"]" > /dev/null unset_env SUPERMARKET_MCP_TOKEN disallow "$OLD_MCP_LOCAL" # --- 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")" set_transport mcphub "$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}" # The Core gives each user their own token for the hub: its MCP settings # read `Authorization: "Basic "`. echo "Your Core gives you your own token for it, in an MCP block like: Authorization: \"Basic \"" read -rsp "Paste the token, the part after \"Basic \" (not shown): " hub_token echo # Forgive pasting the whole header value, or stray quotes and spaces hub_token="$(tr -d '"'"'"' \t\r\n' <<< "${hub_token#*Basic }")" hub_token="${hub_token#Basic}" if [ -z "$hub_token" ]; then echo "No token given; skipping the MCP hub. Run \`supermarket hermes --reconfigure\` to try again." else # The hub streams once it accepts us, so only the status line matters code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \ -H "Authorization: Basic $hub_token" -H 'Accept: text/event-stream' "$hub_url" 2> /dev/null || true)" case "$code" in 200) echo "The hub accepted the token." ;; 401 | 403) echo "Warning: the hub turned the token down (HTTP $code); saving it anyway." echo " Check that it's your own token for this hub, copied whole, and that the address's" echo " as_user= is your user name. Get a current one from your Core, then: supermarket hermes --reconfigure" ;; *) echo "Couldn't check the token with the hub (HTTP ${code:-no answer}); saving it anyway." ;; esac set_env MCPHUB_AUTH "$hub_token" unset hub_token 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 token is in $HERMES_ENV, readable only by you." fi fi if ! "${HERMES[@]}" config get mcp_servers.supermarket.args 2> /dev/null | grep -qF "$MCP_SOCKET"; then echo "Warning: $HERMES_CONFIG doesn't point Hermes at $MCP_SOCKET as expected; check its mcp_servers section." fi # --- Hermes' sandbox --- need=() for host in $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. # A gateway started inside CoreDesktop's sandbox doesn't always answer # `gateway stop` from outside it, so check its pid file rather than trust the # exit code: a gateway left running would keep the old servers. gateway_pid="$HERMES_HOME/gateway.pid" 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 [ -s "$gateway_pid" ]; then before="$(cat "$gateway_pid")" "${HERMES[@]}" gateway stop > /dev/null 2>&1 || true sleep 1 if [ -s "$gateway_pid" ] && [ "$(cat "$gateway_pid")" = "$before" ]; then echo "Warning: Hermes' gateway is still running with the old servers. Quit Hermes, then run:" echo " hermes gateway stop" else echo "Stopped Hermes' gateway; the next time you use Hermes, it starts again with the new servers." fi fi echo echo "To check that it works, open Supermarket's \"Core & Hermes\" page and ask Hermes the question shown there."