Skip to main content

lean_ctx/tools/registered/
ctx_context.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
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.",
19            json!({
20                "type": "object",
21                "properties": {}
22            }),
23        )
24    }
25
26    fn handle(
27        &self,
28        _args: &Map<String, Value>,
29        ctx: &ToolContext,
30    ) -> Result<ToolOutput, ErrorData> {
31        let cache = ctx.cache.as_ref().unwrap();
32        let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_context") else {
33            return Ok(ToolOutput::simple(
34                "[context status temporarily unavailable — retry]".to_string(),
35            ));
36        };
37        let turn = ctx
38            .call_count
39            .as_ref()
40            .map_or(0, |c| c.load(std::sync::atomic::Ordering::Relaxed));
41        let result = crate::tools::ctx_context::handle_status(&guard, turn, ctx.crp_mode);
42        Ok(ToolOutput::simple(result))
43    }
44}