lean_ctx/tool_defs/
mod.rs1use std::sync::Arc;
2
3use rmcp::model::Tool;
4use serde_json::{Map, Value};
5
6mod granular;
7pub use granular::{granular_tool_defs, unified_tool_defs};
8
9pub fn tool_def(name: &'static str, description: &'static str, schema_value: Value) -> Tool {
10 let mut schema: Map<String, Value> = match schema_value {
11 Value::Object(map) => map,
12 _ => Map::new(),
13 };
14 normalize_for_strict_validators(&mut schema);
15 Tool::new(name, description, Arc::new(schema))
16}
17
18pub fn normalize_for_strict_validators(schema: &mut Map<String, Value>) {
36 let is_object = schema.get("type").and_then(Value::as_str) == Some("object");
37 let is_array = schema.get("type").and_then(Value::as_str) == Some("array");
38
39 if is_object && schema.contains_key("properties") && !schema.contains_key("required") {
40 schema.insert("required".into(), Value::Array(Vec::new()));
41 }
42 if is_array && !schema.contains_key("items") {
43 schema.insert("items".into(), Value::Object(Map::new()));
44 }
45
46 if let Some(Value::Object(props)) = schema.get_mut("properties") {
47 for prop in props.values_mut() {
48 if let Value::Object(p) = prop {
49 normalize_for_strict_validators(p);
50 }
51 }
52 }
53 if let Some(Value::Object(items)) = schema.get_mut("items") {
54 normalize_for_strict_validators(items);
55 }
56 if let Some(Value::Object(ap)) = schema.get_mut("additionalProperties") {
57 normalize_for_strict_validators(ap);
58 }
59 for combinator in ["anyOf", "oneOf", "allOf"] {
60 if let Some(Value::Array(branches)) = schema.get_mut(combinator) {
61 for branch in branches.iter_mut() {
62 if let Value::Object(b) = branch {
63 normalize_for_strict_validators(b);
64 }
65 }
66 }
67 }
68}
69
70pub const CORE_TOOL_NAMES: &[&str] = &[
71 "ctx_read",
72 "ctx_shell",
73 "shell",
74 "ctx_search",
77 "ctx_glob",
78 "ctx_tree",
79 "ctx_session",
80 "ctx_compose",
81 "ctx_callgraph",
86 "ctx_patch",
93 "ctx_call",
94 "ctx_expand",
95];
96
97pub fn core_tool_names() -> &'static [&'static str] {
98 CORE_TOOL_NAMES
99}
100
101pub fn lazy_tool_defs() -> Vec<Tool> {
102 let all = granular_tool_defs();
103 all.into_iter()
104 .filter(|t| CORE_TOOL_NAMES.contains(&t.name.as_ref()))
105 .collect()
106}
107
108pub fn discover_tools(query: &str) -> String {
109 let all = crate::server::registry::build_registry().tool_defs();
112 let query_lower = query.to_lowercase();
113 let matches: Vec<(String, String)> = all
114 .iter()
115 .filter_map(|t| {
116 let name = t.name.as_ref();
117 let desc = t.description.as_deref().unwrap_or("");
118 if name.to_lowercase().contains(&query_lower)
119 || desc.to_lowercase().contains(&query_lower)
120 {
121 Some((name.to_string(), desc.to_string()))
122 } else {
123 None
124 }
125 })
126 .collect();
127
128 if matches.is_empty() {
129 return format!(
130 "No tools found matching '{query}'. Try broader terms like: graph, cost, session, search, compress, agent, workflow, gain."
131 );
132 }
133
134 let mut out = format!("{} tools matching '{query}':\n", matches.len());
135 for (name, desc) in &matches {
136 let first = desc.lines().next().unwrap_or(desc);
138 let short = if first.len() > 80 {
139 &first[..first.floor_char_boundary(80)]
140 } else {
141 first
142 };
143 out.push_str(&format!(" {name} — {short}\n"));
144 }
145 out.push_str(
146 "\nIf your MCP client registers tools only once at startup (static tools/list), \
147use ctx_call (available in lazy mode) to invoke discovered tools:\n\
148 ctx_call {\"name\":\"ctx_graph\",\"arguments\":{\"action\":\"status\"}}\n",
149 );
150 out
151}
152
153pub fn is_full_mode() -> bool {
154 std::env::var("LEAN_CTX_FULL_TOOLS").is_ok_and(|v| v != "0" && !v.eq_ignore_ascii_case("false"))
155 || std::env::var("LEAN_CTX_LAZY_TOOLS")
156 .is_ok_and(|v| v == "0" || v.eq_ignore_ascii_case("false"))
157}