lean_ctx_server/tools/registered/
ctx_heatmap.rs1use rmcp::ErrorData;
3use rmcp::model::Tool;
4use serde_json::{Map, Value, json};
5
6use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
7use lean_ctx::tool_defs::tool_def;
8
9pub struct CtxHeatmapTool;
10
11impl McpTool for CtxHeatmapTool {
12 fn name(&self) -> &'static str {
13 "ctx_heatmap"
14 }
15
16 fn tool_def(&self) -> Tool {
17 tool_def(
18 "ctx_heatmap",
19 "File access heatmap — shows most frequently accessed files per session.\n\
20 action=status (default) for summary, action=detail for per-file access counts.\n\
21 Identify hot files to optimize context usage.",
22 json!({
23 "type": "object",
24 "properties": {
25 "action": { "type": "string", "description": "status|detail" },
26 "path": { "type": "string" }
27 }
28 }),
29 )
30 }
31
32 fn handle(
33 &self,
34 args: &Map<String, Value>,
35 ctx: &ToolContext,
36 ) -> Result<ToolOutput, ErrorData> {
37 let action = get_str(args, "action").unwrap_or_else(|| "status".to_string());
38 let path = jailed_path(args, ctx)?;
39 let result = crate::tools::ctx_heatmap::handle(&action, path.as_deref());
40 Ok(ToolOutput {
41 text: result,
42 original_tokens: 0,
43 saved_tokens: 0,
44 mode: Some(action),
45 path: None,
46 changed: false,
47 shell_outcome: None,
48 content_blocks: None,
49 })
50 }
51}
52
53fn jailed_path(args: &Map<String, Value>, ctx: &ToolContext) -> Result<Option<String>, ErrorData> {
57 if get_str(args, "path").is_none() {
58 return Ok(None);
59 }
60 if let Some(err) = ctx.path_error("path") {
61 return Err(ErrorData::invalid_params(format!("path: {err}"), None));
62 }
63 let resolved = ctx
64 .resolved_path("path")
65 .ok_or_else(|| ErrorData::invalid_params("path: not resolved by PathJail", None))?;
66 lean_ctx::core::workspace_authority::confine(resolved, &ctx.project_root, false)
68 .map(Some)
69 .map_err(|error| ErrorData::invalid_params(error, None))
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 fn args(path: &str) -> Map<String, Value> {
77 let mut map = Map::new();
78 map.insert("action".into(), json!("cold"));
79 map.insert("path".into(), json!(path));
80 map
81 }
82
83 #[test]
84 fn raw_path_without_a_jail_resolution_is_refused() {
85 let ctx = ToolContext::default();
86 assert!(jailed_path(&args("/etc"), &ctx).is_err());
87 }
88
89 #[test]
90 fn a_jail_refusal_is_returned_not_walked() {
91 let mut ctx = ToolContext::default();
92 ctx.path_errors
93 .insert("path".into(), "path escapes project root".into());
94 let err = jailed_path(&args("/etc"), &ctx).unwrap_err();
95 assert!(err.message.contains("escapes project root"), "{err:?}");
96 }
97
98 #[test]
99 fn the_resolved_path_is_used() {
100 let dir = tempfile::tempdir().unwrap();
101 let resolved = dir.path().to_string_lossy().to_string();
102 let mut ctx = ToolContext::default();
103 ctx.resolved_paths.insert("path".into(), resolved.clone());
104 assert_eq!(
105 jailed_path(&args("raw/other"), &ctx).unwrap().as_deref(),
106 Some(resolved.as_str())
107 );
108 assert_eq!(jailed_path(&Map::new(), &ctx).unwrap(), None);
109 }
110}