Skip to content

fix(auth): silence authlib.jose DeprecationWarning at JWT import - #4100

Merged
jlowin merged 3 commits into
PrefectHQ:mainfrom
SarthakB11:fix/issue-4098
May 7, 2026
Merged

jlowin merged 3 commits into
PrefectHQ:mainfrom
SarthakB11:fix/issue-4098

Conversation

@SarthakB11

Copy link
Copy Markdown
Contributor

authlib.jose emits AuthlibDeprecationWarning at module import time, so any caller running with warnings.simplefilter("error") hits an exception just from touching FastMCP's JWT auth providers (from fastmcp.server.auth.providers.jwt import StaticTokenVerifier is enough to trigger it).

Wraps the two from authlib.jose ... imports in warnings.catch_warnings() filtering AuthlibDeprecationWarning. The filter snapshot is restored on context exit, so this stays scoped to the one import site and doesn't leak into user code. Migrating off authlib.jose to joserfc is a separate, larger change; this is the minimal patch for the warning.

from authlib.deprecate import AuthlibDeprecationWarning

with warnings.catch_warnings():
    warnings.simplefilter("ignore", AuthlibDeprecationWarning)
    from authlib.jose import JsonWebToken
    from authlib.jose.errors import JoseError

Closes #4098.

  • If I used an LLM, it followed the repo's contributing conventions (not generic output)

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. labels May 5, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4206177e41

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/fastmcp/server/auth/jwt_issuer.py Outdated

from authlib.jose import JsonWebToken
from authlib.jose.errors import JoseError
from authlib.deprecate import AuthlibDeprecationWarning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope the Authlib warning filter import

When this is the first authlib.deprecate import in a process, Authlib 1.7 executes warnings.simplefilter("always", AuthlibDeprecationWarning) while importing this line. Because that happens before catch_warnings(), the context snapshots the already-mutated filters and restores the global always filter on exit; a caller that had set warnings.simplefilter("error", AuthlibDeprecationWarning) can import this module, but subsequent Authlib deprecations are no longer errors. Move this import inside the catch_warnings() block before installing the ignore filter so Authlib's filter mutation is restored too.

Useful? React with 👍 / 👎.

import httpx
from authlib.jose import JsonWebKey, JsonWebToken
from authlib.jose.errors import JoseError
from authlib.deprecate import AuthlibDeprecationWarning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope the Authlib warning filter import

When this is the first authlib.deprecate import in a process, Authlib 1.7 executes warnings.simplefilter("always", AuthlibDeprecationWarning) while importing this line. Because that happens before catch_warnings(), the context snapshots the already-mutated filters and restores the global always filter on exit; a caller that had set warnings.simplefilter("error", AuthlibDeprecationWarning) can import this provider, but subsequent Authlib deprecations are no longer errors. Move this import inside the catch_warnings() block before installing the ignore filter so Authlib's filter mutation is restored too.

Useful? React with 👍 / 👎.

@SarthakB11

SarthakB11 commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Good catch, pushed b1c0eef. from authlib.deprecate import AuthlibDeprecationWarning is now inside the catch_warnings() block in both files, so authlib's simplefilter("always", ...) mutation gets reverted on exit and the caller's filter survives. Verified locally:

import warnings
warnings.filterwarnings('error', category=DeprecationWarning)
import fastmcp.server.auth.providers.jwt
import authlib.deprecate
authlib.deprecate.deprecate('test')   # still raises AuthlibDeprecationWarning ✓

@jlowin jlowin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@marvin-context-protocol

Copy link
Copy Markdown
Contributor

tl;dr: ruff check failed in the static_analysis job — it auto-fixed a missing blank line between import httpx and the with warnings.catch_warnings(): block in src/fastmcp/server/auth/providers/jwt.py. Run uv run prek run --all-files locally and push the result.

The fix is a one-line whitespace change (a blank line after import httpx on line 12, before the suppressed-warning with block). Prek would have applied it for you on commit if it were installed locally.

Log excerpt
ruff check...............................................................Failed
- hook id: ruff-check
  Found 1 error (1 fixed, 0 remaining).

diff --git a/src/fastmcp/server/auth/providers/jwt.py b/src/fastmcp/server/auth/providers/jwt.py
@@ -10,6 +10,7 @@ from dataclasses import dataclass
 from typing import Any, cast

 import httpx
+
 with warnings.catch_warnings():
     # authlib.jose emits AuthlibDeprecationWarning on import; suppress it so

Failed job

@jlowin
jlowin merged commit aaff924 into PrefectHQ:main May 7, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeprecationWarning from authlib.jose import in JWT auth provider

2 participants