lean_ctx/tools/registered/
ctx_control.rs1use 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, 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|set_view|set_priority|mark_outdated|reset|list|history"
28 },
29 "target": { "type": "string", "description": "@F1 handle (ctx_compile reference) or file path or item ID" },
30 "value": { "type": "string", "description": "New content, view name, or priority" },
31 "scope": { "type": "string", "description": "call (this turn only), session (rest of this session), project (persists)" },
32 "reason": { "type": "string", "description": "Reason for the action" }
33 },
34 "required": ["action"]
35 }),
36 )
37 }
38
39 fn handle(
40 &self,
41 args: &Map<String, Value>,
42 ctx: &ToolContext,
43 ) -> Result<ToolOutput, ErrorData> {
44 let root = if let Some(ref session_lock) = ctx.session {
45 crate::server::bounded_lock::read(session_lock, "ctx_control:session")
46 .as_ref()
47 .and_then(|s| s.project_root.clone())
48 .unwrap_or_else(|| ctx.project_root.clone())
49 } else {
50 ctx.project_root.clone()
51 };
52
53 let mut overlays = crate::core::context_overlay::OverlayStore::load_project(
54 &std::path::PathBuf::from(&root),
55 );
56
57 let result = if let Some(ref ledger_lock) = ctx.ledger {
58 let Some(mut ledger) =
59 crate::server::bounded_lock::write(ledger_lock, "ctx_control:ledger")
60 else {
61 return Ok(ToolOutput::simple(
62 "[control unavailable — ledger busy, retry]".to_string(),
63 ));
64 };
65 let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
66 ledger.save();
67 r
68 } else {
69 let mut ledger = crate::core::context_ledger::ContextLedger::load();
70 let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
71 ledger.save();
72 r
73 };
74 let _ = overlays.save_project(&std::path::PathBuf::from(&root));
75
76 Ok(ToolOutput::simple(result))
77 }
78}