Skip to content

Commit e145d4f

Browse files
committed
more robust solution
1 parent 521d3bc commit e145d4f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/mcp/server/auth/routes.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import posixpath
2+
import urllib.parse
23
from collections.abc import Awaitable, Callable
34
from typing import Any
45

@@ -167,13 +168,28 @@ def build_metadata(
167168
client_registration_options: ClientRegistrationOptions,
168169
revocation_options: RevocationOptions,
169170
) -> OAuthMetadata:
170-
def append_path(base: str, suffix: str) -> str:
171-
return posixpath.join(base.rstrip("/"), suffix.lstrip("/"))
171+
172+
def append_path(issuer_url: AnyHttpUrl, endpoint_path: str) -> str:
173+
parsed = urllib.parse.urlparse(str(issuer_url))
174+
175+
base_path = parsed.path.rstrip("/")
176+
endpoint_path = endpoint_path.lstrip("/")
177+
new_path = posixpath.join(base_path, endpoint_path)
178+
179+
if not new_path.startswith("/"):
180+
new_path = "/" + new_path
181+
182+
new_url = urllib.parse.urlunparse(parsed._replace(path=new_path))
183+
184+
if new_url.startswith("/"):
185+
new_url = new_url[1:]
186+
return new_url
172187

173188
authorization_url = modify_url_path(
174189
issuer_url, lambda path: append_path(path, AUTHORIZATION_PATH)
175190
)
176191
token_url = modify_url_path(issuer_url, lambda path: append_path(path, TOKEN_PATH))
192+
177193
# Create metadata
178194
metadata = OAuthMetadata(
179195
issuer=issuer_url,
@@ -206,4 +222,4 @@ def append_path(base: str, suffix: str) -> str:
206222
)
207223
metadata.revocation_endpoint_auth_methods_supported = ["client_secret_post"]
208224

209-
return metadata
225+
return metadata

0 commit comments

Comments
 (0)