tuls 0.1.4

A compact Rust MCP toolbox for filesystem access, HTTP fetches, persistent memory, local process execution, reusable skills, and provider-backed local subagents.
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
mod edit;
mod format;
mod search;

use std::path::PathBuf;
use std::sync::Arc;

use rmcp::{
    ErrorData as McpError, ServerHandler,
    handler::server::router::tool::ToolRouter,
    handler::server::wrapper::Parameters,
    model::{
        CallToolResult, ContentBlock, Implementation, ResourceContents, ServerCapabilities,
        ServerInfo, Tool,
    },
    schemars, tool, tool_handler, tool_router,
};
use serde::Deserialize;
use tokio::io::AsyncReadExt;

use crate::cli::DirectoryServerOptions;
use crate::policy::{Capability, ToolPolicy, ToolSpec};
use crate::support::{
    AccessControl, MAX_TOOL_RESULT_BYTES, SPEC_VERSION, atomic_write, text_result, tool_error,
    truncate_text,
};

use self::edit::{EditOperation, apply_edits, render_diff};
use self::format::{format_size, head_lines, tail_lines};
use self::search::{TreeEntry, directory_tree, search_files, serialize_tree};

/// Maximum size of a media file (`read_media_file`) that will be base64
/// encoded into a tool result, protecting the client's context window.
const MAX_MEDIA_FILE_BYTES: usize = 1024 * 1024;
const MAX_TEXT_FILE_BYTES: usize = 8 * 1024 * 1024;
const MAX_BATCH_FILES: usize = 32;
const MAX_EDIT_OPERATIONS: usize = 1024;

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for reading a text file")]
pub struct ReadTextFileArgs {
    /// File path to read, must be within an allowed directory
    pub path: String,
    /// If provided, returns only the first N lines of the file
    #[serde(default)]
    pub head: Option<u32>,
    /// If provided, returns only the last N lines of the file
    #[serde(default)]
    pub tail: Option<u32>,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for reading a media file")]
pub struct ReadMediaFileArgs {
    /// File path to read, must be within an allowed directory
    pub path: String,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for reading multiple files")]
pub struct ReadMultipleFilesArgs {
    /// Array of file paths to read. Each path must point to a valid file within allowed directories.
    pub paths: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for writing a file")]
pub struct WriteFileArgs {
    /// File location
    pub path: String,
    /// File content
    pub content: String,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for editing a file")]
pub struct EditFileArgs {
    /// File to edit
    pub path: String,
    /// List of edit operations
    pub edits: Vec<EditOperation>,
    /// Preview changes using git-style diff format without applying them
    #[serde(default)]
    pub dry_run: bool,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for creating a directory")]
pub struct CreateDirectoryArgs {
    /// Directory path to create, must be within an allowed directory
    pub path: String,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for listing a directory")]
pub struct ListDirectoryArgs {
    /// Directory path to list, must be within an allowed directory
    pub path: String,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for listing a directory with sizes")]
pub struct ListDirectoryWithSizesArgs {
    /// Directory path to list, must be within an allowed directory
    pub path: String,
    /// Sort entries by name or size.
    #[serde(default)]
    pub sort_by: DirectorySort,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum DirectorySort {
    #[default]
    Name,
    Size,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for building a directory tree")]
pub struct DirectoryTreeArgs {
    /// Starting directory
    pub path: String,
    /// Exclude any paths matching these patterns
    #[serde(default)]
    pub exclude_patterns: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for moving a file or directory")]
pub struct MoveFileArgs {
    /// Source file or directory
    pub source: String,
    /// Destination file or directory
    pub destination: String,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for searching files")]
pub struct SearchFilesArgs {
    /// Starting directory for the search
    pub path: String,
    /// Glob-style pattern to match, e.g. `*.rs` or `**/*.rs`
    pub pattern: String,
    /// Exclude any paths matching these patterns
    #[serde(default)]
    pub exclude_patterns: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(description = "Arguments for getting file information")]
pub struct GetFileInfoArgs {
    /// File or directory path, must be within an allowed directory
    pub path: String,
}

const TOOL_SPECS: &[ToolSpec] = &[
    ToolSpec::new("filesystem", "read_text_file", Capability::FilesystemRead),
    ToolSpec::new("filesystem", "read_media_file", Capability::FilesystemRead),
    ToolSpec::new(
        "filesystem",
        "read_multiple_files",
        Capability::FilesystemRead,
    ),
    ToolSpec::new("filesystem", "write_file", Capability::FilesystemWrite),
    ToolSpec::new("filesystem", "edit_file", Capability::FilesystemWrite),
    ToolSpec::new(
        "filesystem",
        "create_directory",
        Capability::FilesystemWrite,
    ),
    ToolSpec::new("filesystem", "list_directory", Capability::FilesystemRead),
    ToolSpec::new(
        "filesystem",
        "list_directory_with_sizes",
        Capability::FilesystemRead,
    ),
    ToolSpec::new("filesystem", "directory_tree", Capability::FilesystemRead),
    ToolSpec::new("filesystem", "move_file", Capability::FilesystemWrite),
    ToolSpec::new("filesystem", "search_files", Capability::FilesystemRead),
    ToolSpec::new("filesystem", "get_file_info", Capability::FilesystemRead),
    ToolSpec::new(
        "filesystem",
        "list_allowed_directories",
        Capability::FilesystemRead,
    ),
];

#[derive(Debug, Clone)]
pub struct FilesystemServer {
    access: Arc<AccessControl>,
    tool_router: ToolRouter<FilesystemServer>,
}

impl FilesystemServer {
    pub fn new(access: AccessControl, policy: ToolPolicy) -> Self {
        let mut tool_router = Self::tool_router();
        for spec in TOOL_SPECS {
            if !policy.allows(*spec) {
                tool_router.disable_route(spec.name);
            }
        }
        Self {
            access: Arc::new(access),
            tool_router,
        }
    }
}

#[tool_router(router = tool_router)]
impl FilesystemServer {
    #[tool(
        name = "read_text_file",
        title = "Read Text File",
        description = "Read a bounded UTF-8 text file inside the allowed filesystem scope. Set head or tail to return only the first or last N lines.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn read_text_file(
        &self,
        Parameters(args): Parameters<ReadTextFileArgs>,
    ) -> Result<CallToolResult, McpError> {
        self.read_text_file_impl(args).await
    }

    #[tool(
        name = "read_media_file",
        title = "Read Media File",
        description = "Read a bounded media file inside the allowed filesystem scope and return a typed MCP content block.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn read_media_file(
        &self,
        Parameters(args): Parameters<ReadMediaFileArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(_) => return Ok(tool_error(access_denied(&args.path))),
        };
        let bytes = match read_bounded_file(&valid_path, MAX_MEDIA_FILE_BYTES).await {
            Ok(bytes) => bytes,
            Err(error) => return Ok(tool_error(error)),
        };
        let mime = mime_guess::from_path(&valid_path)
            .first_raw()
            .unwrap_or("application/octet-stream")
            .to_string();
        let encoded = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &bytes);

        let block = if mime.starts_with("image/") {
            ContentBlock::image(encoded, mime)
        } else if mime.starts_with("audio/") {
            ContentBlock::audio(encoded, mime)
        } else {
            let uri = match url::Url::from_file_path(&valid_path) {
                Ok(uri) => uri.to_string(),
                Err(()) => {
                    return Ok(tool_error(format!(
                        "Failed to convert {} to a file URI",
                        valid_path.display()
                    )));
                }
            };
            ContentBlock::resource(ResourceContents::BlobResourceContents {
                uri,
                mime_type: Some(mime),
                blob: encoded,
                meta: None,
            })
        };
        Ok(CallToolResult::success(vec![block]))
    }

