1414from google .auth .exceptions import RefreshError
1515from googleapiclient .discovery import build
1616from googleapiclient .errors import HttpError
17- from auth .scopes import SCOPES , get_current_scopes # noqa
17+ from auth .scopes import SCOPES , get_current_scopes # noqa
1818from auth .oauth21_session_store import get_oauth21_session_store
1919from auth .credential_store import get_credential_store
2020from auth .oauth_config import get_oauth_config , is_stateless_mode
@@ -135,11 +135,15 @@ def save_credentials_to_session(session_id: str, credentials: Credentials):
135135 client_secret = credentials .client_secret ,
136136 scopes = credentials .scopes ,
137137 expiry = credentials .expiry ,
138- mcp_session_id = session_id
138+ mcp_session_id = session_id ,
139+ )
140+ logger .debug (
141+ f"Credentials saved to OAuth21SessionStore for session_id: { session_id } , user: { user_email } "
139142 )
140- logger .debug (f"Credentials saved to OAuth21SessionStore for session_id: { session_id } , user: { user_email } " )
141143 else :
142- logger .warning (f"Could not save credentials to session store - no user email found for session: { session_id } " )
144+ logger .warning (
145+ f"Could not save credentials to session store - no user email found for session: { session_id } "
146+ )
143147
144148
145149def load_credentials_from_session (session_id : str ) -> Optional [Credentials ]:
@@ -477,7 +481,7 @@ def handle_auth_callback(
477481 scopes = credentials .scopes ,
478482 expiry = credentials .expiry ,
479483 mcp_session_id = session_id ,
480- issuer = "https://accounts.google.com" # Add issuer for Google tokens
484+ issuer = "https://accounts.google.com" , # Add issuer for Google tokens
481485 )
482486
483487 # If session_id is provided, also save to session cache for compatibility
@@ -521,7 +525,9 @@ def get_credentials(
521525 # Try to get credentials by MCP session
522526 credentials = store .get_credentials_by_mcp_session (session_id )
523527 if credentials :
524- logger .info (f"[get_credentials] Found OAuth 2.1 credentials for MCP session { session_id } " )
528+ logger .info (
529+ f"[get_credentials] Found OAuth 2.1 credentials for MCP session { session_id } "
530+ )
525531
526532 # Check scopes
527533 if not all (scope in credentials .scopes for scope in required_scopes ):
@@ -537,7 +543,9 @@ def get_credentials(
537543 # Try to refresh
538544 try :
539545 credentials .refresh (Request ())
540- logger .info (f"[get_credentials] Refreshed OAuth 2.1 credentials for session { session_id } " )
546+ logger .info (
547+ f"[get_credentials] Refreshed OAuth 2.1 credentials for session { session_id } "
548+ )
541549 # Update stored credentials
542550 user_email = store .get_user_by_mcp_session (session_id )
543551 if user_email :
@@ -547,11 +555,13 @@ def get_credentials(
547555 refresh_token = credentials .refresh_token ,
548556 scopes = credentials .scopes ,
549557 expiry = credentials .expiry ,
550- mcp_session_id = session_id
558+ mcp_session_id = session_id ,
551559 )
552560 return credentials
553561 except Exception as e :
554- logger .error (f"[get_credentials] Failed to refresh OAuth 2.1 credentials: { e } " )
562+ logger .error (
563+ f"[get_credentials] Failed to refresh OAuth 2.1 credentials: { e } "
564+ )
555565 return None
556566 except ImportError :
557567 pass # OAuth 2.1 store not available
@@ -672,7 +682,9 @@ def get_credentials(
672682 credential_store = get_credential_store ()
673683 credential_store .store_credential (user_google_email , credentials )
674684 else :
675- logger .info (f"Skipping credential file save in stateless mode for { user_google_email } " )
685+ logger .info (
686+ f"Skipping credential file save in stateless mode for { user_google_email } "
687+ )
676688
677689 # Also update OAuth21SessionStore
678690 store = get_oauth21_session_store ()
@@ -686,7 +698,7 @@ def get_credentials(
686698 scopes = credentials .scopes ,
687699 expiry = credentials .expiry ,
688700 mcp_session_id = session_id ,
689- issuer = "https://accounts.google.com" # Add issuer for Google tokens
701+ issuer = "https://accounts.google.com" , # Add issuer for Google tokens
690702 )
691703
692704 if session_id : # Update session cache if it was the source or is active
@@ -775,9 +787,13 @@ async def get_authenticated_google_service(
775787 # First try context variable (works in async context)
776788 session_id = get_fastmcp_session_id ()
777789 if session_id :
778- logger .debug (f"[{ tool_name } ] Got FastMCP session ID from context: { session_id } " )
790+ logger .debug (
791+ f"[{ tool_name } ] Got FastMCP session ID from context: { session_id } "
792+ )
779793 else :
780- logger .debug (f"[{ tool_name } ] Context variable returned None/empty session ID" )
794+ logger .debug (
795+ f"[{ tool_name } ] Context variable returned None/empty session ID"
796+ )
781797 except Exception as e :
782798 logger .debug (
783799 f"[{ tool_name } ] Could not get FastMCP session from context: { e } "
@@ -787,17 +803,25 @@ async def get_authenticated_google_service(
787803 if not session_id and get_fastmcp_context :
788804 try :
789805 fastmcp_ctx = get_fastmcp_context ()
790- if fastmcp_ctx and hasattr (fastmcp_ctx , ' session_id' ):
806+ if fastmcp_ctx and hasattr (fastmcp_ctx , " session_id" ):
791807 session_id = fastmcp_ctx .session_id
792- logger .debug (f"[{ tool_name } ] Got FastMCP session ID directly: { session_id } " )
808+ logger .debug (
809+ f"[{ tool_name } ] Got FastMCP session ID directly: { session_id } "
810+ )
793811 else :
794- logger .debug (f"[{ tool_name } ] FastMCP context exists but no session_id attribute" )
812+ logger .debug (
813+ f"[{ tool_name } ] FastMCP context exists but no session_id attribute"
814+ )
795815 except Exception as e :
796- logger .debug (f"[{ tool_name } ] Could not get FastMCP context directly: { e } " )
816+ logger .debug (
817+ f"[{ tool_name } ] Could not get FastMCP context directly: { e } "
818+ )
797819
798820 # Final fallback: log if we still don't have session_id
799821 if not session_id :
800- logger .warning (f"[{ tool_name } ] Unable to obtain FastMCP session ID from any source" )
822+ logger .warning (
823+ f"[{ tool_name } ] Unable to obtain FastMCP session ID from any source"
824+ )
801825
802826 logger .info (
803827 f"[{ tool_name } ] Attempting to get authenticated { service_name } service. Email: '{ user_google_email } ', Session: '{ session_id } '"
@@ -818,8 +842,12 @@ async def get_authenticated_google_service(
818842 )
819843
820844 if not credentials or not credentials .valid :
821- logger .warning (f"[{ tool_name } ] No valid credentials. Email: '{ user_google_email } '." )
822- logger .info (f"[{ tool_name } ] Valid email '{ user_google_email } ' provided, initiating auth flow." )
845+ logger .warning (
846+ f"[{ tool_name } ] No valid credentials. Email: '{ user_google_email } '."
847+ )
848+ logger .info (
849+ f"[{ tool_name } ] Valid email '{ user_google_email } ' provided, initiating auth flow."
850+ )
823851
824852 # Ensure OAuth callback is available
825853 from auth .oauth_callback_server import ensure_oauth_callback_available
0 commit comments