fix: FastMCP 2.12+ auth import compatibility

This commit is contained in:
saidsurucu
2026-01-15 00:59:54 +03:00
parent 8818a7809a
commit a5e6baeec8
+50 -32
View File
@@ -55,43 +55,61 @@ logger = logging.getLogger(__name__)
# Configure CORS and Auth middleware # Configure CORS and Auth middleware
cors_origins = os.getenv("ALLOWED_ORIGINS", "*").split(",") cors_origins = os.getenv("ALLOWED_ORIGINS", "*").split(",")
# Import FastMCP Bearer Auth Provider
from fastmcp.server.auth import BearerAuthProvider
from fastmcp.server.auth.providers.bearer import RSAKeyPair
# Import Clerk SDK at module level for performance
try:
from clerk_backend_api import Clerk
CLERK_SDK_AVAILABLE = True
except ImportError:
CLERK_SDK_AVAILABLE = False
logger.warning("Clerk SDK not available - falling back to development mode")
# Configure Bearer token authentication based on ENABLE_AUTH # Configure Bearer token authentication based on ENABLE_AUTH
auth_enabled = os.getenv("ENABLE_AUTH", "false").lower() == "true" auth_enabled = os.getenv("ENABLE_AUTH", "false").lower() == "true"
bearer_auth = None bearer_auth = None
if CLERK_SECRET_KEY and CLERK_ISSUER: # Only import and configure auth if enabled
# Production: Use Clerk JWKS endpoint for token validation if auth_enabled:
bearer_auth = BearerAuthProvider( # Import FastMCP JWT Verifier (handles both old and new FastMCP versions)
jwks_uri=f"{CLERK_ISSUER}/.well-known/jwks.json", try:
issuer=None, # FastMCP 2.12+ uses JWTVerifier
algorithm="RS256", from fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPair
audience=None, AuthProviderClass = JWTVerifier
required_scopes=[] except ImportError:
) try:
else: # Older FastMCP versions used BearerAuthProvider
# Development: Generate RSA key pair for testing from fastmcp.server.auth import BearerAuthProvider
dev_key_pair = RSAKeyPair.generate() from fastmcp.server.auth.providers.bearer import RSAKeyPair
bearer_auth = BearerAuthProvider( AuthProviderClass = BearerAuthProvider
public_key=dev_key_pair.public_key, except ImportError:
issuer="https://dev.yargimcp.com", logger.error("No compatible auth provider found in FastMCP")
audience="dev-mcp-server", AuthProviderClass = None
required_scopes=["yargi.read"] RSAKeyPair = None
)
# Create MCP app with Bearer authentication # Import Clerk SDK at module level for performance
mcp_server = create_app(auth=bearer_auth if auth_enabled else None) try:
from clerk_backend_api import Clerk
CLERK_SDK_AVAILABLE = True
except ImportError:
CLERK_SDK_AVAILABLE = False
logger.warning("Clerk SDK not available - falling back to development mode")
if AuthProviderClass:
if CLERK_SECRET_KEY and CLERK_ISSUER:
# Production: Use Clerk JWKS endpoint for token validation
bearer_auth = AuthProviderClass(
jwks_uri=f"{CLERK_ISSUER}/.well-known/jwks.json",
issuer=None,
algorithm="RS256",
audience=None,
required_scopes=[]
)
elif RSAKeyPair:
# Development: Generate RSA key pair for testing
dev_key_pair = RSAKeyPair.generate()
bearer_auth = AuthProviderClass(
public_key=dev_key_pair.public_key,
issuer="https://dev.yargimcp.com",
audience="dev-mcp-server",
required_scopes=["yargi.read"]
)
else:
CLERK_SDK_AVAILABLE = False
logger.info("Authentication disabled (ENABLE_AUTH=false)")
# Create MCP app with Bearer authentication (None if auth disabled)
mcp_server = create_app(auth=bearer_auth)
# Create MCP Starlette sub-application with root path - mount will add /mcp prefix # Create MCP Starlette sub-application with root path - mount will add /mcp prefix
mcp_app = mcp_server.http_app(path="/") mcp_app = mcp_server.http_app(path="/")