Jeremiah Lowin commited on
Commit
ccc6631
·
1 Parent(s): b310c16

Clean up settings

Browse files
src/fastmcp/server/auth/providers/bearer_env.py CHANGED
@@ -1,15 +1,10 @@
1
- from enum import Enum
2
-
3
  from pydantic_settings import BaseSettings, SettingsConfigDict
4
 
5
  from fastmcp.server.auth.providers.bearer import BearerAuthProvider
6
 
7
 
8
- class NotSet(Enum):
9
- sentinel = 0
10
-
11
-
12
- NOTSET = NotSet.sentinel
13
 
14
 
15
  class EnvBearerAuthProviderSettings(BaseSettings):
@@ -35,11 +30,11 @@ class EnvBearerAuthProvider(BearerAuthProvider):
35
 
36
  def __init__(
37
  self,
38
- public_key: str | None | NotSet = NOTSET,
39
- jwks_uri: str | None | NotSet = NOTSET,
40
- issuer: str | None | NotSet = NOTSET,
41
- audience: str | None | NotSet = NOTSET,
42
- required_scopes: list[str] | None | NotSet = NOTSET,
43
  ):
44
  kwargs = {
45
  "public_key": public_key,
@@ -49,6 +44,6 @@ class EnvBearerAuthProvider(BearerAuthProvider):
49
  "required_scopes": required_scopes,
50
  }
51
  settings = EnvBearerAuthProviderSettings(
52
- **{k: v for k, v in kwargs.items() if v is not NOTSET}
53
  )
54
  super().__init__(**settings.model_dump())
 
 
 
1
  from pydantic_settings import BaseSettings, SettingsConfigDict
2
 
3
  from fastmcp.server.auth.providers.bearer import BearerAuthProvider
4
 
5
 
6
+ class NotSet:
7
+ pass
 
 
 
8
 
9
 
10
  class EnvBearerAuthProviderSettings(BaseSettings):
 
30
 
31
  def __init__(
32
  self,
33
+ public_key: str | None | type[NotSet] = NotSet,
34
+ jwks_uri: str | None | type[NotSet] = NotSet,
35
+ issuer: str | None | type[NotSet] = NotSet,
36
+ audience: str | None | type[NotSet] = NotSet,
37
+ required_scopes: list[str] | None | type[NotSet] = NotSet,
38
  ):
39
  kwargs = {
40
  "public_key": public_key,
 
44
  "required_scopes": required_scopes,
45
  }
46
  settings = EnvBearerAuthProviderSettings(
47
+ **{k: v for k, v in kwargs.items() if v is not NotSet}
48
  )
49
  super().__init__(**settings.model_dump())
src/fastmcp/server/server.py CHANGED
@@ -187,7 +187,7 @@ class FastMCP(Generic[LifespanResultT]):
187
  lifespan=_lifespan_wrapper(self, lifespan),
188
  )
189
 
190
- if auth is None and self.settings.auth_provider == "bearer_env":
191
  auth = EnvBearerAuthProvider()
192
  self.auth = auth
193
 
 
187
  lifespan=_lifespan_wrapper(self, lifespan),
188
  )
189
 
190
+ if auth is None and self.settings.default_auth_provider == "bearer_env":
191
  auth = EnvBearerAuthProvider()
192
  self.auth = auth
193
 
src/fastmcp/settings.py CHANGED
@@ -180,7 +180,23 @@ class ServerSettings(BaseSettings):
180
  )
181
 
182
  # Auth settings
183
- auth_provider: Literal["bearer_env"] | None = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
 
186
  settings = Settings()
 
180
  )
181
 
182
  # Auth settings
183
+ default_auth_provider: Annotated[
184
+ Literal["bearer_env"] | None,
185
+ Field(
186
+ description=inspect.cleandoc(
187
+ """
188
+ Configure the authentication provider. This setting is intended only to
189
+ be used for remote confirugation of providers that fully support
190
+ environment variable configuration.
191
+
192
+ If None, no automatic configuration will take place.
193
+
194
+ This setting is *always* overriden by any auth provider passed to the
195
+ FastMCP constructor.
196
+ """
197
+ ),
198
+ ),
199
+ ] = None
200
 
201
 
202
  settings = Settings()