lean_ctx/tools/registered/
ctx_control.rs1use 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 crate::server::bounded_lock::read(session_lock, "ctx_control:session")
43 .as_ref()
44 .and_then(|s| s.project_root.clone())
45 .unwrap_or_else(|| ctx.project_root.clone())
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 = if let Some(ref ledger_lock) = ctx.ledger {
55 let Some(mut ledger) =
56 crate::server::bounded_lock::write(ledger_lock, "ctx_control:ledger")
57 else {
58 return Ok(ToolOutput::simple(
59 "[control unavailable — ledger busy, retry]".to_string(),
60 ));
61 };
62 let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
63 ledger.save();
64 r
65 } else {
66 let mut ledger = crate::core::context_ledger::ContextLedger::load();
67 let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
68 ledger.save();
69 r
70 };
71 let _ = overlays.save_project(&std::path::PathBuf::from(&root));
72
73 Ok(ToolOutput::simple(result))
74 }
75}