Skip to main content

lean_ctx/tools/registered/
ctx_control.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
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            "Universal context manipulation (Context Field Theory). Actions: exclude|include|pin|unpin|set_view|set_priority|mark_outdated|reset|list|history. Overlay-based, reversible, scoped.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "action": {
23                        "type": "string",
24                        "description": "exclude|include|pin|unpin|set_view|set_priority|mark_outdated|reset|list|history"
25                    },
26                    "target": { "type": "string", "description": "@F1 or path or item ID" },
27                    "value": { "type": "string", "description": "New content, view name, or priority" },
28                    "scope": { "type": "string", "description": "call|session|project (default: session)" },
29                    "reason": { "type": "string", "description": "Reason for the action" }
30                },
31                "required": ["action"]
32            }),
33        )
34    }
35
36    fn handle(
37        &self,
38        args: &Map<String, Value>,
39        ctx: &ToolContext,
40    ) -> Result<ToolOutput, ErrorData> {
41        let root = if let Some(ref session_lock) = ctx.session {
42            let session = tokio::task::block_in_place(|| session_lock.blocking_read());
43            session.project_root.clone().unwrap_or_default()
44        } else {
45            ctx.project_root.clone()
46        };
47
48        let mut overlays = crate::core::context_overlay::OverlayStore::load_project(
49            &std::path::PathBuf::from(&root),
50        );
51
52        let result = if let Some(ref ledger_lock) = ctx.ledger {
53            let mut ledger = tokio::task::block_in_place(|| ledger_lock.blocking_write());
54            let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
55            ledger.save();
56            r
57        } else {
58            let mut ledger = crate::core::context_ledger::ContextLedger::load();
59            let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
60            ledger.save();
61            r
62        };
63        let _ = overlays.save_project(&std::path::PathBuf::from(&root));
64
65        Ok(ToolOutput::simple(result))
66    }
67}