lean_ctx/tools/registered/
ctx_cost.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str, get_usize};
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 — track tokens and cost per agent/tool call. Local-first, no external billing.\n\
19 Actions: report (summary), agent (per-agent), tools (per-tool), json (machine), status (live), reset (zero).\n\
20 WORKFLOW: call report to find top cost drivers, then agent/tools for detail.",
21 json!({
22 "type": "object",
23 "properties": {
24 "action": {
25 "type": "string",
26 "enum": ["report", "agent", "tools", "json", "reset", "status"],
27 "description": "report|agent|tools|json|reset|status"
28 },
29 "agent_id": {
30 "type": "string",
31 "description": "Agent ID"
32 },
33 "limit": {
34 "type": "integer",
35 "description": "Max rows"
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 action = get_str(args, "action").unwrap_or_else(|| "report".to_string());
48 let agent_id = get_str(args, "agent_id");
49 let limit = get_usize(args, "limit").map(|n| n.min(100_000));
50
51 let result = crate::tools::ctx_cost::handle(&action, agent_id.as_deref(), limit);
52
53 Ok(ToolOutput {
54 text: result,
55 original_tokens: 0,
56 saved_tokens: 0,
57 mode: Some(action),
58 path: None,
59 changed: false,
60 shell_outcome: None,
61 })
62 }
63}