    #[tool(
        name = "read_multiple_files",
        title = "Read Multiple Files",
        description = "Read multiple UTF-8 files inside the allowed filesystem scope in one bounded batch. Individual read failures are returned alongside successful results.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn read_multiple_files(
        &self,
        Parameters(args): Parameters<ReadMultipleFilesArgs>,
    ) -> Result<CallToolResult, McpError> {
        if args.paths.is_empty() {
            return Ok(tool_error("At least one file path must be provided"));
        }
        if args.paths.len() > MAX_BATCH_FILES {
            return Ok(tool_error(format!(
                "At most {MAX_BATCH_FILES} files may be read in one call"
            )));
        }
        let mut results = Vec::with_capacity(args.paths.len());
        for file_path in &args.paths {
            match self.resolve(file_path).await {
                Ok(valid) => match read_text_file_limited(&valid, MAX_TOOL_RESULT_BYTES).await {
                    Ok(content) => results.push(format!("{file_path}:\n{content}\n")),
                    Err(error) => results.push(format!("{file_path}: Error - {error}")),
                },
                Err(e) => results.push(format!("{file_path}: Error - {e}")),
            }
        }
        let combined = truncate_text(
            &results.join("\n---\n"),
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: combined output exceeds {MAX_TOOL_RESULT_BYTES} bytes; \
                 read the files individually]"
            ),
        );
        Ok(text_result(combined))
    }

