Skip to content

Commit f2a9b82

Browse files
committed
Include path to resource in oauth-protected-resource request
Signed-off-by: Sid Murching <sid.murching@databricks.com>
1 parent 3eeb0f2 commit f2a9b82

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/mcp/client/auth.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,16 @@ async def _discover_protected_resource(self, init_response: httpx.Response) -> h
233233
url = self._extract_resource_metadata_from_www_auth(init_response)
234234

235235
if not url:
236-
# Fallback to well-known discovery
237-
auth_base_url = self.context.get_authorization_base_url(self.context.server_url)
238-
url = urljoin(auth_base_url, "/.well-known/oauth-protected-resource")
236+
# Fallback to well-known discovery with path component included
237+
parsed = urlparse(self.context.server_url)
238+
auth_base_url = f"{parsed.scheme}://{parsed.netloc}"
239+
240+
if parsed.path and parsed.path != "/":
241+
# Include path component in the well-known URL
242+
path_component = parsed.path.rstrip("/")
243+
url = urljoin(auth_base_url, f"/.well-known/oauth-protected-resource{path_component}")
244+
else:
245+
url = urljoin(auth_base_url, "/.well-known/oauth-protected-resource")
239246

240247
return httpx.Request("GET", url, headers={MCP_PROTOCOL_VERSION: LATEST_PROTOCOL_VERSION})
241248

tests/client/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async def callback_handler() -> tuple[str, str | None]:
222222

223223
request = await provider._discover_protected_resource(init_response)
224224
assert request.method == "GET"
225-
assert str(request.url) == "https://api.example.com/.well-known/oauth-protected-resource"
225+
assert str(request.url) == "https://api.example.com/.well-known/oauth-protected-resource/api/2.0/mcp"
226226
assert "mcp-protocol-version" in request.headers
227227

228228
# Test with WWW-Authenticate header

0 commit comments

Comments
 (0)