octomind 0.18.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Directory operations module - handling file listing with ripgrep (FIXED VERSION)

use super::super::{McpToolCall, McpToolResult};
use anyhow::{anyhow, Result};
use serde_json::{json, Value};
use std::process::Command;

// Parse a ripgrep output line to extract filename and rest, handling Windows paths correctly
// UTF-8 safe version that uses character boundaries instead of byte indices
fn parse_ripgrep_line(line: &str) -> Option<(String, String)> {
	// Find all colon positions
	let colon_positions: Vec<usize> = line.match_indices(':').map(|(i, _)| i).collect();

	// We need at least 2 colons for filename:line_number:content format
	if colon_positions.len() < 2 {
		return None;
	}

	// On Windows, the first colon might be after drive letter (C:)
	// Look for the colon that's followed by digits (line number)
	for i in 0..colon_positions.len() - 1 {
		let colon_pos = colon_positions[i];
		let next_colon_pos = colon_positions[i + 1];

		// Check if the part between these colons is a line number (digits)
		// UTF-8 safe: get substring between character positions
		let chars: Vec<char> = line.chars().collect();
		if colon_pos + 1 < chars.len() && next_colon_pos <= chars.len() {
			let potential_line_num: String =
				chars[(colon_pos + 1)..next_colon_pos].iter().collect();
			if potential_line_num.chars().all(|c| c.is_ascii_digit())
				&& !potential_line_num.is_empty()
			{
				// Found the filename:line_number:content pattern
				// UTF-8 safe: split at character boundaries
				let filename = line.chars().take(colon_pos).collect::<String>();
				let rest = line.chars().skip(colon_pos + 1).collect::<String>();
				return Some((filename, rest));
			}
		}
	}

	// Fallback: if no digit pattern found, use the last colon before content
	// This handles edge cases where line numbers might have non-digit characters
	if colon_positions.len() >= 2 {
		let colon_pos = colon_positions[colon_positions.len() - 2];
		// UTF-8 safe: split at character boundaries
		let filename = line.chars().take(colon_pos).collect::<String>();
		let rest = line.chars().skip(colon_pos + 1).collect::<String>();
		return Some((filename, rest));
	}

	None
}

// Parse a ripgrep context line (with dashes) to extract filename and rest, handling Windows paths
// UTF-8 safe version that uses character boundaries instead of byte indices
fn parse_ripgrep_dash_line(line: &str) -> Option<(String, String)> {
	// Find all dash positions
	let dash_positions: Vec<usize> = line.match_indices('-').map(|(i, _)| i).collect();

	// We need at least 2 dashes for filename-line_number-content format
	if dash_positions.len() < 2 {
		return None;
	}

	// On Windows, look for the dash that's followed by digits (line number)
	for i in 0..dash_positions.len() - 1 {
		let dash_pos = dash_positions[i];
		let next_dash_pos = dash_positions[i + 1];

		// Check if the part between these dashes is a line number (digits)
		// UTF-8 safe: get substring between character positions
		let chars: Vec<char> = line.chars().collect();
		if dash_pos + 1 < chars.len() && next_dash_pos <= chars.len() {
			let potential_line_num: String = chars[(dash_pos + 1)..next_dash_pos].iter().collect();
			if potential_line_num.chars().all(|c| c.is_ascii_digit())
				&& !potential_line_num.is_empty()
			{
				// Found the filename-line_number-content pattern
				// UTF-8 safe: split at character boundaries
				let filename = line.chars().take(dash_pos).collect::<String>();
				let rest = line.chars().skip(dash_pos + 1).collect::<String>();
				return Some((filename, rest));
			}
		}
	}

	// Fallback: use the last dash before content
	if dash_positions.len() >= 2 {
		let dash_pos = dash_positions[dash_positions.len() - 2];
		// UTF-8 safe: split at character boundaries
		let filename = line.chars().take(dash_pos).collect::<String>();
		let rest = line.chars().skip(dash_pos + 1).collect::<String>();
		return Some((filename, rest));
	}

	None
}