    #[tool(
        name = "write_file",
        title = "Write File",
        description = "Atomically create or replace a UTF-8 file inside the allowed filesystem scope.",
        annotations(
            read_only_hint = false,
            idempotent_hint = true,
            destructive_hint = true,
            open_world_hint = false
        )
    )]
    async fn write_file(
        &self,
        Parameters(args): Parameters<WriteFileArgs>,
    ) -> Result<CallToolResult, McpError> {
        if args.content.len() > MAX_TEXT_FILE_BYTES {
            return Ok(tool_error(format!(
                "content exceeds the {} byte text file limit",
                MAX_TEXT_FILE_BYTES
            )));
        }
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        // Atomic write via temp file + rename: replaces the target without
        // following a symlink that appears between validation and write.
        match atomic_write(&valid_path, args.content.as_bytes()).await {
            Ok(()) => Ok(text_result(format!("Successfully wrote to {}", args.path))),
            Err(e) => Ok(tool_error(format!(
                "Failed to write to {}: {e}",
                valid_path.display()
            ))),
        }
    }

    #[tool(
        name = "edit_file",
        title = "Edit File",
        description = "Apply ordered exact text replacements inside the allowed filesystem scope and return a bounded diff. Each nonempty oldText must occur exactly once in the current content. Set dryRun to use identical matching without writing.",
        annotations(
            read_only_hint = false,
            idempotent_hint = false,
            destructive_hint = true,
            open_world_hint = false
        )
    )]
    async fn edit_file(
        &self,
        Parameters(args): Parameters<EditFileArgs>,
    ) -> Result<CallToolResult, McpError> {
        let edit_bytes = args.edits.iter().fold(0usize, |total, edit| {
            total
                .saturating_add(edit.old_text.len())
                .saturating_add(edit.new_text.len())
        });
        if args.edits.len() > MAX_EDIT_OPERATIONS || edit_bytes > MAX_TEXT_FILE_BYTES {
            return Ok(tool_error(format!(
                "edit batch must contain at most {MAX_EDIT_OPERATIONS} operations and {MAX_TEXT_FILE_BYTES} total text bytes"
            )));
        }
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let original = match read_text_file(&valid_path).await {
            Ok(content) => content,
            Err(error) => return Ok(tool_error(error)),
        };
        let modified = match apply_edits(&original, &args.edits) {
            Ok(modified) => modified,
            Err(e) => return Ok(tool_error(e)),
        };
        if modified.len() > MAX_TEXT_FILE_BYTES {
            return Ok(tool_error(format!(
                "edited file exceeds the {} byte text file limit",
                MAX_TEXT_FILE_BYTES
            )));
        }
        let diff = render_diff(&original, &modified);

        if !args.dry_run
            && modified != original
            && let Err(e) = atomic_write(&valid_path, modified.as_bytes()).await
        {
            return Ok(tool_error(format!(
                "Failed to write {}: {e}",
                valid_path.display()
            )));
        }
        let diff = truncate_text(
            &diff,
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: diff exceeds {MAX_TOOL_RESULT_BYTES} bytes; \
                 consider editing a smaller portion of the file]"
            ),
        );
        Ok(text_result(diff))
    }

    #[tool(
        name = "create_directory",
        title = "Create Directory",
        description = "Create a directory and any missing parent directories inside the allowed filesystem scope. Existing directories succeed without modification.",
        annotations(
            read_only_hint = false,
            idempotent_hint = true,
            destructive_hint = false,
            open_world_hint = false
        )
    )]
    async fn create_directory(
        &self,
        Parameters(args): Parameters<CreateDirectoryArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        match tokio::fs::create_dir_all(&valid_path).await {
            Ok(()) => Ok(text_result(format!(
                "Successfully created directory {}",
                args.path
            ))),
            Err(e) => Ok(tool_error(format!(
                "Failed to create directory {}: {e}",
                valid_path.display()
            ))),
        }
    }

    #[tool(
        name = "list_directory",
        title = "List Directory",
        description = "List immediate entries of a directory inside the allowed filesystem scope, distinguishing files, directories, and symbolic links.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn list_directory(
        &self,
        Parameters(args): Parameters<ListDirectoryArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let mut entries = match tokio::fs::read_dir(&valid_path).await {
            Ok(entries) => entries,
            Err(e) => {
                return Ok(tool_error(format!(
                    "Failed to list directory {}: {e}",
                    valid_path.display()
                )));
            }
        };
        let mut names = Vec::new();
        loop {
            let next = match entries.next_entry().await {
                Ok(next) => next,
                Err(e) => {
                    return Ok(tool_error(format!(
                        "Failed to list directory {}: {e}",
                        valid_path.display()
                    )));
                }
            };
            let Some(entry) = next else { break };
            let file_type = match entry.file_type().await {
                Ok(file_type) => file_type,
                Err(e) => {
                    return Ok(tool_error(format!(
                        "Failed to list directory {}: {e}",
                        valid_path.display()
                    )));
                }
            };
            let kind = if file_type.is_dir() {
                "[DIR]"
            } else if file_type.is_symlink() {
                "[SYMLINK]"
            } else {
                "[FILE]"
            };
            names.push(format!("{kind} {}", entry.file_name().to_string_lossy()));
        }
        names.sort();
        let listing = truncate_text(
            &names.join("\n"),
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: listing exceeds {MAX_TOOL_RESULT_BYTES} bytes; \
                 use search_files or directory_tree with excludePatterns to narrow]"
            ),
        );
        Ok(text_result(listing))
    }

    #[tool(
        name = "list_directory_with_sizes",
        title = "List Directory with Sizes",
        description = "List immediate directory entries with file sizes inside the allowed filesystem scope. Symbolic links are reported without following their targets.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn list_directory_with_sizes(
        &self,
        Parameters(args): Parameters<ListDirectoryWithSizesArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let mut entries = match tokio::fs::read_dir(&valid_path).await {
            Ok(entries) => entries,
            Err(e) => {
                return Ok(tool_error(format!(
                    "Failed to list directory {}: {e}",
                    valid_path.display()
                )));
            }
        };

        struct Entry {
            name: String,
            is_directory: bool,
            is_symlink: bool,
            size: u64,
        }

        let mut detailed = Vec::new();
        loop {
            let next = match entries.next_entry().await {
                Ok(next) => next,
                Err(e) => {
                    return Ok(tool_error(format!(
                        "Failed to list directory {}: {e}",
                        valid_path.display()
                    )));
                }
            };
            let Some(entry) = next else { break };
            let name = entry.file_name().to_string_lossy().into_owned();
            let file_type = match entry.file_type().await {
                Ok(file_type) => file_type,
                Err(error) => {
                    return Ok(tool_error(format!(
                        "Failed to inspect directory entry {}: {error}",
                        entry.path().display()
                    )));
                }
            };
            let is_directory = file_type.is_dir();
            let is_symlink = file_type.is_symlink();
            let size = if is_directory {
                0
            } else {
                match tokio::fs::symlink_metadata(entry.path()).await {
                    Ok(metadata) => metadata.len(),
                    Err(error) => {
                        return Ok(tool_error(format!(
                            "Failed to stat {}: {error}",
                            entry.path().display()
                        )));
                    }
                }
            };
            detailed.push(Entry {
                name,
                is_directory,
                is_symlink,
                size,
            });
        }

        detailed.sort_by(|a, b| {
            if args.sort_by == DirectorySort::Size {
                b.size.cmp(&a.size)
            } else {
                a.name.cmp(&b.name)
            }
        });

        let mut formatted = Vec::new();
        for entry in &detailed {
            let size = if entry.is_directory {
                String::new()
            } else {
                format_size(entry.size)
            };
            formatted.push(format!(
                "{} {:<30} {:>10}",
                if entry.is_directory {
                    "[DIR]"
                } else if entry.is_symlink {
                    "[SYMLINK]"
                } else {
                    "[FILE]"
                },
                entry.name,
                size
            ));
        }

        let total_files = detailed.iter().filter(|e| !e.is_directory).count();
        let total_dirs = detailed.iter().filter(|e| e.is_directory).count();
        let total_size = detailed
            .iter()
            .filter(|e| !e.is_directory)
            .map(|e| e.size)
            .sum::<u64>();
        formatted.push(String::new());
        formatted.push(format!(
            "Total: {total_files} files, {total_dirs} directories"
        ));
        formatted.push(format!("Combined size: {}", format_size(total_size)));

        let listing = truncate_text(
            &formatted.join("\n"),
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: listing exceeds {MAX_TOOL_RESULT_BYTES} bytes; \
                 use search_files or directory_tree with excludePatterns to narrow]"
            ),
        );
        Ok(text_result(listing))
    }

    #[tool(
        name = "directory_tree",
        title = "Directory Tree",
        description = "Return a bounded recursive JSON tree inside the allowed filesystem scope. The entry limit uses an explicit marker; JSON that exceeds the result byte limit returns an error. Use excludePatterns to narrow the tree.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn directory_tree(
        &self,
        Parameters(args): Parameters<DirectoryTreeArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let access = self.access.clone();
        let tree: Vec<TreeEntry> =
            match directory_tree(&valid_path, &args.exclude_patterns, &access).await {
                Ok(tree) => tree,
                Err(e) => return Ok(tool_error(e)),
            };
        match serialize_tree(&tree) {
            Ok(json) => Ok(text_result(json)),
            Err(e) => Ok(tool_error(e)),
        }
    }

    #[tool(
        name = "move_file",
        title = "Move File",
        description = "Move or rename a file or directory inside the allowed filesystem scope. The destination must not already exist.",
        annotations(
            read_only_hint = false,
            idempotent_hint = false,
            destructive_hint = true,
            open_world_hint = false
        )
    )]
    async fn move_file(
        &self,
        Parameters(args): Parameters<MoveFileArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_source = match self.resolve(&args.source).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let valid_dest = match self.resolve(&args.destination).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        match tokio::fs::try_exists(&valid_dest).await {
            Ok(true) => {
                return Ok(tool_error(format!(
                    "Destination already exists: {}",
                    valid_dest.display()
                )));
            }
            Ok(false) => {}
            Err(error) => {
                return Ok(tool_error(format!(
                    "Failed to inspect destination {}: {error}",
                    valid_dest.display()
                )));
            }
        }
        match tokio::fs::rename(&valid_source, &valid_dest).await {
            Ok(()) => Ok(text_result(format!(
                "Successfully moved {} to {}",
                args.source, args.destination
            ))),
            Err(e) => Ok(tool_error(format!(
                "Failed to move {} to {}: {e}",
                args.source, args.destination
            ))),
        }
    }

    #[tool(
        name = "search_files",
        title = "Search Files",
        description = "Recursively search inside the allowed filesystem scope using a glob pattern. Use excludePatterns to omit matching paths; results are bounded.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn search_files(
        &self,
        Parameters(args): Parameters<SearchFilesArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let access = self.access.clone();
        let (results, search_truncated) =
            match search_files(&valid_path, &args.pattern, &args.exclude_patterns, &access).await {
                Ok(results) => results,
                Err(error) => return Ok(tool_error(error)),
            };
        if results.is_empty() {
            return Ok(text_result("No matches found"));
        }
        let mut text = results
            .iter()
            .map(|path| path.display().to_string())
            .collect::<Vec<_>>()
            .join("\n");
        if search_truncated {
            text.push_str("\n[truncated: search safety limit reached]");
        }
        let text = truncate_text(
            &text,
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: too many matches to list in {MAX_TOOL_RESULT_BYTES} bytes; \
                 narrow the pattern or add excludePatterns]"
            ),
        );
        Ok(text_result(text))
    }

    #[tool(
        name = "get_file_info",
        title = "Get File Info",
        description = "Return bounded filesystem metadata for a file or directory inside the allowed filesystem scope.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn get_file_info(
        &self,
        Parameters(args): Parameters<GetFileInfoArgs>,
    ) -> Result<CallToolResult, McpError> {
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let meta = match tokio::fs::metadata(&valid_path).await {
            Ok(meta) => meta,
            Err(e) => {
                return Ok(tool_error(format!(
                    "Failed to stat {}: {e}",
                    valid_path.display()
                )));
            }
        };
        let lines = [
            format!("size: {}", meta.len()),
            format!("created: {}", rfc3339(meta.created())),
            format!("modified: {}", rfc3339(meta.modified())),
            format!("accessed: {}", rfc3339(meta.accessed())),
            format!("isFile: {}", meta.is_file()),
            format!("isDirectory: {}", meta.is_dir()),
            format!("permissions: {}", permissions_string(&meta)),
        ];
        Ok(text_result(lines.join("\n")))
    }

    #[tool(
        name = "list_allowed_directories",
        title = "List Allowed Directories",
        description = "Return the configured filesystem roots available to this server.",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn list_allowed_directories(&self) -> Result<CallToolResult, McpError> {
        let text = format!(
            "Allowed directories:\n{}",
            self.access
                .roots()
                .iter()
                .map(|p| p.display().to_string())
                .collect::<Vec<_>>()
                .join("\n")
        );
        Ok(text_result(text))
    }
}

