MCPcopy Index your code
hub / github.com/AI45Lab/Code / execute

Method execute

core/src/tools/builtin/ls.rs:39–91  ·  view source on GitHub ↗
(&self, args: &serde_json::Value, ctx: &ToolContext)

Source from the content-addressed store, hash-verified

37 }
38
39 async fn execute(&self, args: &serde_json::Value, ctx: &ToolContext) -> Result<ToolOutput> {
40 let path_str = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");
41
42 let workspace_path = match ctx.resolve_workspace_path(path_str) {
43 Ok(p) => p,
44 Err(e) => return Ok(ToolOutput::error(format!("Failed to resolve path: {}", e))),
45 };
46
47 let fs = ctx.workspace_services.fs();
48 let path_for_list = workspace_path.clone();
49 let mut entries = match ctx
50 .workspace_services
51 .run_with_timeout("list_dir", async move { fs.list_dir(&path_for_list).await })
52 .await
53 {
54 Ok(entries) => entries,
55 Err(e) => {
56 return Ok(ToolOutput::error(format!(
57 "Failed to read directory {}: {}",
58 ctx.workspace_services.display_path(&workspace_path),
59 e
60 )))
61 }
62 };
63
64 entries.sort_by(|a, b| {
65 // Directories first, then alphabetical
66 let dir_order = (a.kind.as_tool_kind() != "dir").cmp(&(b.kind.as_tool_kind() != "dir"));
67 dir_order.then(a.name.to_lowercase().cmp(&b.name.to_lowercase()))
68 });
69
70 let mut output = format!(
71 "Directory: {}\n\n",
72 ctx.workspace_services.display_path(&workspace_path)
73 );
74
75 if entries.is_empty() {
76 output.push_str("(empty directory)\n");
77 } else {
78 for entry in &entries {
79 let kind = entry.kind.as_tool_kind();
80 let size_str = format_size(entry.size);
81 let suffix = if kind == "dir" { "/" } else { "" };
82 output.push_str(&format!(
83 "{:<6} {:>8} {}{}\n",
84 kind, size_str, entry.name, suffix
85 ));
86 }
87 output.push_str(&format!("\n{} entries\n", entries.len()));
88 }
89
90 Ok(ToolOutput::success(output))
91 }
92}
93
94fn format_size(bytes: u64) -> String {

Callers 3

test_ls_directoryFunction · 0.45
test_ls_empty_dirFunction · 0.45
test_ls_nonexistentFunction · 0.45

Calls 10

format_sizeFunction · 0.85
fsMethod · 0.80
run_with_timeoutMethod · 0.80
as_tool_kindMethod · 0.80
getMethod · 0.45
as_strMethod · 0.45
cloneMethod · 0.45
list_dirMethod · 0.45
is_emptyMethod · 0.45

Tested by 3

test_ls_directoryFunction · 0.36
test_ls_empty_dirFunction · 0.36
test_ls_nonexistentFunction · 0.36