Skip to content

Commit d01d4be

Browse files
committed
feat: enhance directory_tree response with metadata
1 parent 0f2c6cb commit d01d4be

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/fs_service.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl FileSystemService {
522522
max_depth: Option<usize>,
523523
max_files: Option<usize>,
524524
current_count: &mut usize,
525-
) -> ServiceResult<Value> {
525+
) -> ServiceResult<(Value, bool)> {
526526
let valid_path = self.validate_path(root_path.as_ref())?;
527527

528528
let metadata = fs::metadata(&valid_path)?;
@@ -533,6 +533,7 @@ impl FileSystemService {
533533
}
534534

535535
let mut children = Vec::new();
536+
let mut reached_max_depth = false;
536537

537538
if max_depth != Some(0) {
538539
for entry in WalkDir::new(valid_path)
@@ -568,17 +569,21 @@ impl FileSystemService {
568569

569570
if metadata.is_dir() {
570571
let next_depth = max_depth.map(|d| d - 1);
571-
let child_children =
572+
let (child_children, child_reached_max_depth) =
572573
self.directory_tree(child_path, next_depth, max_files, current_count)?;
573574
json_entry
574575
.as_object_mut()
575576
.unwrap()
576577
.insert("children".to_string(), child_children);
578+
reached_max_depth |= child_reached_max_depth;
577579
}
578580
children.push(json_entry);
579581
}
582+
} else {
583+
// If max_depth is 0, we skip processing this directory's children
584+
reached_max_depth = true;
580585
}
581-
Ok(Value::Array(children))
586+
Ok((Value::Array(children), reached_max_depth))
582587
}
583588

584589
pub fn create_unified_diff(

src/tools/directory_tree.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rust_mcp_sdk::macros::{mcp_tool, JsonSchema};
22
use rust_mcp_sdk::schema::TextContent;
33
use rust_mcp_sdk::schema::{schema_utils::CallToolError, CallToolResult};
4-
use serde_json::json;
4+
use serde_json::{json, Map, Value};
55

66
use crate::error::ServiceError;
77
use crate::fs_service::FileSystemService;
@@ -33,7 +33,7 @@ impl DirectoryTreeTool {
3333
context: &FileSystemService,
3434
) -> std::result::Result<CallToolResult, CallToolError> {
3535
let mut entry_counter: usize = 0;
36-
let entries = context
36+
let (entries, reached_max_depth) = context
3737
.directory_tree(
3838
params.path,
3939
params.max_depth.map(|v| v as usize),
@@ -49,8 +49,22 @@ impl DirectoryTreeTool {
4949
}
5050

5151
let json_str = serde_json::to_string_pretty(&json!(entries)).map_err(CallToolError::new)?;
52-
Ok(CallToolResult::text_content(vec![TextContent::from(
53-
json_str,
54-
)]))
52+
53+
// Include meta flag to denote that max depth was hit; some files and directories might be omitted
54+
let meta = if reached_max_depth {
55+
let mut meta = Map::new();
56+
meta.insert(
57+
"warning".to_string(),
58+
Value::String(
59+
"Incomplete listing: subdirectories beyond the maximum depth were skipped."
60+
.to_string(),
61+
),
62+
);
63+
Some(meta)
64+
} else {
65+
None
66+
};
67+
68+
Ok(CallToolResult::text_content(vec![TextContent::from(json_str)]).with_meta(meta))
5569
}
5670
}

0 commit comments

Comments
 (0)