impl FilesystemServer {
    /// Resolve a user path against the allowed directories, or return an
    /// access-denied style error message.
    async fn resolve(&self, path: &str) -> Result<PathBuf, String> {
        self.access.validate_path(path).await
    }

    async fn read_text_file_impl(
        &self,
        args: ReadTextFileArgs,
    ) -> Result<CallToolResult, McpError> {
        if args.head.is_some() && args.tail.is_some() {
            return Ok(tool_error(
                "Cannot specify both head and tail parameters simultaneously",
            ));
        }
        let valid_path = match self.resolve(&args.path).await {
            Ok(p) => p,
            Err(e) => return Ok(tool_error(e)),
        };
        let content = match read_text_file(&valid_path).await {
            Ok(content) => content,
            Err(error) => return Ok(tool_error(error)),
        };
        let text = if let Some(n) = args.tail {
            tail_lines(&content, n)
        } else if let Some(n) = args.head {
            head_lines(&content, n)
        } else {
            content
        };
        let text = truncate_text(
            &text,
            MAX_TOOL_RESULT_BYTES,
            &format!(
                "\n\n[truncated: file exceeds {MAX_TOOL_RESULT_BYTES} bytes; \
                 use the head or tail parameter to read a specific portion]"
            ),
        );
        Ok(text_result(text))
    }
}

