Serve MCP and hooks with a secret through the portal tunnel

The connector now serves /mcp, its OAuth documents and /hooks/ besides
/api/v1/, refuses any dot segment (httpx resolves them, which carried
/api/v1/../../mcp onto the MCP mount), addresses calls to the hub's host,
and bridges served sockets by calling the app with a websocket scope.
A hook without a secret answers the tunnel with the 404 (or 1008) of a
wrong secret. Requests through the hub get the hub's issuer, resource and
consent page in the OAuth metadata and the 401, and the MCP host check
admits the hub while enrolled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgBfkm4rCmtd1T4vHWjEPx
This commit is contained in:
2026-10-15 11:00:19 +02:00
co-authored by Claude Opus 5.5
parent 7a56818213
commit 391edc8766
14 changed files with 526 additions and 31 deletions
+18
View File
@@ -20,6 +20,7 @@ import json
import logging
import os
import secrets
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any
@@ -114,6 +115,23 @@ def delete() -> None:
_cache = None
def via_portal(headers: Mapping[str, str]) -> bool:
"""Whether the connector replayed this request off the tunnel."""
return secrets.compare_digest(headers.get(VIA_HEADER, ""), VIA_PORTAL)
def portal_base(headers: Mapping[str, str]) -> str | None:
"""Where this instance answers at the hub, for a request that came through it.
The hub serves an instance under ``/i/{id}`` on the host it signs as, the
issuer enrolment pinned. ``None`` for a request made here.
"""
config = _read()
if config is None or not via_portal(headers):
return None
return f"{config.issuer.rstrip('/')}/i/{config.instance_id}"
def decode_portal_token(token: str) -> dict[str, Any]:
"""Validate a token the portal minted for this instance.