intermcp 0.3.0

Ultra-fast, safe Model Context Protocol (MCP) engine and multiplexing hub in pure Rust, built for Interlayer Blockchain and open for all
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
use crate::error::FastMcpError;
use std::path::{Component, Path, PathBuf};

#[cfg(windows)]
pub mod win32 {
    use std::os::windows::ffi::OsStrExt;
    use std::os::windows::io::RawHandle;
    use std::path::Path;
    use windows::core::PCWSTR;
    use windows::Win32::Foundation::{CloseHandle, HANDLE};
    use windows::Win32::Storage::FileSystem::{
        CreateFileW, GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION, CREATE_NEW,
        FILE_ATTRIBUTE_REPARSE_POINT, FILE_FLAGS_AND_ATTRIBUTES, FILE_FLAG_BACKUP_SEMANTICS,
        FILE_FLAG_OPEN_REPARSE_POINT, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_SHARE_DELETE,
        FILE_SHARE_MODE, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
    };

    pub fn check_no_reparse_point_and_no_hardlink(
        path: &Path,
    ) -> Result<(bool, bool), std::io::Error> {
        let mut wide: Vec<u16> = path.as_os_str().encode_wide().collect();
        wide.push(0);

        // # Safety
        // Calls Win32 CreateFileW with PCWSTR pointing to a valid null-terminated UTF-16 buffer.
        let handle = unsafe {
            CreateFileW(
                PCWSTR(wide.as_ptr()),
                FILE_GENERIC_READ.0,
                FILE_SHARE_MODE(FILE_SHARE_READ.0 | FILE_SHARE_WRITE.0 | FILE_SHARE_DELETE.0),
                None,
                OPEN_EXISTING,
                FILE_FLAGS_AND_ATTRIBUTES(
                    FILE_FLAG_OPEN_REPARSE_POINT.0 | FILE_FLAG_BACKUP_SEMANTICS.0,
                ),
                HANDLE::default(),
            )
        };

        match handle {
            Ok(h) => {
                let mut info = BY_HANDLE_FILE_INFORMATION::default();
                // # Safety
                // `h` is a valid handle and `info` points to a stack-allocated BY_HANDLE_FILE_INFORMATION.
                let res = unsafe { GetFileInformationByHandle(h, &mut info) };
                // # Safety
                // `h` is an open Win32 HANDLE that is closed once.
                unsafe {
                    let _ = CloseHandle(h);
                };

                if res.is_ok() {
                    let is_reparse = (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT.0) != 0;
                    let is_hardlink = info.nNumberOfLinks > 1;
                    Ok((is_reparse, is_hardlink))
                } else {
                    Err(std::io::Error::last_os_error())
                }
            }
            Err(e) => Err(std::io::Error::from_raw_os_error(e.code().0)),
        }
    }

    pub fn check_is_hardlink(path: &Path) -> Result<bool, std::io::Error> {
        check_no_reparse_point_and_no_hardlink(path).map(|(_, is_hl)| is_hl)
    }

    pub fn check_is_hardlink_handle(raw_handle: RawHandle) -> Result<bool, std::io::Error> {
        let handle = HANDLE(raw_handle as _);
        let mut info = BY_HANDLE_FILE_INFORMATION::default();
        // # Safety
        // `handle` is checked and borrowed from an active File; info is a valid pointer.
        let res = unsafe { GetFileInformationByHandle(handle, &mut info) };
        if res.is_ok() {
            Ok(info.nNumberOfLinks > 1)
        } else {
            Err(std::io::Error::last_os_error())
        }
    }

    pub fn open_new_or_verify_reparse_point(path: &Path) -> Result<(), std::io::Error> {
        if path.exists() {
            let (is_reparse, is_hardlink) = check_no_reparse_point_and_no_hardlink(path)?;
            if is_reparse {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::PermissionDenied,
                    "Reparse point / symlink target prohibited",
                ));
            }
            if is_hardlink {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::PermissionDenied,
                    "Hardlink target prohibited",
                ));
            }
            Ok(())
        } else {
            let mut wide: Vec<u16> = path.as_os_str().encode_wide().collect();
            wide.push(0);

            // # Safety
            // Calls Win32 CreateFileW with CREATE_NEW to atomically create a new file and reject existing symlinks.
            let handle = unsafe {
                CreateFileW(
                    PCWSTR(wide.as_ptr()),
                    FILE_GENERIC_WRITE.0,
                    FILE_SHARE_MODE(FILE_SHARE_READ.0 | FILE_SHARE_WRITE.0 | FILE_SHARE_DELETE.0),
                    None,
                    CREATE_NEW,
                    FILE_FLAGS_AND_ATTRIBUTES(FILE_FLAG_OPEN_REPARSE_POINT.0),
                    HANDLE::default(),
                )
            };

            match handle {
                Ok(h) => {
                    // # Safety
                    // `h` is safely closed.
                    unsafe {
                        let _ = CloseHandle(h);
                    };
                    Ok(())
                }
                Err(e) => Err(std::io::Error::from_raw_os_error(e.code().0)),
            }
        }
    }
}

