File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ from mcp_bridge .config .env_subst import substitute_env_vars
12from mcp_bridge .config .initial import initial_settings
23from mcp_bridge .config .final import Settings
34from typing import Any , Callable
3839 for cfg in configs :
3940 always_merger .merge (result , cfg )
4041
42+ result = substitute_env_vars (result )
43+
4144 # build the config
4245 try :
4346 config = Settings (** result )
Original file line number Diff line number Diff line change 1+ from string import Template
2+ from typing import Any
3+ import os
4+
5+ from loguru import logger
6+
7+
8+ def substitute_env_vars (config : Any , env : dict [str , str ] | None = None ) -> Any :
9+ """Substitute environment variables in a configuration object."""
10+
11+ # copy the environment if it is not provided
12+ if env is None :
13+ env = os .environ .copy ()
14+
15+ assert env is not None , "env is None" # the guard should have caught this
16+
17+ # handle strings
18+ if isinstance (config , str ):
19+ return Template (config ).safe_substitute (env )
20+
21+ # handle other types
22+ elif isinstance (config , dict ):
23+ return {
24+ k : substitute_env_vars (v , env ) for k , v in config .items () if v is not None
25+ }
26+
27+ # handle lists
28+ elif isinstance (config , list ):
29+ return [substitute_env_vars (v , env ) for v in config ]
30+
31+ return config
You can’t perform that action at this time.
0 commit comments