Skip to content

Commit 9efb608

Browse files
Merge pull request #41 from SecretiveShell/envsubst
add native envsubst
2 parents e0a5b94 + 7865e74 commit 9efb608

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

mcp_bridge/config/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from mcp_bridge.config.env_subst import substitute_env_vars
12
from mcp_bridge.config.initial import initial_settings
23
from mcp_bridge.config.final import Settings
34
from typing import Any, Callable
@@ -38,6 +39,8 @@
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)

mcp_bridge/config/env_subst.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

0 commit comments

Comments
 (0)