Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/cortex-cli/src/agent_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#[cfg(test)]
mod tests {
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
use crate::agent_cmd::loader::{
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
};
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
use crate::agent_cmd::types::AgentMode;
use crate::utils::file::read_file_with_encoding;

#[test]
fn test_read_file_with_utf8() {
Expand Down
22 changes: 21 additions & 1 deletion src/cortex-cli/src/mcp_cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

use anyhow::{Context, Result};
use cortex_engine::config::find_cortex_home;
use std::path::Path;

/// Parse MCP config TOML with the file path included in parse errors.
pub(crate) fn parse_config_content(content: &str, config_path: &Path) -> Result<toml::Value> {
toml::from_str(content)
.with_context(|| format!("failed to parse config: {}", config_path.display()))
}

/// Load the config file as a toml::Value.
pub(crate) fn load_config() -> Result<Option<toml::Value>> {
Expand All @@ -19,7 +26,7 @@ pub(crate) fn load_config() -> Result<Option<toml::Value>> {
let content = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config: {}", config_path.display()))?;

let config: toml::Value = toml::from_str(&content).with_context(|| "failed to parse config")?;
let config = parse_config_content(&content, &config_path)?;

Ok(Some(config))
}
Expand Down Expand Up @@ -87,4 +94,17 @@ mod tests {
assert!(result.is_ok());
assert!(result.unwrap().is_none());
}

#[test]
fn test_parse_config_content_error_includes_path() {
let config_path = std::path::PathBuf::from("custom-cortex-home/config.toml");
let err = parse_config_content("invalid = [unclosed", &config_path)
.expect_err("invalid TOML should fail");
let message = err.to_string();

assert!(
message.contains("failed to parse config: custom-cortex-home/config.toml"),
"error should include config path, got: {message}"
);
}
}
17 changes: 6 additions & 11 deletions src/cortex-cli/src/mcp_cmd/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cortex_engine::config::find_cortex_home;
use std::io::{self, BufRead, Write};

use super::auth::{get_auth_status_for_display, remove_auth_silent};
use super::config::{get_mcp_server, get_mcp_servers};
use super::config::{get_mcp_server, get_mcp_servers, parse_config_content};
use super::types::{
AddArgs, AddMcpSseArgs, AddMcpStreamableHttpArgs, AddMcpTransportArgs, DisableArgs, EnableArgs,
GetArgs, ListArgs, RemoveArgs, RenameArgs,
Expand Down Expand Up @@ -301,8 +301,7 @@ pub(crate) async fn run_add(args: AddArgs) -> Result<()> {
if config_path.exists() {
let content = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config: {}", config_path.display()))?;
let mut config: toml::Value =
toml::from_str(&content).with_context(|| "failed to parse config")?;
let mut config = parse_config_content(&content, &config_path)?;

if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut())
{
Expand Down Expand Up @@ -576,8 +575,7 @@ pub(crate) async fn run_remove(args: RemoveArgs) -> Result<()> {
.with_context(|| format!("failed to read config: {}", config_path.display()))?;

// Parse the TOML and remove the server section
let mut config: toml::Value =
toml::from_str(&content).with_context(|| "failed to parse config")?;
let mut config = parse_config_content(&content, &config_path)?;

if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut()) {
mcp_servers.remove(&name);
Expand Down Expand Up @@ -622,8 +620,7 @@ pub(crate) async fn run_enable(args: EnableArgs) -> Result<()> {
let content = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config: {}", config_path.display()))?;

let mut config: toml::Value =
toml::from_str(&content).with_context(|| "failed to parse config")?;
let mut config = parse_config_content(&content, &config_path)?;

if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut())
&& let Some(server) = mcp_servers.get_mut(&args.name)
Expand Down Expand Up @@ -674,8 +671,7 @@ pub(crate) async fn run_disable(args: DisableArgs) -> Result<()> {
let content = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config: {}", config_path.display()))?;

let mut config: toml::Value =
toml::from_str(&content).with_context(|| "failed to parse config")?;
let mut config = parse_config_content(&content, &config_path)?;

if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut())
&& let Some(server) = mcp_servers.get_mut(&args.name)
Expand Down Expand Up @@ -740,8 +736,7 @@ pub(crate) async fn run_rename(args: RenameArgs) -> Result<()> {
let content = std::fs::read_to_string(&config_path)
.with_context(|| format!("failed to read config: {}", config_path.display()))?;

let mut config: toml::Value =
toml::from_str(&content).with_context(|| "failed to parse config")?;
let mut config = parse_config_content(&content, &config_path)?;

if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut())
&& let Some(server_config) = mcp_servers.remove(&args.old_name)
Expand Down