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 mut ledger = crate::core::context_ledger::ContextLedger::load();
42
43        let root = if let Some(ref session_lock) = ctx.session {
44            let session = tokio::task::block_in_place(|| session_lock.blocking_read());
45            session.project_root.clone().unwrap_or_default()
46        } else {
47            ctx.project_root.clone()
48        };
49
50        let mut overlays = crate::core::context_overlay::OverlayStore::load_project(
51            &std::path::PathBuf::from(&root),
52        );
53
54        let result = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
55        ledger.save();
56        let _ = overlays.save_project(&std::path::PathBuf::from(&root));
57
58        Ok(ToolOutput::simple(result))
59    }
60}