Skip to main content

lean_ctx/tools/registered/
ctx_overview.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
6use crate::tool_defs::tool_def;
7
8pub struct CtxOverviewTool;
9
10impl McpTool for CtxOverviewTool {
11    fn name(&self) -> &'static str {
12        "ctx_overview"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_overview",
18            "WORKFLOW: call at session START before ctx_compose/ctx_read.\n\
19            ANTIPATTERN: NOT for source code — structure only. Use ctx_compose for code understanding.\n\
20            Project map — task='your goal' scopes files by relevance (PageRank on symbol graph) and \
21            appends a task briefing plus up to 5 task-relevant knowledge facts. With \
22            enable_wakeup_ctx (default on) a WAKE-UP BRIEFING follows: last session task, recent \
23            decisions, reminders, active peer agents on this project. behavior_nudges=\"off\" \
24            drops the OUTPUT-HINT line.\n\
25            High-level structure only, no source body. ~10x cheaper than ctx_compose.",
26            json!({
27                "type": "object",
28                "properties": {
29                    "task": {
30                        "type": "string",
31                        "description": "Short English task for relevance scoring"
32                    },
33                    "path": {
34                        "type": "string",
35                        "description": "Project root"
36                    }
37                }
38            }),
39        )
40    }
41
42    fn handle(
43        &self,
44        args: &Map<String, Value>,
45        ctx: &ToolContext,
46    ) -> Result<ToolOutput, ErrorData> {
47        let task = get_str(args, "task");
48
49        let resolved_path = if get_str(args, "path").is_some() {
50            if let Some(p) = ctx.resolved_path("path") {
51                Some(p.to_string())
52            } else if let Some(err) = ctx.path_error("path") {
53                return Err(ErrorData::invalid_params(format!("path: {err}"), None));
54            } else {
55                None
56            }
57        } else if let Some(ref session) = ctx.session {
58            let guard = crate::server::bounded_lock::read(session, "ctx_overview:session");
59            guard.as_ref().and_then(|g| g.project_root.clone())
60        } else {
61            None
62        };
63
64        let cache = ctx
65            .cache
66            .as_ref()
67            .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
68        let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_overview:cache") else {
69            return Ok(ToolOutput::simple(
70                "[overview temporarily unavailable — cache busy]".to_string(),
71            ));
72        };
73        let (result, _complete) = crate::tools::ctx_overview::handle(
74            &guard,
75            task.as_deref(),
76            resolved_path.as_deref(),
77            ctx.crp_mode,
78        );
79
80        Ok(ToolOutput {
81            text: result,
82            original_tokens: 0,
83            saved_tokens: 0,
84            mode: Some("overview".to_string()),
85            path: None,
86            changed: false,
87            shell_outcome: None,
88            content_blocks: None,
89        })
90    }
91}