AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): stop deleting valid MCP credentials on a 403, and verify tokens server-side (#14303)
Krzysztof Czerwinskie97e7ef800ae
diff --git a/autogpt_platform/backend/backend/api/features/mcp/conftest.py b/autogpt_platform/backend/backend/api/features/mcp/conftest.pynew file mode 100644index 0000000000..d87d125707--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/mcp/conftest.py@@ -0,0 +1,13 @@+"""Override session-scoped fixtures so unit tests run without the server."""++import pytest++[email protected](scope="session")+def server():+ yield None++[email protected](scope="session", autouse=True)+def graph_cleanup():+ yielddiff --git a/autogpt_platform/backend/backend/api/features/mcp/routes.py b/autogpt_platform/backend/backend/api/features/mcp/routes.pyindex 07f1a599ad..b95e60a084 100644--- a/autogpt_platform/backend/backend/api/features/mcp/routes.py+++ b/autogpt_platform/backend/backend/api/features/mcp/routes.py@@ -5,6 +5,7 @@ Provides endpoints for MCP tool discovery and OAuth authentication so thefrontend can list available tools on an MCP server before placing a block."""+import asyncioimport loggingfrom typing import Annotated, Anyfrom urllib.parse import urlparse@@ -34,7 +35,14 @@ from backend.blocks.mcp.oauth import MCPOAuthHandlerfrom backend.data.model import OAuth2Credentialsfrom backend.integrations.creds_manager import IntegrationCredentialsManagerfrom backend.integrations.providers import ProviderName-from backend.util.request import HTTPClientError, Requests, validate_url_host+from backend.util.request import (+ AUTH_STATUS_CODES,+ CREDENTIAL_REJECTED_STATUS_CODES,+ HTTPClientError,+ HTTPServerError,+ Requests,+ validate_url_host,+)from backend.util.settings import Settingslogger = logging.getLogger(__name__)@@ -43,6 +51,10 @@ settings = Settings()router = fastapi.APIRouter(tags=["mcp"])creds_manager = IntegrationCredentialsManager()+# Verifying a token is best-effort; it must never outlast a user's patience.+_PROBE_TIMEOUT_SECONDS = 10+_PROBE_CLOSE_TIMEOUT_SECONDS = 5+# ====================== Tool Discovery ====================== #@@ -127,7 +139,7 @@ async def discover_tools(init_result = await client.initialize()tools = await client.list_tools()except HTTPClientError as e:- if e.status_code in (401, 403):+ if e.status_code in AUTH_STATUS_CODES:raise fastapi.HTTPException(status_code=401,detail="This MCP server requires authentication. "@@ -509,10 +521,68 @@ async def mcp_store_token(except ValueError as e:raise fastapi.HTTPException(status_code=400, detail=f"Invalid server URL: {e}")- # Normalize URL so trailing-slash variants match existing credentials.+ # Normalize URL so trailing-slash and scheme-less variants match existing+ # credentials — and so the value stored below is the one every lookup path+ # re-derives from the same user input.server_url = normalize_mcp_url(request.server_url)+hostname = server_host(server_url)+ # ``validate_url_host`` permits http:// for MCP servers generally, but a+ # credential travels in a header and must not go out in cleartext.+ if not server_url.lower().startswith("https://"):+ raise fastapi.HTTPException(+ status_code=400,+ detail=f"{hostname} must be reached over https:// — a credential "+ "cannot be sent over an unencrypted connection.",+ )++ # A 2xx from this endpoint is what turns the setup card's pill green, so+ # it has to mean "this credential authenticates against the server" rather+ # than "a row was written". One ``initialize`` round-trip is the cheapest+ # proof. Only an unambiguous rejection blocks the save: any other outcome+ # says nothing about the credential, and refusing to store would strand+ # the user.+ #+ # Redirects are not followed: a cross-host hop either carries the+ # credential somewhere the user never named, or drops it and earns a 401+ # we would wrongly report as "you mistyped this".+ probe_client = MCPClient(+ server_url, authorization=authorization, follow_redirects=False+ )+ try:+ # MCPClient sets no timeout and no retry ceiling of its own.+ await asyncio.wait_for(+ probe_client.initialize(), timeout=_PROBE_TIMEOUT_SECONDS+ )+ except (HTTPClientError, HTTPServerError) as e:+ if e.status_code in CREDENTIAL_REJECTED_STATUS_CODES:+ raise fastapi.HTTPException(+ status_code=400,+ detail=f"{hostname} rejected this credential. "+ "Please check that you copied it correctly and try again.",+ )+ logger.info(+ "Could not verify MCP credential against %s (HTTP %s) — storing anyway",+ hostname,+ e.status_code,+ )+ except Exception as e:+ # No ``exc_info``: the error text embeds a server-controlled body.+ logger.info(+ "Could not verify MCP credential against %s (%s) — storing anyway",+ hostname,+ type(e).__name__,+ )+ finally:+ # Without the DELETE the probe leaks a session row server-side.+ try:+ await asyncio.wait_for(+ probe_client.close(), timeout=_PROBE_CLOSE_TIMEOUT_SECONDS+ )+ except Exception:+ logger.debug("MCP probe close failed for %s", hostname)+# Rotate the existing manual credential in place so saved graphs keep their# credential ID. OAuth rows are left alone: rewriting one would drop its# refresh token, and deleting one here would not revoke it.diff --git a/autogpt_platform/backend/backend/api/features/mcp/test_routes.py b/autogpt_platform/backend/backend/api/features/mcp/test_routes.pyindex 63deda6f8c..814f80ab80 100644--- a/autogpt_platform/backend/backend/api/features/mcp/test_routes.py+++ b/autogpt_platform/backend/backend/api/features/mcp/test_routes.py@@ -4,6 +4,7 @@ Uses httpx.AsyncClient with ASGITransport instead of fastapi.testclient.TestClieto avoid creating blocking portals that can corrupt pytest-asyncio's session event loop."""+import asynciofrom unittest.mock import AsyncMock, MagicMock, patchimport fastapi@@ -16,7 +17,7 @@ from pydantic import SecretStrfrom backend.api.features.mcp.routes import routerfrom backend.blocks.mcp.client import MCPClientError, MCPToolfrom backend.data.model import OAuth2Credentials-from backend.util.request import HTTPClientError+from backend.util.request import HTTPClientError, HTTPServerErrorapp = fastapi.FastAPI()app.include_router(router)@@ -884,10 +885,33 @@ class TestOAuthCallback:assert "token exchange failed" in response.json()["detail"].lower()+def _probe_client(error: Exception | None = None):+ client = AsyncMock()+ client.initialize = AsyncMock(side_effect=error)+ client.close = AsyncMock()+ return client+++def _probe_server(error: Exception | None = None):+ """Patch the ``/token`` verification probe. No *error* means the server+ accepts the token."""+ return patch(+ "backend.api.features.mcp.routes.MCPClient",+ return_value=_probe_client(error),+ )+++def _accepting_server():+ return _probe_server()++class TestStoreToken:@pytest.mark.asyncio(loop_scope="session")async def test_store_token_success(self, client):- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])mock_cm.create = AsyncMock()@@ -913,7 +937,10 @@ class TestStoreToken:@pytest.mark.asyncio(loop_scope="session")async def test_stored_manual_token_is_reused_by_discovery(self, client):"""A manual token must survive the real auto-lookup path on retry."""- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])mock_cm.create = AsyncMock()@@ -989,7 +1016,10 @@ class TestStoreToken:scopes=[],metadata={"mcp_server_url": "https://mcp.example.com/mcp"},)- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[old_cred])mock_cm.create = AsyncMock()mock_cm.update = AsyncMock()@@ -1034,7 +1064,10 @@ class TestStoreToken:)for index in range(2)]- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=old_creds)mock_cm.create = AsyncMock()mock_cm.update = AsyncMock()@@ -1094,7 +1127,10 @@ class TestStoreToken:"mcp_client_id": "client-abc",},)- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[oauth_cred])mock_cm.create = AsyncMock()mock_cm.update = AsyncMock()@@ -1134,7 +1170,10 @@ class TestStoreToken:scopes=[],metadata={"mcp_server_url": "https://mcp.example.com/mcp"},)- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[old_cred])mock_cm.create = AsyncMock()mock_cm.update = AsyncMock()@@ -1166,7 +1205,10 @@ class TestStoreToken:metadata={"mcp_server_url": "https://mcp.example.com/mcp"},is_managed=True,)- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[managed])mock_cm.create = AsyncMock()mock_cm.update = AsyncMock()@@ -1189,7 +1231,10 @@ class TestStoreToken:@pytest.mark.asyncio(loop_scope="session")async def test_store_token_fails_closed_when_existing_lookup_fails(self, client):- with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ with (+ _accepting_server(),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):mock_cm.store.get_creds_by_provider = AsyncMock(side_effect=RuntimeError("database unavailable"))@@ -1208,6 +1253,192 @@ class TestStoreToken:mock_cm.create.assert_not_awaited()mock_cm.update.assert_not_awaited()+ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_rejected_by_server_is_not_stored(self, client):+ """A 2xx here turns the setup card green, so it must mean the token+ actually authenticates — not merely that a row was written+ (SECRT-2592)."""+ with (+ _probe_server(HTTPClientError("HTTP 401", status_code=401)),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ response = await client.post(+ "/token",+ json={+ "server_url": "https://mcp.example.com/mcp",+ "token": "wrong-token",+ },+ )++ assert response.status_code == 400+ assert "rejected this credential" in response.json()["detail"]+ mock_cm.create.assert_not_called()++ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_survives_server_outage(self, client):+ """A 5xx says nothing about the token — refusing the save would strand+ the user during someone else's outage."""+ with (+ _probe_server(HTTPServerError("HTTP 503", status_code=503)),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ response = await client.post(+ "/token",+ json={+ "server_url": "https://mcp.example.com/mcp",+ "token": "probably-fine",+ },+ )++ assert response.status_code == 200+ mock_cm.create.assert_called_once()++ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_verifies_with_the_submitted_token(self, client):+ """The probe must carry the token — verifying anonymously would prove+ nothing."""+ probe = _probe_client()+ with (+ patch(+ "backend.api.features.mcp.routes.MCPClient", return_value=probe+ ) as MockClient,+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ await client.post(+ "/token",+ json={+ "server_url": "https://mcp.example.com/mcp",+ "token": "my-api-key-123",+ },+ )++ MockClient.assert_called_once_with(+ "https://mcp.example.com/mcp",+ authorization="Bearer my-api-key-123",+ follow_redirects=False,+ )+ probe.close.assert_awaited_once()++ @pytest.mark.asyncio(loop_scope="session")+ @pytest.mark.parametrize("status_code", [403, 404, 429])+ async def test_store_token_stores_when_status_is_not_a_rejection(+ self, client, status_code+ ):+ """Only a 401 unambiguously means "this credential was refused".++ A bare 403 is as often a per-scope decision or a WAF blocking our+ egress IP; a 404 is a wrong MCP path. Blocking the save on those would+ tell the user their token is wrong when it isn't.+ """+ with (+ _probe_server(+ HTTPClientError(f"HTTP {status_code}", status_code=status_code)+ ),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ response = await client.post(+ "/token",+ json={+ "server_url": "https://mcp.example.com/mcp",+ "token": "probably-fine",+ },+ )++ assert response.status_code == 200+ mock_cm.create.assert_called_once()++ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_rejects_explicit_http(self, client):+ """The credential rides in an Authorization header — never cleartext."""+ with patch("backend.api.features.mcp.routes.creds_manager") as mock_cm:+ mock_cm.create = AsyncMock()+ response = await client.post(+ "/token",+ json={+ "server_url": "http://mcp.example.com/mcp",+ "token": "my-api-key-123",+ },+ )++ assert response.status_code == 400+ assert "https" in response.json()["detail"].lower()+ mock_cm.create.assert_not_called()++ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_defaults_a_scheme_less_url_to_https(self, client):+ """Typing no scheme is not asking for cleartext, so it must not be+ rejected as though the user had written http://."""+ probe = _probe_client()+ with (+ patch(+ "backend.api.features.mcp.routes.MCPClient", return_value=probe+ ) as MockClient,+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ response = await client.post(+ "/token",+ json={+ "server_url": "mcp.example.com/mcp",+ "token": "my-api-key-123",+ },+ )++ assert response.status_code == 200+ assert response.json()["host"] == "https://mcp.example.com/mcp"+ assert MockClient.call_args.args[0] == "https://mcp.example.com/mcp"++ @pytest.mark.asyncio(loop_scope="session")+ async def test_store_token_survives_a_hanging_server(self, client):+ """MCPClient sets no timeout and no retry ceiling, so an unbounded+ probe could pin the handler forever."""+ hanging = _probe_client()+ cancelled = asyncio.Event()++ async def never_returns():+ # Raising TimeoutError directly would pass with ``wait_for``+ # removed; the probe has to actually hang for this to mean+ # anything.+ try:+ await asyncio.Event().wait()+ finally:+ cancelled.set()++ hanging.initialize = never_returns+ with (+ patch("backend.api.features.mcp.routes.MCPClient", return_value=hanging),+ patch("backend.api.features.mcp.routes._PROBE_TIMEOUT_SECONDS", 0.01),+ patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,+ ):+ mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[])+ mock_cm.create = AsyncMock()++ response = await client.post(+ "/token",+ json={+ "server_url": "https://mcp.example.com/mcp",+ "token": "probably-fine",+ },+ )++ assert response.status_code == 200+ mock_cm.create.assert_called_once()+ assert cancelled.is_set()+class TestSSRFValidation:"""Verify that validate_url_host is enforced on all endpoints."""@@ -1252,7 +1483,7 @@ class TestSSRFValidation:response = await client.post("/token",json={- "server_url": "http://127.0.0.1/mcp",+ "server_url": "https://127.0.0.1/mcp","token": "some-token",},)diff --git a/autogpt_platform/backend/backend/blocks/mcp/client.py b/autogpt_platform/backend/backend/blocks/mcp/client.pyindex 57ec4903a7..6f7a35bedf 100644--- a/autogpt_platform/backend/backend/blocks/mcp/client.py+++ b/autogpt_platform/backend/backend/blocks/mcp/client.py@@ -182,11 +182,21 @@ class MCPClient:self,server_url: str,authorization: str | None = None,+ follow_redirects: bool = True,):+ """*follow_redirects* exists for credential verification.++ A cross-host redirect correctly strips the ``Authorization`` header,+ so the target answers an unauthenticated request — which a caller+ checking "was this credential accepted?" would misread as a rejection.+ Such a caller should pass ``False`` and treat the resulting+ ``MCPClientError`` as "could not verify".+ """from backend.blocks.mcp.helpers import normalize_mcp_urlself.server_url = normalize_mcp_url(server_url)self.authorization = authorization+ self.follow_redirects = follow_redirectsself._request_id = 0self._session_id: str | None = Noneself.era: MCPProtocolEra | None = None@@ -270,7 +280,9 @@ class MCPClient:),extra_headers=headers,)- return await requests.post(self.server_url, json=payload)+ return await requests.post(+ self.server_url, json=payload, allow_redirects=self.follow_redirects+ )def _decode_reply(self, response: Response, *, strict: bool) -> _Reply:"""Decode a reply body as JSON or SSE.@@ -344,7 +356,9 @@ class MCPClient:retry_max_attempts=_HTTP_RETRY_ATTEMPTS,extra_headers=headers,)- await requests.post(self.server_url, json=notification)+ await requests.post(+ self.server_url, json=notification, allow_redirects=self.follow_redirects+ )async def _legacy_initialize(self) -> dict[str, Any]:result = await self._send_legacy(@@ -699,6 +713,7 @@ class MCPClient:requests = Requests(raise_for_status=False,+ retry_max_attempts=_HTTP_RETRY_ATTEMPTS,)for url in candidates:try:@@ -751,6 +766,7 @@ class MCPClient:requests = Requests(raise_for_status=False,+ retry_max_attempts=_HTTP_RETRY_ATTEMPTS,)for url, expected_issuer in candidates:try:@@ -782,7 +798,9 @@ class MCPClient:retry_max_attempts=_HTTP_RETRY_ATTEMPTS,extra_headers=headers,)- await requests.delete(self.server_url)+ await requests.delete(+ self.server_url, allow_redirects=self.follow_redirects+ )except Exception:passfinally:diff --git a/autogpt_platform/backend/backend/blocks/mcp/helpers.py b/autogpt_platform/backend/backend/blocks/mcp/helpers.pyindex a350488b1b..97e3c97096 100644--- a/autogpt_platform/backend/backend/blocks/mcp/helpers.py+++ b/autogpt_platform/backend/backend/blocks/mcp/helpers.py@@ -20,8 +20,17 @@ def normalize_mcp_url(url: str) -> str:Strips leading/trailing whitespace and a single trailing slash so that``https://mcp.example.com/`` and ``https://mcp.example.com`` resolve tothe same stored credential.++ A URL with no scheme gets ``https://``. That is the user omitting a scheme+ rather than asking for cleartext — but the reason it belongs *here* is+ matching, not politeness: the credential is stored under this value and+ every lookup re-derives it from user input through this same function, so+ the default has to be applied in one place or storage and lookup disagree."""- return url.strip().rstrip("/")+ url = url.strip().rstrip("/")+ if url and "://" not in url:+ url = f"https://{url}"+ return urldef server_host(server_url: str) -> str:@@ -145,6 +154,12 @@ async def auto_lookup_mcp_credential(Returns the credential with the latest ``access_token_expires_at``, refreshedif it can expire and needs it, or ``None`` when no match is found.++ A failed refresh also yields ``None``, deliberately. Returning the stale+ access token instead would earn a 401 from the server, and the caller+ treats a 401 on a credential it *has* as proof the token is dead — so a+ transient outage at the provider's token endpoint would delete a row whose+ refresh token is still perfectly good."""try:mgr = IntegrationCredentialsManager()diff --git a/autogpt_platform/backend/backend/blocks/mcp/test_helpers.py b/autogpt_platform/backend/backend/blocks/mcp/test_helpers.pyindex e43600fcbc..96fa22c75e 100644--- a/autogpt_platform/backend/backend/blocks/mcp/test_helpers.py+++ b/autogpt_platform/backend/backend/blocks/mcp/test_helpers.py@@ -1,6 +1,6 @@"""Unit tests for the shared MCP helpers."""-from unittest.mock import AsyncMock, patch+from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom pydantic import SecretStr@@ -235,3 +235,62 @@ async def test_a_manual_credential_outranks_a_surviving_oauth_row():assert result.id == manual.id# And a manual credential is never put through the OAuth refresh path.manager.refresh_if_needed.assert_not_awaited()+++# ---------------------------------------------------------------------------+# auto_lookup_mcp_credential+# ---------------------------------------------------------------------------+++_SERVER_URL = "https://mcp.example.com/mcp"+++def _oauth_credential() -> OAuth2Credentials:+ """A real OAuth MCP credential: expired access token, good refresh token."""+ return OAuth2Credentials(+ provider="mcp",+ title="MCP: mcp.example.com",+ access_token=SecretStr("expired-access-token"),+ refresh_token=SecretStr("still-good-refresh-token"),+ access_token_expires_at=1,+ scopes=[],+ metadata={+ "mcp_server_url": _SERVER_URL,+ "mcp_token_url": "https://auth.example.com/token",+ },+ )+++def _real_manager_over(creds: list[OAuth2Credentials]):+ """Patch only the *store*, so the real ``IntegrationCredentialsManager``+ and its real ``refresh_if_needed`` run.++ Mocking ``refresh_if_needed`` here would make these tests pass against the+ unfixed code — the bug lived inside it.+ """+ store = MagicMock()+ store.get_creds_by_provider = AsyncMock(return_value=creds)+ return patch(+ "backend.integrations.creds_manager.IntegrationCredentialsStore",+ return_value=store,+ )+++async def test_auto_lookup_returns_none_when_a_real_refresh_fails():+ """A transient outage at the provider's token endpoint must not hand back+ the stale access token.++ The caller reads a 401 on a credential it *has* as proof the token is dead+ and deletes the row — taking a still-valid refresh token with it.+ """+ cred = _oauth_credential()+ with (+ _real_manager_over([cred]),+ patch(+ "backend.integrations.creds_manager.IntegrationCredentialsManager"+ "._get_oauth_handler",+ new_callable=AsyncMock,+ side_effect=RuntimeError("token endpoint 503"),+ ),+ ):+ assert await auto_lookup_mcp_credential("user-1", _SERVER_URL) is Nonediff --git a/autogpt_platform/backend/backend/blocks/mcp/test_mcp.py b/autogpt_platform/backend/backend/blocks/mcp/test_mcp.pyindex 7871b521df..2a2e5b3979 100644--- a/autogpt_platform/backend/backend/blocks/mcp/test_mcp.py+++ b/autogpt_platform/backend/backend/blocks/mcp/test_mcp.py@@ -697,3 +697,49 @@ class TestMCPToolBlock:assert captured_tokens == [None]assert outputs == [("result", "ok")]+++async def test_verification_client_does_not_follow_redirects():+ """A verification probe must not chase a redirect.++ ``Requests`` strips ``Authorization`` when a redirect crosses origins, so+ the target would answer an anonymous request — and a caller asking "was+ this credential accepted?" would read the resulting 401 as a rejection.+ """+ client = MCPClient(+ "https://mcp.example.com/mcp",+ authorization="Bearer secret",+ follow_redirects=False,+ )+ with patch("backend.blocks.mcp.client.Requests") as MockRequests:+ MockRequests.return_value.post = AsyncMock(+ side_effect=RuntimeError("stop here")+ )+ with pytest.raises(RuntimeError):+ await client._send_request("initialize")++ assert MockRequests.return_value.post.call_args.kwargs["allow_redirects"] is False+++async def test_verification_client_does_not_follow_redirects_on_close():+ """The session-closing DELETE carries the token too, so it must not redirect.++ ``close()`` builds its own ``Requests`` with ``_build_headers()`` — which+ includes ``Authorization``. A legacy server that mints an ``Mcp-Session-Id``+ and then answers the DELETE with a cross-host 307 would otherwise receive+ the freshly typed credential.+ """+ client = MCPClient(+ "https://mcp.example.com/mcp",+ authorization="Bearer secret",+ follow_redirects=False,+ )+ client._session_id = "session-abc"++ with patch("backend.blocks.mcp.client.Requests") as MockRequests:+ MockRequests.return_value.delete = AsyncMock()+ await client.close()++ assert MockRequests.return_value.delete.call_args.kwargs["allow_redirects"] is Falsediff --git a/autogpt_platform/backend/backend/copilot/tools/mcp_manual_token_e2e_test.py b/autogpt_platform/backend/backend/copilot/tools/mcp_manual_token_e2e_test.pynew file mode 100644index 0000000000..4bae34d9da--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/mcp_manual_token_e2e_test.py@@ -0,0 +1,208 @@+"""Round-trip regression for MCP server-URL normalization (SECRT-2592).++``/mcp/token`` writes ``metadata["mcp_server_url"]`` from the URL the user+typed; every lookup path re-derives that key from the same user input. If the+two sides ever normalize differently, the row is stored under a key no lookup+produces — 2xx, a green pill, and an agent that reports the server as not+connected. That is the contradiction this PR removes, and it is not visible+from either side alone, so this walks the whole path with only the database+faked:++ POST /mcp/token → credential store → auto_lookup_mcp_credential+ → run_mcp_tool → MCPClient(authorization=...)++Nothing in between is stubbed. The behaviours these tests used to also cover —+the token reaching the client, the connect card, 401 invalidation, 403+retention, per-user isolation — landed on ``dev`` in #14075 or are covered by+``test_run_mcp_tool.py``, so only the normalization round trips remain here.+"""++import contextlib+from unittest.mock import AsyncMock, MagicMock, patch++import fastapi+import httpx+import pytest+import pytest_asyncio+from autogpt_libs.auth import get_user_id++from backend.api.features.mcp.routes import router+from backend.data.model import Credentials++from ._test_data import make_session+from .run_mcp_tool import RunMCPToolTool++_USER_ID = "test-user-mcp-manual-token"+_SERVER_URL = "https://mcp.datafa.st/mcp"+_SERVER_URL_NO_SCHEME = "mcp.datafa.st/mcp"+_TOKEN = "dft_live_token_value"+# A bare token is stored (and sent) as a complete Bearer header.+_AUTHORIZATION = f"Bearer {_TOKEN}"++app = fastapi.FastAPI()+app.include_router(router)+app.dependency_overrides[get_user_id] = lambda: _USER_ID+++class FakeCredentialsStore:+ """In-memory stand-in for ``IntegrationCredentialsStore``.++ Rows are keyed by ``user_id`` — the real store scopes every read to the+ owner, and a harness that ignored it would pass just as happily against+ code that served one user another's MCP token.++ Only the handful of methods this path touches are implemented; anything+ else should fail loudly rather than silently return a mock.+ """++ def __init__(self) -> None:+ self.by_user: dict[str, list[Credentials]] = {}++ @property+ def rows(self) -> list[Credentials]:+ return self.by_user.get(_USER_ID, [])++ async def add_creds(self, user_id: str, credentials: Credentials) -> None:+ self.by_user.setdefault(user_id, []).append(credentials)++ async def get_creds_by_provider(+ self, user_id: str, provider: str+ ) -> list[Credentials]:+ return [c for c in self.by_user.get(user_id, []) if c.provider == provider]++ async def get_creds_by_id(self, user_id: str, credentials_id: str):+ owned = self.by_user.get(user_id, [])+ return next((c for c in owned if c.id == credentials_id), None)++ async def delete_creds_by_id(self, user_id: str, credentials_id: str) -> None:+ owned = self.by_user.get(user_id, [])+ self.by_user[user_id] = [c for c in owned if c.id != credentials_id]+++@pytest_asyncio.fixture+async def store():+ """Route every ``IntegrationCredentialsManager`` at one in-memory store.++ The routes module builds its manager at import time, so that instance is+ repointed directly; ``auto_lookup_mcp_credential`` builds a fresh manager+ per call, which the class-level patch covers.+ """+ fake = FakeCredentialsStore()+ with (+ patch(+ "backend.integrations.creds_manager.IntegrationCredentialsStore",+ return_value=fake,+ ),+ patch("backend.api.features.mcp.routes.creds_manager.store", fake),+ # ``invalidate_mcp_credential`` goes through ``mgr.delete``, which+ # takes a Redis lock, and every write publishes a creds-changed event+ # over Redis. Neither is what this test is about; only the store is+ # faked, everything between the route and it runs for real.+ patch(+ "backend.integrations.creds_manager.IntegrationCredentialsManager._locked",+ _noop_lock,+ ),+ patch(+ "backend.integrations.creds_manager._invoke_creds_changed_hook",+ new_callable=AsyncMock,+ ),+ ):+ yield fake+++async def _noop_lock(*_args, **_kwargs):+ yield+++@pytest_asyncio.fixture+async def client():+ transport = httpx.ASGITransport(app=app)+ async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:+ yield c++[email protected](autouse=True)+def _bypass_ssrf_validation():+ """Test URLs don't resolve; SSRF enforcement has its own tests."""+ with (+ patch(+ "backend.api.features.mcp.routes.validate_url_host",+ new_callable=AsyncMock,+ ),+ patch(+ "backend.copilot.tools.run_mcp_tool.validate_url_host",+ new_callable=AsyncMock,+ ),+ ):+ yield+++def _mcp_client(tools: list | None = None):+ client = AsyncMock()+ client.initialize = AsyncMock(return_value={"protocolVersion": "2025-06-18"})+ client.list_tools = AsyncMock(return_value=tools or [])+ client.close = AsyncMock()+ return client+++def _tool(name: str):+ t = MagicMock()+ t.name = name+ t.description = f"Description for {name}"+ t.input_schema = {"type": "object", "properties": {}, "required": []}+ return t+++async def _store_token(client) -> None:+ with patch("backend.api.features.mcp.routes.MCPClient", return_value=_mcp_client()):+ response = await client.post(+ "/token", json={"server_url": _SERVER_URL, "token": _TOKEN}+ )+ assert response.status_code == 200, response.text+++async def test_trailing_slash_variant_still_resolves(client, store):+ """The card and the agent can disagree on the trailing slash; the stored+ credential has to be found either way."""+ await _store_token(client)++ with patch(+ "backend.copilot.tools.run_mcp_tool.MCPClient",+ return_value=_mcp_client([_tool("get_analytics")]),+ ) as MockClient:+ await RunMCPToolTool()._execute(+ user_id=_USER_ID,+ session=make_session(_USER_ID),+ server_url=f"{_SERVER_URL}/",+ )++ assert MockClient.call_args.kwargs["authorization"] == _AUTHORIZATION+++async def test_scheme_less_server_url_resolves_after_storing(client, store):+ """Storing and looking up must apply the same scheme rule.++ Users type ``mcp.example.com/mcp``. ``/token`` canonicalizes that to+ ``https://`` before writing ``metadata["mcp_server_url"]``, so every lookup+ has to canonicalize identically or the row is stored under a key no lookup+ ever produces — a 2xx and a green pill over a credential the agent cannot+ find, which is the exact contradiction this PR removes.+ """+ with patch("backend.api.features.mcp.routes.MCPClient", return_value=_mcp_client()):+ response = await client.post(+ "/token", json={"server_url": _SERVER_URL_NO_SCHEME, "token": _TOKEN}+ )+ assert response.status_code == 200, response.text++ with patch(+ "backend.copilot.tools.run_mcp_tool.MCPClient",+ return_value=_mcp_client([_tool("get_analytics")]),+ ) as MockClient:+ await RunMCPToolTool()._execute(+ user_id=_USER_ID,+ session=make_session(_USER_ID),+ server_url=_SERVER_URL_NO_SCHEME,+ )++ assert MockClient.call_args.kwargs["authorization"] == _AUTHORIZATIONdiff --git a/autogpt_platform/backend/backend/copilot/tools/run_mcp_tool.py b/autogpt_platform/backend/backend/copilot/tools/run_mcp_tool.pyindex 50d018a0fa..88931bb5ec 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_mcp_tool.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_mcp_tool.py@@ -22,7 +22,12 @@ from backend.copilot.sdk.file_ref import (expand_file_refs_in_args,)from backend.copilot.tools.utils import build_missing_credentials_from_field_info-from backend.util.request import HTTPClientError, validate_url_host+from backend.util.request import (+ AUTH_STATUS_CODES,+ CREDENTIAL_REJECTED_STATUS_CODES,+ HTTPClientError,+ validate_url_host,+)from .base import BaseToolfrom .models import (@@ -47,9 +52,6 @@ _ERROR_SCHEMA_MAX_CHARS = 4000_DISCOVERY_MAX_TOOLS = 100_PARAMS_SUMMARY_MAX_CHARS = 400-# HTTP status codes that indicate authentication is required-_AUTH_STATUS_CODES = {401, 403}-def _service_name(host: str) -> str:"""Strip the 'mcp.' prefix from an MCP hostname: 'mcp.sentry.dev' → 'sentry.dev'"""@@ -247,9 +249,13 @@ class RunMCPToolTool(BaseTool):try:await probe_client.initialize()except HTTPClientError as probe_err:- if probe_err.status_code in _AUTH_STATUS_CODES:- await invalidate_mcp_credential(user_id, creds.id)+ if probe_err.status_code in AUTH_STATUS_CODES:connected = False+ if (+ probe_err.status_code+ in CREDENTIAL_REJECTED_STATUS_CODES+ ):+ await invalidate_mcp_credential(user_id, creds.id)# Other HTTP statuses (5xx, redirects, etc.) →# leave the cred in place and report# "optimistically connected" — the user can@@ -305,18 +311,23 @@ class RunMCPToolTool(BaseTool):)except HTTPClientError as e:- if e.status_code in _AUTH_STATUS_CODES:- # 401/403 → user needs to (re)authenticate. Fire the setup- # card whether or not we have a stored credential row: when- # `creds` is None the user has never connected, and when it- # is non-None the stored token has been revoked / expired- # server-side without us knowing (refresh_if_needed only- # refreshes when local `access_token_expires_at` says so).- # If we have a stale row, delete it so the next attempt- # doesn't loop on the same dead token.- if creds is not None:+ if e.status_code in AUTH_STATUS_CODES:+ credential_rejected = e.status_code in CREDENTIAL_REJECTED_STATUS_CODES+ # Fire the setup card whether or not a credential row exists.+ if creds is not None and credential_rejected:await invalidate_mcp_credential(user_id, creds.id)- return self._build_setup_requirements(server_url, session_id)+ # A 403 over a credential we deliberately kept means "this+ # token is fine, it just may not call *this* tool". Rendering+ # a bare Connect button there invites the user to re-paste the+ # same working token: ``/token``'s probe 403s too, which is+ # not a rejection, so it stores, returns 2xx and greens the+ # pill — and the next call 403s again. Reporting it as+ # connected breaks that loop.+ return self._build_setup_requirements(+ server_url,+ session_id,+ connected=creds is not None and not credential_rejected,+ )host = server_host(server_url)logger.warning("MCP HTTP error for %s: status=%s", host, e.status_code)return ErrorResponse(diff --git a/autogpt_platform/backend/backend/copilot/tools/test_run_mcp_tool.py b/autogpt_platform/backend/backend/copilot/tools/test_run_mcp_tool.pyindex 68f87c6f06..ed7c33d007 100644--- a/autogpt_platform/backend/backend/copilot/tools/test_run_mcp_tool.py+++ b/autogpt_platform/backend/backend/copilot/tools/test_run_mcp_tool.py@@ -919,15 +919,27 @@ async def test_surface_connect_card_probe_timeout_reports_optimistically_connect@pytest.mark.asyncio(loop_scope="session")-async def test_auth_error_with_stale_creds_fires_setup_and_invalidates():- """HTTP 403 when creds ARE present → still fire setup card, drop the stale row.+ "status_code, expect_invalidated, expect_connected",+ [+ # 401 is the one status that means "this credential was refused".+ (401, True, False),+ # A 403 routinely means "valid token, not allowed to call *this*" —+ # deleting on it forces a re-entry that fails identically, and a bare+ # Connect button invites re-pasting the token that already works.+ (403, False, True),+ ],+)+async def test_auth_error_with_stale_creds_fires_setup_and_invalidates(+ status_code, expect_invalidated, expect_connected+):+ """Auth error when creds ARE present → always fire the setup card; drop the+ row only when the credential itself was refused.Stored creds whose ``access_token_expires_at`` is in the future locally- but which the server has revoked/expired don't get refreshed by+ but which the server has revoked don't get refreshed by``auto_lookup_mcp_credential`` — they come back live, the request 401s,- and the user is stuck. The fix: on any 401/403, surface the setup card- so the user can re-auth, and delete the stale row so the next attempt- doesn't loop on the same dead token.+ and the user is stuck until the dead row goes."""from backend.util.request import HTTPClientError@@ -948,7 +960,7 @@ async def test_auth_error_with_stale_creds_fires_setup_and_invalidates():):mock_client = AsyncMock()mock_client.initialize = AsyncMock(- side_effect=HTTPClientError("Forbidden", status_code=403)+ side_effect=HTTPClientError("Auth error", status_code=status_code))with patch("backend.copilot.tools.run_mcp_tool.MCPClient",@@ -969,9 +981,15 @@ async def test_auth_error_with_stale_creds_fires_setup_and_invalidates():server_url=_SERVER_URL,)mock_build.assert_called_once()- mock_invalidate.assert_awaited_once_with(- _USER_ID, "stale-cred-id"+ assert (+ mock_build.call_args.kwargs["connected"] is expect_connected)+ if expect_invalidated:+ mock_invalidate.assert_awaited_once_with(+ _USER_ID, "stale-cred-id"+ )+ else:+ mock_invalidate.assert_not_awaited()assert response is mock_build.return_valuediff --git a/autogpt_platform/backend/backend/util/request.py b/autogpt_platform/backend/backend/util/request.pyindex 5cbb357690..446a3cbb69 100644--- a/autogpt_platform/backend/backend/util/request.py+++ b/autogpt_platform/backend/backend/util/request.py@@ -44,6 +44,16 @@ DEFAULT_USER_AGENT = "AutoGPT-Platform/1.0 (https://github.com/Significant-Gravi# Retry status codes for which we will automatically retry the requestTHROTTLE_RETRY_STATUS_CODES: set[int] = {429, 500, 502, 503, 504, 408}+# Statuses meaning the request was not authenticated or not authorised. Both+# warrant asking the user to (re)connect, so they gate the setup-card paths.+AUTH_STATUS_CODES: set[int] = {401, 403}++# Only 401 unambiguously means "this credential was rejected". A bare 403 is+# routinely a scope decision or a WAF blocking our egress IP, both with a+# perfectly valid token — so anything destructive or blocking keys off this+# narrower set, never AUTH_STATUS_CODES.+CREDENTIAL_REJECTED_STATUS_CODES: set[int] = {401}+# List of IP networks to blockBLOCKED_IP_NETWORKS = [# --8<-- [start:BLOCKED_IP_NETWORKS]diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/MCPSetupCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/MCPSetupCard.tsxindex b03c20ffff..3270fc878b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/MCPSetupCard.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/MCPSetupCard.tsx@@ -81,7 +81,11 @@ export function MCPSetupCard({ output, retryInstruction }: Props) {// Connect successfully in this component, ``localConnected`` stays// true even if the live-cred query is briefly stale.const normalizedServer = normalizeMcpUrl(serverUrl);- const { data: liveCredsRes } = useGetV1ListCredentials({+ const {+ data: liveCredsRes,+ isFetchedAfterMount: liveCredsFetched,+ isError: liveCredsError,+ } = useGetV1ListCredentials({query: {select: (res) => (res.status === 200 ? res.data : null),// No staleTime — when this card mounts (e.g. immediately after the@@ -94,12 +98,16 @@ export function MCPSetupCard({ output, retryInstruction }: Props) {refetchOnMount: "always",},});- // Tri-state: ``true``/``false`` when the live API responded, ``"unknown"``- // while loading or after a network/auth failure (``select`` returned- // ``null``). Treating an unknown live state as ``false`` would override- // a still-valid persisted snapshot — see review for the- // initiallyConnected=false + 5xx race that surfaces a bare Connect- // button despite an existing cred.+ // Tri-state, because "we don't know yet" must fall back to the persisted+ // snapshot rather than to disconnected. Unknown covers: this mount's fetch+ // hasn't landed (React Query keeps serving the previous cache until it+ // does, and that cache still lists rows the backend just invalidated), the+ // refetch settled but errored (stale data is still served), and a non-200+ // that ``select`` mapped to null.+ //+ // ``isFetchedAfterMount`` rather than ``isFetching``: this query key is+ // app-wide, so ``isFetching`` also goes true on window focus and on every+ // credential mutation elsewhere, blanking a genuinely connected card.const liveCredential = !Array.isArray(liveCredsRes)? null: liveCredsRes.find(@@ -108,9 +116,10 @@ export function MCPSetupCard({ output, retryInstruction }: Props) {typeof c.host === "string" &&normalizeMcpUrl(c.host) === normalizedServer,);- const liveHasCred: boolean | "unknown" = !Array.isArray(liveCredsRes)- ? "unknown"- : Boolean(liveCredential);+ const liveHasCred: boolean | "unknown" =+ !liveCredsFetched || liveCredsError || !Array.isArray(liveCredsRes)+ ? "unknown"+ : Boolean(liveCredential);const storedManualAuthScheme: MCPAuthScheme =liveCredential?.mcp_auth_scheme === "basic" ? "basic" : "bearer";@@ -488,7 +497,7 @@ export function MCPSetupCard({ output, retryInstruction }: Props) {onClick={() => handleManualToken()}disabled={loading || !manualToken.trim()}>- Use Token+ {loading ? "Verifying…" : "Use Token"}</Button></div></div>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/__tests__/MCPSetupCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/__tests__/MCPSetupCard.test.tsxindex c9a6a2e220..f698cf2d55 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/__tests__/MCPSetupCard.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunMCPTool/components/MCPSetupCard/__tests__/MCPSetupCard.test.tsx@@ -50,19 +50,28 @@ let mockLiveCreds: Array<{host?: string | null;mcp_auth_scheme?: "basic" | "bearer" | null;}> = [];+// Defaults to "this mount's fetch has landed cleanly" so existing cases read+// as before; the guard cases below flip them explicitly.+let mockLiveCredsFetched = true;+let mockLiveCredsError = false;function setMockLiveCreds(next: Array<{provider: string;host?: string | null;mcp_auth_scheme?: "basic" | "bearer" | null;}>,+ opts: { fetchedAfterMount?: boolean; isError?: boolean } = {},) {mockLiveCreds = next;+ mockLiveCredsFetched = opts.fetchedAfterMount ?? true;+ mockLiveCredsError = opts.isError ?? false;}vi.mock("@/app/api/__generated__/endpoints/integrations/integrations", () => ({useGetV1ListCredentials: () => ({data: mockLiveCreds,isLoading: false,+ isFetchedAfterMount: mockLiveCredsFetched,+ isError: mockLiveCredsError,}),}));@@ -141,6 +150,35 @@ describe("MCPSetupCard", () => {expect(screen.getByRole("button", { name: /reconnect/i })).toBeDefined();});+ it("does not show Connected from a cache whose post-mount fetch has not landed", () => {+ // SECRT-2592: ``refetchOnMount`` does not stop React Query serving the+ // previous cache until the refetch lands. Right after the backend+ // invalidated a dead row that cache still lists it, and OR-ing it in+ // painted a green pill over the backend's authoritative+ // ``has_all_credentials=false`` — the exact contradiction users reported+ // (UI says Connected, agent says not connected).+ setMockLiveCreds(+ [{ provider: "mcp", host: "https://mcp.example.com/mcp" }],+ {+ fetchedAfterMount: false,+ },+ );+ render(<MCPSetupCard output={makeSetupOutput()} />);+ expect(screen.queryByText(/connected to example\.com/i)).toBeNull();+ expect(+ screen.getByRole("button", { name: /connect example\.com/i }),+ ).toBeDefined();+ });++ it("does not drop Connected when a background refetch fails but serves stale data", () => {+ // A settled-but-errored refetch keeps the previous data, so ``data``+ // alone still looks authoritative. Treating it as unknown falls back to+ // the persisted snapshot instead of trusting a row we can't confirm.+ setMockLiveCreds([], { isError: true });+ render(<MCPSetupCard output={makeSetupOutput(undefined, true)} />);+ expect(screen.getByText(/connected to example\.com/i)).toBeDefined();+ });+it("matches live creds across a trailing slash on the server URL", () => {// Card was emitted with no trailing slash; stored cred has one.// The frontend ``normalizeMcpUrl`` mirrors the backend so they match.@@ -766,6 +804,40 @@ describe("MCPSetupCard", () => {expect(postV2StoreABearerTokenForAnMcpServer).not.toHaveBeenCalled();});+ it("labels the Use Token button while verification is in flight", async () => {+ // Verification is a round-trip to the MCP server, so the button can no+ // longer stay static and merely disabled the way it did when storing was+ // a local write.+ const {+ postV2DiscoverAvailableToolsOnAnMcpServer,+ postV2InitiateOauthLoginForAnMcpServer,+ } = await import("@/app/api/__generated__/endpoints/mcp/mcp");+ vi.mocked(postV2InitiateOauthLoginForAnMcpServer).mockResolvedValueOnce({+ status: 400,+ data: { detail: "No OAuth" },+ headers: new Headers(),+ } as never);+ // Never settles: pins the in-flight state.+ vi.mocked(postV2DiscoverAvailableToolsOnAnMcpServer).mockReturnValueOnce(+ new Promise(() => {}) as never,+ );++ render(<MCPSetupCard output={makeSetupOutput()} />);+ fireEvent.click(screen.getByRole("button", { name: /connect example/i }));+ await waitFor(() => {+ expect(screen.getByPlaceholderText(manualTokenPlaceholder)).toBeDefined();+ });++ fireEvent.change(screen.getByPlaceholderText(manualTokenPlaceholder), {+ target: { value: "some-token" },+ });+ fireEvent.click(screen.getByRole("button", { name: /use token/i }));++ await waitFor(() => {+ expect(screen.getByRole("button", { name: /verifying/i })).toBeDefined();+ });+ });+it("re-renders not-connected branch when manual token POST fails (forceDisconnected flips on)", async () => {// ``handleManualToken`` catch must flip ``forceDisconnected=true`` —// otherwise an existing live cred would re-show the Connected pill