Skip to content

Commit d5763bf

Browse files
committed
chore: update config loading logic to use MC_PROJECT_ROOT for configuration path and improve error handling for missing config files in load_config function
1 parent 1aa74ad commit d5763bf

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/config.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,20 @@ impl Default for ReferenceConfig {
121121

122122
// Example function to load config (will be used in main.rs)
123123
pub fn load_config() -> Result<McpConfig> {
124-
// Support MCP_CONFIG_PATH env var for config file path
125-
let config_path_env = std::env::var("MCP_CONFIG_PATH").ok();
126-
let config_path = config_path_env.clone().unwrap_or_else(|| "mcp_config.toml".to_string());
127-
128-
if let Some(ref env_path) = config_path_env {
129-
if !std::path::Path::new(env_path).exists() {
130-
return Err(anyhow::anyhow!("Config file not found at MCP_CONFIG_PATH: {}", env_path));
131-
}
132-
log::info!("MCP_CONFIG_PATH is set: {}", env_path);
124+
// Always use MC_PROJECT_ROOT for config path
125+
let project_root = std::env::var("MC_PROJECT_ROOT")
126+
.map_err(|_| anyhow::anyhow!("MC_PROJECT_ROOT is not set. Please set MC_PROJECT_ROOT to your project root directory."))?;
127+
let project_root_path = std::path::Path::new(&project_root);
128+
if !project_root_path.exists() {
129+
return Err(anyhow::anyhow!("MC_PROJECT_ROOT does not exist: {}", project_root));
130+
}
131+
let config_path = project_root_path.join("mcp_config.toml");
132+
let config_file_exists = config_path.exists();
133+
if config_file_exists {
134+
log::info!("MC_PROJECT_ROOT is set: {}", project_root_path.display());
135+
log::info!("Loading config from: {}", config_path.display());
133136
} else {
134-
log::info!("MCP_CONFIG_PATH not set, falling back to default: {}", config_path);
137+
log::warn!("Config file not found at: {}. Using defaults.", config_path.display());
135138
}
136139
// Use ReferenceConfig::default() to get defaults including calculated paths
137140
let default_config = McpConfig {
@@ -140,13 +143,13 @@ pub fn load_config() -> Result<McpConfig> {
140143
setup: SetupConfig::default(),
141144
};
142145

143-
let figment = Figment::new()
146+
let mut figment = Figment::new()
144147
// Start with our programmatically defined defaults
145-
.merge(Serialized::defaults(default_config)) // Use our instance
146-
// Merge TOML file if it exists
147-
.merge(Toml::file(&config_path))
148-
// Merge environment variables prefixed with MCP_
149-
.merge(Env::prefixed("MCP_").split("__"));
148+
.merge(Serialized::defaults(default_config)); // Use our instance
149+
if config_file_exists {
150+
figment = figment.merge(Toml::file(&config_path));
151+
}
152+
figment = figment.merge(Env::prefixed("MCP_").split("__"));
150153

151154
let config: McpConfig = figment.extract().context("Failed to extract McpConfig")?;
152155
validate_config(&config)?;

src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,20 @@ impl MyHandler {
570570
use std::env;
571571
use std::fs;
572572
use std::process::Command;
573-
// 1. Check if current directory is empty
573+
// 1. Get MC_PROJECT_ROOT and move to it
574+
let project_root = std::env::var("MC_PROJECT_ROOT")
575+
.map_err(|_| McpError::internal_error("MC_PROJECT_ROOT is not set. Please set MC_PROJECT_ROOT to your project root directory.", None))?;
576+
let project_root_path = std::path::Path::new(&project_root);
577+
if !project_root_path.exists() {
578+
return Ok(CallToolResult::error(vec![Content::text(format!(
579+
"MC_PROJECT_ROOT does not exist: {}", project_root
580+
))]));
581+
}
582+
std::env::set_current_dir(&project_root_path)
583+
.map_err(|e| McpError::internal_error(format!("Failed to set current dir: {e}"), None))?;
584+
log::info!("Changed working directory to MC_PROJECT_ROOT: {:?}", project_root_path);
585+
// 2. Check if current directory is empty
586+
log::info!("Current working directory for mc_setup: {:?}", env::current_dir());
574587
let current_dir = env::current_dir().map_err(|e| {
575588
McpError::internal_error(format!("Failed to get current dir: {e}"), None)
576589
})?;
@@ -588,20 +601,20 @@ impl MyHandler {
588601
if force && !is_empty {
589602
log::warn!("[setup.force=true] Forcing setup: existing files in the directory may be overwritten.");
590603
}
591-
// 2. Run forge init . -t <repo>
604+
// 3. Run forge init . -t <repo>
592605
// Use local template cache if specified
593606
let template_arg = if let Ok(local_template) = std::env::var("MC_TEMPLATE_CACHE") {
594607
local_template
595608
} else {
596609
MC_TEMPLATE_REPO.to_string()
597610
};
598611

599-
// Use output() to capture stderr as well
600612
let mut cmd = Command::new("forge");
601613
cmd.args(["init", ".", "-t", &template_arg]);
602614
if force {
603615
cmd.arg("--no-git");
604616
}
617+
log::info!("Running forge init command: {:?}", cmd);
605618
let output_result = cmd.output();
606619

607620
match output_result {

0 commit comments

Comments
 (0)