async fn read_bounded_file(path: &std::path::Path, limit: usize) -> Result<Vec<u8>, String> {
    let file = tokio::fs::File::open(path)
        .await
        .map_err(|error| format!("Failed to open {}: {error}", path.display()))?;
    let mut bytes = Vec::with_capacity(limit.min(64 * 1024));
    let mut reader = file.take(limit.saturating_add(1) as u64);
    reader
        .read_to_end(&mut bytes)
        .await
        .map_err(|error| format!("Failed to read {}: {error}", path.display()))?;
    if bytes.len() > limit {
        return Err(format!(
            "Failed to read {}: file exceeds the {} byte limit",
            path.display(),
            limit
        ));
    }
    Ok(bytes)
}

async fn read_text_file_limited(path: &std::path::Path, limit: usize) -> Result<String, String> {
    let bytes = read_bounded_file(path, limit).await?;
    String::from_utf8(bytes)
        .map_err(|_| format!("Failed to read {}: file is not valid UTF-8", path.display()))
}

async fn read_text_file(path: &std::path::Path) -> Result<String, String> {
    read_text_file_limited(path, MAX_TEXT_FILE_BYTES).await
}

#[cfg(unix)]
fn permissions_string(meta: &std::fs::Metadata) -> String {
    use std::os::unix::fs::PermissionsExt;
    format!("{:o}", meta.permissions().mode() & 0o777)
}