// Group ripgrep output by file for token efficiency while preserving line numbers
fn group_ripgrep_output(lines: &[String]) -> String {
	let mut result = Vec::new();
	let mut current_file = String::new();
	let mut file_lines = Vec::new();

	for line in lines {
		if line.contains("[") && line.contains("truncated") {
			// Handle truncation markers
			if !file_lines.is_empty() {
				result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
				file_lines.clear();
			}
			result.push(line.clone());
			continue;
		}

		// Parse ripgrep output: filename:line_number:content or filename-line_number-content (context)
		// Need to handle Windows paths (C:\path\file.rs:123:content) by finding the colon before line number
		if let Some((filename, rest)) = parse_ripgrep_line(line) {
			if filename != current_file {
				// New file - output previous file's lines
				if !file_lines.is_empty() {
					result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
					file_lines.clear();
				}
				current_file = filename.to_string();
			}

			// Add the line content (without filename)
			file_lines.push(rest.to_string());
		} else if let Some((filename, rest)) = parse_ripgrep_dash_line(line) {
			// Context line (filename-line_number-content)

			if filename != current_file {
				// New file - output previous file's lines
				if !file_lines.is_empty() {
					result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
					file_lines.clear();
				}
				current_file = filename.to_string();
			}

			// Add the context line (with dash to indicate context)
			file_lines.push(format!("-{}", rest));
		} else if line == "--" {
			// Separator between match groups - preserve it
			file_lines.push("--".to_string());
		} else {
			// Other lines (shouldn't happen in normal ripgrep output, but handle gracefully)
			file_lines.push(line.clone());
		}
	}

	// Output the last file's lines
	if !file_lines.is_empty() {
		result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
	}

	result.join("\n\n")
}

// Convert glob pattern to regex pattern for use with ripgrep
fn convert_glob_to_regex(glob_pattern: &str) -> String {
	// Handle multiple patterns separated by |
	let patterns: Vec<&str> = glob_pattern.split('|').collect();

	if patterns.len() > 1 {
		// Multiple patterns - convert each and join with |
		let regex_patterns: Vec<String> = patterns
			.iter()
			.map(|p| convert_single_glob_to_regex(p.trim()))
			.collect();
		format!("({})", regex_patterns.join("|"))
	} else {
		// Single pattern
		convert_single_glob_to_regex(glob_pattern)
	}
}

// Convert a single glob pattern to regex
fn convert_single_glob_to_regex(pattern: &str) -> String {
	let mut regex = String::new();
	let chars: Vec<char> = pattern.chars().collect();
	let mut i = 0;

	while i < chars.len() {
		match chars[i] {
			'*' => {
				// Convert * to .*? (non-greedy match any characters)
				regex.push_str(".*?");
			}
			'?' => {
				// Convert ? to . (match any single character)
				regex.push('.');
			}
			'[' => {
				// Character class - pass through as-is
				regex.push('[');
				i += 1;
				while i < chars.len() && chars[i] != ']' {
					regex.push(chars[i]);
					i += 1;
				}
				if i < chars.len() {
					regex.push(']');
				}
			}
			c if "(){}^$+|\\".contains(c) => {
				// Escape regex special characters
				regex.push('\\');
				regex.push(c);
			}
			c => {
				// Regular character
				regex.push(c);
			}
		}
		i += 1;
	}

	regex
}