pub trait HardlinkExt {
    fn is_hardlink(&self) -> bool;
}

impl HardlinkExt for Path {
    fn is_hardlink(&self) -> bool {
        #[cfg(unix)]
        {
            if let Ok(meta) = std::fs::symlink_metadata(self) {
                use std::os::unix::fs::MetadataExt;
                return meta.is_file() && meta.nlink() > 1;
            }
            false
        }
        #[cfg(windows)]
        {
            win32::check_is_hardlink(self).unwrap_or(false)
        }
        #[cfg(not(any(unix, windows)))]
        {
            false
        }
    }
}

impl HardlinkExt for PathBuf {
    fn is_hardlink(&self) -> bool {
        self.as_path().is_hardlink()
    }
}

impl HardlinkExt for std::fs::Metadata {
    fn is_hardlink(&self) -> bool {
        if !self.is_file() {
            return false;
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            self.nlink() > 1
        }
        #[cfg(not(unix))]
        {
            false
        }
    }
}

#[inline]
fn path_components_equal(a: &Path, b: &Path) -> bool {
    #[cfg(any(windows, target_os = "macos"))]
    {
        let a_str = a.as_os_str().to_string_lossy();
        let b_str = b.as_os_str().to_string_lossy();
        a_str.eq_ignore_ascii_case(&b_str)
    }
    #[cfg(not(any(windows, target_os = "macos")))]
    {
        a == b
    }
}

const DEFAULT_SENSITIVE_FILES: &[&str] = &[
    ".env",
    ".env.local",
    ".env.production",
    ".env.development",
    ".netrc",
    ".pgpass",
    ".bash_history",
    ".zsh_history",
    "kubeconfig",
    "credentials.json",
    "service_account.json",
    "vault.json",
    ".npmrc",
    "secrets.json",
    "secret.json",
    "token.json",
    "id_rsa",
    "id_ed25519",
    "id_ecdsa",
    "id_dsa",
];

const DEFAULT_SENSITIVE_KEYWORDS: &[&str] = &[
    "secret",
    "token",
    "key",
    "password",
    "credential",
    "mnemonic",
    "seed",
    "wallet",
    "private",
];

const SENSITIVE_EXTENSIONS: &[&str] = &["pem", "key", "pkcs12", "pfx"];

#[derive(Clone, Debug)]
pub struct SandboxPolicy {
    allowed_roots: Vec<PathBuf>,
    enabled: bool,
    shield_secrets: bool,
    custom_sensitive_files: Vec<String>,
    custom_sensitive_keywords: Vec<String>,
}

impl SandboxPolicy {
    pub fn unrestricted() -> Self {
        Self {
            allowed_roots: Vec::new(),
            enabled: false,
            shield_secrets: true,
            custom_sensitive_files: Vec::new(),
            custom_sensitive_keywords: Vec::new(),
        }
    }

    pub fn new(roots: Vec<PathBuf>) -> Self {
        let mut canonical_roots: Vec<PathBuf> = Vec::new();
        for p in roots {
            if let Ok(c) = dunce::canonicalize(&p) {
                if !canonical_roots.iter().any(|r| path_components_equal(r, &c)) {
                    canonical_roots.push(c);
                }
            }
            if !canonical_roots.iter().any(|r| path_components_equal(r, &p)) {
                canonical_roots.push(p);
            }
        }

        Self {
            allowed_roots: canonical_roots,
            enabled: true,
            shield_secrets: true,
            custom_sensitive_files: Vec::new(),
            custom_sensitive_keywords: Vec::new(),
        }
    }

    pub fn with_secret_shield(mut self, enabled: bool) -> Self {
        self.shield_secrets = enabled;
        self
    }

    pub fn with_additional_sensitive_files(mut self, files: Vec<String>) -> Self {
        self.custom_sensitive_files.extend(files);
        self
    }

    pub fn with_additional_sensitive_keywords(mut self, keywords: Vec<String>) -> Self {
        self.custom_sensitive_keywords.extend(keywords);
        self
    }

