Skip to content

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

Resolve deferred defaults by alias and keep unverified GitHub scopes out of the cache (#6417)

nate nowackb69b51fc7f6b
diff --git a/fastmcp_slim/fastmcp/server/auth/providers/github.py b/fastmcp_slim/fastmcp/server/auth/providers/github.py
index 5a6b427db..fa2cb169c 100644
--- a/fastmcp_slim/fastmcp/server/auth/providers/github.py
+++ b/fastmcp_slim/fastmcp/server/auth/providers/github.py
@@ -268,7 +268,10 @@ class GitHubTokenVerifier(TokenVerifier):
"github_user_data": user_data,
},
)
- self._cache.set(token, result)
+ # A token verified while scope discovery was down carries no
+ # scopes; caching it would keep it scopeless after recovery.
+ if not scope_discovery_unavailable:
+ self._cache.set(token, result)
return result
except TokenVerificationError:
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.py
index 3a5220cfc..e981ef024 100644
--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py
+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py
@@ -455,6 +455,13 @@ def _resolve_recursive_types(conversion: _Conversion) -> None:
rechecked: set[type[BaseModel]] = set()
for cls, field_name in conversion.unchecked_defaults:
if issubclass(cls, BaseModel):
+ # safe_create_model stores a reserved property name under a
+ # generated field name with the property name as its alias.
+ field_name = next(
+ name
+ for name, info in cls.model_fields.items()
+ if (info.alias or name) == field_name
+ )
field_info = cls.model_fields[field_name]
# `annotation` alone drops constraints such as `minimum`
annotation = field_info.rebuild_annotation()
diff --git a/tests/server/auth/providers/test_github.py b/tests/server/auth/providers/test_github.py
index 2d5734a3f..ab9b71188 100644
--- a/tests/server/auth/providers/test_github.py
+++ b/tests/server/auth/providers/test_github.py
@@ -305,7 +305,12 @@ class TestGitHubTokenVerifierCaching:
) as mock_cls:
mock_cls.return_value.__aenter__.return_value = mock_client
- mock_client.get.side_effect = [user_response, scopes_response]
+ mock_client.get.side_effect = [
+ user_response,
+ scopes_response,
+ user_response,
+ scopes_response,
+ ]
first = await verifier.verify_token("tok-1")
second = await verifier.verify_token("tok-1")
@@ -314,8 +319,8 @@ class TestGitHubTokenVerifierCaching:
assert second is not None
assert second.scopes == []
assert second.client_id == first.client_id
- assert second is not first
- assert mock_client.get.call_count == 2
+ # The scopeless result is not cached, so the next call re-checks.
+ assert mock_client.get.call_count == 4
def test_provider_passes_cache_params(self, memory_storage: MemoryStore):
provider = GitHubProvider(
diff --git a/tests/server/auth/providers/test_github_transient_failures.py b/tests/server/auth/providers/test_github_transient_failures.py
index d59973e05..c7a41ecfc 100644
--- a/tests/server/auth/providers/test_github_transient_failures.py
+++ b/tests/server/auth/providers/test_github_transient_failures.py
@@ -235,6 +235,28 @@ async def test_scope_server_failure_does_not_grant_required_user_scope_and_recov
assert client.get.call_count == 4
[email protected]("status_code", [500, 503])
+async def test_unverified_scopes_are_not_cached_without_required_scopes(status_code):
+ client = AsyncMock()
+ client.get.side_effect = [
+ _github_user_response(),
+ _response(status_code),
+ _github_user_response(),
+ _response(200, headers={"x-oauth-scopes": "repo, user"}),
+ ]
+ verifier = GitHubTokenVerifier(cache_ttl_seconds=300, http_client=client)
+
+ degraded = await verifier.verify_token("still-valid-token")
+ assert degraded is not None
+ assert degraded.scopes == []
+ assert len(verifier._cache._entries) == 0
+
+ recovered = await verifier.verify_token("still-valid-token")
+ assert recovered is not None
+ assert recovered.scopes == ["repo", "user"]
+ assert client.get.call_count == 4
+
+
async def test_scope_transport_failure_does_not_grant_user_scope_and_recovers():
client = AsyncMock()
client.get.side_effect = [
diff --git a/tests/utilities/json_schema_type/test_recursive_hydration.py b/tests/utilities/json_schema_type/test_recursive_hydration.py
index e5609ff71..c84449499 100644
--- a/tests/utilities/json_schema_type/test_recursive_hydration.py
+++ b/tests/utilities/json_schema_type/test_recursive_hydration.py
@@ -135,6 +135,31 @@ def test_recursive_default_hydrates_after_resolution() -> None:
assert result.node.child is None
+def test_recursive_default_on_a_reserved_property_name() -> None:
+ generated = json_schema_type.json_schema_to_type(
+ {
+ "type": "object",
+ "properties": {"node": {"$ref": "#/$defs/Node"}},
+ "$defs": {
+ "Node": {
+ "type": "object",
+ "additionalProperties": True,
+ "properties": {
+ "name": {"type": "string"},
+ "_children": {
+ "type": "array",
+ "items": {"$ref": "#/$defs/Node"},
+ "default": [],
+ },
+ },
+ }
+ },
+ }
+ )
+ result = TypeAdapter[Any](generated).validate_python({"node": {"name": "x"}})
+ assert result.node.field_children == []
+
+
class HydrationPerson(BaseModel):
name: str
pet: HydrationPet | None = None