77
77
# Get logger
78
78
logger = logging .getLogger (__name__ )
79
79
80
- def load_env_config () -> Dict [ str , Optional [ str ]] :
80
+ def get_auth_mode_from_args () -> bool :
81
81
"""
82
- Load configuration from .env file if available.
82
+ Parse command line arguments to determine authentication mode.
83
+ This is done before loading environment variables to choose the correct .env file.
84
+
85
+ Returns:
86
+ bool: True if using session cookie authentication, False for M2M authentication
87
+ """
88
+ parser = argparse .ArgumentParser (add_help = False )
89
+ parser .add_argument ('--use-session-cookie' , action = 'store_true' ,
90
+ help = 'Use session cookie authentication instead of M2M' )
91
+ args , _ = parser .parse_known_args ()
92
+ return args .use_session_cookie
93
+
94
+ def print_env_file_banner (env_file_name : str , use_session_cookie : bool , file_found : bool , file_path : str = None ):
95
+ """
96
+ Print a prominent banner showing which .env file is being used and why.
97
+
98
+ Args:
99
+ env_file_name: Name of the .env file being used
100
+ use_session_cookie: Whether session cookie authentication is being used
101
+ file_found: Whether the .env file was found
102
+ file_path: Full path to the .env file if found
103
+ """
104
+ print ("\n " + "=" * 80 )
105
+ print ("🔧 ENVIRONMENT CONFIGURATION" )
106
+ print ("=" * 80 )
107
+
108
+ auth_mode = "Session Cookie Authentication" if use_session_cookie else "M2M Authentication"
109
+ print (f"Authentication Mode: { auth_mode } " )
110
+ print (f"Expected .env file: { env_file_name } " )
111
+
112
+ if use_session_cookie :
113
+ print ("Reason: --use-session-cookie flag specified, using .env.user for user credentials" )
114
+ else :
115
+ print ("Reason: M2M authentication (default), using .env.agent for machine credentials" )
116
+
117
+ if file_found and file_path :
118
+ print (f"✅ Found and loaded: { file_path } " )
119
+ else :
120
+ print (f"⚠️ File not found: { env_file_name } " )
121
+ print (" Falling back to system environment variables" )
122
+
123
+ print ("=" * 80 + "\n " )
124
+
125
+ def load_env_config (use_session_cookie : bool ) -> Dict [str , Optional [str ]]:
126
+ """
127
+ Load configuration from .env file based on authentication mode.
128
+ Uses .env.user for session cookie auth, .env.agent for M2M auth.
129
+
130
+ Args:
131
+ use_session_cookie: True for session cookie auth (.env.user), False for M2M auth (.env.agent)
83
132
84
133
Returns:
85
134
Dict[str, Optional[str]]: Dictionary containing environment variables
@@ -92,29 +141,53 @@ def load_env_config() -> Dict[str, Optional[str]]:
92
141
'domain' : None
93
142
}
94
143
144
+ # Choose .env file based on authentication mode
145
+ env_file_name = '.env.user' if use_session_cookie else '.env.agent'
146
+
95
147
if DOTENV_AVAILABLE :
148
+ file_found = False
149
+ file_path = None
150
+
96
151
# Try to load from .env file in the current directory
97
- env_file = os .path .join (os .path .dirname (__file__ ), '.env' )
152
+ env_file = os .path .join (os .path .dirname (__file__ ), env_file_name )
98
153
if os .path .exists (env_file ):
99
154
load_dotenv (env_file )
155
+ file_found = True
156
+ file_path = env_file
100
157
logger .info (f"Loading environment variables from { env_file } " )
101
158
else :
102
159
# Try to load from .env file in the parent directory
103
- env_file = os .path .join (os .path .dirname (__file__ ), '..' , '.env' )
160
+ env_file = os .path .join (os .path .dirname (__file__ ), '..' , env_file_name )
104
161
if os .path .exists (env_file ):
105
162
load_dotenv (env_file )
163
+ file_found = True
164
+ file_path = env_file
106
165
logger .info (f"Loading environment variables from { env_file } " )
107
166
else :
108
167
# Try to load from current working directory
109
- load_dotenv ()
110
- logger .info ("Loading environment variables from current directory" )
168
+ env_file = os .path .join (os .getcwd (), env_file_name )
169
+ if os .path .exists (env_file ):
170
+ load_dotenv (env_file )
171
+ file_found = True
172
+ file_path = env_file
173
+ logger .info (f"Loading environment variables from { env_file } " )
174
+ else :
175
+ # Fallback to default .env loading
176
+ load_dotenv ()
177
+ logger .info ("Loading environment variables from default .env file" )
178
+
179
+ # Print banner showing which file is being used
180
+ print_env_file_banner (env_file_name , use_session_cookie , file_found , file_path )
111
181
112
182
# Get values from environment
113
183
env_config ['client_id' ] = os .getenv ('COGNITO_CLIENT_ID' )
114
184
env_config ['client_secret' ] = os .getenv ('COGNITO_CLIENT_SECRET' )
115
185
env_config ['region' ] = os .getenv ('AWS_REGION' )
116
186
env_config ['user_pool_id' ] = os .getenv ('COGNITO_USER_POOL_ID' )
117
187
env_config ['domain' ] = os .getenv ('COGNITO_DOMAIN' )
188
+ else :
189
+ # Print banner even when dotenv is not available
190
+ print_env_file_banner (env_file_name , use_session_cookie , False )
118
191
119
192
return env_config
120
193
@@ -126,8 +199,11 @@ def parse_arguments() -> argparse.Namespace:
126
199
Returns:
127
200
argparse.Namespace: The parsed command line arguments
128
201
"""
129
- # Load environment configuration first
130
- env_config = load_env_config ()
202
+ # First, determine authentication mode to choose correct .env file
203
+ use_session_cookie = get_auth_mode_from_args ()
204
+
205
+ # Load environment configuration using the appropriate .env file
206
+ env_config = load_env_config (use_session_cookie )
131
207
132
208
parser = argparse .ArgumentParser (description = 'LangGraph MCP Client with Cognito Authentication' )
133
209
@@ -145,7 +221,7 @@ def parse_arguments() -> argparse.Namespace:
145
221
146
222
# Authentication method arguments
147
223
parser .add_argument ('--use-session-cookie' , action = 'store_true' ,
148
- help = 'Use session cookie authentication instead of M2M' )
224
+ help = 'Use session cookie authentication instead of M2M (loads .env.user instead of .env.agent) ' )
149
225
parser .add_argument ('--session-cookie-file' , type = str , default = '~/.mcp/session_cookie' ,
150
226
help = 'Path to session cookie file (default: ~/.mcp/session_cookie)' )
151
227
0 commit comments