Files
mcp-nextcloud/nextcloud_mcp_server/embedding/gateway_client.py
T
Chris CoutinhoandClaude Opus 4.8 b5ed1e3b4d fix: address PR #814 review + SonarCloud gate
SonarCloud:
- Resolve 6 S5332 hotspots (http→https in test fixture URLs).
- S6418: hoist the unauthenticated AsyncOpenAI placeholder to a named constant
  + NOSONAR (genuine non-secret; gateway ignores it when unauthenticated).
- Fix two reliability bugs: None-index guard in the gateway token-cache test
  (S2259) and float `> 0.0` instead of `!= 0.0` in the sentinel test (S1244).
- status.py idle path sleeps 0.1s instead of sleep(0) (S7491); NOSONAR on the
  protocol-required async no-await aclose() stubs (S7503).

Claude review:
- Remove three leftover debug print() calls in app.py (logger.info already
  covers them).
- payload_backfill: drop parsed_at from the backfilled-keys docstring (it is
  per-document state, not a deployment scalar); add a clean 404 precondition
  for BasicAuth deployments without an OAuth token verifier.
- status.py: task_status typed TaskStatus | None (drop type: ignore).
- nats.py: TODO to thread etags for file/deck/news; note etag default → None.
- factory: warn on unknown INGEST_BUS_URL scheme; raise ValueError instead of
  assert for the external-mode preconditions.
- docs/configuration.md: document the decomposition hook-point env vars + that
  nats-py ships core (lazy-imported) and external+bus uses two NATS connections.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 18:36:42 +02:00

143 lines
5.2 KiB
Python

"""OpenAI-compatible embedding provider targeting the Astrolabe Cloud embedding
gateway (design §10.2).
Active only when ``EMBEDDING_PROVIDER=gateway``. Registered *manually* in
``providers/registry.py`` — never part of the autodetect chain — so self-hosters
who don't opt in are unaffected.
**Auth model.** The MCP server is an OIDC *client* in the gateway's own
machine-to-machine realm — a realm *parallel to, and distinct from*, the tenant
realm the MCP server already serves as a client (Nextcloud user_oidc). It
obtains a ``client_credentials`` token and presents it as a Bearer; the gateway
maps the token's client-id → the tenant's underlying provider API key. This
mirrors the control-plane CLI's ``fetch_m2m_token`` pattern
(astrolabe-cloud-website ``services/control-plane/.../cli/_common.py``). When no
M2M creds are configured the client calls the gateway unauthenticated — matching
the gateway's current (not-yet-authenticated) state.
The gateway speaks the OpenAI ``/v1/embeddings`` wire format and routes by model
name (e.g. ``mistral-embed`` → Mistral for the MVP). Embeddings-only: ``generate``
is disabled (inherited ``NotImplementedError``).
"""
from __future__ import annotations
import logging
import time
import httpx
from ..providers.openai import OpenAIProvider
logger = logging.getLogger(__name__)
# Refresh the cached token this many seconds before its stated expiry, so a
# token never expires mid-flight (matches AstrolabeClient / CP CLI behavior).
_EARLY_REFRESH_SECONDS = 60
# Non-secret placeholder for AsyncOpenAI, which rejects an empty key. In
# unauthenticated mode the gateway ignores the bearer; when a token provider is
# configured, the real M2M token replaces this before each request.
_UNAUTHENTICATED_PLACEHOLDER = "unauthenticated"
class GatewayTokenProvider:
"""Caches a gateway M2M access token via the ``client_credentials`` grant.
HTTP Basic client auth + form-encoded grant, mirroring the website's
``fetch_m2m_token``. Tokens are cached until ``_EARLY_REFRESH_SECONDS``
before expiry.
"""
def __init__(
self,
token_url: str,
client_id: str,
client_secret: str,
scope: str | None = None,
timeout: float = 10.0,
):
self.token_url = token_url
self.client_id = client_id
self.client_secret = client_secret
self.scope = scope
self.timeout = timeout
self._cache: tuple[str, float] | None = None # (token, expires_at)
async def get_token(self, *, force_refresh: bool = False) -> str:
if (
self._cache is not None
and not force_refresh
and time.time() < self._cache[1]
):
return self._cache[0]
data = {"grant_type": "client_credentials"}
if self.scope:
data["scope"] = self.scope
async with httpx.AsyncClient(
timeout=httpx.Timeout(self.timeout, connect=5.0)
) as client:
resp = await client.post(
self.token_url,
data=data,
auth=(self.client_id, self.client_secret),
)
resp.raise_for_status()
body = resp.json()
expires_in = body.get("expires_in", 3600)
self._cache = (
body["access_token"],
time.time() + expires_in - _EARLY_REFRESH_SECONDS,
)
logger.info("Obtained embedding-gateway M2M token (expires in %ss)", expires_in)
return self._cache[0]
class GatewayProvider(OpenAIProvider):
"""Embeddings-only OpenAI-compatible provider pointed at the gateway."""
def __init__(
self,
*,
base_url: str,
embedding_model: str,
token_provider: GatewayTokenProvider | None = None,
timeout: float = 120.0,
):
# AsyncOpenAI rejects an empty key; use a non-secret placeholder when
# the gateway is unauthenticated. When a token provider is configured,
# the real Bearer is set on the client before each request.
super().__init__(
api_key=_UNAUTHENTICATED_PLACEHOLDER, # NOSONAR: placeholder, not a secret
base_url=base_url,
embedding_model=embedding_model,
generation_model=None, # gateway never generates
timeout=timeout,
)
self._token_provider = token_provider
logger.info(
"Initialized gateway embedding provider: base_url=%s, model=%s, auth=%s",
base_url,
embedding_model,
"oidc-m2m" if token_provider else "none",
)
async def _ensure_bearer(self) -> None:
"""Refresh the OIDC M2M token onto the OpenAI client (no-op when
unauthenticated). AsyncOpenAI reads ``api_key`` per request to build
the Authorization header, so updating it here applies to the next call.
"""
if self._token_provider is not None:
self.client.api_key = await self._token_provider.get_token()
async def embed(self, text: str) -> list[float]:
await self._ensure_bearer()
return await super().embed(text)
async def embed_batch(self, texts: list[str]) -> list[list[float]]:
await self._ensure_bearer()
return await super().embed_batch(texts)