Skip to main content

lean_ctx/tools/registered/
ctx_prefetch.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{
6    get_int, get_str, get_str_array, McpTool, ToolContext, ToolOutput,
7};
8use crate::tool_defs::tool_def;
9
10pub struct CtxPrefetchTool;
11
12impl McpTool for CtxPrefetchTool {
13    fn name(&self) -> &'static str {
14        "ctx_prefetch"
15    }
16
17    fn tool_def(&self) -> Tool {
18        tool_def(
19            "ctx_prefetch",
20            "Predictive prefetch — prewarm cache for blast radius files (graph + task signals) within budgets.",
21            json!({
22                "type": "object",
23                "properties": {
24                    "root": { "type": "string", "description": "Project root (default: .)" },
25                    "task": { "type": "string", "description": "Optional task for relevance scoring" },
26                    "changed_files": { "type": "array", "items": { "type": "string" }, "description": "Optional changed files (paths) to compute blast radius" },
27                    "budget_tokens": { "type": "integer", "description": "Soft budget hint for mode selection (default: 3000)" },
28                    "max_files": { "type": "integer", "description": "Max files to prefetch (default: 10)" }
29                }
30            }),
31        )
32    }
33
34    fn handle(
35        &self,
36        args: &Map<String, Value>,
37        ctx: &ToolContext,
38    ) -> Result<ToolOutput, ErrorData> {
39        let root = if get_str(args, "root").is_some() {
40            ctx.resolved_path("root")
41                .unwrap_or(&ctx.project_root)
42                .to_string()
43        } else if let Some(ref session) = ctx.session {
44            let guard = tokio::task::block_in_place(|| session.blocking_read());
45            guard
46                .project_root
47                .clone()
48                .unwrap_or_else(|| ".".to_string())
49        } else {
50            ".".to_string()
51        };
52
53        let task = get_str(args, "task");
54        let changed_files = get_str_array(args, "changed_files");
55        let budget_tokens = get_int(args, "budget_tokens").map_or(3000, |n| n.max(0) as usize);
56        let max_files = get_int(args, "max_files").map(|n| n.max(1) as usize);
57
58        let resolved_changed: Option<Vec<String>> = changed_files.map(|files| {
59            files
60                .iter()
61                .map(|p| ctx.resolve_path_sync(p).unwrap_or_else(|_| p.clone()))
62                .collect()
63        });
64
65        let cache = ctx.cache.as_ref().unwrap();
66        let mut guard = tokio::task::block_in_place(|| cache.blocking_write());
67        let result = crate::tools::ctx_prefetch::handle(
68            &mut guard,
69            &root,
70            task.as_deref(),
71            resolved_changed.as_deref(),
72            budget_tokens,
73            max_files,
74            ctx.crp_mode,
75        );
76
77        Ok(ToolOutput {
78            text: result,
79            original_tokens: 0,
80            saved_tokens: 0,
81            mode: Some("prefetch".to_string()),
82            path: None,
83        })
84    }
85}