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
+73 -5
View File
@@ -4,6 +4,9 @@ The transport is streamable HTTP, stateless, one JSON response per call: no
sticky sessions to route and nothing for a proxy to buffer. The SDK serves both
``/mcp`` and the protected-resource metadata beside it, which is why the sub-app
is mounted at the root rather than under ``/mcp``.
An enrolled instance also answers at ``/i/{id}/mcp`` on its hub, so an agent
that came through the tunnel is shown the hub's addresses instead.
"""
from __future__ import annotations
@@ -14,12 +17,16 @@ from urllib.parse import urlparse
import jwt
from fastapi.concurrency import run_in_threadpool
from mcp.server.auth.provider import AccessToken
from mcp.server.auth.routes import build_resource_metadata_url
from mcp.server.auth.settings import AuthSettings
from mcp.server.transport_security import TransportSecuritySettings
from pydantic import AnyHttpUrl
from starlette.applications import Starlette
from starlette.datastructures import Headers
from starlette.responses import JSONResponse
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from fluksio.api.deps import oauth_client_lives
from fluksio.cloud import config as cloud_config
from fluksio.core import security
from fluksio.core.config import settings
from fluksio.mcp import server
@@ -67,6 +74,20 @@ class _JWTVerifier:
)
class _AndTheHub(list[str]):
"""The hosts named here, plus the hub's while this instance is enrolled.
A call through the tunnel is addressed to the hub, as its Host says. The
hub is looked up per request, since enrolment comes and goes while the
endpoint is up. Its origin is not needed: the hub never forwards one.
"""
def __contains__(self, host: object) -> bool:
config = cloud_config.load()
hub = urlparse(config.issuer).netloc if config is not None else None
return super().__contains__(host) or (hub is not None and host == hub)
def _transport_security() -> TransportSecuritySettings:
"""Which Host headers to trust.
@@ -87,14 +108,61 @@ def _transport_security() -> TransportSecuritySettings:
"testserver",
]
allowed_origins += ["http://localhost", "http://127.0.0.1"]
return TransportSecuritySettings(
# Unvalidated, since validating would copy the hosts into a plain list.
return TransportSecuritySettings.model_construct(
enable_dns_rebinding_protection=True,
allowed_hosts=allowed_hosts,
allowed_hosts=_AndTheHub(allowed_hosts),
allowed_origins=allowed_origins,
)
def build_http_app(app: object) -> Starlette:
class _ThroughThePortal:
"""Show an agent that came through the hub the hub's addresses.
The SDK builds its metadata once, for the API host, and names it in every
401. An agent at ``/i/{id}/mcp`` on the hub cannot reach that host, so its
discovery has to lead to the hub's documents instead.
"""
def __init__(self, app: ASGIApp) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
base = None
if scope["type"] == "http":
base = cloud_config.portal_base(Headers(scope=scope))
if base is None:
await self.app(scope, receive, send)
return
if scope["path"] == "/.well-known/oauth-protected-resource/mcp":
document = {
"resource": f"{base}/mcp",
"authorization_servers": [base],
"bearer_methods_supported": ["header"],
}
headers = {"Cache-Control": "public, max-age=3600"}
await JSONResponse(document, headers=headers)(scope, receive, send)
return
here = str(build_resource_metadata_url(AnyHttpUrl(settings.mcp_resource)))
there = str(build_resource_metadata_url(AnyHttpUrl(f"{base}/mcp")))
async def send_theirs(message: Message) -> None:
if message["type"] == "http.response.start":
message["headers"] = [
(
key,
value.replace(here.encode(), there.encode())
if key == b"www-authenticate"
else value,
)
for key, value in message["headers"]
]
await send(message)
await self.app(scope, receive, send_theirs)
def build_http_app(app: object) -> ASGIApp:
"""The MCP sub-app, wired to this app's API and its OAuth server."""
server.mcp.settings.stateless_http = True
server.mcp.settings.json_response = True
@@ -109,4 +177,4 @@ def build_http_app(app: object) -> Starlette:
server.mcp._token_verifier = _JWTVerifier() # noqa: SLF001
server.connect(app)
return server.mcp.streamable_http_app()
return _ThroughThePortal(server.mcp.streamable_http_app())