lean-ctx 3.7.3

Context Runtime for AI Agents with CCP. 68 MCP tools, 10 read modes, 60+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Per-agent MCP configuration (configure/disable, target resolution).
//!
//! Split out of `setup/mod.rs`; `use super::*` re-imports the parent module’s
//! aliases and sibling helpers. Public fns are re-exported via `pub(crate) use`.

#[allow(clippy::wildcard_imports)]
use super::*;

/// Result of setting up a single agent with all steps.
#[derive(Debug, Default)]
pub struct AgentSetupResult {
    pub mcp_ok: bool,
    pub rules: crate::rules_inject::InjectResult,
    pub skill_installed: bool,
    pub errors: Vec<String>,
}

/// Complete per-agent setup: MCP config + global rules + skill + hook.
/// Single source of truth — called by both `init --agent` and `setup`.
pub fn setup_single_agent(
    agent_name: &str,
    global: bool,
    mode: crate::hooks::HookMode,
) -> AgentSetupResult {
    let home = dirs::home_dir().unwrap_or_default();
    let mut result = AgentSetupResult::default();

    crate::hooks::install_agent_hook_with_mode(agent_name, global, mode);

    match configure_agent_mcp(agent_name) {
        Ok(()) => result.mcp_ok = true,
        Err(e) => result.errors.push(format!("MCP config: {e}")),
    }

    result.rules = crate::rules_inject::inject_rules_for_agent(&home, agent_name);

    if let Ok(path) = crate::rules_inject::install_skill_for_agent(&home, agent_name) {
        result.skill_installed = path.exists();
    }

    result
}

pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
    let binary = resolve_portable_binary();

    let targets = agent_mcp_targets(agent, &home)?;

    let mut errors = Vec::new();
    for t in &targets {
        if let Err(e) = crate::core::editor_registry::write_config_with_options(
            t,
            &binary,
            WriteOptions {
                overwrite_invalid: true,
            },
        ) {
            eprintln!(
                "\x1b[33m⚠\x1b[0m  Could not configure {}: {}",
                t.config_path.display(),
                e
            );
            errors.push(e);
        }
    }

    if agent == "kiro" {
        install_kiro_steering(&home);
    }

    if agent == "vscode" || agent == "copilot" {
        if let Err(e) = crate::core::editor_registry::plan_mode::write_vscode_plan_settings() {
            eprintln!("\x1b[33m⚠\x1b[0m  VS Code plan mode: {e}");
        }
    }
    if agent == "claude" || agent == "claude-code" {
        if let Err(e) =
            crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions()
        {
            eprintln!("\x1b[33m⚠\x1b[0m  Claude Code plan mode: {e}");
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(format!(
            "{} config(s) could not be written. See warnings above.",
            errors.len()
        ))
    }
}

