lean_ctx_server/server/
mod.rs1pub(crate) mod activity;
3pub mod background_shell;
4pub use lean_ctx::core::bounded_lock;
5pub use lean_ctx_ext::core::bypass_hint;
6mod call_identity;
7#[cfg(test)]
8mod catalog_tests;
9#[doc(hidden)]
10pub mod client_sessions;
11pub mod compaction_sync;
12pub mod context_gate;
13#[cfg(test)]
14mod decision_loop_integration_tests;
15mod dispatch;
16pub use dispatch::canonicalize_arg_aliases;
17pub mod dynamic_tools;
18pub mod elicitation;
19pub(crate) mod execute;
20#[cfg(unix)]
21pub use execute::register_protected_shell_runner;
22mod file_resource;
23pub mod helpers;
24#[cfg(any(test, feature = "test-support"))]
25pub mod isolation_fixture;
26pub mod mcp_idle;
27pub mod multi_path;
28pub mod native_receipts;
29pub mod notifications;
30pub mod permission_inheritance;
31pub mod policy_guard;
32pub mod predictive_preload;
33pub mod progress;
34pub mod prompts;
35pub use lean_ctx::core::reference_store;
36pub mod registry;
37pub mod resources;
38pub mod role_guard;
39pub use lean_ctx::core::mcp_roots as roots;
40pub mod schema_hook;
41pub(crate) mod shell_approval;
42pub(crate) mod shell_tasks;
43#[cfg(all(test, unix))]
44mod shell_tasks_tests;
45pub use lean_ctx::core::mcp_roots::{derive_project_root_from, derive_project_root_from_cwd};
46use lean_ctx::core::pathutil::has_project_marker;
47pub mod tool_trait;
48pub mod tool_visibility;
49pub mod tools_config_watch;
50pub(crate) mod walk_hint;
51#[cfg(all(test, unix))]
53mod workspace_roots_tests;
54
55use futures::FutureExt;
56use rmcp::ErrorData;
57use rmcp::handler::server::ServerHandler;
58use rmcp::model::{
59 CallToolRequestParams, CallToolResult, ContentBlock, Implementation, InitializeRequestParams,
60 InitializeResult, ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig,
61};
62use rmcp::service::{RequestContext, RoleServer};
63
64use crate::tools::{CrpMode, LeanCtxServer};
65mod call_tool;
66pub(crate) use call_tool::SessionSwitch;
67mod post_dispatch;
68mod post_process;
69mod server_handler;
70
71pub fn build_instructions_for_test(crp_mode: CrpMode) -> String {
72 lean_ctx::instructions::build_instructions_for_test(crp_mode)
73}
74
75pub fn build_claude_code_instructions_for_test() -> String {
76 lean_ctx::instructions::claude_code_instructions()
77}
78
79pub fn build_claude_code_static_instructions_for_test() -> String {
82 lean_ctx::instructions::claude_code_static_instructions_for_test()
83}
84
85#[cfg(test)]
87use lean_ctx::core::pathutil::is_broad_or_unsafe_root;
88
89pub fn tool_descriptions_for_test() -> Vec<(String, String)> {
90 crate::server::registry::build_registry()
91 .tool_defs()
92 .into_iter()
93 .map(|t| {
94 (
95 t.name.to_string(),
96 t.description.as_deref().unwrap_or("").to_string(),
97 )
98 })
99 .collect()
100}
101
102pub fn tool_schemas_json_for_test() -> String {
103 crate::server::registry::build_registry()
104 .tool_defs()
105 .iter()
106 .map(|t| {
107 format!(
108 "{}: {}",
109 t.name,
110 serde_json::to_string(&t.input_schema).unwrap_or_default()
111 )
112 })
113 .collect::<Vec<_>>()
114 .join("\n")
115}
116
117pub use lean_ctx::core::workflow::WORKFLOW_PASSTHROUGH_TOOLS;
118
119pub fn is_workflow_stale(run: &lean_ctx::core::workflow::types::WorkflowRun) -> bool {
125 lean_ctx::core::workflow::is_run_stale(run)
126}
127
128fn is_client_hook_covered(client_name: &str) -> bool {
132 lean_ctx::core::home::resolve_home_dir()
133 .is_some_and(|home| lean_ctx::core::rules_channel::client_hook_covered(client_name, &home))
134}
135
136fn is_shell_tool_name(name: &str) -> bool {
137 matches!(name, "ctx_shell" | "ctx_execute")
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn home_dir_detected_as_broad_root() {
146 let _env = lean_ctx::core::data_dir::test_env_lock();
149 if let Some(home) = dirs::home_dir() {
150 assert!(is_broad_or_unsafe_root(&home));
151 }
152 }
153
154 #[test]
155 fn agent_dirs_detected() {
156 let claude = std::path::PathBuf::from("/home/user/.claude");
157 assert!(is_broad_or_unsafe_root(&claude));
158 let codex = std::path::PathBuf::from("/home/user/.codex");
159 assert!(is_broad_or_unsafe_root(&codex));
160 let project = std::path::PathBuf::from("/home/user/projects/myapp");
161 assert!(!is_broad_or_unsafe_root(&project));
162 }
163
164 #[test]
165 fn test_unified_tool_count() {
166 let tools = lean_ctx::tool_defs::unified_tool_defs();
167 assert_eq!(tools.len(), 5, "Expected 5 unified tools");
168 }
169
170 #[test]
171 fn test_granular_tool_count() {
172 registry::register_tool_catalog();
173 let tools = lean_ctx::tool_defs::granular_tool_defs();
174 assert!(tools.len() >= 25, "Expected at least 25 granular tools");
175 }
176
177 #[test]
178 fn production_server_always_has_registry() {
179 let server = crate::tools::create_server();
185 assert!(
186 server.registry.is_some(),
187 "production server must carry a tool registry"
188 );
189 }
190
191 #[test]
192 fn disabled_tools_filters_list() {
193 registry::register_tool_catalog();
194 let all = lean_ctx::tool_defs::granular_tool_defs();
195 let total = all.len();
196 let disabled = ["ctx_graph".to_string()];
201 let filtered: Vec<_> = all
202 .into_iter()
203 .filter(|t| !disabled.iter().any(|d| t.name.as_ref() == d.as_str()))
204 .collect();
205 assert_eq!(filtered.len(), total - 1);
206 assert!(!filtered.iter().any(|t| t.name.as_ref() == "ctx_graph"));
207 assert!(!filtered.iter().any(|t| t.name.as_ref() == "ctx_agent"));
208 }
209
210 #[test]
211 fn empty_disabled_tools_returns_all() {
212 let all = lean_ctx::tool_defs::granular_tool_defs();
213 let total = all.len();
214 let disabled: Vec<String> = vec![];
215 let filtered: Vec<_> = all
216 .into_iter()
217 .filter(|t| !disabled.iter().any(|d| t.name.as_ref() == d.as_str()))
218 .collect();
219 assert_eq!(filtered.len(), total);
220 }
221
222 #[test]
223 fn misspelled_disabled_tool_is_silently_ignored() {
224 let all = lean_ctx::tool_defs::granular_tool_defs();
225 let total = all.len();
226 let disabled = ["ctx_nonexistent_tool".to_string()];
227 let filtered: Vec<_> = all
228 .into_iter()
229 .filter(|t| !disabled.iter().any(|d| t.name.as_ref() == d.as_str()))
230 .collect();
231 assert_eq!(filtered.len(), total);
232 }
233
234 #[test]
235 fn is_broad_or_unsafe_root_rejects_home() {
236 let _env = lean_ctx::core::data_dir::test_env_lock();
239 if let Some(home) = dirs::home_dir() {
240 assert!(is_broad_or_unsafe_root(&home));
241 }
242 }
243
244 #[test]
245 fn is_broad_or_unsafe_root_rejects_filesystem_root() {
246 assert!(is_broad_or_unsafe_root(std::path::Path::new("/")));
247 }
248
249 #[test]
250 fn is_broad_or_unsafe_root_rejects_agent_dirs() {
251 assert!(is_broad_or_unsafe_root(std::path::Path::new(
252 "/home/user/.claude"
253 )));
254 assert!(is_broad_or_unsafe_root(std::path::Path::new(
255 "/home/user/.codex"
256 )));
257 }
258
259 #[test]
260 fn is_broad_or_unsafe_root_allows_project_subdir() {
261 let tmp = tempfile::tempdir().unwrap();
262 let subdir = tmp.path().join("my-project");
263 std::fs::create_dir_all(&subdir).unwrap();
264 assert!(!is_broad_or_unsafe_root(&subdir));
265 }
266
267 #[test]
268 fn is_broad_or_unsafe_root_allows_tmp_subdirs() {
269 assert!(!is_broad_or_unsafe_root(std::path::Path::new(
270 "/tmp/leanctx-test"
271 )));
272 assert!(!is_broad_or_unsafe_root(std::path::Path::new(
273 "/tmp/my-project"
274 )));
275 }
276
277 #[test]
278 fn is_broad_or_unsafe_root_allows_home_subdirs() {
279 let _env = lean_ctx::core::data_dir::test_env_lock();
282 if let Some(home) = dirs::home_dir() {
283 let subdir = home.join("projects").join("my-app");
284 assert!(!is_broad_or_unsafe_root(&subdir));
285 }
286 }
287}