Skip to content

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

Reorganize auth and use secretstr

Jeremiah Lowin5ac996d13457
diff --git a/src/fastmcp/client/auth/__init__.py b/src/fastmcp/client/auth/__init__.py
new file mode 100644
index 000000000..6ec3ecf4b
--- /dev/null
+++ b/src/fastmcp/client/auth/__init__.py
@@ -0,0 +1,4 @@
+from .bearer import BearerAuth
+from .oauth import OAuth
+
+__all__ = ["BearerAuth", "OAuth"]
diff --git a/src/fastmcp/client/auth/bearer.py b/src/fastmcp/client/auth/bearer.py
new file mode 100644
index 000000000..0c38a11b0
--- /dev/null
+++ b/src/fastmcp/client/auth/bearer.py
@@ -0,0 +1,17 @@
+import httpx
+from pydantic import SecretStr
+
+from fastmcp.utilities.logging import get_logger
+
+__all__ = ["BearerAuth"]
+
+logger = get_logger(__name__)
+
+
+class BearerAuth(httpx.Auth):
+ def __init__(self, token: str):
+ self.token = SecretStr(token)
+
+ def auth_flow(self, request):
+ request.headers["Authorization"] = f"Bearer {self.token.get_secret_value()}"
+ yield request
diff --git a/src/fastmcp/client/auth.py b/src/fastmcp/client/auth/oauth.py
similarity index 98%
rename from src/fastmcp/client/auth.py
rename to src/fastmcp/client/auth/oauth.py
index 28c2bba84..43df9d442 100644
--- a/src/fastmcp/client/auth.py
+++ b/src/fastmcp/client/auth/oauth.py
@@ -392,12 +392,3 @@ def OAuth(
)
return oauth_provider
-
-
-class BearerAuth(httpx.Auth):
- def __init__(self, token: str):
- self.token = token
-
- def auth_flow(self, request):
- request.headers["Authorization"] = f"Bearer {self.token}"
- yield request
diff --git a/tests/auth/providers/test_bearer.py b/tests/auth/providers/test_bearer.py
index ff127f727..aed70af71 100644
--- a/tests/auth/providers/test_bearer.py
+++ b/tests/auth/providers/test_bearer.py
@@ -6,7 +6,7 @@ import pytest
from pytest_httpx import HTTPXMock
from fastmcp import Client, FastMCP
-from fastmcp.client.auth import BearerAuth
+from fastmcp.client.auth.bearer import BearerAuth
from fastmcp.server.auth.providers.bearer import (
BearerAuthProvider,
JWKData,