Skip to main content

lean_ctx/tools/registered/
ctx_control.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxControlTool;
9
10impl McpTool for CtxControlTool {
11    fn name(&self) -> &'static str {
12        "ctx_control"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_control",
18            "Fine-tune context — exclude, include, pin, unpin, evict, set_view, set_priority, mark_outdated, reset, list, history.\n\
19            Overlay-based, reversible, scoped to call/session/project.\n\
20            WORKFLOW: after ctx_compose, exclude low-relevance files.\n\
21            ANTIPATTERN: not for initial context building — use ctx_compose/ctx_read first.",
22            json!({
23                "type": "object",
24                "properties": {
25                    "action": {
26                        "type": "string",
27                        "description": "exclude|include|pin|unpin|evict|set_view|set_priority|mark_outdated|reset|list|history"
28                    },
29                    "target": { "type": "string", "description": "@F1 handle, file path or item ID" },
30                    "targets": { "type": "string", "description": "evict: comma-separated file paths" },
31                    "value": { "type": "string", "description": "New content, view name, or priority" },
32                    "scope": { "type": "string", "description": "call (this turn only), session (rest of this session), project (persists)" },
33                    "reason": { "type": "string", "description": "Reason for the action" }
34                },
35                "required": ["action"]
36            }),
37        )
38    }
39
40    fn handle(
41        &self,
42        args: &Map<String, Value>,
43        ctx: &ToolContext,
44    ) -> Result<ToolOutput, ErrorData> {
45        let root = if let Some(ref session_lock) = ctx.session {
46            crate::server::bounded_lock::read(session_lock, "ctx_control:session")
47                .as_ref()
48                .and_then(|s| s.project_root.clone())
49                .unwrap_or_else(|| ctx.project_root.clone())
50        } else {
51            ctx.project_root.clone()
52        };
53
54        let mut overlays = crate::core::context_overlay::OverlayStore::load_project(
55            &std::path::PathBuf::from(&root),
56        );
57
58        let result = if let Some(ref ledger_lock) = ctx.ledger {
59            let Some(mut ledger) =
60                crate::server::bounded_lock::write(ledger_lock, "ctx_control:ledger")
61            else {
62                return Ok(ToolOutput::simple(
63                    "[control unavailable — ledger busy, retry]".to_string(),
64                ));
65            };
66            let r = control(args, &mut ledger, &mut overlays, &root);
67            ledger.save();
68            r
69        } else {
70            let mut ledger = crate::core::context_ledger::ContextLedger::load();
71            let r = control(args, &mut ledger, &mut overlays, &root);
72            ledger.save();
73            r
74        };
75        let _ = overlays.save_project(&std::path::PathBuf::from(&root));
76
77        Ok(ToolOutput::simple(result))
78    }
79}
80
81/// `evict` needs the project root to resolve partial paths; every other
82/// action is the shared overlay handler (CLI and dashboard use it too).
83fn control(
84    args: &Map<String, Value>,
85    ledger: &mut crate::core::context_ledger::ContextLedger,
86    overlays: &mut crate::core::context_overlay::OverlayStore,
87    root: &str,
88) -> String {
89    if args.get("action").and_then(Value::as_str) == Some("evict") {
90        let targets = args
91            .get("targets")
92            .or_else(|| args.get("target"))
93            .and_then(Value::as_str)
94            .unwrap_or_default();
95        return crate::tools::ctx_control::evict(targets, ledger, overlays, root);
96    }
97    crate::tools::ctx_control::handle(Some(args), ledger, overlays)
98}