Skip to main content

lean_ctx/setup/
mcp.rs

1//! Per-agent MCP configuration (configure/disable, target resolution).
2//!
3//! Split out of `setup/mod.rs`; `use super::*` re-imports the parent module’s
4//! aliases and sibling helpers. Public fns are re-exported via `pub(crate) use`.
5
6#[allow(clippy::wildcard_imports)]
7use super::*;
8
9/// Result of setting up a single agent with all steps.
10#[derive(Debug, Default)]
11pub struct AgentSetupResult {
12    pub mcp_ok: bool,
13    /// MCP registration was intentionally skipped because `[setup]
14    /// auto_update_mcp = false` (#281), not because it failed.
15    pub mcp_skipped: bool,
16    pub rules: crate::rules_inject::InjectResult,
17    pub skill_installed: bool,
18    pub errors: Vec<String>,
19}
20
21/// Complete per-agent setup: MCP config + global rules + skill + hook.
22/// Single source of truth — called by both `init --agent` and `setup`.
23pub fn setup_single_agent(
24    agent_name: &str,
25    global: bool,
26    mode: crate::hooks::HookMode,
27) -> AgentSetupResult {
28    // Prefer HOME/USERPROFILE so tests and portable installs work on Windows
29    // (dirs::home_dir uses Known Folder API and ignores env overrides).
30    let home = crate::core::home::resolve_home_dir().unwrap_or_default();
31    let mut result = AgentSetupResult::default();
32
33    crate::hooks::install_agent_hook_respecting_rules_off(agent_name, global, mode);
34
35    // #281: honor `[setup] auto_update_mcp = false` — skip MCP registration but
36    // still install the hook, rules and skill. Locked-down environments can keep
37    // the MCP server out of agent settings without losing the CLI integration.
38    if crate::core::config::Config::load()
39        .setup
40        .should_update_mcp()
41    {
42        match configure_agent_mcp(agent_name) {
43            Ok(()) => result.mcp_ok = true,
44            Err(e) => result.errors.push(format!("MCP config: {e}")),
45        }
46    } else {
47        result.mcp_skipped = true;
48    }
49
50    result.rules = crate::rules_inject::inject_rules_for_agent(&home, agent_name);
51
52    if let Ok(path) = crate::rules_inject::install_skill_for_agent(&home, agent_name) {
53        result.skill_installed = path.exists();
54    }
55
56    result
57}
58
59pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
60    let allow_rule_steering = crate::core::config::Config::load().rules_injection_effective()
61        != crate::core::config::RulesInjection::Off;
62
63    configure_agent_mcp_with_rule_steering(agent, allow_rule_steering)
64}
65
66pub(crate) fn configure_agent_mcp_with_rule_steering(
67    agent: &str,
68    allow_rule_steering: bool,
69) -> Result<(), String> {
70    // Prefer HOME/USERPROFILE (dirs::home_dir ignores env on Windows).
71    let home = crate::core::home::resolve_home_dir()
72        .ok_or_else(|| "Cannot determine home directory".to_string())?;
73    let binary = resolve_agent_binary();
74
75    let targets = agent_mcp_targets(agent, &home)?;
76
77    let mut errors = Vec::new();
78    for t in &targets {
79        if let Err(e) = crate::core::editor_registry::write_config_with_options_and_rule_steering(
80            t,
81            &binary,
82            WriteOptions {
83                overwrite_invalid: true,
84            },
85            allow_rule_steering,
86        ) {
87            eprintln!(
88                "\x1b[33m⚠\x1b[0m  Could not configure {}: {}",
89                t.config_path.display(),
90                e
91            );
92            errors.push(e);
93        }
94    }
95
96    if agent == "kiro" && allow_rule_steering {
97        install_kiro_steering(&home);
98    }
99
100    if (agent == "vscode" || agent == "copilot")
101        && let Err(e) = crate::core::editor_registry::plan_mode::write_vscode_plan_settings()
102    {
103        eprintln!("\x1b[33m⚠\x1b[0m  VS Code plan mode: {e}");
104    }
105    if (agent == "claude" || agent == "claude-code")
106        && let Err(e) =
107            crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions()
108    {
109        eprintln!("\x1b[33m⚠\x1b[0m  Claude Code plan mode: {e}");
110    }
111    if agent == "codebuddy"
112        && let Err(e) =
113            crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions()
114    {
115        eprintln!("\x1b[33m⚠\x1b[0m  CodeBuddy plan mode: {e}");
116    }
117
118    if errors.is_empty() {
119        Ok(())
120    } else {
121        Err(format!(
122            "{} config(s) could not be written. See warnings above.",
123            errors.len()
124        ))
125    }
126}
127
128pub(crate) fn agent_mcp_targets(
129    agent: &str,
130    home: &std::path::Path,
131) -> Result<Vec<EditorTarget>, String> {
132    let mut targets = Vec::<EditorTarget>::new();
133
134    let push = |targets: &mut Vec<EditorTarget>,
135                name: &'static str,
136                config_path: PathBuf,
137                config_type: ConfigType| {
138        targets.push(EditorTarget {
139            name,
140            agent_key: agent.to_string(),
141            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
142            config_path,
143            config_type,
144        });
145    };
146
147    match agent {
148        "cursor" => push(
149            &mut targets,
150            "Cursor",
151            home.join(".cursor/mcp.json"),
152            ConfigType::McpJson,
153        ),
154        "claude" | "claude-code" => push(
155            &mut targets,
156            "Claude Code",
157            crate::core::editor_registry::claude_mcp_json_path(home),
158            ConfigType::McpJson,
159        ),
160        "codebuddy" => push(
161            &mut targets,
162            "CodeBuddy",
163            crate::core::editor_registry::codebuddy_mcp_json_path(home),
164            ConfigType::McpJson,
165        ),
166        "commandcode" => push(
167            &mut targets,
168            "Command Code",
169            home.join(".commandcode/mcp.json"),
170            ConfigType::CommandCode,
171        ),
172        "augment" => {
173            push(
174                &mut targets,
175                "Augment CLI",
176                crate::core::editor_registry::augment_cli_settings_path(home),
177                ConfigType::McpJson,
178            );
179            push(
180                &mut targets,
181                "Augment (VS Code)",
182                crate::core::editor_registry::augment_vscode_mcp_path(home),
183                ConfigType::AugmentVsCode,
184            );
185        }
186        "windsurf" => push(
187            &mut targets,
188            "Windsurf",
189            home.join(".codeium/windsurf/mcp_config.json"),
190            ConfigType::McpJson,
191        ),
192        "codex" => push(
193            &mut targets,
194            "Codex CLI",
195            crate::core::home::resolve_codex_config_path()
196                .unwrap_or_else(|| home.join(".codex/config.toml")),
197            ConfigType::Codex,
198        ),
199        "grok" => push(
200            &mut targets,
201            "Grok",
202            home.join(".grok/config.toml"),
203            ConfigType::Codex,
204        ),
205        "gemini" => {
206            push(
207                &mut targets,
208                "Gemini CLI",
209                home.join(".gemini/settings.json"),
210                ConfigType::GeminiSettings,
211            );
212            push(
213                &mut targets,
214                "Antigravity IDE",
215                home.join(".gemini/antigravity/mcp_config.json"),
216                ConfigType::McpJson,
217            );
218            push(
219                &mut targets,
220                "Antigravity CLI",
221                home.join(".gemini/antigravity-cli/mcp_config.json"),
222                ConfigType::McpJson,
223            );
224        }
225        "antigravity" => push(
226            &mut targets,
227            "Antigravity IDE",
228            home.join(".gemini/antigravity/mcp_config.json"),
229            ConfigType::McpJson,
230        ),
231        "antigravity-cli" => push(
232            &mut targets,
233            "Antigravity CLI",
234            home.join(".gemini/antigravity-cli/mcp_config.json"),
235            ConfigType::McpJson,
236        ),
237        "copilot" => push(
238            &mut targets,
239            "Copilot CLI",
240            home.join(".copilot/mcp-config.json"),
241            ConfigType::CopilotCli,
242        ),
243        "crush" => push(
244            &mut targets,
245            "Crush",
246            home.join(".config/crush/crush.json"),
247            ConfigType::Crush,
248        ),
249        "qoder" => {
250            for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
251                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
252            }
253        }
254        "qoderwork" => push(
255            &mut targets,
256            "QoderWork",
257            crate::core::editor_registry::qoderwork_mcp_path(home),
258            ConfigType::McpJson,
259        ),
260        "qodercli" => push(
261            &mut targets,
262            "Qoder CLI",
263            crate::core::editor_registry::qodercli_settings_path(home),
264            ConfigType::QoderSettings,
265        ),
266        "cline" => push(
267            &mut targets,
268            "Cline",
269            crate::core::editor_registry::cline_mcp_path(),
270            ConfigType::McpJson,
271        ),
272        "cline-cli" => push(
273            &mut targets,
274            "Cline CLI",
275            crate::core::editor_registry::cline_cli_mcp_settings_path(),
276            ConfigType::ClineCli,
277        ),
278        "omp" => push(
279            &mut targets,
280            "Oh My Pi",
281            crate::core::editor_registry::omp_mcp_path(home),
282            ConfigType::OmpMcp,
283        ),
284        "roo" => push(
285            &mut targets,
286            "Roo Code",
287            crate::core::editor_registry::roo_mcp_path(),
288            ConfigType::McpJson,
289        ),
290        "kiro" => push(
291            &mut targets,
292            "AWS Kiro",
293            home.join(".kiro/settings/mcp.json"),
294            ConfigType::McpJson,
295        ),
296        "verdent" => push(
297            &mut targets,
298            "Verdent",
299            home.join(".verdent/mcp.json"),
300            ConfigType::McpJson,
301        ),
302        // pi: deliberately no MCP target. Pi has no native MCP adapter — a
303        // ~/.pi/agent/mcp.json entry is never served and made older pi-lean-ctx
304        // versions disable their embedded bridge (GitHub #361). Pi runs through
305        // the pi-lean-ctx npm package; install_pi_hook_with_mode removes stale
306        // entries instead.
307        "pi" | "jetbrains" | "amp" | "openclaw" => {
308            // jetbrains/amp/openclaw: handled by dedicated install hooks
309            // (servers[] array / amp.mcpServers / mcp.servers).
310        }
311        "qwen" => push(
312            &mut targets,
313            "Qwen Code",
314            home.join(".qwen/settings.json"),
315            ConfigType::McpJson,
316        ),
317        "trae" => push(
318            &mut targets,
319            "Trae",
320            home.join(".trae/mcp.json"),
321            ConfigType::McpJson,
322        ),
323        // #1402: resolves `~/.codewhale/mcp.json`, or the legacy
324        // `~/.deepseek/mcp.json` when that is the file CodeWhale still reads.
325        "codewhale" => push(
326            &mut targets,
327            "CodeWhale",
328            crate::core::editor_registry::codewhale_mcp_json_path(home),
329            ConfigType::CodeWhale,
330        ),
331        "amazonq" => push(
332            &mut targets,
333            "Amazon Q Developer",
334            home.join(".aws/amazonq/default.json"),
335            ConfigType::McpJson,
336        ),
337        "opencode" => {
338            // #1585: write into the user's existing opencode.jsonc when that is
339            // the file they have, instead of creating a competing opencode.json.
340            let opencode_path = crate::core::opencode_config::resolve(home).path;
341            push(
342                &mut targets,
343                "OpenCode",
344                opencode_path,
345                ConfigType::OpenCode,
346            );
347        }
348        "hermes" => push(
349            &mut targets,
350            "Hermes Agent",
351            home.join(".hermes/config.yaml"),
352            ConfigType::HermesYaml,
353        ),
354        "vscode" => push(
355            &mut targets,
356            "VS Code",
357            crate::core::editor_registry::vscode_mcp_path(),
358            ConfigType::VsCodeMcp,
359        ),
360        "vscode-insiders" => push(
361            &mut targets,
362            "VS Code Insiders",
363            crate::core::editor_registry::vscode_insiders_mcp_path(),
364            ConfigType::VsCodeMcp,
365        ),
366        "zed" => push(
367            &mut targets,
368            "Zed",
369            crate::core::editor_registry::zed_settings_path(home),
370            ConfigType::Zed,
371        ),
372        "aider" => push(
373            &mut targets,
374            "Aider",
375            home.join(".aider/mcp.json"),
376            ConfigType::McpJson,
377        ),
378        "continue" => push(
379            &mut targets,
380            "Continue",
381            home.join(".continue/mcp.json"),
382            ConfigType::McpJson,
383        ),
384        "neovim" => push(
385            &mut targets,
386            "Neovim (mcphub.nvim)",
387            home.join(".config/mcphub/servers.json"),
388            ConfigType::McpJson,
389        ),
390        "emacs" => push(
391            &mut targets,
392            "Emacs (mcp.el)",
393            home.join(".emacs.d/mcp.json"),
394            ConfigType::McpJson,
395        ),
396        "sublime" => push(
397            &mut targets,
398            "Sublime Text",
399            home.join(".config/sublime-text/mcp.json"),
400            ConfigType::McpJson,
401        ),
402        _ => {
403            return Err(format!("Unknown agent '{agent}'"));
404        }
405    }
406
407    Ok(targets)
408}
409
410pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
411    // Prefer HOME/USERPROFILE (dirs::home_dir ignores env on Windows).
412    let home = crate::core::home::resolve_home_dir()
413        .ok_or_else(|| "Cannot determine home directory".to_string())?;
414
415    let mut targets = Vec::<EditorTarget>::new();
416
417    let push = |targets: &mut Vec<EditorTarget>,
418                name: &'static str,
419                config_path: PathBuf,
420                config_type: ConfigType| {
421        targets.push(EditorTarget {
422            name,
423            agent_key: agent.to_string(),
424            detect_path: PathBuf::from("/nonexistent"),
425            config_path,
426            config_type,
427        });
428    };
429
430    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
431
432    match agent {
433        "cursor" => push(
434            &mut targets,
435            "Cursor",
436            home.join(".cursor/mcp.json"),
437            ConfigType::McpJson,
438        ),
439        "claude" | "claude-code" => push(
440            &mut targets,
441            "Claude Code",
442            crate::core::editor_registry::claude_mcp_json_path(&home),
443            ConfigType::McpJson,
444        ),
445        "codebuddy" => push(
446            &mut targets,
447            "CodeBuddy",
448            crate::core::editor_registry::codebuddy_mcp_json_path(&home),
449            ConfigType::McpJson,
450        ),
451        "commandcode" => push(
452            &mut targets,
453            "Command Code",
454            home.join(".commandcode/mcp.json"),
455            ConfigType::CommandCode,
456        ),
457        "augment" => {
458            push(
459                &mut targets,
460                "Augment CLI",
461                crate::core::editor_registry::augment_cli_settings_path(&home),
462                ConfigType::McpJson,
463            );
464            push(
465                &mut targets,
466                "Augment (VS Code)",
467                crate::core::editor_registry::augment_vscode_mcp_path(&home),
468                ConfigType::AugmentVsCode,
469            );
470        }
471        "windsurf" => push(
472            &mut targets,
473            "Windsurf",
474            home.join(".codeium/windsurf/mcp_config.json"),
475            ConfigType::McpJson,
476        ),
477        "codex" => push(
478            &mut targets,
479            "Codex CLI",
480            crate::core::home::resolve_codex_config_path()
481                .unwrap_or_else(|| home.join(".codex/config.toml")),
482            ConfigType::Codex,
483        ),
484        "grok" => push(
485            &mut targets,
486            "Grok",
487            home.join(".grok/config.toml"),
488            ConfigType::Codex,
489        ),
490        "gemini" => {
491            push(
492                &mut targets,
493                "Gemini CLI",
494                home.join(".gemini/settings.json"),
495                ConfigType::GeminiSettings,
496            );
497            push(
498                &mut targets,
499                "Antigravity IDE",
500                home.join(".gemini/antigravity/mcp_config.json"),
501                ConfigType::McpJson,
502            );
503            push(
504                &mut targets,
505                "Antigravity CLI",
506                home.join(".gemini/antigravity-cli/mcp_config.json"),
507                ConfigType::McpJson,
508            );
509        }
510        "antigravity" => push(
511            &mut targets,
512            "Antigravity IDE",
513            home.join(".gemini/antigravity/mcp_config.json"),
514            ConfigType::McpJson,
515        ),
516        "antigravity-cli" => push(
517            &mut targets,
518            "Antigravity CLI",
519            home.join(".gemini/antigravity-cli/mcp_config.json"),
520            ConfigType::McpJson,
521        ),
522        "copilot" => push(
523            &mut targets,
524            "Copilot CLI",
525            home.join(".copilot/mcp-config.json"),
526            ConfigType::CopilotCli,
527        ),
528        "crush" => push(
529            &mut targets,
530            "Crush",
531            home.join(".config/crush/crush.json"),
532            ConfigType::Crush,
533        ),
534        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
535        "qoder" => {
536            for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
537                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
538            }
539        }
540        "qoderwork" => push(
541            &mut targets,
542            "QoderWork",
543            crate::core::editor_registry::qoderwork_mcp_path(&home),
544            ConfigType::McpJson,
545        ),
546        "qodercli" => push(
547            &mut targets,
548            "Qoder CLI",
549            crate::core::editor_registry::qodercli_settings_path(&home),
550            ConfigType::QoderSettings,
551        ),
552        "cline" => push(
553            &mut targets,
554            "Cline",
555            crate::core::editor_registry::cline_mcp_path(),
556            ConfigType::McpJson,
557        ),
558        "cline-cli" => push(
559            &mut targets,
560            "Cline CLI",
561            crate::core::editor_registry::cline_cli_mcp_settings_path(),
562            ConfigType::ClineCli,
563        ),
564        "omp" => push(
565            &mut targets,
566            "Oh My Pi",
567            crate::core::editor_registry::omp_mcp_path(&home),
568            ConfigType::OmpMcp,
569        ),
570        "roo" => push(
571            &mut targets,
572            "Roo Code",
573            crate::core::editor_registry::roo_mcp_path(),
574            ConfigType::McpJson,
575        ),
576        "kiro" => push(
577            &mut targets,
578            "AWS Kiro",
579            home.join(".kiro/settings/mcp.json"),
580            ConfigType::McpJson,
581        ),
582        "verdent" => push(
583            &mut targets,
584            "Verdent",
585            home.join(".verdent/mcp.json"),
586            ConfigType::McpJson,
587        ),
588        "jetbrains" | "amp" | "openclaw" => {
589            // Not supported for disable via this helper.
590        }
591        "qwen" => push(
592            &mut targets,
593            "Qwen Code",
594            home.join(".qwen/settings.json"),
595            ConfigType::McpJson,
596        ),
597        "trae" => push(
598            &mut targets,
599            "Trae",
600            home.join(".trae/mcp.json"),
601            ConfigType::McpJson,
602        ),
603        // #1402: same resolution as the enable path — disable must visit the
604        // exact file we wrote, never the shadowed legacy one.
605        "codewhale" => push(
606            &mut targets,
607            "CodeWhale",
608            crate::core::editor_registry::codewhale_mcp_json_path(home.as_ref()),
609            ConfigType::CodeWhale,
610        ),
611        "amazonq" => push(
612            &mut targets,
613            "Amazon Q Developer",
614            home.join(".aws/amazonq/default.json"),
615            ConfigType::McpJson,
616        ),
617        "opencode" => {
618            // #1585: write into the user's existing opencode.jsonc when that is
619            // the file they have, instead of creating a competing opencode.json.
620            let opencode_path = crate::core::opencode_config::resolve(home.as_ref()).path;
621            push(
622                &mut targets,
623                "OpenCode",
624                opencode_path,
625                ConfigType::OpenCode,
626            );
627        }
628        "hermes" => push(
629            &mut targets,
630            "Hermes Agent",
631            home.join(".hermes/config.yaml"),
632            ConfigType::HermesYaml,
633        ),
634        "vscode" => push(
635            &mut targets,
636            "VS Code",
637            crate::core::editor_registry::vscode_mcp_path(),
638            ConfigType::VsCodeMcp,
639        ),
640        "vscode-insiders" => push(
641            &mut targets,
642            "VS Code Insiders",
643            crate::core::editor_registry::vscode_insiders_mcp_path(),
644            ConfigType::VsCodeMcp,
645        ),
646        "zed" => push(
647            &mut targets,
648            "Zed",
649            crate::core::editor_registry::zed_settings_path(&home),
650            ConfigType::Zed,
651        ),
652        "aider" => push(
653            &mut targets,
654            "Aider",
655            home.join(".aider/mcp.json"),
656            ConfigType::McpJson,
657        ),
658        "continue" => push(
659            &mut targets,
660            "Continue",
661            home.join(".continue/mcp.json"),
662            ConfigType::McpJson,
663        ),
664        "neovim" => push(
665            &mut targets,
666            "Neovim (mcphub.nvim)",
667            home.join(".config/mcphub/servers.json"),
668            ConfigType::McpJson,
669        ),
670        "emacs" => push(
671            &mut targets,
672            "Emacs (mcp.el)",
673            home.join(".emacs.d/mcp.json"),
674            ConfigType::McpJson,
675        ),
676        "sublime" => push(
677            &mut targets,
678            "Sublime Text",
679            home.join(".config/sublime-text/mcp.json"),
680            ConfigType::McpJson,
681        ),
682        _ => {
683            return Err(format!("Unknown agent '{agent}'"));
684        }
685    }
686
687    for t in &targets {
688        crate::core::editor_registry::remove_lean_ctx_server(
689            t,
690            WriteOptions { overwrite_invalid },
691        )?;
692    }
693
694    Ok(())
695}
696
697#[cfg(test)]
698mod qodercli_tests {
699    use super::*;
700    use crate::test_env::CurrentDirGuard;
701
702    #[test]
703    fn kiro_mcp_configuration_respects_rule_steering_policy() {
704        let _env_lock = crate::core::data_dir::test_env_lock();
705        let tmp = tempfile::tempdir().unwrap();
706        let home = tmp.path().join("home");
707        let project = tmp.path().join("project");
708
709        std::fs::create_dir_all(&home).unwrap();
710        std::fs::create_dir_all(&project).unwrap();
711
712        let _home_guard = crate::setup::EnvVarGuard::set("HOME", home.to_string_lossy().as_ref());
713        let _profile_guard =
714            crate::setup::EnvVarGuard::set("USERPROFILE", home.to_string_lossy().as_ref());
715        let _cwd_guard = CurrentDirGuard::enter(&project);
716
717        configure_agent_mcp_with_rule_steering("kiro", false).unwrap();
718
719        assert!(
720            home.join(".kiro/settings/mcp.json").exists(),
721            "Kiro MCP registration must still be written when steering is disabled"
722        );
723
724        let steering = project.join(".kiro/steering/lean-ctx.md");
725
726        assert!(
727            !steering.exists(),
728            "steering-disabled Kiro setup must not create lean-ctx steering"
729        );
730
731        configure_agent_mcp_with_rule_steering("kiro", true).unwrap();
732
733        assert!(
734            steering.exists(),
735            "test precondition failed: steering-enabled Kiro setup should create its steering file"
736        );
737    }
738
739    #[test]
740    fn commandcode_agent_target_uses_command_code_schema() {
741        let home = std::path::Path::new("/home/tester");
742        let targets = agent_mcp_targets("commandcode", home).unwrap();
743
744        assert_eq!(targets.len(), 1);
745        assert_eq!(targets[0].name, "Command Code");
746        assert_eq!(targets[0].config_path, home.join(".commandcode/mcp.json"));
747        assert_eq!(targets[0].config_type, ConfigType::CommandCode);
748    }
749
750    #[test]
751    fn codewhale_agent_target_uses_codewhale_schema_and_current_path() {
752        // No `~/.codewhale` or `~/.deepseek` exists under this synthetic home,
753        // so resolution must land on the current path, not the legacy one.
754        let home = std::path::Path::new("/home/tester");
755        let targets = agent_mcp_targets("codewhale", home).unwrap();
756
757        assert_eq!(targets.len(), 1);
758        assert_eq!(targets[0].name, "CodeWhale");
759        assert_eq!(targets[0].agent_key, "codewhale");
760        assert_eq!(targets[0].config_type, ConfigType::CodeWhale);
761        assert_eq!(targets[0].config_path, home.join(".codewhale/mcp.json"));
762    }
763
764    #[test]
765    fn codewhale_agent_target_follows_legacy_deepseek_config_when_that_is_the_live_one() {
766        let tmp = tempfile::tempdir().unwrap();
767        let home = tmp.path();
768        std::fs::create_dir_all(home.join(".deepseek")).unwrap();
769        std::fs::write(home.join(".deepseek/mcp.json"), "{}").unwrap();
770
771        let targets = agent_mcp_targets("codewhale", home).unwrap();
772        assert_eq!(targets[0].config_path, home.join(".deepseek/mcp.json"));
773    }
774
775    #[test]
776    fn cline_cli_agent_target_uses_cline_cli_schema() {
777        let home = std::path::Path::new("/home/tester");
778        let targets = agent_mcp_targets("cline-cli", home).unwrap();
779
780        assert_eq!(targets.len(), 1);
781        assert_eq!(targets[0].name, "Cline CLI");
782        assert_eq!(targets[0].config_type, ConfigType::ClineCli);
783    }
784
785    #[test]
786    fn omp_agent_target_uses_the_native_agent_dir_and_schema() {
787        let home = std::path::Path::new("/home/tester");
788        let targets = agent_mcp_targets("omp", home).unwrap();
789
790        assert_eq!(targets.len(), 1);
791        assert_eq!(targets[0].name, "Oh My Pi");
792        assert_eq!(
793            targets[0].config_path,
794            crate::core::editor_registry::omp_mcp_path(home)
795        );
796        assert_eq!(targets[0].config_type, ConfigType::OmpMcp);
797        // Default layout only holds when the environment does not relocate the
798        // OMP agent dir; PI_CODING_AGENT_DIR is a documented full override.
799        if std::env::var_os("PI_CODING_AGENT_DIR").is_none() {
800            assert_eq!(targets[0].config_path, home.join(".omp/agent/mcp.json"));
801        }
802    }
803
804    #[test]
805    fn omp_and_pi_stay_separate_setup_targets() {
806        let home = std::path::Path::new("/home/tester");
807        let omp = agent_mcp_targets("omp", home).unwrap();
808        let pi = agent_mcp_targets("pi", home).unwrap();
809        assert!(
810            pi.iter().all(|t| t.config_path != omp[0].config_path),
811            "Oh My Pi must not write into the stock Pi config"
812        );
813    }
814
815    #[test]
816    fn qodercli_agent_target_uses_settings_json() {
817        let home = std::path::Path::new("/home/tester");
818        let targets = agent_mcp_targets("qodercli", home).unwrap();
819
820        assert_eq!(targets.len(), 1);
821        assert_eq!(targets[0].name, "Qoder CLI");
822        assert_eq!(targets[0].config_path, home.join(".qoder/settings.json"));
823    }
824
825    #[test]
826    fn qodercli_agent_target_uses_qoder_settings_writer() {
827        let home = std::path::Path::new("/home/tester");
828        let targets = agent_mcp_targets("qodercli", home).unwrap();
829
830        assert_eq!(targets[0].agent_key, "qodercli");
831        assert_eq!(targets[0].config_type, ConfigType::QoderSettings);
832    }
833
834    #[test]
835    fn qodercli_agent_target_is_separate_from_qoder_ide_targets() {
836        let home = std::path::Path::new("/home/tester");
837        let cli = agent_mcp_targets("qodercli", home).unwrap();
838        let ide = agent_mcp_targets("qoder", home).unwrap();
839
840        assert!(
841            ide.iter()
842                .all(|target| target.config_path != cli[0].config_path)
843        );
844    }
845}