    pub fn is_reserved_device_name(name: &str) -> bool {
        let stem = match name.split('.').next() {
            Some(s) => s.to_ascii_uppercase(),
            None => return false,
        };
        matches!(
            stem.as_str(),
            "CON"
                | "PRN"
                | "AUX"
                | "NUL"
                | "CLOCK$"
                | "CONIN$"
                | "CONOUT$"
                | "COM1"
                | "COM2"
                | "COM3"
                | "COM4"
                | "COM5"
                | "COM6"
                | "COM7"
                | "COM8"
                | "COM9"
                | "LPT1"
                | "LPT2"
                | "LPT3"
                | "LPT4"
                | "LPT5"
                | "LPT6"
                | "LPT7"
                | "LPT8"
                | "LPT9"
        )
    }

    pub fn is_windows_short_name(name: &str) -> bool {
        if let Some(pos) = name.find('~') {
            let rest = &name[pos + 1..];
            let digits = rest.split('.').next().unwrap_or("");
            if !digits.is_empty() && digits.chars().all(|c| c.is_ascii_digit()) {
                return true;
            }
            if rest
                .chars()
                .next()
                .map(|c| c.is_ascii_digit())
                .unwrap_or(false)
            {
                return true;
            }
        }
        false
    }

    pub fn is_sensitive_path(&self, path: &Path) -> bool {
        Self::check_sensitive_path(
            path,
            &self.custom_sensitive_files,
            &self.custom_sensitive_keywords,
        )
    }

    pub fn is_sensitive_path_default(path: &Path) -> bool {
        Self::check_sensitive_path(path, &[], &[])
    }

