Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

fix(mcp-oauth): retire a grant whose refresh is answered with invalid_grant (#3774)

蓝鱼bluefish810cdfe9dee7
diff --git a/src/xagent/web/api/mcp.py b/src/xagent/web/api/mcp.py
index 9ab80ca5c..04bc562dd 100644
--- a/src/xagent/web/api/mcp.py
+++ b/src/xagent/web/api/mcp.py
@@ -44,6 +44,7 @@ from sqlalchemy import cast as sa_cast
from sqlalchemy import literal, text
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
+from sqlalchemy.orm.exc import StaleDataError
from ...config import (
get_app_base_url,
@@ -104,6 +105,7 @@ from ..services.mcp_oauth import (
MCP_OAUTH_RESOURCE_OWNER_KEY_MAX_LENGTH,
MCP_OAUTH_TOKEN_ENDPOINT_AUTH_METHOD_MAX_LENGTH,
MCP_OAUTH_TOKEN_TYPE_MAX_LENGTH,
+ OAUTH_TOKEN_PAYLOAD_EXCLUDED_METADATA_KEYS,
MCPOAuthDiscoveryError,
SafeOAuthAsyncHTTPTransport,
_canonical_issuer,
@@ -2436,6 +2438,7 @@ def _upsert_mcp_oauth_grant(
.filter(
MCPOAuthGrant.lookup_hash == lookup_hash,
)
+ .with_for_update()
.first()
)
grant = existing or MCPOAuthGrant(
@@ -2469,7 +2472,7 @@ def _upsert_mcp_oauth_grant(
{
key: value
for key, value in token_data.items()
- if key not in {"access_token", "refresh_token"}
+ if key not in OAUTH_TOKEN_PAYLOAD_EXCLUDED_METADATA_KEYS
},
)
setattr(grant, "expires_at", oauth_token_expires_at(token_data))
@@ -7868,4 +7871,8 @@ async def delete_mcp_oauth_grant(
)
setattr(grant, "status", "revoked")
setattr(grant, "revoked_at", _utc_now())
- db.commit()
+ try:
+ db.commit()
+ except StaleDataError:
+ # A runtime refresh deleted the row while the provider revoke ran.
+ db.rollback()
diff --git a/src/xagent/web/services/mcp_oauth.py b/src/xagent/web/services/mcp_oauth.py
index 0ec5dd055..ef257b037 100644
--- a/src/xagent/web/services/mcp_oauth.py
+++ b/src/xagent/web/services/mcp_oauth.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
+import hashlib
import json
import logging
import os
@@ -14,6 +15,7 @@ from urllib.parse import SplitResult, urljoin, urlsplit, urlunsplit
from urllib.request import parse_http_list, parse_keqv_list
import httpx
+from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from ...config import get_mcp_oauth_allow_private_hosts, get_mcp_oauth_proxy_url
@@ -34,6 +36,24 @@ MCP_OAUTH_RESOURCE_OWNER_KEY_MAX_LENGTH = 512
MCP_OAUTH_TOKEN_ENDPOINT_AUTH_METHOD_MAX_LENGTH = 100
MCP_OAUTH_TOKEN_TYPE_MAX_LENGTH = 50
MCP_OAUTH_CLIENT_REGISTRATION_MAX_RESPONSE_BYTES = 64 * 1024
+# A refresh cannot outlive the deadline below, so a rotation that started
+# before this rejection has committed well before the retire window closes.
+MCP_OAUTH_INVALID_GRANT_RETIRE_SECONDS = 60
+# Kept below the retire window so one slow response cannot straddle both strikes.
+MCP_OAUTH_REFRESH_DEADLINE_SECONDS = 30
+# Long enough for a rarely used dead grant to collect two strikes, short
+# enough to bound how long a spurious strike can arm a delete.
+MCP_OAUTH_INVALID_GRANT_MARKER_TTL_SECONDS = 86400
+MCP_OAUTH_INVALID_GRANT_AT_KEY = "invalid_grant_at"
+MCP_OAUTH_INVALID_GRANT_TOKEN_KEY = "invalid_grant_token_sha256"
+OAUTH_TOKEN_PAYLOAD_EXCLUDED_METADATA_KEYS = frozenset(
+ {
+ "access_token",
+ "refresh_token",
+ MCP_OAUTH_INVALID_GRANT_AT_KEY,
+ MCP_OAUTH_INVALID_GRANT_TOKEN_KEY,
+ }
+)
OAUTH_ERROR_MESSAGE_MAX_LENGTH = 500
OAUTH_LOG_PAYLOAD_MAX_LENGTH = 2000
OAUTH_REDIRECT_STATUSES = frozenset({301, 302, 303, 307, 308})
@@ -70,6 +90,10 @@ class _RetryRuntimeGrantSelection(RuntimeError):
self.message = message
+class _InvalidGrantRefreshError(MCPOAuthRuntimeError):
+ """The token endpoint answered ``invalid_grant`` for the grant being refreshed."""
+
+
@dataclass(frozen=True)
class MCPAuthorizationChallenge:
"""Bearer challenge advertised by a protected MCP resource."""
@@ -808,15 +832,80 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913
)
refresh_db.rollback()
- token_data = await _refresh_mcp_oauth_grant(
- oauth_client_snapshot,
- refresh_token=refresh_token,
- resource=refresh_resource,
- client=client,
- credential_provider=_platform_credential_provider(
- oauth_client_snapshot.client_id
- ),
- )
+ try:
+ token_data = await _refresh_mcp_oauth_grant(
+ oauth_client_snapshot,
+ refresh_token=refresh_token,
+ resource=refresh_resource,
+ client=client,
+ credential_provider=_platform_credential_provider(
+ oauth_client_snapshot.client_id
+ ),
+ )
+ except _InvalidGrantRefreshError as exc:
+ # Same lock as the success path, so a refresh that rotated the
+ # grant while this request ran is never overwritten.
+ retire_grant = False
+ try:
+ with refresh_db.begin():
+ rejected_grant: Any = (
+ refresh_db.query(MCPOAuthGrant)
+ .filter(MCPOAuthGrant.id == grant_id)
+ .with_for_update()
+ .first()
+ )
+ if (
+ rejected_grant is None
+ or rejected_grant.status != "active"
+ or str(rejected_grant.refresh_token) != encrypted_refresh_token
+ or not _grant_needs_refresh(rejected_grant.expires_at)
+ ):
+ raise _RetryRuntimeGrantSelection(
+ "authorization_required",
+ "MCP OAuth grant changed while preparing runtime "
+ "authorization",
+ )
+ metadata = (
+ dict(rejected_grant.metadata_json)
+ if isinstance(rejected_grant.metadata_json, dict)
+ else {}
+ )
+ token_digest = hashlib.sha256(
+ encrypted_refresh_token.encode("utf-8")
+ ).hexdigest()
+ marked_for_seconds = _invalid_grant_marker_age_seconds(
+ metadata, token_digest
+ )
+ if (
+ marked_for_seconds is None
+ or marked_for_seconds
+ >= MCP_OAUTH_INVALID_GRANT_MARKER_TTL_SECONDS
+ ):
+ metadata[MCP_OAUTH_INVALID_GRANT_AT_KEY] = (
+ _utc_now().isoformat()
+ )
+ metadata[MCP_OAUTH_INVALID_GRANT_TOKEN_KEY] = token_digest
+ rejected_grant.metadata_json = metadata
+ elif marked_for_seconds >= MCP_OAUTH_INVALID_GRANT_RETIRE_SECONDS:
+ refresh_db.delete(rejected_grant)
+ retire_grant = True
+ except SQLAlchemyError as db_exc:
+ refresh_db.rollback()
+ raise MCPOAuthRuntimeError(
+ "token_refresh_failed",
+ "MCP OAuth grant rejection could not be recorded",
+ ) from db_exc
+ if retire_grant:
+ logger.warning(
+ "MCP OAuth grant %s for server %s was rejected twice; deleted",
+ grant_id,
+ server_id,
+ )
+ raise _RetryRuntimeGrantSelection(
+ "authorization_required",
+ "MCP OAuth grant changed while preparing runtime authorization",
+ )
+ raise MCPOAuthRuntimeError("token_refresh_failed", exc.message) from exc
with refresh_db.begin():
locked_grant: Any = (
@@ -894,7 +983,7 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913
locked_grant.metadata_json = {
key: value
for key, value in token_data.items()
- if key not in {"access_token", "refresh_token"}
+ if key not in OAUTH_TOKEN_PAYLOAD_EXCLUDED_METADATA_KEYS
}
locked_grant.expires_at = oauth_token_expires_at(token_data)
runtime_auth = MCPOAuthRuntimeAuth(
@@ -1357,6 +1446,16 @@ async def _refresh_mcp_oauth_grant(
affects every user of the server. Any other client, a user's custom
static client or a per-user registered one, keeps
``token_refresh_failed``.
+
+ A 400/401 answer whose error is ``invalid_grant`` raises
+ ``_InvalidGrantRefreshError`` instead. ``web/tools/config.py`` does not
+ trust that code generically, but here the grant is bound to one
+ ``MCPOAuthClient`` row, and the caller deletes the grant only after the
+ same refresh token is answered this way twice, at least
+ ``MCP_OAUTH_INVALID_GRANT_RETIRE_SECONDS`` apart and within
+ ``MCP_OAUTH_INVALID_GRANT_MARKER_TTL_SECONDS``. The whole request is
+ bounded by ``MCP_OAUTH_REFRESH_DEADLINE_SECONDS``. 5xx, network errors
+ and every other code stay transient.
"""
data = {
"grant_type": "refresh_token",
@@ -1382,30 +1481,35 @@ async def _refresh_mcp_oauth_grant(
)
try:
- request_kwargs: dict[str, Any] = {
- "data": data,
- "headers": {"Content-Type": "application/x-www-form-urlencoded"},
- }
- if auth is not None:
- request_kwargs["auth"] = auth
- if client is not None:
- response = await oauth_post(
- str(oauth_client.token_endpoint),
- client=client,
- resolve_dns_for_url_policy=False,
- **request_kwargs,
- )
- else:
- async with create_mcp_oauth_http_client(
- timeout=MCP_OAUTH_HTTP_TIMEOUT_SECONDS,
- ) as owned_client:
+ async with asyncio.timeout(MCP_OAUTH_REFRESH_DEADLINE_SECONDS):
+ request_kwargs: dict[str, Any] = {
+ "data": data,
+ "headers": {"Content-Type": "application/x-www-form-urlencoded"},
+ }
+ if auth is not None:
+ request_kwargs["auth"] = auth
+ if client is not None:
response = await oauth_post(
str(oauth_client.token_endpoint),
- client=owned_client,
+ client=client,
resolve_dns_for_url_policy=False,
**request_kwargs,
)
- payload = response.json()
+ else:
+ async with create_mcp_oauth_http_client(
+ timeout=MCP_OAUTH_HTTP_TIMEOUT_SECONDS,
+ ) as owned_client:
+ response = await oauth_post(
+ str(oauth_client.token_endpoint),
+ client=owned_client,
+ resolve_dns_for_url_policy=False,
+ **request_kwargs,
+ )
+ payload = response.json()
+ except TimeoutError as exc:
+ raise MCPOAuthRuntimeError(
+ "token_refresh_failed", "MCP OAuth refresh timed out"
+ ) from exc
except (MCPOAuthDiscoveryError, httpx.HTTPError, ValueError) as exc:
raise MCPOAuthRuntimeError(
"token_refresh_failed",
@@ -1442,6 +1546,15 @@ async def _refresh_mcp_oauth_grant(
client_rejected_reason(credential_provider),
"The OAuth provider rejected the platform's client",
)
+ if (
+ response.status_code in {400, 401}
+ and isinstance(payload, dict)
+ and payload.get("error") == "invalid_grant"
+ ):
+ raise _InvalidGrantRefreshError(
+ "token_refresh_failed",
+ oauth_error_message(payload, "MCP OAuth refresh failed"),
+ )
raise MCPOAuthRuntimeError(
"token_refresh_failed",
oauth_error_message(payload, "MCP OAuth refresh failed"),
@@ -1650,6 +1763,27 @@ def _grant_needs_refresh(expires_at: Any) -> bool:
return bool(expires_at <= _utc_now() + timedelta(minutes=5))
+def _invalid_grant_marker_age_seconds(
+ metadata: dict[str, Any], token_digest: str
+) -> float | None:
+ """Seconds since this token's ``invalid_grant`` marker.
+
+ ``None`` when absent, for another token, or unusable.
+ """
+ if metadata.get(MCP_OAUTH_INVALID_GRANT_TOKEN_KEY) != token_digest:
+ return None
+ value = metadata.get(MCP_OAUTH_INVALID_GRANT_AT_KEY)
+ if not isinstance(value, str):
+ return None
+ try:
+ marked_at = datetime.fromisoformat(value)
+ except ValueError:
+ return None
+ if marked_at.tzinfo is None:
+ marked_at = marked_at.replace(tzinfo=timezone.utc)
+ return (_utc_now() - marked_at).total_seconds()
+
+
def _utc_now() -> datetime:
return datetime.now(timezone.utc)
diff --git a/tests/web/api/test_mcp_oauth_flow.py b/tests/web/api/test_mcp_oauth_flow.py
index b7258579a..e785422c5 100644
--- a/tests/web/api/test_mcp_oauth_flow.py
+++ b/tests/web/api/test_mcp_oauth_flow.py
@@ -16,7 +16,7 @@ import pytest
from fastapi import HTTPException
from sqlalchemy import create_engine, event
from sqlalchemy.exc import IntegrityError
-from sqlalchemy.orm import sessionmaker
+from sqlalchemy.orm import Query, sessionmaker
from starlette.requests import Request
from tests.shared.async_waits import DB_PROGRESS_TIMEOUT
@@ -4854,6 +4854,123 @@ async def test_delete_grant_revokes_external_tokens_when_endpoint_is_advertised(
assert grant.revoked_at is not None
+async def test_delete_grant_treats_a_runtime_delete_during_revoke_as_done(
+ db_session, monkeypatch
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ client = _add_oauth_client(
+ db,
+ server,
+ client_secret=encrypt_value("client-secret"),
+ token_endpoint_auth_method="client_secret_post",
+ metadata_json={"revocation_endpoint": "https://auth.example.com/revoke"},
+ )
+ grant = MCPOAuthGrant(
+ mcp_server_id=server.id,
+ user_id=user.id,
+ mcp_oauth_client_id=client.id,
+ resource_owner_key=f"xagent:user:{user.id}",
+ issuer="https://auth.example.com",
+ resource="https://mcp.example.com/mcp",
+ scope="records.read",
+ access_token=encrypt_value("access-token"),
+ refresh_token=encrypt_value("refresh-token"),
+ )
+ db.add(grant)
+ db.commit()
+ db.refresh(grant)
+ grant_id = grant.id
+ SessionLocal = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)
+
+ async def revoke_and_delete(*args, **kwargs):
+ with SessionLocal() as other_db:
+ other_db.delete(other_db.get(MCPOAuthGrant, grant_id))
+ other_db.commit()
+
+ monkeypatch.setattr(
+ mcp_api, "_revoke_mcp_oauth_grant_externally", revoke_and_delete
+ )
+
+ await delete_mcp_oauth_grant(server.id, grant_id, user, db)
+
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+
+
+def test_upsert_mcp_oauth_grant_locks_and_reinserts_a_deleted_row(
+ db_session, monkeypatch
+):
+ db, user, _ = db_session
+ _server, _client, flow_state = _add_callback_client_and_state(
+ db, user, state="state-upsert-lock"
+ )
+ token_data = {
+ "access_token": "first-access",
+ "refresh_token": "first-refresh",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ }
+ grant = mcp_api._upsert_mcp_oauth_grant(
+ db, flow_state=flow_state, token_data=token_data
+ )
+ db.commit()
+ grant_id = grant.id
+ lookup_hash = grant.lookup_hash
+ SessionLocal = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)
+ with SessionLocal() as other_db:
+ other_db.delete(other_db.get(MCPOAuthGrant, grant_id))
+ other_db.commit()
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+
+ lock_queries: list[bool] = []
+ original_with_for_update = Query.with_for_update
+
+ def record_with_for_update(self, *args, **kwargs):
+ lock_queries.append(True)
+ return original_with_for_update(self, *args, **kwargs)
+
+ monkeypatch.setattr(Query, "with_for_update", record_with_for_update)
+
+ mcp_api._upsert_mcp_oauth_grant(db, flow_state=flow_state, token_data=token_data)
+ db.commit()
+
+ assert lock_queries
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is not None
+ assert (
+ db.query(MCPOAuthGrant).filter(MCPOAuthGrant.lookup_hash == lookup_hash).count()
+ == 1
+ )
+
+
+def test_upsert_mcp_oauth_grant_drops_marker_keys_from_a_token_payload(db_session):
+ db, user, _ = db_session
+ _server, _client, flow_state = _add_callback_client_and_state(
+ db, user, state="state-upsert-marker"
+ )
+
+ grant = mcp_api._upsert_mcp_oauth_grant(
+ db,
+ flow_state=flow_state,
+ token_data={
+ "access_token": "access-token",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ "invalid_grant_at": "2020-01-01T00:00:00+00:00",
+ "invalid_grant_token_sha256": "deadbeef",
+ },
+ )
+ db.commit()
+
+ assert "invalid_grant_at" not in grant.metadata_json
+ assert "invalid_grant_token_sha256" not in grant.metadata_json
+
+
@pytest.mark.asyncio
async def test_delete_mcp_server_revokes_external_tokens_when_endpoint_is_advertised(
db_session, monkeypatch
diff --git a/tests/web/tools/test_mcp_oauth_runtime.py b/tests/web/tools/test_mcp_oauth_runtime.py
index 13ac6a6b3..4a34eafc6 100644
--- a/tests/web/tools/test_mcp_oauth_runtime.py
+++ b/tests/web/tools/test_mcp_oauth_runtime.py
@@ -1,11 +1,15 @@
from __future__ import annotations
+import asyncio
+import hashlib
+import logging
from datetime import datetime, timedelta, timezone
from urllib.parse import parse_qs
import httpx
import pytest
from sqlalchemy import create_engine
+from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Query, sessionmaker
from xagent.core.utils.encryption import decrypt_value, encrypt_value
@@ -174,6 +178,20 @@ def _assert_unavailable_runtime_config(
assert "connector_runtime" not in config
+_REAL_ASYNC_CLIENT = httpx.AsyncClient
+
+
+def _mock_token_endpoint(monkeypatch, handler) -> None:
+ def async_client_factory(*args, **kwargs):
+ return _REAL_ASYNC_CLIENT(transport=httpx.MockTransport(handler))
+
+ async def skip_url_policy(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(mcp_oauth_service, "validate_oauth_http_url", skip_url_policy)
+ monkeypatch.setattr(mcp_oauth_service.httpx, "AsyncClient", async_client_factory)
+
+
def test_effective_mcp_oauth_resource_prefers_selector_verbatim(db_session):
db, user, _ = db_session
server = _add_mcp_oauth_server(db, user)
@@ -728,6 +746,8 @@ async def test_a_preregistered_client_rejected_on_refresh_is_client_rejected(
assert configs[0]["config"]["reason"] == "client_rejected:hubspot"
db.refresh(grant)
assert decrypt_value(grant.refresh_token) == "refresh-token-123"
+ assert grant.status == "active"
+ assert grant.revoked_at is None
@pytest.mark.asyncio
@@ -849,7 +869,7 @@ async def test_mcp_oauth_runtime_refresh_failure_retains_unavailable_without_sta
return httpx.Response(
400,
json={
- "error": "invalid_grant",
+ "error": "invalid_request",
"error_description": "refresh token is invalid",
"access_token": "leaked-access-token",
"refresh_token": "leaked-refresh-token",
@@ -878,6 +898,840 @@ async def test_mcp_oauth_runtime_refresh_failure_retains_unavailable_without_sta
assert "leaked-refresh-token" not in str(configs[0])
db.refresh(grant)
assert decrypt_value(grant.access_token) == "expired-access-token"
+ assert grant.status == "active"
+ assert grant.revoked_at is None
+
+
[email protected]("status_code", [400, 401])
+async def test_mcp_oauth_runtime_records_a_first_invalid_grant_without_retiring(
+ db_session,
+ monkeypatch,
+ status_code,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant.metadata_json = {"token_type": "Bearer"}
+ db.commit()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ status_code,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ configs, cfg = await _load_configs(db, user)
+
+ diagnostics = cfg.get_mcp_oauth_diagnostics()
+ assert diagnostics[0]["code"] == "token_refresh_failed"
+ assert diagnostics[0]["message"] == "OAuth grant revoked"
+ assert len(configs) == 1
+ _assert_unavailable_runtime_config(configs[0], server, diagnostics[0])
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.revoked_at is None
+ assert decrypt_value(grant.refresh_token) == "refresh-token-123"
+ metadata = grant.metadata_json
+ assert metadata["token_type"] == "Bearer"
+ assert (
+ metadata["invalid_grant_token_sha256"]
+ == hashlib.sha256(str(grant.refresh_token).encode("utf-8")).hexdigest()
+ )
+ marked_at = datetime.fromisoformat(metadata["invalid_grant_at"])
+ assert marked_at.tzinfo is not None
+ assert abs((datetime.now(timezone.utc) - marked_at).total_seconds()) < 30
+
+
+async def test_mcp_oauth_runtime_deletes_a_grant_rejected_twice(
+ db_session,
+ monkeypatch,
+ caplog,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+ encrypted_refresh_token = str(grant.refresh_token)
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ _first_configs, first_cfg = await _load_configs(db, user)
+ assert first_cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+
+ db.refresh(grant)
+ metadata = dict(grant.metadata_json)
+ metadata["invalid_grant_at"] = (
+ datetime.now(timezone.utc) - timedelta(seconds=61)
+ ).isoformat()
+ grant.metadata_json = metadata
+ db.commit()
+
+ caplog.set_level(logging.WARNING, logger="xagent.web.services.mcp_oauth")
+ configs, cfg = await _load_configs(db, user)
+
+ diagnostics = cfg.get_mcp_oauth_diagnostics()
+ assert diagnostics[0]["code"] == "authorization_required"
+ assert len(configs) == 1
+ _assert_unavailable_runtime_config(configs[0], server, diagnostics[0])
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+ assert (
+ mcp_oauth_service.select_mcp_oauth_grants(
+ db,
+ server_id=server.id,
+ user_id=user.id,
+ auth_config=server._decrypt_auth_config(server.auth),
+ resource_owner_key=f"xagent:user:{user.id}",
+ )
+ == []
+ )
+ messages = [
+ record.getMessage()
+ for record in caplog.records
+ if "rejected twice" in record.getMessage()
+ ]
+ assert len(messages) == 1
+ assert str(grant_id) in messages[0]
+ assert str(server.id) in messages[0]
+ assert "refresh-token-123" not in messages[0]
+ assert encrypted_refresh_token not in messages[0]
+ assert user.username not in messages[0]
+
+
+async def test_mcp_oauth_runtime_re_marks_a_stale_marker(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ token_digest = hashlib.sha256(str(grant.refresh_token).encode("utf-8")).hexdigest()
+ grant.metadata_json = {
+ "invalid_grant_at": (
+ datetime.now(timezone.utc) - timedelta(seconds=86401)
+ ).isoformat(),
+ "invalid_grant_token_sha256": token_digest,
+ }
+ db.commit()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+ assert len(configs) == 1
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.revoked_at is None
+ assert grant.metadata_json["invalid_grant_token_sha256"] == token_digest
+ marked_at = datetime.fromisoformat(grant.metadata_json["invalid_grant_at"])
+ assert abs((datetime.now(timezone.utc) - marked_at).total_seconds()) < 30
+
+
+ "case", ["foreign_digest", "malformed", "number", "naive", "hours_old"]
+)
+async def test_mcp_oauth_runtime_reader_handles_unusable_markers(
+ db_session,
+ monkeypatch,
+ case,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+ token_digest = hashlib.sha256(str(grant.refresh_token).encode("utf-8")).hexdigest()
+ marked_at = datetime.now(timezone.utc) - timedelta(seconds=61)
+ grant.metadata_json = {
+ "foreign_digest": {
+ "invalid_grant_at": marked_at.isoformat(),
+ "invalid_grant_token_sha256": "0" * 64,
+ },
+ "malformed": {
+ "invalid_grant_at": "garbage",
+ "invalid_grant_token_sha256": token_digest,
+ },
+ "number": {
+ "invalid_grant_at": 12345,
+ "invalid_grant_token_sha256": token_digest,
+ },
+ "naive": {
+ "invalid_grant_at": marked_at.replace(tzinfo=None).isoformat(),
+ "invalid_grant_token_sha256": token_digest,
+ },
+ "hours_old": {
+ "invalid_grant_at": (
+ datetime.now(timezone.utc) - timedelta(hours=3)
+ ).isoformat(),
+ "invalid_grant_token_sha256": token_digest,
+ },
+ }[case]
+ db.commit()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+ configs, cfg = await _load_configs(db, user)
+
+ if case in {"naive", "hours_old"}:
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "authorization_required"
+ assert len(configs) == 1
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+ return
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+ assert len(configs) == 1
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.metadata_json["invalid_grant_token_sha256"] == token_digest
+ re_marked_at = datetime.fromisoformat(grant.metadata_json["invalid_grant_at"])
+ assert abs((datetime.now(timezone.utc) - re_marked_at).total_seconds()) < 30
+
+
+async def test_mcp_oauth_runtime_refresh_deadline_is_transient(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+
+ async def slow_oauth_post(*args, **kwargs):
+ await asyncio.sleep(0.2)
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "slow-access-token",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ },
+ )
+
+ monkeypatch.setattr(mcp_oauth_service, "MCP_OAUTH_REFRESH_DEADLINE_SECONDS", 0.01)
+ monkeypatch.setattr(mcp_oauth_service, "oauth_post", slow_oauth_post)
+
+ configs, cfg = await _load_configs(db, user)
+
+ diagnostics = cfg.get_mcp_oauth_diagnostics()
+ assert diagnostics[0]["code"] == "token_refresh_failed"
+ assert diagnostics[0]["message"] == "MCP OAuth refresh timed out"
+ assert len(configs) == 1
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert decrypt_value(grant.access_token) == "expired-access-token"
+ assert "invalid_grant_at" not in (grant.metadata_json or {})
+
+
+async def test_mcp_oauth_runtime_lock_failure_is_transient(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ def raise_lock_failure(*args, **kwargs):
+ raise SQLAlchemyError("database is locked")
+
+ monkeypatch.setattr(Query, "with_for_update", raise_lock_failure)
+
+ configs, cfg = await _load_configs(db, user)
+
+ diagnostics = cfg.get_mcp_oauth_diagnostics()
+ assert diagnostics[0]["code"] == "token_refresh_failed"
+ assert diagnostics[0]["message"] == (
+ "MCP OAuth grant rejection could not be recorded"
+ )
+ assert len(configs) == 1
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert "invalid_grant_at" not in (grant.metadata_json or {})
+
+
+async def test_mcp_oauth_runtime_does_not_store_marker_keys_from_a_token_payload(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "fresh-access-token",
+ "refresh_token": "fresh-refresh-token",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ "invalid_grant_at": "2020-01-01T00:00:00+00:00",
+ "invalid_grant_token_sha256": "deadbeef",
+ },
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ db.refresh(grant)
+ assert "invalid_grant_at" not in grant.metadata_json
+ assert "invalid_grant_token_sha256" not in grant.metadata_json
+
+
+async def test_mcp_oauth_runtime_keeps_the_first_strike_timestamp(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+ started_at = datetime.now(timezone.utc)
+ clock = {"now": started_at}
+ monkeypatch.setattr(mcp_oauth_service, "_utc_now", lambda: clock["now"])
+
+ _first_configs, first_cfg = await _load_configs(db, user)
+ assert first_cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+ db.refresh(grant)
+ first_marker = dict(grant.metadata_json)
+ assert "invalid_grant_at" in first_marker
+ assert grant.revoked_at is None
+
+ clock["now"] = started_at + timedelta(seconds=30)
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+ assert len(configs) == 1
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.metadata_json == first_marker
+
+ clock["now"] = started_at + timedelta(seconds=61)
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "authorization_required"
+ assert len(configs) == 1
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+
+
+async def test_mcp_oauth_runtime_keeps_a_replaced_token_after_a_late_rejection(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+
+ def invalid_grant_handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, invalid_grant_handler)
+
+ # B is answered invalid_grant first, so it records a strike on T1.
+ _first_configs, first_cfg = await _load_configs(db, user)
+ assert first_cfg.get_mcp_oauth_diagnostics()[0]["code"] == "token_refresh_failed"
+ db.refresh(grant)
+ assert "invalid_grant_at" in grant.metadata_json
+
+ # A then commits its rotated token; persisting it clears the strike.
+ def rotated_handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "rotated-access-token",
+ "refresh_token": "rotated-refresh-token",
+ "token_type": "Bearer",
+ "expires_in": 60,
+ "scope": "records.read",
+ },
+ )
+
+ _mock_token_endpoint(monkeypatch, rotated_handler)
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ assert configs[0]["config"]["headers"]["Authorization"] == (
+ "Bearer rotated-access-token"
+ )
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert decrypt_value(grant.refresh_token) == "rotated-refresh-token"
+ assert "invalid_grant_at" not in (grant.metadata_json or {})
+ assert "invalid_grant_token_sha256" not in (grant.metadata_json or {})
+
+ # A late answer for the replaced token does not touch the rotated grant.
+ def stale_handler(request: httpx.Request) -> httpx.Response:
+ sent_token = parse_qs(request.content.decode())["refresh_token"][0]
+ if sent_token == "rotated-refresh-token":
+ other_db = sessionmaker(
+ bind=db.get_bind(), autoflush=False, autocommit=False
+ )()
+ try:
+ rotated = other_db.get(MCPOAuthGrant, grant_id)
+ rotated.refresh_token = encrypt_value("third-refresh-token")
+ rotated.expires_at = datetime.now(timezone.utc) - timedelta(minutes=1)
+ other_db.commit()
+ finally:
+ other_db.close()
+ return httpx.Response(
+ 400,
+ json={
+ "error": "invalid_grant",
+ "error_description": "OAuth grant revoked",
+ },
+ )
+ assert sent_token == "third-refresh-token"
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "third-access-token",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ },
+ )
+
+ _mock_token_endpoint(monkeypatch, stale_handler)
+ configs, cfg = await _load_configs(db, user)
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ assert configs[0]["config"]["headers"]["Authorization"] == (
+ "Bearer third-access-token"
+ )
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert decrypt_value(grant.refresh_token) == "third-refresh-token"
+ assert "invalid_grant_at" not in (grant.metadata_json or {})
+
+
+async def test_mcp_oauth_runtime_retries_when_the_grant_disappears_mid_refresh(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ other_db = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)()
+ try:
+ other_db.delete(other_db.get(MCPOAuthGrant, grant_id))
+ other_db.commit()
+ finally:
+ other_db.close()
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "authorization_required"
+ assert len(configs) == 1
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, grant_id) is None
+
+
+async def test_mcp_oauth_runtime_retries_when_the_grant_is_no_longer_active(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ other_db = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)()
+ try:
+ rejected = other_db.get(MCPOAuthGrant, grant_id)
+ rejected.status = "revoked"
+ rejected.revoked_at = datetime.now(timezone.utc)
+ other_db.commit()
+ finally:
+ other_db.close()
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics()[0]["code"] == "authorization_required"
+ assert len(configs) == 1
+ db.expire_all()
+ rejected = db.get(MCPOAuthGrant, grant_id)
+ assert rejected is not None
+ assert rejected.status == "revoked"
+ assert "invalid_grant_at" not in (rejected.metadata_json or {})
+
+
+async def test_mcp_oauth_runtime_keeps_a_reconnect_that_kept_the_refresh_token(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ grant_id = grant.id
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ other_db = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)()
+ try:
+ reconnected = other_db.get(MCPOAuthGrant, grant_id)
+ reconnected.expires_at = datetime.now(timezone.utc) + timedelta(hours=1)
+ other_db.commit()
+ finally:
+ other_db.close()
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ assert configs[0]["config"]["headers"]["Authorization"] == (
+ "Bearer expired-access-token"
+ )
+ db.expire_all()
+ reconnected = db.get(MCPOAuthGrant, grant_id)
+ assert reconnected.status == "active"
+ assert decrypt_value(reconnected.refresh_token) == "refresh-token-123"
+ assert "invalid_grant_at" not in (reconnected.metadata_json or {})
+
+
+async def test_mcp_oauth_runtime_reselects_another_grant_after_retiring_one(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ first_grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="first-refresh-token",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ second_grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="second-refresh-token",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ scope="records.read records.write",
+ )
+ second_grant.metadata_json = {
+ "invalid_grant_at": (
+ datetime.now(timezone.utc) - timedelta(seconds=61)
+ ).isoformat(),
+ "invalid_grant_token_sha256": hashlib.sha256(
+ str(second_grant.refresh_token).encode("utf-8")
+ ).hexdigest(),
+ }
+ second_grant.updated_at = datetime.now(timezone.utc)
+ db.commit()
+ first_grant_id = first_grant.id
+ second_grant_id = second_grant.id
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ sent_token = parse_qs(request.content.decode())["refresh_token"][0]
+ if sent_token == "second-refresh-token":
+ return httpx.Response(
+ 400,
+ json={
+ "error": "invalid_grant",
+ "error_description": "OAuth grant revoked",
+ },
+ )
+ assert sent_token == "first-refresh-token"
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "first-access-token",
+ "token_type": "Bearer",
+ "expires_in": 3600,
+ "scope": "records.read",
+ },
+ )
+
+ _mock_token_endpoint(monkeypatch, handler)
+
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ assert configs[0]["config"]["headers"]["Authorization"] == (
+ "Bearer first-access-token"
+ )
+ db.expire_all()
+ assert db.get(MCPOAuthGrant, second_grant_id) is None
+ remaining = db.get(MCPOAuthGrant, first_grant_id)
+ assert remaining is not None
+ assert remaining.status == "active"
+ assert decrypt_value(remaining.access_token) == "first-access-token"
+
+
+async def test_mcp_oauth_runtime_keeps_a_grant_a_concurrent_refresh_rotated(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ rotated_expiry = datetime.now(timezone.utc) + timedelta(hours=1)
+ real_async_client = httpx.AsyncClient
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ # Another request refreshed the grant and committed while this one ran.
+ other_db = sessionmaker(bind=db.get_bind(), autoflush=False, autocommit=False)()
+ try:
+ rotated = other_db.get(MCPOAuthGrant, grant.id)
+ rotated.access_token = encrypt_value("rotated-access-token")
+ rotated.refresh_token = encrypt_value("rotated-refresh-token")
+ rotated.expires_at = rotated_expiry
+ other_db.commit()
+ finally:
+ other_db.close()
+ return httpx.Response(
+ 400,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ def async_client_factory(*args, **kwargs):
+ return real_async_client(transport=httpx.MockTransport(handler))
+
+ async def skip_url_policy(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(mcp_oauth_service, "validate_oauth_http_url", skip_url_policy)
+ monkeypatch.setattr(mcp_oauth_service.httpx, "AsyncClient", async_client_factory)
+
+ configs, cfg = await _load_configs(db, user)
+
+ assert cfg.get_mcp_oauth_diagnostics() == []
+ assert configs[0]["config"]["headers"]["Authorization"] == (
+ "Bearer rotated-access-token"
+ )
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.revoked_at is None
+ assert decrypt_value(grant.refresh_token) == "rotated-refresh-token"
+
+
+async def test_mcp_oauth_runtime_keeps_a_grant_on_a_5xx_invalid_grant_answer(
+ db_session,
+ monkeypatch,
+):
+ db, user, _ = db_session
+ server = _add_mcp_oauth_server(db, user)
+ grant = _add_grant(
+ db,
+ server=server,
+ user=user,
+ resource_owner_key=f"xagent:user:{user.id}",
+ access_token="expired-access-token",
+ refresh_token="refresh-token-123",
+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),
+ )
+ real_async_client = httpx.AsyncClient
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 500,
+ json={"error": "invalid_grant", "error_description": "OAuth grant revoked"},
+ )
+
+ def async_client_factory(*args, **kwargs):
+ return real_async_client(transport=httpx.MockTransport(handler))
+
+ async def skip_url_policy(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(mcp_oauth_service, "validate_oauth_http_url", skip_url_policy)
+ monkeypatch.setattr(mcp_oauth_service.httpx, "AsyncClient", async_client_factory)
+
+ configs, cfg = await _load_configs(db, user)
+
+ diagnostics = cfg.get_mcp_oauth_diagnostics()
+ assert diagnostics[0]["code"] == "token_refresh_failed"
+ assert len(configs) == 1
+ _assert_unavailable_runtime_config(configs[0], server, diagnostics[0])
+ db.refresh(grant)
+ assert grant.status == "active"
+ assert grant.revoked_at is None
+ metadata = grant.metadata_json or {}
+ assert "invalid_grant_at" not in metadata
+ assert "invalid_grant_token_sha256" not in metadata
@pytest.mark.asyncio
@@ -921,6 +1775,8 @@ async def test_mcp_oauth_runtime_refresh_sanitizes_transport_exception(
assert "secret-token" not in str(configs[0])
db.refresh(grant)
assert decrypt_value(grant.access_token) == "expired-access-token"
+ assert grant.status == "active"
+ assert grant.revoked_at is None
@pytest.mark.asyncio
diff --git a/tests/web/tools/test_oauth_client_rejected.py b/tests/web/tools/test_oauth_client_rejected.py
index adea053ab..93967d80b 100644
--- a/tests/web/tools/test_oauth_client_rejected.py
+++ b/tests/web/tools/test_oauth_client_rejected.py
@@ -207,7 +207,7 @@ def _mcp_client() -> SimpleNamespace:
async def _refresh_mcp(
status: int, payload: object, *, credential_provider: str | None
-) -> str:
+) -> str | mcp_oauth.MCPOAuthRuntimeError:
async def no_url_policy(url, *, resolve_dns=False):
return None
@@ -227,7 +227,7 @@ async def _refresh_mcp(
credential_provider=credential_provider,
)
except mcp_oauth.MCPOAuthRuntimeError as exc:
- return exc.code
+ return exc
finally:
mcp_oauth.validate_oauth_http_url = original
return "ok"
@@ -240,16 +240,26 @@ async def _refresh_mcp(
# Any other client, a user's own static or registered one, only
# affects that user.
({"error": "invalid_client"}, None, "token_refresh_failed"),
+ # A dead grant is its holder's to fix, not the platform client's.
({"error": "invalid_grant"}, "hubspot", "token_refresh_failed"),
],
)
def test_an_mcp_refresh_rejects_only_the_platform_client(
payload, credential_provider, code
):
- assert (
- asyncio.run(_refresh_mcp(400, payload, credential_provider=credential_provider))
- == code
+ exc = asyncio.run(
+ _refresh_mcp(400, payload, credential_provider=credential_provider)
)
+ assert isinstance(exc, mcp_oauth.MCPOAuthRuntimeError)
+ assert exc.code == code
+
+
+def test_an_mcp_refresh_raises_the_invalid_grant_error_for_a_dead_grant():
+ exc = asyncio.run(
+ _refresh_mcp(400, {"error": "invalid_grant"}, credential_provider="hubspot")
+ )
+ assert isinstance(exc, mcp_oauth._InvalidGrantRefreshError)
+ assert exc.code == "token_refresh_failed"
def test_only_the_deployment_client_is_the_platform_client(monkeypatch):