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",
75 "ctx_semantic_search",
76 "ctx_glob",
77 "ctx_tree",
78 "ctx_session",
79 "ctx_compose",
80 "ctx_graph",
81 "ctx_call",
82 "ctx_expand",
83];
84
85pub fn core_tool_names() -> &'static [&'static str] {
86 CORE_TOOL_NAMES
87}
88
89pub fn lazy_tool_defs() -> Vec<Tool> {
90 let all = granular_tool_defs();
91 all.into_iter()
92 .filter(|t| CORE_TOOL_NAMES.contains(&t.name.as_ref()))
93 .collect()
94}
95
96pub fn discover_tools(query: &str) -> String {
97 let all = crate::server::registry::build_registry().tool_defs();
100 let query_lower = query.to_lowercase();
101 let matches: Vec<(String, String)> = all
102 .iter()
103 .filter_map(|t| {
104 let name = t.name.as_ref();
105 let desc = t.description.as_deref().unwrap_or("");
106 if name.to_lowercase().contains(&query_lower)
107 || desc.to_lowercase().contains(&query_lower)
108 {
109 Some((name.to_string(), desc.to_string()))
110 } else {
111 None
112 }
113 })
114 .collect();
115
116 if matches.is_empty() {
117 return format!(
118 "No tools found matching '{query}'. Try broader terms like: graph, cost, session, search, compress, agent, workflow, gain."
119 );
120 }
121
122 let mut out = format!("{} tools matching '{query}':\n", matches.len());
123 for (name, desc) in &matches {
124 let first = desc.lines().next().unwrap_or(desc);
126 let short = if first.len() > 80 {
127 &first[..first.floor_char_boundary(80)]
128 } else {
129 first
130 };
131 out.push_str(&format!(" {name} — {short}\n"));
132 }
133 out.push_str(
134 "\nIf your MCP client registers tools only once at startup (static tools/list), \
135use ctx_call (available in lazy mode) to invoke discovered tools:\n\
136 ctx_call {\"name\":\"ctx_graph\",\"arguments\":{\"action\":\"status\"}}\n",
137 );
138 out
139}
140
141pub fn is_full_mode() -> bool {
142 std::env::var("LEAN_CTX_FULL_TOOLS").is_ok_and(|v| v != "0" && !v.eq_ignore_ascii_case("false"))
143 || std::env::var("LEAN_CTX_LAZY_TOOLS")
144 .is_ok_and(|v| v == "0" || v.eq_ignore_ascii_case("false"))
145}