#[cfg(not(unix))]
fn permissions_string(_meta: &std::fs::Metadata) -> String {
    "unknown".to_string()
}

fn access_denied(path: &str) -> String {
    format!("Access denied - path outside allowed directories: {path}")
}

fn rfc3339(time: std::io::Result<std::time::SystemTime>) -> String {
    let Some(seconds) = time.ok().and_then(unix_seconds) else {
        return "unknown".to_string();
    };
    let days = seconds.div_euclid(86_400);
    let rem = seconds.rem_euclid(86_400);
    let (h, m, s) = (rem / 3600, (rem % 3600) / 60, rem % 60);
    let (y, mo, d) = civil_from_days(days);
    format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z")
}

fn unix_seconds(time: std::time::SystemTime) -> Option<i64> {
    match time.duration_since(std::time::UNIX_EPOCH) {
        Ok(duration) => i64::try_from(duration.as_secs()).ok(),
        Err(error) => i64::try_from(error.duration().as_secs())
            .ok()
            .and_then(i64::checked_neg),
    }
}

/// Civil-from-days algorithm (Howard Hinnant), returns (year, month, day).
fn civil_from_days(z: i64) -> (i64, i64, i64) {
    let z = z + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    (if m <= 2 { y + 1 } else { y }, m, d)
}

