Skip to main content

lean_ctx_cli/setup/
mcp.rs

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