    pub fn check_sensitive_path(
        path: &Path,
        custom_files: &[String],
        custom_keywords: &[String],
    ) -> bool {
        let path_str = path.to_string_lossy().to_lowercase().replace('\\', "/");

        if path_str.contains(".docker/config.json") || path_str.contains("gcloud/credentials.db") {
            return true;
        }

        for component in path.components() {
            if let Component::Normal(c) = component {
                let segment = c.to_string_lossy().to_lowercase();
                if segment == ".ssh"
                    || segment == ".aws"
                    || segment == ".git"
                    || segment == ".gnupg"
                    || segment == ".docker"
                {
                    return true;
                }
            }
        }

        if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
            let lower_name = file_name.to_lowercase();
            for sensitive in DEFAULT_SENSITIVE_FILES {
                if lower_name == *sensitive || lower_name.starts_with(".env.") {
                    return true;
                }
            }
            for custom in custom_files {
                if lower_name == custom.to_lowercase() {
                    return true;
                }
            }

            if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                let lower_stem = stem.to_lowercase();
                for kw in DEFAULT_SENSITIVE_KEYWORDS {
                    if lower_stem.contains(kw) {
                        return true;
                    }
                }
                for kw in custom_keywords {
                    if lower_stem.contains(&kw.to_lowercase()) {
                        return true;
                    }
                }
            }

            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                let ext_lower = ext.to_lowercase();
                for sensitive_ext in SENSITIVE_EXTENSIONS {
                    if ext_lower == *sensitive_ext {
                        return true;
                    }
                }
            }
        }

        false
    }

    pub fn validate_path(&self, requested: &Path) -> Result<PathBuf, FastMcpError> {
        let raw_str = requested.to_string_lossy();
        if raw_str.contains('\0') {
            return Err(FastMcpError::ToolExecution(
                "Access denied: Null bytes are prohibited in file paths.".into(),
            ));
        }

        if raw_str.starts_with(r"\\?\") || raw_str.starts_with("//?/") {
            return Err(FastMcpError::ToolExecution(
                "Access denied: Verbatim UNC prefixes (\\\\?\\) are prohibited.".into(),
            ));
        }

        if raw_str.contains("//") || raw_str.contains(r"\\") {
            return Err(FastMcpError::ToolExecution(
                "Access denied: Consecutive path separators are prohibited.".into(),
            ));
        }

        #[cfg(not(windows))]
        if raw_str.len() >= 2
            && raw_str.as_bytes()[1] == b':'
            && raw_str.as_bytes()[0].is_ascii_alphabetic()
        {
            return Err(FastMcpError::ToolExecution(format!(
                "Access denied: Windows drive path '{}' is outside Unix root.",
                raw_str
            )));
        }

        for segment in raw_str.split(['/', '\\']) {
            if Self::is_reserved_device_name(segment) {
                return Err(FastMcpError::ToolExecution(format!(
                    "Access denied: '{}' is a reserved NTFS device name.",
                    segment
                )));
            }
            if Self::is_windows_short_name(segment) {
                return Err(FastMcpError::ToolExecution(format!(
                    "Access denied: 8.3 short name alias '{}' is prohibited.",
                    segment
                )));
            }
        }

        if self.shield_secrets && self.is_sensitive_path(requested) {
            return Err(FastMcpError::ToolExecution(format!(
                "SafeFS Secret Shield: Access denied to '{}'. Credential and sensitive files are protected.",
                requested.display()
            )));
        }

        let mut existing_prefix = PathBuf::new();
        let mut non_existing_tail = Vec::new();
        let mut in_non_existing = false;

        for comp in requested.components() {
            match comp {
                Component::CurDir => {}
                Component::ParentDir => {
                    if in_non_existing {
                        return Err(FastMcpError::ToolExecution(
                            "Access denied: Parent directory traversal ('..') in non-existent path component is prohibited.".into(),
                        ));
                    }
                    existing_prefix.push("..");
                }
                Component::Prefix(p) => {
                    existing_prefix.push(p.as_os_str());
                }
                Component::RootDir => {
                    existing_prefix.push(Component::RootDir.as_os_str());
                }
                Component::Normal(c) => {
                    if in_non_existing {
                        non_existing_tail.push(c);
                    } else {
                        let candidate = existing_prefix.join(c);
                        if candidate.exists() {
                            existing_prefix = candidate;
                        } else {
                            in_non_existing = true;
                            non_existing_tail.push(c);
                        }
                    }
                }
            }
        }

        let base = if existing_prefix.as_os_str().is_empty() {
            dunce::canonicalize(Path::new(".")).map_err(|e| {
                FastMcpError::ToolExecution(format!("Failed to resolve base directory: {}", e))
            })?
        } else if existing_prefix.exists() {
            dunce::canonicalize(&existing_prefix).map_err(|e| {
                FastMcpError::ToolExecution(format!("Path canonicalization failed: {}", e))
            })?
        } else {
            return Err(FastMcpError::ToolExecution(format!(
                "Failed to resolve path prefix: '{}' does not exist",
                existing_prefix.display()
            )));
        };

        let mut target_to_check = base;
        for comp in non_existing_tail {
            target_to_check.push(comp);
        }

        let mut ancestor = target_to_check.as_path();
        loop {
            if ancestor.exists() {
                #[cfg(windows)]
                {
                    match win32::check_no_reparse_point_and_no_hardlink(ancestor) {
                        Ok((is_reparse, is_hardlink)) => {
                            if is_reparse {
                                return Err(FastMcpError::ToolExecution(format!(
                                    "SafeFS Violation: Reparse point/symlink detected at '{}'. Symlinks are prohibited.",
                                    ancestor.display()
                                )));
                            }
                            if is_hardlink {
                                return Err(FastMcpError::ToolExecution(format!(
                                    "SafeFS Violation: Hardlink detected at '{}'. Hardlinks are prohibited.",
                                    ancestor.display()
                                )));
                            }
                        }
                        Err(e) => {
                            return Err(FastMcpError::ToolExecution(format!(
                                "SafeFS Violation: Failed to inspect file attributes at '{}': {}",
                                ancestor.display(),
                                e
                            )));
                        }
                    }
                }
                #[cfg(unix)]
                {
                    if let Ok(meta) = std::fs::symlink_metadata(ancestor) {
                        if meta.file_type().is_symlink() {
                            return Err(FastMcpError::ToolExecution(format!(
                                "SafeFS Violation: Symlink detected in path component '{}'. Symlinks are prohibited.",
                                ancestor.display()
                            )));
                        }
                        if (ancestor.is_file() || meta.is_file())
                            && (ancestor.is_hardlink() || meta.is_hardlink())
                        {
                            return Err(FastMcpError::ToolExecution(format!(
                                "SafeFS Violation: Hardlink detected at '{}'. Hardlinks are prohibited.",
                                ancestor.display()
                            )));
                        }
                    }
                }
            }
            match ancestor.parent() {
                Some(p) if !p.as_os_str().is_empty() && p != ancestor => ancestor = p,
                _ => break,
            }
        }

        if target_to_check.exists() {
            #[cfg(windows)]
            {
                let (is_reparse, is_hardlink) =
                    win32::check_no_reparse_point_and_no_hardlink(&target_to_check).map_err(
                        |e| FastMcpError::ToolExecution(format!("Failed to verify target: {}", e)),
                    )?;
                if is_reparse {
                    return Err(FastMcpError::ToolExecution(format!(
                        "SafeFS Violation: Symlink/reparse point detected at target '{}'. Symlinks are prohibited.",
                        target_to_check.display()
                    )));
                }
                if is_hardlink {
                    return Err(FastMcpError::ToolExecution(format!(
                        "SafeFS Violation: Hardlink detected at target '{}'. Hardlinks are prohibited.",
                        target_to_check.display()
                    )));
                }
            }
            #[cfg(unix)]
            {
                if let Ok(meta) = std::fs::symlink_metadata(&target_to_check) {
                    if meta.file_type().is_symlink() {
                        return Err(FastMcpError::ToolExecution(format!(
                            "SafeFS Violation: Symlink detected at target '{}'. Symlinks are prohibited.",
                            target_to_check.display()
                        )));
                    }
                    if (target_to_check.is_file() || meta.is_file())
                        && (target_to_check.is_hardlink() || meta.is_hardlink())
                    {
                        return Err(FastMcpError::ToolExecution(format!(
                            "SafeFS Violation: Hardlink detected at '{}'. Hardlinks are prohibited.",
                            target_to_check.display()
                        )));
                    }
                }
            }
            target_to_check = dunce::canonicalize(&target_to_check).map_err(|e| {
                FastMcpError::ToolExecution(format!("Canonicalization failed: {}", e))
            })?;
        }

        if self.shield_secrets && self.is_sensitive_path(&target_to_check) {
            return Err(FastMcpError::ToolExecution(format!(
                "SafeFS Secret Shield: Access denied to '{}'.",
                target_to_check.display()
            )));
        }

        if !self.enabled || self.allowed_roots.is_empty() {
            return Ok(target_to_check);
        }

        for root in &self.allowed_roots {
            if target_to_check
                .ancestors()
                .any(|a| path_components_equal(a, root))
            {
                return Ok(target_to_check);
            }
        }

        Err(FastMcpError::ToolExecution(format!(
            "SafeFS Security Violation: Target '{}' escapes authorized directories: {:?}",
            target_to_check.display(),
            self.allowed_roots
        )))
    }

    pub fn open_for_write_no_follow(&self, requested: &Path) -> Result<PathBuf, FastMcpError> {
        let safe_path = self.validate_path(requested)?;

        if let Some(parent) = safe_path.parent() {
            if !parent.as_os_str().is_empty() && !parent.exists() {
                std::fs::create_dir_all(parent).map_err(|e| {
                    FastMcpError::ToolExecution(format!("Failed to create parent directory: {}", e))
                })?;
            }
        }

        #[cfg(windows)]
        {
            win32::open_new_or_verify_reparse_point(&safe_path).map_err(|e| {
                FastMcpError::ToolExecution(format!(
                    "SafeFS Violation: open_for_write_no_follow rejected '{}': {}",
                    safe_path.display(),
                    e
                ))
            })?;
        }

        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            if safe_path.exists() {
                let meta = std::fs::symlink_metadata(&safe_path).map_err(|e| {
                    FastMcpError::ToolExecution(format!("Failed to inspect target metadata: {}", e))
                })?;
                if meta.file_type().is_symlink() {
                    return Err(FastMcpError::ToolExecution(format!(
                        "SafeFS Violation: Symlink detected at target '{}'. Overwrite prohibited.",
                        safe_path.display()
                    )));
                }
                if meta.is_hardlink() {
                    return Err(FastMcpError::ToolExecution(format!(
                        "SafeFS Violation: Hardlink detected at target '{}'. Overwrite prohibited.",
                        safe_path.display()
                    )));
                }
                let _ = std::fs::OpenOptions::new()
                    .read(true)
                    .custom_flags(libc::O_NOFOLLOW)
                    .open(&safe_path)
                    .map_err(|e| {
                        FastMcpError::ToolExecution(format!(
                            "Symlink traversal blocked by O_NOFOLLOW: {}",
                            e
                        ))
                    })?;
            } else {
                let _ = std::fs::OpenOptions::new()
                    .write(true)
                    .create_new(true)
                    .custom_flags(libc::O_NOFOLLOW)
                    .open(&safe_path)
                    .map_err(|e| {
                        FastMcpError::ToolExecution(format!(
                            "Failed to create file with O_NOFOLLOW: {}",
                            e
                        ))
                    })?;
            }
        }

        Ok(safe_path)
    }
}