Skip to content

Commit 689c8d2

Browse files
digiogithubtuananh
authored andcommitted
feat: Add environment variable resolution in plugin configuration, now you can send env_vars in config file using the value ${ENV-VAR-KEY}, instead a fixed value in a text file, the value is replaced by the system env variable if exists. More flexibleand standard use for passing env variables into mcp.json
1 parent a423ba7 commit 689c8d2

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/plugins.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ fn parse_namespaced_tool_name(
5555
Err(ToolNameParseError)
5656
}
5757

58+
/// Check if a value contains an environment variable reference in the format ${ENVVARKEY}
59+
/// and replace it with the actual environment variable value if it exists.
60+
/// If the environment variable doesn't exist, returns the original value.
61+
fn check_env_reference(value: &str) -> String {
62+
// Check if the value matches the pattern ${ENVVARKEY}
63+
if let Some(stripped) = value.strip_prefix("${").and_then(|s| s.strip_suffix("}")) {
64+
// Try to get the environment variable
65+
match std::env::var(stripped) {
66+
Ok(env_value) => {
67+
tracing::debug!(
68+
"Resolved environment variable reference ${{{stripped}}} to actual value"
69+
);
70+
env_value
71+
}
72+
Err(_) => {
73+
tracing::warn!(
74+
"Environment variable {stripped} not found, keeping original value {value}"
75+
);
76+
value.to_string()
77+
}
78+
}
79+
} else {
80+
value.to_string()
81+
}
82+
}
83+
5884
#[derive(Clone)]
5985
pub struct PluginService {
6086
config: Config,
@@ -206,7 +232,8 @@ impl PluginService {
206232
// Add plugin configurations if present
207233
if let Some(env_vars) = &runtime_cfg.env_vars {
208234
for (key, value) in env_vars {
209-
manifest = manifest.with_config_key(key, value);
235+
let resolved_value = check_env_reference(value);
236+
manifest = manifest.with_config_key(key, &resolved_value);
210237
}
211238
}
212239

0 commit comments

Comments
 (0)