Skip to main content

lean_ctx/server/
server_handler.rs

1//! `rmcp::ServerHandler` trait implementation for [`LeanCtxServer`].
2//!
3//! Split out of `server/mod.rs`; `use super::*` re-imports the parent module’s
4//! aliases and sibling submodules. Methods attach to `LeanCtxServer` regardless
5//! of which module the impl block lives in.
6
7#[allow(clippy::wildcard_imports)]
8use super::*;
9
10impl ServerHandler for LeanCtxServer {
11    fn get_info(&self) -> ServerInfo {
12        let capabilities = ServerCapabilities::builder()
13            .enable_tools()
14            .enable_resources()
15            .enable_resources_subscribe()
16            .enable_prompts()
17            .build();
18
19        let config = crate::core::config::Config::load();
20        let level = crate::core::config::CompressionLevel::effective(&config);
21        let _ = crate::core::terse::rules_inject::inject(&level);
22
23        let instructions = crate::instructions::build_instructions(CrpMode::effective());
24
25        InitializeResult::new(capabilities)
26            .with_server_info(Implementation::new("lean-ctx", env!("CARGO_PKG_VERSION")))
27            .with_instructions(instructions)
28    }
29
30    async fn initialize(
31        &self,
32        request: InitializeRequestParams,
33        context: RequestContext<RoleServer>,
34    ) -> Result<InitializeResult, ErrorData> {
35        let name = request.client_info.name.clone();
36        tracing::info!("MCP client connected: {:?}", name);
37        *self.client_name.write().await = name.clone();
38        *self.peer.write().await = Some(context.peer.clone());
39
40        if self.session_mode != crate::tools::SessionMode::Shared {
41            crate::core::budget_tracker::BudgetTracker::global().reset();
42            if let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() {
43                let radar = data_dir.join("context_radar.jsonl");
44                if radar.exists() {
45                    let prev = data_dir.join("context_radar.prev.jsonl");
46                    let _ = std::fs::rename(&radar, &prev);
47                }
48            }
49        }
50
51        let has_roots = request.capabilities.roots.is_some();
52        self.has_client_roots
53            .store(has_roots, std::sync::atomic::Ordering::Relaxed);
54        if has_roots {
55            tracing::info!("Client supports MCP roots/list — will resolve on first tool call");
56        }
57
58        let env_root = roots::root_from_env();
59        let derived_root = derive_project_root_from_cwd();
60        let effective_root = env_root.or(derived_root);
61
62        let cwd_str = std::env::current_dir()
63            .ok()
64            .map(|p| p.to_string_lossy().to_string())
65            .unwrap_or_default();
66        {
67            let mut session = self.session.write().await;
68            if !cwd_str.is_empty() {
69                session.shell_cwd = Some(cwd_str.clone());
70            }
71            if let Some(ref root) = effective_root {
72                session.project_root = Some(root.clone());
73                tracing::info!("Project root set to: {root}");
74            } else if let Some(ref root) = session.project_root {
75                // A previously persisted session may carry a contaminated root
76                // (e.g. HOME from an older build or a client that reported HOME
77                // as its workspace). Drop it unless it is a real, safe project
78                // dir — otherwise PROJECT MEMORY leaks across projects.
79                let root_path = std::path::Path::new(root);
80                let root_has_marker = has_project_marker(root_path);
81                let root_str = root_path.to_string_lossy();
82                let root_suspicious = crate::core::pathutil::is_broad_or_unsafe_root(root_path)
83                    || root_str.contains("/var/folders/")
84                    || root_str.contains("/tmp/")
85                    || root_str.contains("\\AppData\\Local\\Temp")
86                    || root_str.contains("\\Temp\\");
87                if root_suspicious && !root_has_marker {
88                    tracing::info!("Dropping suspicious persisted project root: {root}");
89                    session.project_root = None;
90                }
91            }
92            let cfg_extra = crate::core::config::Config::load().extra_roots;
93            if !cfg_extra.is_empty() {
94                let existing: std::collections::HashSet<_> =
95                    session.extra_roots.iter().cloned().collect();
96                for r in cfg_extra {
97                    if !existing.contains(&r) {
98                        session.extra_roots.push(r);
99                    }
100                }
101            }
102            if self.session_mode == crate::tools::SessionMode::Shared {
103                if let Some(ref root) = session.project_root {
104                    if let Some(ref rt) = self.context_os {
105                        rt.shared_sessions.persist_best_effort(
106                            root,
107                            &self.workspace_id,
108                            &self.channel_id,
109                            &session,
110                        );
111                        rt.metrics.record_session_persisted();
112                    }
113                }
114            } else if let Err(e) = session.save() {
115                tracing::warn!("lean-ctx: failed to persist session state: {e}");
116            }
117        }
118
119        // Indices are warmed lazily on first use of a tool that needs them
120        // (issue #152), not eagerly here — a session that only uses
121        // ctx_read/ctx_shell/ctx_tree must not pay a full graph + BM25 scan.
122        // See `index_orchestrator::ensure_warm_for_tool`, driven from dispatch.
123
124        let agent_name = name.clone();
125        let agent_root = effective_root.clone().unwrap_or_default();
126        let agent_id_handle = self.agent_id.clone();
127        tokio::task::spawn_blocking(move || {
128            if std::env::var("LEAN_CTX_HEADLESS").is_ok() {
129                return;
130            }
131
132            // Avoid startup stampedes when multiple agent sessions initialize at once.
133            // These are best-effort maintenance tasks; it's fine to skip if another
134            // lean-ctx instance is already doing them.
135            let maintenance = crate::core::startup_guard::try_acquire_lock(
136                "startup-maintenance",
137                std::time::Duration::from_secs(2),
138                std::time::Duration::from_mins(2),
139            );
140            if maintenance.is_some() {
141                if let Some(home) = dirs::home_dir() {
142                    let _ = crate::rules_inject::inject_all_rules(&home);
143                }
144                crate::hooks::refresh_installed_hooks();
145                crate::core::version_check::check_background();
146                // Enforce the on-disk budget: prune accumulated quarantined BM25
147                // indexes and cap the archive FTS DB (#2364). Silent (tracing
148                // only) so it never corrupts the MCP stdio protocol.
149                let _ = crate::core::storage_maintenance::run_quiet();
150            }
151            drop(maintenance);
152
153            if !agent_root.is_empty() {
154                let heuristic_role = match agent_name.to_lowercase().as_str() {
155                    n if n.contains("cursor") => Some("coder"),
156                    n if n.contains("claude") => Some("coder"),
157                    n if n.contains("codex") => Some("coder"),
158                    n if n.contains("antigravity") || n.contains("gemini") => Some("coder"),
159                    n if n.contains("review") => Some("reviewer"),
160                    n if n.contains("test") => Some("debugger"),
161                    _ => None,
162                };
163                let env_role = std::env::var("LEAN_CTX_ROLE")
164                    .or_else(|_| std::env::var("LEAN_CTX_AGENT_ROLE"))
165                    .ok();
166                let effective_role = env_role.as_deref().or(heuristic_role).unwrap_or("coder");
167
168                let _ = crate::core::roles::set_active_role_with_source(effective_role, true);
169
170                let mut registry = crate::core::agents::AgentRegistry::load_or_create();
171                registry.cleanup_stale(24);
172                let id = registry.register("mcp", Some(effective_role), &agent_root);
173                let _ = registry.save();
174                if let Ok(mut guard) = agent_id_handle.try_write() {
175                    *guard = Some(id);
176                }
177            }
178        });
179
180        let client_caps = crate::core::client_capabilities::ClientMcpCapabilities::detect(&name);
181        tracing::info!("Client capabilities: {}", client_caps.format_summary());
182
183        {
184            let cfg = crate::core::config::Config::load();
185            let cats = cfg.default_tool_categories_effective();
186            dynamic_tools::init_from_config(&cats);
187        }
188
189        if client_caps.dynamic_tools {
190            if let Ok(mut dt) = dynamic_tools::global().lock() {
191                dt.set_supports_list_changed(true);
192            }
193        }
194        if let Some(max) = client_caps.max_tools {
195            if let Ok(mut dt) = dynamic_tools::global().lock() {
196                dt.set_supports_list_changed(true);
197                if max < 100 {
198                    dt.unload_category(dynamic_tools::ToolCategory::Debug);
199                    dt.unload_category(dynamic_tools::ToolCategory::Memory);
200                }
201            }
202        }
203
204        crate::core::client_capabilities::set_detected(&client_caps);
205
206        let instructions =
207            crate::instructions::build_instructions_with_client(CrpMode::effective(), &name);
208
209        let capabilities = match (client_caps.resources, client_caps.prompts) {
210            (true, true) => ServerCapabilities::builder()
211                .enable_tools()
212                .enable_resources()
213                .enable_resources_subscribe()
214                .enable_prompts()
215                .build(),
216            (true, false) => ServerCapabilities::builder()
217                .enable_tools()
218                .enable_resources()
219                .enable_resources_subscribe()
220                .build(),
221            (false, true) => ServerCapabilities::builder()
222                .enable_tools()
223                .enable_prompts()
224                .build(),
225            (false, false) => ServerCapabilities::builder().enable_tools().build(),
226        };
227
228        Ok(InitializeResult::new(capabilities)
229            .with_server_info(Implementation::new("lean-ctx", env!("CARGO_PKG_VERSION")))
230            .with_instructions(instructions))
231    }
232
233    async fn list_tools(
234        &self,
235        _request: Option<PaginatedRequestParams>,
236        _context: RequestContext<RoleServer>,
237    ) -> Result<ListToolsResult, ErrorData> {
238        let cfg = crate::core::config::Config::load();
239        let disabled = cfg.disabled_tools_effective();
240        let tool_profile = cfg.tool_profile_effective();
241        // A profile is "explicit" when the user opted into one (config field,
242        // env var, or a custom tools list). Without an explicit choice we keep
243        // the token-lean lazy core set as the default. With one, the profile is
244        // authoritative and resolves against the full registry, so e.g.
245        // `standard` advertises its full balanced set instead of the accidental
246        // `core ∩ standard` intersection.
247        let explicit_profile = cfg.tool_profile.is_some()
248            || !cfg.tools_enabled.is_empty()
249            || std::env::var("LEAN_CTX_TOOL_PROFILE").is_ok();
250
251        let all_tools = if crate::tool_defs::is_full_mode() {
252            if let Some(ref reg) = self.registry {
253                reg.tool_defs()
254            } else {
255                crate::tool_defs::granular_tool_defs()
256            }
257        } else if std::env::var("LEAN_CTX_UNIFIED").is_ok() {
258            crate::tool_defs::unified_tool_defs()
259        } else if let Some(ref reg) = self.registry {
260            if explicit_profile {
261                reg.tool_defs()
262            } else {
263                let core_names = crate::tool_defs::core_tool_names();
264                reg.tool_defs()
265                    .into_iter()
266                    .filter(|t| core_names.contains(&t.name.as_ref()))
267                    .collect()
268            }
269        } else {
270            crate::tool_defs::lazy_tool_defs()
271        };
272        let client = self.client_name.read().await.clone();
273        let is_zed = !client.is_empty() && client.to_lowercase().contains("zed");
274
275        let active_role = crate::core::roles::active_role();
276        let tools: Vec<_> = all_tools
277            .into_iter()
278            .filter(|t| {
279                let name = t.name.as_ref();
280                crate::server::tool_visibility::is_tool_visible(
281                    name,
282                    &tool_profile,
283                    &disabled,
284                    is_zed,
285                    active_role.is_tool_allowed(name),
286                )
287            })
288            .collect();
289
290        // Guarantee the universal invoker is advertised in non-full mode. Lazy
291        // and profile filtering hide most tools; without ctx_call a static-list
292        // client (one that only calls advertised tools) could not reach them.
293        // ctx_call enforces the same role/workflow gates on the inner tool.
294        let tools = {
295            let mut tools = tools;
296            use crate::server::tool_visibility::INVOKER;
297            let already = tools.iter().any(|t| t.name.as_ref() == INVOKER);
298            if crate::server::tool_visibility::needs_invoker(
299                crate::tool_defs::is_full_mode(),
300                already,
301                active_role.is_tool_allowed(INVOKER),
302                &disabled,
303            ) {
304                if let Some(def) = self.registry.as_ref().and_then(|reg| {
305                    reg.tool_defs()
306                        .into_iter()
307                        .find(|t| t.name.as_ref() == INVOKER)
308                }) {
309                    tools.push(def);
310                }
311            }
312            tools
313        };
314
315        let tools = {
316            let Ok(dyn_state) = dynamic_tools::global().lock() else {
317                tracing::warn!("dynamic_tools mutex poisoned in list_tools; returning unfiltered");
318                return Ok(ListToolsResult {
319                    tools,
320                    ..Default::default()
321                });
322            };
323            // The lazy category gate (load tools on demand for dynamic_tools
324            // clients) only applies to the *default* lean-core surface. When the
325            // user opted into an explicit profile, that profile IS the
326            // authoritative surface — gating it by category would silently drop
327            // profile-enabled tools like Standard's ctx_architecture /
328            // ctx_semantic_search for Codex et al. (#358), so the advertised set
329            // would no longer match `lean-ctx tools show`.
330            if crate::server::tool_visibility::category_gate_applies(
331                dyn_state.supports_list_changed(),
332                explicit_profile,
333            ) {
334                tools
335                    .into_iter()
336                    .filter(|t| dyn_state.is_tool_active(t.name.as_ref()))
337                    .collect()
338            } else {
339                tools
340            }
341        };
342
343        let tools = {
344            let active = self.workflow.read().await.clone();
345            if let Some(run) = active {
346                if run.current == "done" || is_workflow_stale(&run) {
347                    let mut wf = self.workflow.write().await;
348                    *wf = None;
349                    let _ = crate::core::workflow::clear_active();
350                } else if let Some(state) = run.spec.state(&run.current) {
351                    if let Some(allowed) = &state.allowed_tools {
352                        let mut allow: std::collections::HashSet<&str> =
353                            allowed.iter().map(std::string::String::as_str).collect();
354                        for passthrough in WORKFLOW_PASSTHROUGH_TOOLS {
355                            allow.insert(passthrough);
356                        }
357                        return Ok(ListToolsResult {
358                            tools: tools
359                                .into_iter()
360                                .filter(|t| allow.contains(t.name.as_ref()))
361                                .collect(),
362                            ..Default::default()
363                        });
364                    }
365                }
366            }
367            tools
368        };
369
370        let tools = {
371            let cfg = crate::core::config::Config::load();
372            let level = crate::core::config::CompressionLevel::effective(&cfg);
373            let mode =
374                crate::core::terse::mcp_compress::DescriptionMode::from_compression_level(&level);
375            if mode == crate::core::terse::mcp_compress::DescriptionMode::Full {
376                tools
377            } else {
378                tools
379                    .into_iter()
380                    .map(|mut t| {
381                        let compressed = crate::core::terse::mcp_compress::compress_description(
382                            t.name.as_ref(),
383                            t.description.as_deref().unwrap_or(""),
384                            mode,
385                        );
386                        t.description = Some(compressed.into());
387                        t
388                    })
389                    .collect()
390            }
391        };
392
393        Ok(ListToolsResult {
394            tools,
395            ..Default::default()
396        })
397    }
398
399    async fn list_prompts(
400        &self,
401        _request: Option<PaginatedRequestParams>,
402        _context: RequestContext<RoleServer>,
403    ) -> Result<rmcp::model::ListPromptsResult, ErrorData> {
404        Ok(rmcp::model::ListPromptsResult::with_all_items(
405            prompts::list_prompts(),
406        ))
407    }
408
409    async fn get_prompt(
410        &self,
411        request: rmcp::model::GetPromptRequestParams,
412        _context: RequestContext<RoleServer>,
413    ) -> Result<rmcp::model::GetPromptResult, ErrorData> {
414        let ledger = self.ledger.read().await;
415        match prompts::get_prompt(&request, &ledger) {
416            Some(result) => Ok(result),
417            None => Err(ErrorData::invalid_params(
418                format!("Unknown prompt: {}", request.name),
419                None,
420            )),
421        }
422    }
423
424    async fn list_resources(
425        &self,
426        _request: Option<PaginatedRequestParams>,
427        _context: RequestContext<RoleServer>,
428    ) -> Result<rmcp::model::ListResourcesResult, rmcp::ErrorData> {
429        Ok(rmcp::model::ListResourcesResult::with_all_items(
430            resources::list_resources(),
431        ))
432    }
433
434    async fn read_resource(
435        &self,
436        request: rmcp::model::ReadResourceRequestParams,
437        _context: RequestContext<RoleServer>,
438    ) -> Result<rmcp::model::ReadResourceResult, rmcp::ErrorData> {
439        let ledger = self.ledger.read().await;
440        match resources::read_resource(&request.uri, &ledger) {
441            Some(contents) => Ok(rmcp::model::ReadResourceResult::new(contents)),
442            None => Err(rmcp::ErrorData::resource_not_found(
443                format!("Unknown resource: {}", request.uri),
444                None,
445            )),
446        }
447    }
448
449    async fn call_tool(
450        &self,
451        request: CallToolRequestParams,
452        context: RequestContext<RoleServer>,
453    ) -> Result<CallToolResult, ErrorData> {
454        use std::panic::AssertUnwindSafe;
455
456        let progress_token = request
457            .meta
458            .as_ref()
459            .and_then(rmcp::model::Meta::get_progress_token);
460        if let Some(ref token) = progress_token {
461            let sender =
462                crate::server::progress::ProgressSender::new(context.peer.clone(), token.clone());
463            *self
464                .progress_sender
465                .lock()
466                .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(sender);
467        }
468
469        let tool_name_for_panic = request.name.as_ref().to_string();
470        let args_fp_for_panic = request
471            .arguments
472            .as_ref()
473            .map(|a| {
474                crate::core::loop_detection::LoopDetector::fingerprint(&serde_json::Value::Object(
475                    a.clone(),
476                ))
477            })
478            .unwrap_or_default();
479
480        let loop_detector = self.loop_detector.clone();
481
482        match AssertUnwindSafe(self.call_tool_guarded(request))
483            .catch_unwind()
484            .await
485        {
486            Ok(result) => result,
487            Err(panic_payload) => {
488                let detail = if let Some(s) = panic_payload.downcast_ref::<&str>() {
489                    (*s).to_string()
490                } else if let Some(s) = panic_payload.downcast_ref::<String>() {
491                    s.clone()
492                } else {
493                    "unknown".to_string()
494                };
495                tracing::error!("call_tool panicked: {detail}");
496
497                if let Ok(mut detector) =
498                    tokio::time::timeout(std::time::Duration::from_secs(1), loop_detector.write())
499                        .await
500                {
501                    detector.record_error_outcome(&tool_name_for_panic, &args_fp_for_panic);
502                }
503
504                Ok(CallToolResult::error(vec![Content::text(
505                    "ERROR: lean-ctx internal error. The MCP server is still running. \
506                     Please retry or use a different approach."
507                        .to_string(),
508                )]))
509            }
510        }
511    }
512
513    async fn on_roots_list_changed(
514        &self,
515        _context: rmcp::service::NotificationContext<RoleServer>,
516    ) {
517        tracing::info!("Received roots/list_changed — will re-resolve on next tool call");
518        self.roots_resolved
519            .store(false, std::sync::atomic::Ordering::Relaxed);
520    }
521}