Skip to content

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): a URL that is not an MCP server gets a one-line 400, not its 404 page as a 502 (#15232)

Reinier van der Leera927d967e7df
diff --git a/autogpt_platform/backend/backend/api/features/mcp/routes.py b/autogpt_platform/backend/backend/api/features/mcp/routes.py
index 1732ef1047..7171d9d1c1 100644
--- a/autogpt_platform/backend/backend/api/features/mcp/routes.py
+++ b/autogpt_platform/backend/backend/api/features/mcp/routes.py
@@ -159,6 +159,7 @@ async def discover_tools(
authorization = mcp_authorization_header(stored_credential)
client = MCPClient(request.server_url, authorization=authorization)
+ hostname = server_host(request.server_url)
try:
init_result = await client.initialize()
@@ -170,7 +171,24 @@ async def discover_tools(
detail="This MCP server requires authentication. "
"Please provide a valid auth credential.",
)
- raise fastapi.HTTPException(status_code=502, detail=str(e))
+ if e.status_code == 429:
+ raise fastapi.HTTPException(
+ status_code=429,
+ detail=f"{hostname} is rate-limiting requests (HTTP 429). "
+ "Please try again shortly.",
+ )
+ # The error text carries the site's whole error page; keep only the status.
+ raise fastapi.HTTPException(
+ status_code=400,
+ detail=f"No MCP server answered at {hostname} (HTTP {e.status_code}). "
+ "Please check the URL.",
+ )
+ except HTTPServerError as e:
+ raise fastapi.HTTPException(
+ status_code=502,
+ detail=f"{hostname} failed to answer (HTTP {e.status_code}). "
+ "Please try again later.",
+ )
except MCPClientError as e:
raise fastapi.HTTPException(status_code=502, detail=str(e))
except Exception as e:
@@ -192,9 +210,7 @@ async def discover_tools(
for t in tools
],
server_name=(
- init_result.get("serverInfo", {}).get("name")
- or server_host(request.server_url)
- or "MCP"
+ init_result.get("serverInfo", {}).get("name") or hostname or "MCP"
),
protocol_version=init_result.get("protocolVersion"),
)
diff --git a/autogpt_platform/backend/backend/api/features/mcp/test_routes.py b/autogpt_platform/backend/backend/api/features/mcp/test_routes.py
index 4989a975d6..a9c2f107a9 100644
--- a/autogpt_platform/backend/backend/api/features/mcp/test_routes.py
+++ b/autogpt_platform/backend/backend/api/features/mcp/test_routes.py
@@ -5,6 +5,7 @@ to avoid creating blocking portals that can corrupt pytest-asyncio's session eve
"""
import asyncio
+from http import HTTPStatus
from unittest.mock import AsyncMock, MagicMock, patch
from urllib.parse import parse_qs, urlsplit
@@ -13,13 +14,14 @@ import httpx
import pytest
import pytest_asyncio
from autogpt_libs.auth import get_user_id
+from multidict import CIMultiDict, CIMultiDictProxy
from pydantic import SecretStr
from backend.api.features.mcp.oauth_registration import preregistered_client
from backend.api.features.mcp.routes import NO_OAUTH_CODE, router
from backend.blocks.mcp.client import MCPClientError, MCPTool
from backend.data.model import OAuth2Credentials
-from backend.util.request import HTTPClientError, HTTPServerError
+from backend.util.request import HTTPClientError, HTTPServerError, Response
app = fastapi.FastAPI()
app.include_router(router)
@@ -271,6 +273,36 @@ class TestDiscoverTools:
assert response.status_code == 401
assert "requires authentication" in response.json()["detail"]
+ @pytest.mark.parametrize(
+ "remote_status, expected_status", [(404, 400), (429, 429), (503, 502)]
+ )
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_discover_tools_never_shows_the_remote_error_page(
+ self, client, remote_status, expected_status
+ ):
+ # Only the HTTP layer is faked, so the real client's era fallback
+ # produces the exception the route has to map.
+ with (
+ patch("backend.blocks.mcp.client.Requests") as MockRequests,
+ patch(
+ "backend.api.features.mcp.routes.auto_lookup_mcp_credential",
+ new_callable=AsyncMock,
+ return_value=None,
+ ),
+ ):
+ MockRequests.return_value.post = AsyncMock(
+ return_value=_website_error_page(remote_status)
+ )
+ response = await client.post(
+ "/discover-tools",
+ json={"server_url": "https://blog.example.com/mcp"},
+ )
+
+ detail = response.json()["detail"]
+ assert response.status_code == expected_status
+ assert f"HTTP {remote_status}" in detail
+ assert "<!doctype" not in detail.lower()
+
@pytest.mark.asyncio(loop_scope="session")
async def test_discover_tools_missing_url(self, client):
response = await client.post("/discover-tools", json={})
@@ -1733,3 +1765,14 @@ class TestSSRFValidation:
assert response.status_code == 400
assert "blocked loopback" in response.json()["detail"].lower()
+
+
+def _website_error_page(status: int) -> Response:
+ """What a website answers at a URL that is not an MCP endpoint."""
+ page = Response.__new__(Response)
+ page.status = status
+ page.reason = HTTPStatus(status).phrase
+ page.headers = CIMultiDictProxy(CIMultiDict({"content-type": "text/html"}))
+ page.url = "https://blog.example.com/mcp"
+ page.content = b"<!doctype html><title>Page not found</title><p>Sorry.</p>"
+ return page