Skip to content

Commit c7068b3

Browse files
committed
refactor append_path
1 parent 628b492 commit c7068b3

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/mcp/server/auth/routes.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import posixpath
21
import urllib.parse
32
from collections.abc import Awaitable, Callable
43
from typing import Any
@@ -167,23 +166,18 @@ def build_metadata(
167166
service_documentation_url: AnyHttpUrl | None,
168167
client_registration_options: ClientRegistrationOptions,
169168
revocation_options: RevocationOptions,
170-
) -> OAuthMetadata:
169+
) -> OAuthMetadata:
170+
def append_path(path: str, endpoint_path: str) -> str:
171+
# Ensures the path ends with a slash
172+
path = f"{path}/"
171173

172-
def append_path(issuer_url: str, endpoint_path: str) -> str:
173-
parsed = urllib.parse.urlparse(issuer_url)
174+
# Ensures the endpoint path does not start with a slash
175+
endpoint_path_lstrip = endpoint_path.lstrip("/")
174176

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
177+
# Join the two paths and remove leading slashes This ensures that the final
178+
# path doesn't have double slashes between the host and the endpoint
179+
return urllib.parse.urljoin(path, endpoint_path_lstrip).lstrip("/")
181180

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
187181

188182
authorization_url = modify_url_path(
189183
issuer_url, lambda path: append_path(path, AUTHORIZATION_PATH)

0 commit comments

Comments
 (0)