Skip to main content

lean_ctx/tools/registered/
ctx_fill.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 CtxFillTool;
11
12impl McpTool for CtxFillTool {
13    fn name(&self) -> &'static str {
14        "ctx_fill"
15    }
16
17    fn tool_def(&self) -> Tool {
18        tool_def(
19            "ctx_fill",
20            "Budget-aware context fill — auto-selects compression per file within token limit.",
21            json!({
22                "type": "object",
23                "properties": {
24                    "paths": {
25                        "type": "array",
26                        "items": { "type": "string" },
27                        "description": "File paths to consider"
28                    },
29                    "budget": {
30                        "type": "integer",
31                        "description": "Maximum token budget to fill"
32                    },
33                    "task": {
34                        "type": "string",
35                        "description": "Optional task for POP intent-driven pruning"
36                    }
37                },
38                "required": ["paths", "budget"]
39            }),
40        )
41    }
42
43    fn handle(
44        &self,
45        args: &Map<String, Value>,
46        ctx: &ToolContext,
47    ) -> Result<ToolOutput, ErrorData> {
48        let raw_paths = get_str_array(args, "paths")
49            .ok_or_else(|| ErrorData::invalid_params("paths array is required", None))?;
50        let budget = get_int(args, "budget")
51            .ok_or_else(|| ErrorData::invalid_params("budget is required", None))?
52            as usize;
53        let task = get_str(args, "task");
54
55        tokio::task::block_in_place(|| {
56            let session_lock = ctx
57                .session
58                .as_ref()
59                .ok_or_else(|| ErrorData::internal_error("session not available", None))?;
60            let cache_lock = ctx
61                .cache
62                .as_ref()
63                .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
64
65            let mut paths = Vec::with_capacity(raw_paths.len());
66            {
67                let session = session_lock.blocking_read();
68                for p in &raw_paths {
69                    match super::resolve_path_sync(&session, p) {
70                        Ok(resolved) => paths.push(resolved),
71                        Err(e) => {
72                            return Err(ErrorData::invalid_params(e, None));
73                        }
74                    }
75                }
76            }
77
78            let mut cache = cache_lock.blocking_write();
79            let output = crate::tools::ctx_fill::handle(
80                &mut cache,
81                &paths,
82                budget,
83                ctx.crp_mode,
84                task.as_deref(),
85            );
86            drop(cache);
87
88            Ok(ToolOutput {
89                text: output,
90                original_tokens: 0,
91                saved_tokens: 0,
92                mode: Some(format!("budget:{budget}")),
93                path: None,
94            })
95        })
96    }
97}