fix: discover MCP Protected Resource Metadata when server does not return 401 (#25980)

get_protected_resource_metadata() only attempted RFC 9728 discovery when
the anonymous `initialize` probe returned 401 with a WWW-Authenticate
header. Some remote MCP servers — notably Google's Gmail/Drive/Calendar
MCPs (gmailmcp.googleapis.com, etc.) — answer 200 to an anonymous
initialize, so OAuth scope and authorization-server discovery silently
failed and connections to them could not be established.

Run the discovery regardless of the probe's HTTP status: prefer the
resource_metadata URL from WWW-Authenticate when present, and otherwise
fall back to the RFC 9728 §4.2 well-known URIs. The trade-off is a couple
of extra well-known GETs during MCP connection setup for servers that
expose no PRM document; behavior for 401-responding servers is unchanged.
This commit is contained in:
Alberto de la Cruz authored and GitHub committed 2026-06-29 04:34:36 -05:00
1 parent f8ec63203c
commit 718b226177
1 file changed
+5 -1
+5 -1
View File
@@ -372,7 +372,11 @@ async def get_protected_resource_metadata(server_url: str) -> ProtectedResourceM
headers={'Content-Type': 'application/json'}, headers={'Content-Type': 'application/json'},
ssl=AIOHTTP_CLIENT_SESSION_SSL, ssl=AIOHTTP_CLIENT_SESSION_SSL,
) as response: ) as response:
if response.status == 401: # Discover Protected Resource Metadata regardless of HTTP status.
# A 401 carries a WWW-Authenticate header pointing at the PRM, but
# some MCP servers (e.g. Google's gmail/drive/calendar remote MCPs)
# answer 200 to an anonymous `initialize`, so we must still fall
# back to the RFC 9728 well-known URIs when there is no 401/header.
resource_metadata_urls = [] resource_metadata_urls = []
match = re.search( match = re.search(
r'resource_metadata=(?:"([^"]+)"|([^\s,]+))', r'resource_metadata=(?:"([^"]+)"|([^\s,]+))',