Skip to content

The agent that grows with you

fix(mcp-oauth): classify discovery URLs by path

Mathias Huque86aa9dec9ae7
diff --git a/tests/tools/test_mcp_oauth_issuer_origin.py b/tests/tools/test_mcp_oauth_issuer_origin.py
index e34d69f2453..64703bb5df8 100644
--- a/tests/tools/test_mcp_oauth_issuer_origin.py
+++ b/tests/tools/test_mcp_oauth_issuer_origin.py
@@ -24,12 +24,19 @@ PATH_DOC = "/.well-known/oauth-authorization-server/mcp-issuer"
@pytest.mark.asyncio
-async def test_origin_issued_metadata_shim_does_not_read_mcp_resource_response():
+ pytest.param(RESOURCE, id="mcp-resource"),
+ pytest.param(f"{AS_ORIGIN}/proxy/.well-known/oauth-authorization-server", id="unrelated-prefix"),
+ pytest.param(f"{AS_ORIGIN}/.well-known/oauth-authorization-server-extra", id="suffix-lookalike"),
+ pytest.param(f"{RESOURCE}?next=/.well-known/oauth-authorization-server", id="query"),
+ pytest.param(f"{RESOURCE}#/.well-known/openid-configuration", id="fragment"),
+])
+async def test_origin_issued_metadata_shim_does_not_read_non_discovery_response(url):
from tools.mcp_oauth_provider import HermesProviderMixin
class ResourceResponse:
status_code = 200
- request = SimpleNamespace(url=RESOURCE)
+ request = SimpleNamespace(url=url)
async def aread(self):
raise AssertionError("normal MCP resource response must not be consumed as OAuth metadata")
diff --git a/tools/mcp_oauth_provider.py b/tools/mcp_oauth_provider.py
index 18f62a1d416..063feb4b579 100644
--- a/tools/mcp_oauth_provider.py
+++ b/tools/mcp_oauth_provider.py
@@ -11,6 +11,7 @@ from __future__ import annotations
import logging
import re
from typing import TYPE_CHECKING, Any
+from urllib.parse import urlsplit
if TYPE_CHECKING:
from tools.mcp_oauth import HermesTokenStorage
@@ -170,7 +171,9 @@ class HermesProviderMixin:
# and response.aread() would wait for that stream to end while holding
# the OAuth state semaphore.
req = getattr(response, "request", None)
- if req is None or not any(path in str(req.url) for path in _ASM_DISCOVERY_PATHS):
+ request_path = urlsplit(str(req.url)).path if req is not None else ""
+ if not any(request_path == base or request_path.startswith(f"{base}/")
+ for base in _ASM_DISCOVERY_PATHS):
return response
from mcp.shared.auth import OAuthMetadata