Skip to main content

lean_ctx/tools/registered/
ctx_cost.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_int, get_str, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxCostTool;
9
10impl McpTool for CtxCostTool {
11    fn name(&self) -> &'static str {
12        "ctx_cost"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_cost",
18            "Cost attribution (local-first). Actions: report|agent|tools|json|reset.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "action": {
23                        "type": "string",
24                        "enum": ["report", "agent", "tools", "json", "reset", "status"],
25                        "description": "Operation to perform (default: report)"
26                    },
27                    "agent_id": {
28                        "type": "string",
29                        "description": "Agent ID for action=agent (optional)"
30                    },
31                    "limit": {
32                        "type": "integer",
33                        "description": "Max rows (default: 10)"
34                    }
35                }
36            }),
37        )
38    }
39
40    fn handle(
41        &self,
42        args: &Map<String, Value>,
43        _ctx: &ToolContext,
44    ) -> Result<ToolOutput, ErrorData> {
45        let action = get_str(args, "action").unwrap_or_else(|| "report".to_string());
46        let agent_id = get_str(args, "agent_id");
47        let limit = get_int(args, "limit").map(|n| n as usize);
48
49        let result = crate::tools::ctx_cost::handle(&action, agent_id.as_deref(), limit);
50
51        Ok(ToolOutput {
52            text: result,
53            original_tokens: 0,
54            saved_tokens: 0,
55            mode: Some(action),
56            path: None,
57            changed: false,
58        })
59    }
60}