π The fast, Pythonic way to build MCP servers and clients.
fix(auth): silence authlib.jose DeprecationWarning at JWT import (#4100)
Sarthak Bhardwajaaff9243a3ae
diff --git a/src/fastmcp/server/auth/jwt_issuer.py b/src/fastmcp/server/auth/jwt_issuer.pyindex 78635b90c..15996efd4 100644--- a/src/fastmcp/server/auth/jwt_issuer.py+++ b/src/fastmcp/server/auth/jwt_issuer.py@@ -9,10 +9,23 @@ from __future__ import annotationsimport base64import time+import warningsfrom typing import Any, overload-from authlib.jose import JsonWebToken-from authlib.jose.errors import JoseError+with warnings.catch_warnings():+ # authlib.jose emits AuthlibDeprecationWarning on import; suppress it so+ # importing this module does not trigger the warning under+ # `warnings.simplefilter("error")`. The `authlib.deprecate` import lives+ # inside this block too: importing it for the first time runs+ # `warnings.simplefilter("always", AuthlibDeprecationWarning)` at module+ # scope, which would otherwise leak past `catch_warnings()` and clobber+ # the caller's filter for subsequent Authlib deprecations. See+ # jlowin/fastmcp#4098.+ from authlib.deprecate import AuthlibDeprecationWarning++ warnings.simplefilter("ignore", AuthlibDeprecationWarning)+ from authlib.jose import JsonWebToken+ from authlib.jose.errors import JoseErrorfrom cryptography.hazmat.primitives import hashesfrom cryptography.hazmat.primitives.kdf.hkdf import HKDFfrom cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMACdiff --git a/src/fastmcp/server/auth/providers/jwt.py b/src/fastmcp/server/auth/providers/jwt.pyindex 2417cd067..9b73e6ae9 100644--- a/src/fastmcp/server/auth/providers/jwt.py+++ b/src/fastmcp/server/auth/providers/jwt.py@@ -5,12 +5,26 @@ from __future__ import annotationsimport contextlibimport jsonimport time+import warningsfrom dataclasses import dataclassfrom typing import Any, castimport httpx-from authlib.jose import JsonWebKey, JsonWebToken-from authlib.jose.errors import JoseError++with warnings.catch_warnings():+ # authlib.jose emits AuthlibDeprecationWarning on import; suppress it so+ # importing this provider does not trigger the warning under+ # `warnings.simplefilter("error")`. The `authlib.deprecate` import lives+ # inside this block too: importing it for the first time runs+ # `warnings.simplefilter("always", AuthlibDeprecationWarning)` at module+ # scope, which would otherwise leak past `catch_warnings()` and clobber+ # the caller's filter for subsequent Authlib deprecations. See+ # jlowin/fastmcp#4098.+ from authlib.deprecate import AuthlibDeprecationWarning++ warnings.simplefilter("ignore", AuthlibDeprecationWarning)+ from authlib.jose import JsonWebKey, JsonWebToken+ from authlib.jose.errors import JoseErrorfrom cryptography.hazmat.primitives import serializationfrom cryptography.hazmat.primitives.asymmetric import rsafrom pydantic import AnyHttpUrl, SecretStr