#[tool_handler(router = self.tool_router)]
impl ServerHandler for FilesystemServer {
    fn initialize(
        &self,
        _request: rmcp::model::InitializeRequestParams,
        _context: rmcp::service::RequestContext<rmcp::RoleServer>,
    ) -> impl std::future::Future<Output = Result<rmcp::model::InitializeResult, McpError>>
    + rmcp::service::MaybeSendFuture
    + '_ {
        crate::support::reject_unsupported_initialize()
    }

    fn supported_protocol_versions(
        &self,
    ) -> std::borrow::Cow<'static, [rmcp::model::ProtocolVersion]> {
        std::borrow::Cow::Borrowed(crate::support::SUPPORTED_PROTOCOL_VERSIONS)
    }

    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(if self.tool_router.list_all().is_empty() {
            ServerCapabilities::builder().build()
        } else {
            ServerCapabilities::builder().enable_tools().build()
        })
        .with_server_info(Implementation::new(
            "tuls-filesystem",
            env!("CARGO_PKG_VERSION"),
        ))
        .with_instructions(
            "This server provides secure file access restricted to the allowed \
                 directories passed on the command line. Use list_allowed_directories \
                 to see which directories are currently accessible.",
        )
    }

    fn get_tool(&self, name: &str) -> Option<Tool> {
        self.tool_router.get(name).cloned()
    }
}

/// Start the filesystem server on stdio.
pub async fn run(options: DirectoryServerOptions) -> anyhow::Result<()> {
    use rmcp::ServiceExt;
    use rmcp::transport::stdio;

    let access = AccessControl::from_args(&options.dirs).map_err(anyhow::Error::msg)?;
    let policy = ToolPolicy::from_selectors(&options.tools.allow, &options.tools.deny, TOOL_SPECS)
        .map_err(anyhow::Error::msg)?;

    let server = FilesystemServer::new(access, policy);
    let service = server
        .serve(stdio())
        .await
        .inspect_err(|e| tracing::error!("serving error: {e:?}"))?;
    tracing::info!("Filesystem MCP server running on stdio (MCP {SPEC_VERSION})");

    service.waiting().await?;
    Ok(())
}

#[cfg(test)]
mod tests;