pub(crate) fn agent_mcp_targets(
    agent: &str,
    home: &std::path::Path,
) -> Result<Vec<EditorTarget>, String> {
    let mut targets = Vec::<EditorTarget>::new();

    let push = |targets: &mut Vec<EditorTarget>,
                name: &'static str,
                config_path: PathBuf,
                config_type: ConfigType| {
        targets.push(EditorTarget {
            name,
            agent_key: agent.to_string(),
            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
            config_path,
            config_type,
        });
    };

    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");

    match agent {
        "cursor" => push(
            &mut targets,
            "Cursor",
            home.join(".cursor/mcp.json"),
            ConfigType::McpJson,
        ),
        "claude" | "claude-code" => push(
            &mut targets,
            "Claude Code",
            crate::core::editor_registry::claude_mcp_json_path(home),
            ConfigType::McpJson,
        ),
        "augment" => {
            push(
                &mut targets,
                "Augment CLI",
                crate::core::editor_registry::augment_cli_settings_path(home),
                ConfigType::McpJson,
            );
            push(
                &mut targets,
                "Augment (VS Code)",
                crate::core::editor_registry::augment_vscode_mcp_path(home),
                ConfigType::AugmentVsCode,
            );
        }
        "windsurf" => push(
            &mut targets,
            "Windsurf",
            home.join(".codeium/windsurf/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "codex" => {
            let codex_dir =
                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
            push(
                &mut targets,
                "Codex CLI",
                codex_dir.join("config.toml"),
                ConfigType::Codex,
            );
        }
        "gemini" => {
            push(
                &mut targets,
                "Gemini CLI",
                home.join(".gemini/settings.json"),
                ConfigType::GeminiSettings,
            );
            push(
                &mut targets,
                "Antigravity IDE",
                home.join(".gemini/antigravity/mcp_config.json"),
                ConfigType::McpJson,
            );
            push(
                &mut targets,
                "Antigravity CLI",
                home.join(".gemini/antigravity-cli/mcp_config.json"),
                ConfigType::McpJson,
            );
        }
        "antigravity" => push(
            &mut targets,
            "Antigravity IDE",
            home.join(".gemini/antigravity/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "antigravity-cli" => push(
            &mut targets,
            "Antigravity CLI",
            home.join(".gemini/antigravity-cli/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "copilot" => push(
            &mut targets,
            "Copilot CLI",
            home.join(".copilot/mcp-config.json"),
            ConfigType::CopilotCli,
        ),
        "crush" => push(
            &mut targets,
            "Crush",
            home.join(".config/crush/crush.json"),
            ConfigType::Crush,
        ),
        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
        "qoder" => {
            for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
            }
        }
        "qoderwork" => push(
            &mut targets,
            "QoderWork",
            crate::core::editor_registry::qoderwork_mcp_path(home),
            ConfigType::McpJson,
        ),
        "cline" => push(
            &mut targets,
            "Cline",
            crate::core::editor_registry::cline_mcp_path(),
            ConfigType::McpJson,
        ),
        "roo" => push(
            &mut targets,
            "Roo Code",
            crate::core::editor_registry::roo_mcp_path(),
            ConfigType::McpJson,
        ),
        "kiro" => push(
            &mut targets,
            "AWS Kiro",
            home.join(".kiro/settings/mcp.json"),
            ConfigType::McpJson,
        ),
        "verdent" => push(
            &mut targets,
            "Verdent",
            home.join(".verdent/mcp.json"),
            ConfigType::McpJson,
        ),
        "jetbrains" | "amp" | "openclaw" => {
            // Handled by dedicated install hooks (servers[] array / amp.mcpServers / mcp.servers)
        }
        "qwen" => push(
            &mut targets,
            "Qwen Code",
            home.join(".qwen/settings.json"),
            ConfigType::McpJson,
        ),
        "trae" => push(
            &mut targets,
            "Trae",
            home.join(".trae/mcp.json"),
            ConfigType::McpJson,
        ),
        "amazonq" => push(
            &mut targets,
            "Amazon Q Developer",
            home.join(".aws/amazonq/default.json"),
            ConfigType::McpJson,
        ),
        "opencode" => {
            #[cfg(windows)]
            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
                std::path::PathBuf::from(appdata)
                    .join("opencode")
                    .join("opencode.json")
            } else {
                home.join(".config/opencode/opencode.json")
            };
            #[cfg(not(windows))]
            let opencode_path = home.join(".config/opencode/opencode.json");
            push(
                &mut targets,
                "OpenCode",
                opencode_path,
                ConfigType::OpenCode,
            );
        }
        "hermes" => push(
            &mut targets,
            "Hermes Agent",
            home.join(".hermes/config.yaml"),
            ConfigType::HermesYaml,
        ),
        "vscode" => push(
            &mut targets,
            "VS Code",
            crate::core::editor_registry::vscode_mcp_path(),
            ConfigType::VsCodeMcp,
        ),
        "zed" => push(
            &mut targets,
            "Zed",
            crate::core::editor_registry::zed_settings_path(home),
            ConfigType::Zed,
        ),
        "aider" => push(
            &mut targets,
            "Aider",
            home.join(".aider/mcp.json"),
            ConfigType::McpJson,
        ),
        "continue" => push(
            &mut targets,
            "Continue",
            home.join(".continue/mcp.json"),
            ConfigType::McpJson,
        ),
        "neovim" => push(
            &mut targets,
            "Neovim (mcphub.nvim)",
            home.join(".config/mcphub/servers.json"),
            ConfigType::McpJson,
        ),
        "emacs" => push(
            &mut targets,
            "Emacs (mcp.el)",
            home.join(".emacs.d/mcp.json"),
            ConfigType::McpJson,
        ),
        "sublime" => push(
            &mut targets,
            "Sublime Text",
            home.join(".config/sublime-text/mcp.json"),
            ConfigType::McpJson,
        ),
        _ => {
            return Err(format!("Unknown agent '{agent}'"));
        }
    }

    Ok(targets)
}

pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;

    let mut targets = Vec::<EditorTarget>::new();

    let push = |targets: &mut Vec<EditorTarget>,
                name: &'static str,
                config_path: PathBuf,
                config_type: ConfigType| {
        targets.push(EditorTarget {
            name,
            agent_key: agent.to_string(),
            detect_path: PathBuf::from("/nonexistent"),
            config_path,
            config_type,
        });
    };

    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");

    match agent {
        "cursor" => push(
            &mut targets,
            "Cursor",
            home.join(".cursor/mcp.json"),
            ConfigType::McpJson,
        ),
        "claude" | "claude-code" => push(
            &mut targets,
            "Claude Code",
            crate::core::editor_registry::claude_mcp_json_path(&home),
            ConfigType::McpJson,
        ),
        "augment" => {
            push(
                &mut targets,
                "Augment CLI",
                crate::core::editor_registry::augment_cli_settings_path(&home),
                ConfigType::McpJson,
            );
            push(
                &mut targets,
                "Augment (VS Code)",
                crate::core::editor_registry::augment_vscode_mcp_path(&home),
                ConfigType::AugmentVsCode,
            );
        }
        "windsurf" => push(
            &mut targets,
            "Windsurf",
            home.join(".codeium/windsurf/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "codex" => {
            let codex_dir =
                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
            push(
                &mut targets,
                "Codex CLI",
                codex_dir.join("config.toml"),
                ConfigType::Codex,
            );
        }
        "gemini" => {
            push(
                &mut targets,
                "Gemini CLI",
                home.join(".gemini/settings.json"),
                ConfigType::GeminiSettings,
            );
            push(
                &mut targets,
                "Antigravity IDE",
                home.join(".gemini/antigravity/mcp_config.json"),
                ConfigType::McpJson,
            );
            push(
                &mut targets,
                "Antigravity CLI",
                home.join(".gemini/antigravity-cli/mcp_config.json"),
                ConfigType::McpJson,
            );
        }
        "antigravity" => push(
            &mut targets,
            "Antigravity IDE",
            home.join(".gemini/antigravity/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "antigravity-cli" => push(
            &mut targets,
            "Antigravity CLI",
            home.join(".gemini/antigravity-cli/mcp_config.json"),
            ConfigType::McpJson,
        ),
        "copilot" => push(
            &mut targets,
            "Copilot CLI",
            home.join(".copilot/mcp-config.json"),
            ConfigType::CopilotCli,
        ),
        "crush" => push(
            &mut targets,
            "Crush",
            home.join(".config/crush/crush.json"),
            ConfigType::Crush,
        ),
        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
        "qoder" => {
            for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
            }
        }
        "qoderwork" => push(
            &mut targets,
            "QoderWork",
            crate::core::editor_registry::qoderwork_mcp_path(&home),
            ConfigType::McpJson,
        ),
        "cline" => push(
            &mut targets,
            "Cline",
            crate::core::editor_registry::cline_mcp_path(),
            ConfigType::McpJson,
        ),
        "roo" => push(
            &mut targets,
            "Roo Code",
            crate::core::editor_registry::roo_mcp_path(),
            ConfigType::McpJson,
        ),
        "kiro" => push(
            &mut targets,
            "AWS Kiro",
            home.join(".kiro/settings/mcp.json"),
            ConfigType::McpJson,
        ),
        "verdent" => push(
            &mut targets,
            "Verdent",
            home.join(".verdent/mcp.json"),
            ConfigType::McpJson,
        ),
        "jetbrains" | "amp" | "openclaw" => {
            // Not supported for disable via this helper.
        }
        "qwen" => push(
            &mut targets,
            "Qwen Code",
            home.join(".qwen/settings.json"),
            ConfigType::McpJson,
        ),
        "trae" => push(
            &mut targets,
            "Trae",
            home.join(".trae/mcp.json"),
            ConfigType::McpJson,
        ),
        "amazonq" => push(
            &mut targets,
            "Amazon Q Developer",
            home.join(".aws/amazonq/default.json"),
            ConfigType::McpJson,
        ),
        "opencode" => {
            #[cfg(windows)]
            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
                std::path::PathBuf::from(appdata)
                    .join("opencode")
                    .join("opencode.json")
            } else {
                home.join(".config/opencode/opencode.json")
            };
            #[cfg(not(windows))]
            let opencode_path = home.join(".config/opencode/opencode.json");
            push(
                &mut targets,
                "OpenCode",
                opencode_path,
                ConfigType::OpenCode,
            );
        }
        "hermes" => push(
            &mut targets,
            "Hermes Agent",
            home.join(".hermes/config.yaml"),
            ConfigType::HermesYaml,
        ),
        "vscode" => push(
            &mut targets,
            "VS Code",
            crate::core::editor_registry::vscode_mcp_path(),
            ConfigType::VsCodeMcp,
        ),
        "zed" => push(
            &mut targets,
            "Zed",
            crate::core::editor_registry::zed_settings_path(&home),
            ConfigType::Zed,
        ),
        "aider" => push(
            &mut targets,
            "Aider",
            home.join(".aider/mcp.json"),
            ConfigType::McpJson,
        ),
        "continue" => push(
            &mut targets,
            "Continue",
            home.join(".continue/mcp.json"),
            ConfigType::McpJson,
        ),
        "neovim" => push(
            &mut targets,
            "Neovim (mcphub.nvim)",
            home.join(".config/mcphub/servers.json"),
            ConfigType::McpJson,
        ),
        "emacs" => push(
            &mut targets,
            "Emacs (mcp.el)",
            home.join(".emacs.d/mcp.json"),
            ConfigType::McpJson,
        ),
        "sublime" => push(
            &mut targets,
            "Sublime Text",
            home.join(".config/sublime-text/mcp.json"),
            ConfigType::McpJson,
        ),
        _ => {
            return Err(format!("Unknown agent '{agent}'"));
        }
    }

    for t in &targets {
        crate::core::editor_registry::remove_lean_ctx_server(
            t,
            WriteOptions { overwrite_invalid },
        )?;
    }

    Ok(())
}