Skip to content

Commit f075802

Browse files
committed
update fetch config
1 parent 73e5026 commit f075802

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

crates/q_chat/src/mcp.rs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use eyre::{
77
bail,
88
};
99
use fig_os_shim::Context;
10+
use fig_util::directories::chat_profiles_dir;
11+
use tokio::fs;
1012
use tracing::warn;
1113

1214
use crate::cli::{
@@ -154,10 +156,10 @@ pub async fn import_mcp_server(ctx: &Context, args: McpImport) -> Result<()> {
154156
let src_cfg: McpServerConfig = serde_json::from_str(&ctx.fs().read_to_string(&src_path).await?)?;
155157
let mut dst_cfg: McpServerConfig = McpServerConfig::load_from_file(ctx, &config_path).await?;
156158

157-
let before = dst_cfg.mcp_servers.len();
159+
let mut added = 0;
158160
for (name, cfg) in src_cfg.mcp_servers {
159161
let exists = dst_cfg.mcp_servers.contains_key(&name);
160-
if exists && !args.force {
162+
if exists && !args.force {
161163
bail!(
162164
"MCP server '{}' already exists in {} (scope {}). Use --force to overwrite.",
163165
name,
@@ -166,9 +168,9 @@ pub async fn import_mcp_server(ctx: &Context, args: McpImport) -> Result<()> {
166168
);
167169
}
168170
dst_cfg.mcp_servers.insert(name.clone(), cfg);
171+
added +=1;
169172
}
170173

171-
let added = dst_cfg.mcp_servers.len() - before;
172174
dst_cfg.save_to_file(ctx, &config_path).await?;
173175

174176
println!(
@@ -180,50 +182,78 @@ pub async fn import_mcp_server(ctx: &Context, args: McpImport) -> Result<()> {
180182

181183
pub async fn get_mcp_server_status(ctx: &Context, name: String) -> Result<()> {
182184
let configs = get_mcp_server_configs(ctx, None, None).await?;
185+
let mut found = false;
183186

184-
for (_, _, _, cfg_opt) in configs {
187+
for (sc, prof, path, cfg_opt) in configs {
185188
if let Some(cfg) = cfg_opt {
186189
if let Some(tool_cfg) = cfg.mcp_servers.get(&name) {
187-
println!("MCP Server: {name}");
188-
println!("Command : {}", tool_cfg.command);
189-
println!("Timeout : {} ms", tool_cfg.timeout);
190+
found = true;
191+
println!("─────────────\n");
192+
match sc {
193+
Scope::Workspace => println!("Scope : workspace"),
194+
Scope::Global => println!("Scope : global"),
195+
Scope::Profile => {
196+
let p = prof.as_deref().unwrap_or("<unknown>");
197+
println!("Scope : profile ({p})");
198+
},
199+
}
200+
println!("File : {}", path.display());
201+
println!("Command : {}", tool_cfg.command);
202+
println!("Timeout : {} ms", tool_cfg.timeout);
190203
println!(
191-
"Env Vars : {}",
204+
"Env Vars: {}",
192205
tool_cfg
193206
.env
194207
.as_ref()
195208
.map(|e| e.keys().cloned().collect::<Vec<_>>().join(", "))
196209
.unwrap_or_else(|| "(none)".into())
197210
);
198-
// todo yifan how can I know the server status
199-
println!("Status : ");
200-
return Ok(());
201211
}
202212
}
203213
}
204-
bail!("No MCP server named '{name}' found\n")
214+
215+
if !found {
216+
bail!("No MCP server named '{name}' found in any scope/profile");
217+
}
218+
219+
Ok(())
205220
}
206221

207222
async fn get_mcp_server_configs(
208223
ctx: &Context,
209224
scope: Option<Scope>,
210225
profile: Option<String>,
211226
) -> Result<Vec<(Scope, Option<String>, PathBuf, Option<McpServerConfig>)>> {
227+
let mut all_profiles: Vec<String> = Vec::new();
228+
if let Ok(dir) = chat_profiles_dir(ctx) {
229+
if dir.exists() {
230+
let mut rd = fs::read_dir(&dir).await?;
231+
while let Some(entry) = rd.next_entry().await? {
232+
if entry.file_type().await?.is_dir() {
233+
if let Some(name) = entry.file_name().to_str() {
234+
all_profiles.push(name.to_owned());
235+
}
236+
}
237+
}
238+
}
239+
}
240+
212241
let targets: Vec<(Scope, Option<String>)> = match (scope, profile) {
213242
(Some(Scope::Workspace), _) => vec![(Scope::Workspace, None)],
214243
(Some(Scope::Global), _) => vec![(Scope::Global, None)],
215244
(Some(Scope::Profile), Some(p)) => vec![(Scope::Profile, Some(p))],
216245
(Some(Scope::Profile), None) => vec![(Scope::Profile, Some("default".to_string()))],
217-
218-
// no scope but have profile ⇒ search profile
219246
(None, Some(p)) => vec![(Scope::Profile, Some(p))],
220247

221-
// give nothing ⇒ default priority:workspace → global
222-
(None, None) => vec![
223-
(Scope::Workspace, None),
224-
(Scope::Global, None),
225-
(Scope::Profile, Some("default".to_string())),
226-
],
248+
// give nothing ⇒ default priority:workspace → global -> profile
249+
(None, None) => {
250+
let mut v = vec![(Scope::Workspace, None), (Scope::Global, None)];
251+
all_profiles.sort();
252+
for p in &all_profiles {
253+
v.push((Scope::Profile, Some(p.clone())));
254+
}
255+
v
256+
},
227257
};
228258

229259
let mut results = Vec::new();

0 commit comments

Comments
 (0)