Implements DESIGN_MCP_HUB_AUTH.md phase 1, executed on erwin: - hub_entrypoint.py override: _AuthMiddleware authenticates every /sse and /messages/ request against hash-only mhub_ tokens (hub_tokens.json, mtime-cached, revocation without restart), binds sessions to the credential that opened them, and keeps ?as_user= as an admin-only override; legacy Basic-auth (traefik) and container loopback map to a legacy admin identity during the transition (HUB_ALLOW_LEGACY_BASIC). - _guarded_call_tool: non-admin tokens refused nextcloud_admin_*, superbrain_*, gitea_* and superpanel mutations; nextcloud_user_* pinned to the token's own user; per-call audit log without arguments. - mcphub-token CLI: add/list/revoke, Superpanel user verification, admin from group membership, --with-nextcloud mints the DAV app token too. - Dockerfile COPYs the hub override; STRATEGY.md records the rollout, the Traefik v3 HeaderRegexp spelling, and the nc_tokens.json directory bug fixed en passant. Verified on the public endpoint: invalid/revoked tokens 401, operator 232 tools, non-admin scoping refusals, own-user reads, cross-token session riding 401.
8.2 KiB
Design: per-user auth for the core MCP hub (phase 1 IMPLEMENTED on erwin 2026-11-25)
Local planning doc — phase 1 (bearer tokens, call-time scoping, dual-router
transition, CLI) is live on erwin as mcpserver-superbrain:1.8.9; see
STRATEGY.md "Per-user hub auth" for the executed state and the verified
matrix. The Superbrain-side suggestion (step 3) remains design-only.
The subject is the federated suite's mcpserver-hub
(the interim federatedcomputer/mcpserver-superbrain image, see
deploy/federated/STRATEGY.md), NOT the Superbrain binary — but the point
is to make the hub safe to offer as a one-connection MCP suggestion in
Superbrain's Settings → MCP catalog. Prerequisite context: STRATEGY.md
(the gateway attach), the hub source in the image
(/app/hub_entrypoint.py, 833 lines at 1.8.8+superbrain).
Problem
Today the hub (mcphub.<domain>/sse, 232 tools on erwin: nextcloud 155,
gitea 54, superbrain 14, superpanel 9) sits behind ONE shared traefik
Basic-auth credential (admin@<domain>). The code says it plainly:
"there is no finer-grained identity in this connection today".
?as_user=<uid>SELECTS a Nextcloud identity but does not AUTHENTICATE it: anyone holding the shared credential can act as any user present innc_tokens.json, andnextcloud_user_*tools accept an explicitusername=override besides.- Admin-grade tools (
nextcloud_admin_*,superpanel_create_user,superpanel_enable_mcp_app, the superbrain tools riding the operator's personalsb_key, gitea riding an all-scopes admin token) are exposed to every holder of the credential. - No revocation per person, no audit identity, and the credential cannot be handed to users — so the hub cannot become a user-facing suggestion.
Goals / non-goals
Goals:
- Every user gets their OWN credential; identity is authenticated, not selected. Revocable per user, hash-only storage, shown once.
- Tool authorization follows the identity: personal tools act as that user only; admin tools require an admin credential.
- Works with Superbrain's existing outbound MCP client unchanged (bearer auth + SSE transport — both already supported).
- Minimal, portable delta to the interim image; no Superpanel binary changes required for phase 1 (its source is upstream).
Non-goals (later):
- OAuth 2.1 / PKCE (the MCP-spec flow; needed for HubSpot-class servers and federation, overkill for one operator's core today).
- Per-user BACKEND credentials for gitea/superbrain (their hub tools stay admin-only until per-user tokens exist there).
- Superpanel self-service UI for token issuance (phase 2; phase 1 is a
host CLI, mirroring how
nc_tokens.jsonis populated today).
Token model
Format mhub_<43 chars base62> (the sb_ personal-key pattern).
Storage: /federated/apps/mcpserver/hub_tokens.json, mounted into the hub
at /app/hub_tokens.json (the nc_tokens.json mount pattern), chmod 600:
{
"<sha256-hex-of-token>": {
"user": "derek",
"admin": false,
"label": "derek laptop",
"created": "2026-11-25T12:00:00Z"
}
}
Hash-only: the file never holds a usable credential (Superbrain ApiKey
discipline). Reload on mtime change per request — revocation takes effect
without a restart. The token IS the identity: user comes from the entry,
never from a query parameter.
Issuance CLI (host)
/federated/bin/mcphub-token add <user> [--admin] [--label X] [--with-nextcloud]
- verifies the user exists in Superpanel (GET /api/users with the
.admin.secretbearer — the API is live on erwin now); --admindefaults from the user's Superpanel group membership (admins), overridable explicitly;--with-nextcloudalso mints the Nextcloud app token (occ user:auth-tokens:add) intonc_tokens.jsonso personal tools work immediately — one command, both credentials;- generates the token, stores the hash, prints the token ONCE.
list (users/labels/created, never hashes) and revoke <user|hash-prefix>
round it out. Phase 2 moves issuance into Superpanel's UI/MCP actions
(superpanel_issue_hub_token) once upstream can carry it.
Hub changes (interim image)
Replace _AsUserMiddleware with _AuthMiddleware:
/sseand/messages/requireAuthorization: Bearer <token>; sha256 → entry lookup; unknown/missing → 401 withWWW-Authenticate: Bearer(no body detail).- Identity binding: the ContextVar carries
{user, admin}from the entry. On/sse, mapsession_id → token-hash(the existing_session_usersmechanism); on/messages/, the presented token must hash to the SAME entry the session was opened with — a guessed session_id cannot ride someone else's identity. ?as_user=is ignored for non-admin tokens (kept as an admin debug override, logged). Personal tools default to the token's user.
Call-time authorization (the security boundary; per-session tool-LIST filtering is polish, not the gate — FastMCP proxy composition makes list filtering awkward, and a hidden tool that still executes is no control):
| Tool class | Non-admin token | Admin token |
|---|---|---|
nextcloud_user_* |
forced username = token.user; explicit mismatch → error; no nc token on file → clear error naming the gap |
any username (current behavior) |
nextcloud_admin_* |
refused | allowed |
superpanel_* |
list_apps/list_mcp_apps read-only OK; users/groups/mutations refused |
allowed |
superbrain_*, gitea_* |
refused (operator-keyed backends) | allowed |
Implementation: a wrapper applied at @proxy.tool registration (and to
the proxied backends via a FastMCP middleware/tool-hook if the proxy
exposes one — verify against the pinned fastmcp version; worst case,
wrap proxy.call_tool centrally, which every tool call passes through).
Traefik / gateway changes
- Hub router: DROP the
mcpserver-authbasicauth middleware — the bearer token is the authentication. TLS stays; netguard is uninvolved (this is inbound, and Superbrain's outbound dial to the public 443 endpoint is already clean). - Per-app gateway (
mcpserver.<domain>): unchanged in phase 1 (operator tooling). Migrate the same way once the hub proves out. - The
mcpservercontainer's own env secrets (NEXTCLOUD_PASSWORD, GITEA_ACCESS_TOKEN, SUPERBRAIN_API_KEY, SUPERPANEL_MCP_API_KEY) remain server-side only — they are the hub's backend credentials, never exposed through tools.
Superbrain integration (this repo, small)
Deployment-data suggestion exactly like NEXTCLOUD_URL:
MCP_HUB_URLenv (Core installer writeshttps://mcphub.$DOMAIN); unset → no suggestion, never a TIER fork.GET /api/mcp/connectionscatalog gains, when set:{kind:"core-hub", name:"Core tool hub", url:<MCP_HUB_URL>+"/sse", transport:"sse", authKind:"bearer", available:true, secretHint:"Your hub token (from your core operator)", description:"Files, calendar, contacts, repos and more from your core's apps."}- The user pastes their
mhub_token; Test lists the tools their credential can reach. No client changes needed (bearer + SSE exist today).
Rollout (erwin)
- Build the interim image with
_AuthMiddleware+ call-time scoping; add the CLI + tokens mount tolib-superbrain.sh/compose. - Issue the operator's admin token (
--with-nextcloud); switch the operator's clients (Claude Code/OpenCode) to?-free bearer SSE. - Recreate the hub WITHOUT traefik basicauth. Verify: no token → 401; admin token → 232 tools; non-admin token → personal tools only, admin calls refused; revoked token → 401 mid-session.
- Issue user tokens on request; Superbrain suggestion ships in the next
release (
MCP_HUB_URLwritten by the installer). - Upstream: fold the middleware + token model into the mcpserver repo when it bakes superbrain support (retire the interim image per STRATEGY.md).
Open questions
- Session lifetime: SSE connections can live for days; re-validate the
token hash on every
/messages/POST (cheap, file-backed) so revocation kills active sessions too — proposed yes. - Audit: log
{user, tool, ok}per call to the hub log (no arguments — they can carry personal data). Cheap, useful, proposed yes. - Rate limits per token: the outbound budget discipline lives in Superbrain; the hub itself may want a per-token ceiling later. Deferred.