Skip to main content

lean_ctx/tools/registered/
ctx_context.rs

1use 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 CtxContextTool;
9
10impl McpTool for CtxContextTool {
11    fn name(&self) -> &'static str {
12        "ctx_context"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_context",
18            "Session context overview — cached files, seen files, session state, CRP mode.\n\
19            WORKFLOW: track context budget periodically — use before ctx_compress/ctx_compile.\n\
20            ANTIPATTERN: not for reading file content — use ctx_read or ctx_compose.",
21            json!({
22                "type": "object",
23                "properties": {}
24            }),
25        )
26    }
27
28    fn handle(
29        &self,
30        _args: &Map<String, Value>,
31        ctx: &ToolContext,
32    ) -> Result<ToolOutput, ErrorData> {
33        let cache = ctx
34            .cache
35            .as_ref()
36            .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
37        let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_context") else {
38            return Ok(ToolOutput::simple(
39                "[context status temporarily unavailable — retry]".to_string(),
40            ));
41        };
42        let turn = ctx
43            .call_count
44            .as_ref()
45            .map_or(0, |c| c.load(std::sync::atomic::Ordering::Relaxed));
46        let result = crate::tools::ctx_context::handle_status(&guard, turn, ctx.crp_mode);
47        Ok(ToolOutput::simple(result))
48    }
49}