MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / _load_credentials

Function _load_credentials

src/codegraphcontext/cli/main.py:294–527  ·  view source on GitHub ↗

Loads configuration and credentials from various sources into environment variables. Uses per-variable precedence - each variable is loaded from the highest priority source. Priority order (highest to lowest): 1. Runtime environment variables (shell/CI) 2. Local `.codegraphconte

(cli_context_flag: Optional[str] = None)

Source from the content-addressed store, hash-verified

292# ============================================================================
293
294def _load_credentials(cli_context_flag: Optional[str] = None):
295 """
296 Loads configuration and credentials from various sources into environment variables.
297 Uses per-variable precedence - each variable is loaded from the highest priority source.
298 Priority order (highest to lowest):
299 1. Runtime environment variables (shell/CI)
300 2. Local `.codegraphcontext/.env` and `.env` in the current project directory (per-repo mode only)
301 3. Global `~/.codegraphcontext/.env` (user defaults, including `cgc config set`)
302 4. Local `mcp.json` env vars (project defaults)
303
304 Duplicate loading is skipped when the local file resolves to the same path as the global file.
305 Arbitrary parent directory `.env` files are not loaded—ensuring isolation.
306 """
307 from dotenv import dotenv_values
308 from codegraphcontext.cli.config_manager import (
309 ensure_config_dir,
310 codegraphcontext_dotenv_at_cwd,
311 normalize_config_path,
312 )
313
314 # Ensure config directory exists (lazy initialization)
315 ensure_config_dir()
316
317 # Snapshot runtime environment BEFORE merging config files.
318 # These values must remain highest priority.
319 runtime_env = dict(os.environ)
320
321 # Collect all config sources in precedence order (lowest to highest)
322 config_sources = []
323 config_source_names = []
324 key_source_map = {}
325 key_defined_in = {}
326
327 def _append_source(source_name: str, source_values: dict):
328 if not source_values:
329 return
330 config_sources.append(source_values)
331 config_source_names.append(source_name)
332 for k, v in source_values.items():
333 if v is None:
334 continue
335 key_source_map[k] = source_name
336 key_defined_in.setdefault(k, []).append(source_name)
337
338 # 4. Local mcp.json (lowest priority - project defaults)
339 mcp_file_path = Path.cwd() / "mcp.json"
340 if mcp_file_path.exists():
341 try:
342 with open(mcp_file_path, "r", encoding="utf-8", errors="replace") as f:
343 mcp_config = json.load(f)
344 server_env = mcp_config.get("mcpServers", {}).get("CodeGraphContext", {}).get("env", {})
345 if isinstance(server_env, dict):
346 normalized_env = {}
347 for env_key, env_value in server_env.items():
348 if env_value is not None and "PATH" in env_key:
349 normalized_env[env_key] = normalize_config_path(str(env_value), absolute=True)
350 else:
351 normalized_env[env_key] = env_value

Calls 15

ensure_config_dirFunction · 0.90
normalize_config_pathFunction · 0.90
resolve_contextFunction · 0.90
get_database_managerFunction · 0.90
_is_falkordb_availableFunction · 0.90
_append_sourceFunction · 0.85
itemsMethod · 0.80
printMethod · 0.80
getMethod · 0.65
updateMethod · 0.65
appendMethod · 0.65