Skip to content
Python

MCP OAuth: silent refresh actually works across restarts

Rohit C Prasad87245498cca1
diff --git a/coworker/mcp/oauth.py b/coworker/mcp/oauth.py
index d6bcf2af..53c0e77d 100644
--- a/coworker/mcp/oauth.py
+++ b/coworker/mcp/oauth.py
@@ -22,6 +22,7 @@ import asyncio
import logging
import os
import secrets
+import time
from typing import Any, Optional
from mcp.client.auth import OAuthClientProvider, TokenStorage
@@ -64,16 +65,43 @@ class SecretStoreTokenStorage(TokenStorage):
self._secrets.put(_profile(self._name), {**self._data(), **patch})
async def get_tokens(self) -> Optional[OAuthToken]:
- raw = self._data().get("tokens")
+ data = self._data()
+ raw = data.get("tokens")
if not raw:
return None
try:
- return OAuthToken.model_validate(raw)
+ tok = OAuthToken.model_validate(raw)
except Exception:
return None
+ # SDK flaw (mcp 1.29): `_initialize()` loads stored tokens but never computes
+ # `token_expiry_time`, and `is_token_valid()` treats None expiry as valid
+ # forever — so an hour-old access token is sent as-is, the server 401s, and
+ # the SDK's 401 branch goes straight to FULL re-authorization without trying
+ # the refresh token. Non-interactive contexts must refuse the browser, so
+ # every session said "sign-in required" while explicit connects appeared to
+ # work (owner-hit 2026-08-21, DLAI Redshift). Countermeasure lives here, in
+ # storage: when the stored token is past the lifetime we recorded at save
+ # time (unknown age = stale), return the token set WITHOUT the access token —
+ # `is_token_valid()` then fails on its own terms and the SDK runs the
+ # refresh-token grant FIRST, which self-heals silently (no browser).
+ if tok.expires_in is not None:
+ issued = data.get("tokens_issued_at")
+ if isinstance(issued, (int, float)):
+ remaining = int(issued + tok.expires_in - time.time())
+ else:
+ remaining = -1
+ tok = tok.model_copy(update={"expires_in": remaining})
+ if remaining <= 60 and tok.refresh_token:
+ tok = tok.model_copy(update={"access_token": ""})
+ return tok
async def set_tokens(self, tokens: OAuthToken) -> None:
- self._merge({"tokens": tokens.model_dump(mode="json", exclude_none=True)})
+ self._merge(
+ {
+ "tokens": tokens.model_dump(mode="json", exclude_none=True),
+ "tokens_issued_at": int(time.time()),
+ }
+ )
async def get_client_info(self) -> Optional[OAuthClientInformationFull]:
raw = self._data().get("client_info")
@@ -214,6 +242,72 @@ async def _wait_for_callback() -> tuple[str, Optional[str]]:
_expected_state = None # don't let this flow's state gate the next one
+class _MetadataSeededProvider(OAuthClientProvider):
+ """OAuthClientProvider that persists the discovered authorization-server
+ metadata and re-seeds it on load. Without this the SDK's pre-request refresh
+ grant runs BEFORE discovery and falls back to <origin>/token — a 404 on
+ vendors whose real endpoint lives elsewhere (data.dlai.link uses
+ /api/auth/mcp/token), which turned every silent refresh into a full re-auth
+ demand (owner-hit 2026-08-21, with the stale-expiry flaw above)."""
+
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
+ super().__init__(*args, **kwargs)
+ self._ocw_storage: SecretStoreTokenStorage = kwargs.get("storage") or self.context.storage # type: ignore[assignment]
+
+ async def _initialize(self) -> None:
+ await super()._initialize()
+ raw = self._ocw_storage._data().get("oauth_metadata")
+ if raw and self.context.oauth_metadata is None:
+ try:
+ from mcp.shared.auth import OAuthMetadata
+
+ self.context.oauth_metadata = OAuthMetadata.model_validate(raw)
+ except Exception:
+ pass # stale/incompatible cache: discovery will refill it
+ if self.context.oauth_metadata is None and self._ocw_storage._data().get(
+ "tokens"
+ ):
+ # No cache yet (tokens predate this fix): one best-effort fetch from the
+ # standard well-known location, so the refresh grant can target the real
+ # token endpoint on the very next request. Cached on success; any failure
+ # falls back to the SDK's own (post-401) discovery.
+ try:
+ from urllib.parse import urlparse
+
+ import httpx
+ from mcp.shared.auth import OAuthMetadata
+
+ pr = urlparse(self.context.server_url)
+ url = f"{pr.scheme}://{pr.netloc}/.well-known/oauth-authorization-server"
+ async with httpx.AsyncClient(timeout=10) as c:
+ r = await c.get(url, headers={"Accept": "application/json"})
+ if r.status_code == 200:
+ self.context.oauth_metadata = OAuthMetadata.model_validate(r.json())
+ self._persist_metadata()
+ except Exception:
+ pass
+
+ def _persist_metadata(self) -> None:
+ md = self.context.oauth_metadata
+ if md is not None:
+ try:
+ self._ocw_storage._merge(
+ {"oauth_metadata": md.model_dump(mode="json", exclude_none=True)}
+ )
+ except Exception:
+ logger.debug("could not persist oauth metadata", exc_info=True)
+
+ async def _handle_token_response(self, response: Any) -> None:
+ await super()._handle_token_response(response)
+ self._persist_metadata()
+
+ async def _handle_refresh_response(self, response: Any) -> bool:
+ ok = await super()._handle_refresh_response(response)
+ if ok:
+ self._persist_metadata()
+ return ok
+
+
def build_auth(
server_name: str,
server_url: str,
@@ -237,7 +331,7 @@ def build_auth(
"token_endpoint_auth_method": "none",
}
)
- return OAuthClientProvider(
+ return _MetadataSeededProvider(
server_url=server_url,
client_metadata=metadata,
storage=SecretStoreTokenStorage(server_name, secrets),
diff --git a/tests/test_mcp_oauth.py b/tests/test_mcp_oauth.py
index 94e20113..dd551d13 100644
--- a/tests/test_mcp_oauth.py
+++ b/tests/test_mcp_oauth.py
@@ -198,3 +198,78 @@ def test_callback_route(tmp_path, monkeypatch):
assert future.result() == ("c1", "s1")
finally:
loop.close()
+
+
+# -- stale-token countermeasures (owner-hit 2026-08-21, DLAI Redshift) ----------
+
+
+async def test_storage_reports_remaining_lifetime_and_blanks_stale_access(tmp_path, monkeypatch):
+ """The SDK never computes expiry for tokens loaded from storage and treats a
+ None expiry as valid-forever — so storage must (a) report the REMAINING
+ lifetime from the issued_at it persists and (b) blank the access token once
+ stale, which makes the SDK run the refresh grant before the request."""
+ from mcp.shared.auth import OAuthToken
+
+ from coworker.secrets import SecretStore
+
+ secrets = SecretStore(tmp_path / "s.json")
+ storage = mcp_oauth.SecretStoreTokenStorage("dlai", secrets)
+
+ now = 1_000_000.0
+ monkeypatch.setattr(mcp_oauth.time, "time", lambda: now)
+ await storage.set_tokens(
+ OAuthToken(access_token="A", expires_in=3600, refresh_token="R")
+ )
+
+ # Fresh: full remaining lifetime, access token intact.
+ tok = await storage.get_tokens()
+ assert tok.access_token == "A" and tok.expires_in == 3600
+
+ # Half-spent: remaining shrinks with the clock.
+ monkeypatch.setattr(mcp_oauth.time, "time", lambda: now + 1800)
+ tok = await storage.get_tokens()
+ assert tok.access_token == "A" and tok.expires_in == 1800
+
+ # Past expiry: access token blanked (refresh token kept) → SDK refreshes first.
+ monkeypatch.setattr(mcp_oauth.time, "time", lambda: now + 3700)
+ tok = await storage.get_tokens()
+ assert tok.access_token == "" and tok.refresh_token == "R"
+ assert tok.expires_in <= 0
+
+
+async def test_storage_treats_unknown_token_age_as_stale(tmp_path):
+ """Tokens persisted before issued_at existed have unknown age — assume stale
+ (blank access, keep refresh) rather than sending an hour-old bearer."""
+ from coworker.secrets import SecretStore
+
+ secrets = SecretStore(tmp_path / "s.json")
+ secrets.put(
+ "mcp-oauth:dlai",
+ {"tokens": {"access_token": "A", "expires_in": 3600, "refresh_token": "R"}},
+ )
+ storage = mcp_oauth.SecretStoreTokenStorage("dlai", secrets)
+ tok = await storage.get_tokens()
+ assert tok.access_token == "" and tok.refresh_token == "R"
+
+
+async def test_provider_seeds_and_persists_oauth_metadata(tmp_path):
+ """The refresh grant runs BEFORE the SDK's discovery, so the provider must
+ seed persisted authorization-server metadata at load — otherwise refresh
+ POSTs to the default <origin>/token (404 on data.dlai.link)."""
+ from coworker.secrets import SecretStore
+
+ secrets = SecretStore(tmp_path / "s.json")
+ md = {
+ "issuer": "https://data.example",
+ "authorization_endpoint": "https://data.example/api/auth/authorize",
+ "token_endpoint": "https://data.example/api/auth/token",
+ }
+ secrets.put("mcp-oauth:dlai", {"oauth_metadata": md})
+ auth = mcp_oauth.build_auth(
+ "dlai", "https://data.example/api/mcp", secrets, interactive=False
+ )
+ await auth._initialize()
+ assert str(auth.context.oauth_metadata.token_endpoint) == md["token_endpoint"]