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