Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Register browser OAuth clients as public clients (#5231)

Goran93ad0c963394
diff --git a/fastmcp_slim/fastmcp/client/auth/oauth.py b/fastmcp_slim/fastmcp/client/auth/oauth.py
index 442e6ad33..81391a9c1 100644
--- a/fastmcp_slim/fastmcp/client/auth/oauth.py
+++ b/fastmcp_slim/fastmcp/client/auth/oauth.py
@@ -293,13 +293,22 @@ class OAuth(OAuthClientProvider):
else:
scopes_str = ""
+ # No pre-registered secret: tell the server this is a public client.
+ extra = dict(self._additional_client_metadata or {})
+ if (
+ not self._client_id
+ and not self._client_secret
+ and "token_endpoint_auth_method" not in extra
+ ):
+ extra["token_endpoint_auth_method"] = "none"
+
client_metadata = OAuthClientMetadata(
client_name=self._client_name,
redirect_uris=[AnyHttpUrl(redirect_uri)],
grant_types=["authorization_code", "refresh_token"],
response_types=["code"],
scope=scopes_str,
- **(self._additional_client_metadata or {}),
+ **extra,
)
if self._client_id:
diff --git a/tests/client/auth/test_oauth_static_client.py b/tests/client/auth/test_oauth_static_client.py
index 9682c6d0a..c28c910dc 100644
--- a/tests/client/auth/test_oauth_static_client.py
+++ b/tests/client/auth/test_oauth_static_client.py
@@ -66,6 +66,33 @@ class TestStaticClientInfoConstruction:
oauth = OAuth(mcp_url="https://example.com/mcp")
assert oauth._static_client_info is None
+ def test_dynamic_registration_is_a_public_client(self):
+ """Browser login has no secret, so registration must say so."""
+ oauth = OAuth(mcp_url="https://example.com/mcp")
+ body = oauth.context.client_metadata.model_dump(mode="json", exclude_none=True)
+ assert body["token_endpoint_auth_method"] == "none"
+
+ def test_dynamic_registration_keeps_an_explicit_auth_method(self):
+ """A caller who sets the method is left alone."""
+ oauth = OAuth(
+ mcp_url="https://example.com/mcp",
+ additional_client_metadata={
+ "token_endpoint_auth_method": "client_secret_post"
+ },
+ )
+ assert (
+ oauth.context.client_metadata.token_endpoint_auth_method
+ == "client_secret_post"
+ )
+
+ def test_auth_oauth_string_registers_as_a_public_client(self):
+ """Client(..., auth='oauth') uses the same public registration."""
+ transport = StreamableHttpTransport("https://example.com/mcp", auth="oauth")
+ assert isinstance(transport.auth, OAuth)
+ assert (
+ transport.auth.context.client_metadata.token_endpoint_auth_method == "none"
+ )
+
def test_static_client_info_includes_additional_metadata(self):
"""Additional client metadata should be included in static client info."""
oauth = OAuth(