lean_ctx/tools/registered/
ctx_read_dispatch.rs1use super::{
5 CtxReadTool, ErrorData, Map, McpTool, Tool, ToolContext, ToolOutput, Value, engine, get_str,
6 record_attribution_result, require_resolved_path,
7};
8
9impl McpTool for CtxReadTool {
10 fn name(&self) -> &'static str {
11 "ctx_read"
12 }
13
14 fn produces_machine_readable(&self, args: Option<&Map<String, Value>>) -> bool {
15 crate::server::native_receipts::requested("ctx_read", args)
18 }
19
20 fn tool_def(&self) -> Tool {
21 super::schema::ctx_read_tool_def()
22 }
23
24 fn handle(
25 &self,
26 args: &Map<String, Value>,
27 ctx: &ToolContext,
28 ) -> Result<ToolOutput, ErrorData> {
29 let engine_interface_v1 = engine::interface_v1_requested(args)?;
30 engine::validate_v1_request_shape(args, engine_interface_v1)?;
31 if args
34 .get("paths")
35 .and_then(|v| v.as_array())
36 .is_some_and(|a| !a.is_empty())
37 {
38 let result = super::super::ctx_multi_read::batch_read(args, ctx);
39 if let Ok(output) = &result {
40 record_attribution_result(ctx, "ctx_read batch".to_string(), output);
41 }
42 return result;
43 }
44
45 let path = if let Some(repo) = get_str(args, "repo") {
46 let root = crate::core::multi_repo::resolve_repo_root(&repo).ok_or_else(|| {
47 let known = crate::core::multi_repo::known_aliases().join(", ");
48 let known = if known.is_empty() {
49 "none registered — use ctx_multi_repo add_root".to_string()
50 } else {
51 known
52 };
53 ErrorData::invalid_params(
54 format!("unknown repo alias: {repo} (known: {known})"),
55 None,
56 )
57 })?;
58 let rel = get_str(args, "path").unwrap_or_else(|| ".".to_string());
59 crate::core::path_resolve::resolve_tool_path(Some(&root), None, &rel)
60 .map_err(|e| ErrorData::invalid_params(e, None))?
61 } else {
62 require_resolved_path(ctx, args, "path")?
63 };
64
65 let result = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
66 self.handle_inner(args, ctx, &path, engine_interface_v1)
67 })) {
68 Ok(result) => result,
69 Err(_) => Err(ErrorData::internal_error(
70 format!(
71 "ctx_read panicked while processing '{path}'. This is a bug — please report it."
72 ),
73 None,
74 )),
75 };
76 if let Ok(output) = &result {
77 record_attribution_result(ctx, format!("ctx_read {path}"), output);
78 }
79 result
80 }
81}