Connect Hermes to the Core's MCP hub and to Supermarket

Hermes runs in CoreDesktop's sandbox, with its own network, so it can't
reach 127.0.0.1:8470. The app now also serves MCP on a Unix socket in its
data folder (/data/mcp.sock), which Hermes reaches with socat. HTTP /mcp
with the bearer token remains for other agents.

hermes/setup.sh asks for the user's own hub token (the Core now issues
per-user tokens), checks it with the hub, and stores it in Hermes' .env.
The hub only speaks SSE, and Hermes' Streamable HTTP attempt can use up
its connect time, so the setup sets transport: sse. It also warns when
Hermes' gateway is still running with the old servers.

Check-ins store the hub's own tool names, without Hermes' mcp__mcphub__
prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:31:14 -07:00
co-authored by Claude Opus 5.5
parent 1a86589840
commit f3e5b9eff6
12 changed files with 393 additions and 68 deletions
+99 -23
View File
@@ -11,16 +11,19 @@
# mcp_servers:
# mcphub:
# url: "https://mcphub.<core domain>/sse?as_user=<user>"
# transport: sse
# headers:
# Authorization: "Basic ${MCPHUB_AUTH}"
# supermarket:
# url: "http://127.0.0.1:8470/mcp"
# headers:
# Authorization: "Bearer ${SUPERMARKET_MCP_TOKEN}"
# command: socat
# args: ["STDIO", "UNIX-CONNECT:<data folder>/mcp.sock"]
#
# 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.
# 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
@@ -35,7 +38,9 @@ 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"
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."
@@ -62,10 +67,32 @@ set_env() {
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
@@ -73,6 +100,17 @@ 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
@@ -96,8 +134,18 @@ allow() {
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}'
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=""
@@ -111,6 +159,7 @@ if [ -z "$CORE_DOMAIN" ]; then
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%@*}"
@@ -119,29 +168,44 @@ else
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
# The Core gives each user their own token for the hub: its MCP settings
# read `Authorization: "Basic <token>"`.
echo "Your Core gives you your own token for it, in an MCP block like: Authorization: \"Basic <token>\""
read -rsp "Paste the token, the part after \"Basic \" (not shown): " hub_token
echo
if [ -z "$hub_password" ]; then
echo "No password given; skipping the MCP hub. Run \`supermarket hermes\` to try again."
# 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
set_env MCPHUB_AUTH "$(printf '%s:%s' "$hub_user" "$hub_password" | base64 -w0)"
unset hub_password
# 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 password is in $HERMES_ENV, readable only by you."
echo "Hermes will connect to $hub_url. The token 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."
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 "$MCP_LOCAL" $hub_host; do
for host in $hub_host; do
allowed "$host" || need+=("$host")
done
if [ "${#need[@]}" -gt 0 ]; then
@@ -158,10 +222,22 @@ 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 "${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."
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