// Execute list_files command with PROPER content search vs file listing handling
pub async fn execute_list_files(call: &McpToolCall) -> Result<McpToolResult> {
	// Extract directory parameter
	let directory = match call.parameters.get("directory") {
		Some(Value::String(dir)) => dir.clone(),
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"Missing required 'directory' parameter. Use \".\" for the current directory.".to_string(),
			))
		}
		Some(other) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				format!("Invalid 'directory' parameter: expected a string path (e.g. \".\" or \"src/\"), got {other}"),
			))
		}
	};

	// Extract optional parameters
	let pattern = call
		.parameters
		.get("pattern")
		.and_then(|v| v.as_str())
		.map(|s| s.to_string());

	let content = call
		.parameters
		.get("content")
		.and_then(|v| v.as_str())
		.map(|s| s.to_string());

	let max_depth = call
		.parameters
		.get("max_depth")
		.and_then(|v| v.as_u64())
		.map(|n| n as usize);

	let include_hidden = call
		.parameters
		.get("include_hidden")
		.and_then(|v| v.as_bool())
		.unwrap_or(false);

	let line_numbers = call
		.parameters
		.get("line_numbers")
		.and_then(|v| v.as_bool())
		.unwrap_or(true);

	let context_lines = call
		.parameters
		.get("context")
		.and_then(|v| v.as_i64())
		.unwrap_or(0) as usize;

	// Build the ripgrep command using proper argument passing
	let mut cmd = Command::new("rg");

	// Add depth limit if specified
	if let Some(depth) = max_depth {
		cmd.arg("--max-depth").arg(depth.to_string());
	}

	// Add hidden files flag if requested
	if include_hidden {
		cmd.arg("--hidden");
	}

	// Configure the command based on the operation type
	// Only do content search if content is actually provided and non-empty (not just whitespace)
	let has_content = content.as_ref().is_some_and(|c| !c.trim().is_empty());
	let (output_type, is_content_search) = if has_content {
		// Content search: search for content within files
		let content_pattern = content.as_ref().unwrap();
		if line_numbers {
			cmd.arg("--line-number");
		}

		// Add context if specified
		if context_lines > 0 {
			cmd.arg("--context").arg(context_lines.to_string());
		}

		// Use -F flag to treat the pattern as a fixed string (literal) rather than regex
		// This avoids regex parsing errors with special characters
		cmd.arg("-F").arg(content_pattern);
		// Add the directory as the search path
		cmd.arg(&directory);

		("content search", true)
	} else {
		// File listing: list files (optionally filtered by pattern)
		cmd.arg("--files");

		// Add the directory as the search path
		cmd.arg(&directory);

		("file listing", false)
	};

	// Debug: Log the actual command being executed
	crate::log_debug!(
		"Executing list_files ({}): rg {:?}",
		output_type,
		cmd.get_args().collect::<Vec<_>>()
	);

	// Execute the command
	let output = tokio::task::spawn_blocking(move || {
		let output = cmd.output();

		match output {
			Ok(output) => {
				let stdout = String::from_utf8_lossy(&output.stdout).to_string();
				let stderr = String::from_utf8_lossy(&output.stderr).to_string();

				if is_content_search {
					// For content search, preserve the original ripgrep output format
					// which includes filenames, line numbers, and matched content
					let lines: Vec<String> = stdout.lines().map(|s| s.to_string()).collect();

					// Group FIRST to preserve match + context relationships
					let grouped_output = group_ripgrep_output(&lines);

					// Global truncation will be applied by MCP response handler
					let final_output = grouped_output;

					let output_str = if stdout.is_empty() && !stderr.is_empty() {
						stderr
					} else {
						final_output
					};

					// For content search, we return the formatted output with matches
					let result = json!({
							"success": output.status.success(),
							"output": output_str,
							"lines": lines,
							"total_lines": lines.len(),
							"displayed_lines": lines.len(),
							"type": output_type,
							"parameters": {
							"directory": directory,
							"pattern": pattern,
							"content": content,
							"max_depth": max_depth,
							"include_hidden": include_hidden,

							"line_numbers": line_numbers,
							"context": context_lines
						}
					});

					result
				} else {
					// For file listing, parse as files and apply pattern filtering
					let mut files: Vec<String> = stdout.lines().map(|s| s.to_string()).collect();

					// Filter by pattern if we're doing filename pattern matching
					if let Some(ref name_pattern) = pattern {
						let regex_pattern = convert_glob_to_regex(name_pattern);
						if let Ok(regex) = regex::Regex::new(&regex_pattern) {
							files.retain(|file| regex.is_match(file));
						}
					}
					// Global truncation will be applied by MCP response handler
					let files_count = files.len();
					let final_files = files;

					let output_str = if stdout.is_empty() && !stderr.is_empty() {
						stderr
					} else {
						final_files.join("\n")
					};

					let result = json!({
							"success": output.status.success(),
							"output": output_str,
							"files": final_files,
							"count": files_count,
							"displayed_count": final_files.len(),
							"type": output_type,
							"parameters": {
							"directory": directory,
							"pattern": pattern,
							"content": content,
							"max_depth": max_depth,
							"include_hidden": include_hidden,

							"line_numbers": line_numbers,
							"context": context_lines
						}
					});

					result
				}
			}
			Err(e) => json!({
					"success": false,
					"output": format!("Failed to list files: {}", e),
					"files": [],
					"count": 0,
					"displayed_count": 0,
					"parameters": {
					"directory": directory,
					"pattern": pattern,
					"content": content,
					"max_depth": max_depth,
					"include_hidden": include_hidden,
					"line_numbers": line_numbers,
					"context": context_lines
				}
			}),
		}
	})
	.await
	.map_err(|e| anyhow!("Failed to execute file listing command: {}", e))?;

	Ok(McpToolResult {
		tool_name: "list_files".to_string(),
		tool_id: call.tool_id.clone(),
		result: output,
	})
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_parse_ripgrep_line_unix_path() {
		let line = "/home/user/file.rs:123:println!(\"test\");";
		let result = parse_ripgrep_line(line);
		assert_eq!(
			result,
			Some((
				"/home/user/file.rs".to_string(),
				"123:println!(\"test\");".to_string()
			))
		);
	}

	#[test]
	fn test_parse_ripgrep_line_windows_path() {
		let line = "C:\\Users\\Test\\file.rs:123:println!(\"test\");";
		let result = parse_ripgrep_line(line);
		assert_eq!(
			result,
			Some((
				"C:\\Users\\Test\\file.rs".to_string(),
				"123:println!(\"test\");".to_string()
			))
		);
	}

	#[test]
	fn test_parse_ripgrep_line_windows_path_with_spaces() {
		let line = "C:\\Users\\Test User\\My File.rs:456:let x = 42;";
		let result = parse_ripgrep_line(line);
		assert_eq!(
			result,
			Some((
				"C:\\Users\\Test User\\My File.rs".to_string(),
				"456:let x = 42;".to_string()
			))
		);
	}

	#[test]
	fn test_parse_ripgrep_dash_line_unix_path() {
		let line = "/home/user/file.rs-123-some context line";
		let result = parse_ripgrep_dash_line(line);
		assert_eq!(
			result,
			Some((
				"/home/user/file.rs".to_string(),
				"123-some context line".to_string()
			))
		);
	}

	#[test]
	fn test_parse_ripgrep_dash_line_windows_path() {
		let line = "C:\\Users\\Test\\file.rs-123-some context line";
		let result = parse_ripgrep_dash_line(line);
		assert_eq!(
			result,
			Some((
				"C:\\Users\\Test\\file.rs".to_string(),
				"123-some context line".to_string()
			))
		);
	}

	#[test]
	fn test_parse_ripgrep_line_invalid_format() {
		let line = "just some text without proper format";
		let result = parse_ripgrep_line(line);
		assert_eq!(result, None);
	}

	#[test]
	fn test_parse_ripgrep_line_single_colon() {
		let line = "C:\\Users\\file.rs";
		let result = parse_ripgrep_line(line);
		assert_eq!(result, None);
	}
	#[test]
	fn test_content_search_with_special_chars() {
		// Create a mock tool call with content parameter containing special regex characters
		let _call = McpToolCall {
			tool_name: "list_files".to_string(),
			tool_id: "test_id".to_string(),
			parameters: json!({
				"directory": "src",
				"content": "backward_step()"
			}),
		};

		// Use std::process::Command::new to create a command and inspect its arguments
		let mut cmd = Command::new("rg");
		cmd.arg("--line-number");

		// Add the -F flag and content pattern
		cmd.arg("-F").arg("backward_step()");
		cmd.arg("src");

		// Get the arguments as a Vec<String> for comparison
		let args: Vec<String> = cmd
			.get_args()
			.map(|arg| arg.to_string_lossy().to_string())
			.collect();

		// Verify the command contains the -F flag followed by the content pattern
		assert!(args.contains(&"-F".to_string()));
		assert!(args.contains(&"backward_step()".to_string()));

		// Verify the order: -F should come before the pattern
		let f_index = args.iter().position(|arg| arg == "-F").unwrap();
		let pattern_index = args
			.iter()
			.position(|arg| arg == "backward_step()")
			.unwrap();
		assert!(
			f_index < pattern_index,
			"-F flag should come before the pattern"
		);
	}

	#[tokio::test]
	async fn test_list_files_empty_content_should_list_files() {
		// CRITICAL TEST: When content is empty string "", should do file listing, NOT content search
		use crate::mcp::fs::directory::execute_list_files;
		use std::fs;
		use tempfile::TempDir;

		// Create a temporary directory with test files
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		// Create some test files
		for i in 1..=5 {
			let file_path = temp_path.join(format!("test_file_{}.txt", i));
			fs::write(&file_path, format!("Content of file {}", i)).unwrap();
		}

		// Create a specific file that would match the pattern
		let config_path = temp_path.join("config.json");
		fs::write(&config_path, "{}").unwrap();

		// Test with EMPTY content string - should do file listing, not content search
		let call = McpToolCall {
			tool_name: "list_files".to_string(),
			parameters: json!({
				"directory": temp_path.to_str().unwrap(),
				"pattern": "*.json",
				"content": ""  // Empty content - should list files, not search
			}),
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_list_files(&call).await.unwrap();
		let output = result.result.as_object().unwrap();

		// Should be file listing (not content search)
		assert_eq!(output["type"], "file listing");
		assert!(output["success"].as_bool().unwrap());

		// Should have files array (not lines)
		assert!(output.contains_key("files"));
		let files = output["files"].as_array().unwrap();

		// Should find the config.json file via pattern matching
		assert_eq!(
			files.len(),
			1,
			"Should find exactly one file matching *.json pattern"
		);
		assert!(files[0].as_str().unwrap().contains("config.json"));
	}

	#[tokio::test]
	async fn test_list_files_no_content_parameter_should_list_files() {
		// CRITICAL TEST: When content parameter is not provided at all, should do file listing
		use crate::mcp::fs::directory::execute_list_files;
		use std::fs;
		use tempfile::TempDir;

		// Create a temporary directory with test files
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		// Create some test files
		for i in 1..=5 {
			let file_path = temp_path.join(format!("test_file_{}.txt", i));
			fs::write(&file_path, format!("Content of file {}", i)).unwrap();
		}

		// Create a specific file that would match the pattern
		let config_path = temp_path.join("config.json");
		fs::write(&config_path, "{}").unwrap();

		// Test WITHOUT content parameter - should do file listing, not content search
		let call = McpToolCall {
			tool_name: "list_files".to_string(),
			parameters: json!({
				"directory": temp_path.to_str().unwrap(),
				"pattern": "*.json"
				// No "content" key at all
			}),
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_list_files(&call).await.unwrap();
		let output = result.result.as_object().unwrap();

		// Should be file listing (not content search)
		assert_eq!(output["type"], "file listing");
		assert!(output["success"].as_bool().unwrap());

		// Should have files array (not lines)
		assert!(output.contains_key("files"));
		let files = output["files"].as_array().unwrap();

		// Should find the config.json file via pattern matching
		assert_eq!(
			files.len(),
			1,
			"Should find exactly one file matching *.json pattern"
		);
		assert!(files[0].as_str().unwrap().contains("config.json"));
	}

	#[tokio::test]
	async fn test_list_files_whitespace_content_should_list_files() {
		// CRITICAL TEST: When content is only whitespace, should do file listing, NOT content search
		use crate::mcp::fs::directory::execute_list_files;
		use std::fs;
		use tempfile::TempDir;

		// Create a temporary directory with test files
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		// Create some test files
		for i in 1..=5 {
			let file_path = temp_path.join(format!("test_file_{}.txt", i));
			fs::write(&file_path, format!("Content of file {}", i)).unwrap();
		}

		// Create a specific file that would match the pattern
		let config_path = temp_path.join("config.json");
		fs::write(&config_path, "{}").unwrap();

		// Test with whitespace-only content - should do file listing, not content search
		let call = McpToolCall {
			tool_name: "list_files".to_string(),
			parameters: json!({
				"directory": temp_path.to_str().unwrap(),
				"pattern": "*.json",
				"content": "   "  // Whitespace only - should list files, not search
			}),
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_list_files(&call).await.unwrap();
		let output = result.result.as_object().unwrap();

		// Should be file listing (not content search)
		assert_eq!(output["type"], "file listing");
		assert!(output["success"].as_bool().unwrap());

		// Should have files array (not lines)
		assert!(output.contains_key("files"));
		let files = output["files"].as_array().unwrap();

		// Should find the config.json file via pattern matching
		assert_eq!(
			files.len(),
			1,
			"Should find exactly one file matching *.json pattern"
		);
		assert!(files[0].as_str().unwrap().contains("config.json"));
	}
}