« blib/lib/Langertha/Raider/ConnectCheck.pm
↑ Summary
blib/lib/Langertha/Raider/CLI/PromptBuilder.pm »
| 1 | package Langertha::Raider::FileTools; | ||||||||||||||||||||||||||||
| 2 | 76 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: MCP::Server factory with local filesystem tools (list/read/write/edit) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 76 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 76 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 76 | ▶ | use Cwd qw( realpath ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 76 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 76 | ▶ | use MCP::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 76 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 76 | our @EXPORT_OK = qw( build_file_tools_server ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 299 | ▶ | sub build_file_tools_server { | ||||||||||||||||||||||||||
|
Subroutine
build_file_tools_server: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 299 | my %args = @_; | |||||||||||||||||||||||||||
| 17 | 299 | ▶ | my $root = defined $args{root} ? path($args{root})->absolute : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 299 | ▶ | my $root_realpath = $root ? realpath($root) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 299 | ▶ | die "Root does not exist: $root\n" if $root && !defined $root_realpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$root and not defined $root_realpath
MC/DC0%0/2
$root and not defined $root_realpath
$root
not defined $root_realpath
Truth table66%2/3
A and not(B)
A= $rootB= not defined $root_realpath
| |||||||||||||||||||||||||||||
| 20 | 299 | ▶ | my $real_root = $root ? path($root_realpath)->absolute : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 13 | ▶ | my $assert_in_root = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 23 | 13 | my ($p) = @_; | |||||||||||||||||||||||||||
| 24 | 13 | ▶ | return unless $real_root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 13 | ▶ | die "Path escapes root: $p\n" unless $real_root->subsumes($p); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 10 | return; | |||||||||||||||||||||||||||
| 27 | 299 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | my $nearest_existing = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 30 | 9 | my ($p) = @_; | |||||||||||||||||||||||||||
| 31 | 9 | my $cur = $p; | |||||||||||||||||||||||||||
| 32 | 9 | ▶ | while (!-e $cur) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 3 | my $parent = $cur->parent; | |||||||||||||||||||||||||||
| 34 | 3 | ▶ | die "No existing parent for $p\n" if "$parent" eq "$cur"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 3 | $cur = $parent; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 9 | return $cur; | |||||||||||||||||||||||||||
| 38 | 299 | }; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 8 | ▶ | my $resolve = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 41 | 8 | my ($path) = @_; | |||||||||||||||||||||||||||
| 42 | 8 | my $p = path($path); | |||||||||||||||||||||||||||
| 43 | 8 | ▶ | if ($root) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 8 | ▶ | $p = $p->is_absolute ? $p : $root->child($path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 8 | $p = $p->absolute; | |||||||||||||||||||||||||||
| 46 | 8 | my $existing = $nearest_existing->($p); | |||||||||||||||||||||||||||
| 47 | 8 | ▶ | my $existing_realpath = realpath($existing) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | or die "Cannot resolve path: $existing\n"; | ||||||||||||||||||||||||||||
| 49 | 8 | my $real_existing = path($existing_realpath)->absolute; | |||||||||||||||||||||||||||
| 50 | 8 | $assert_in_root->($real_existing); | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | if (-e $p) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | my $p_realpath = realpath($p) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | or die "Cannot resolve path: $p\n"; | ||||||||||||||||||||||||||||
| 54 | 4 | my $real_p = path($p_realpath)->absolute; | |||||||||||||||||||||||||||
| 55 | 4 | $assert_in_root->($real_p); | |||||||||||||||||||||||||||
| 56 | 4 | $p = $real_p; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 5 | return $p; | |||||||||||||||||||||||||||
| 60 | 299 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | my $resolve_for_write = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 63 | 2 | my ($path) = @_; | |||||||||||||||||||||||||||
| 64 | 2 | my $p = $resolve->($path); | |||||||||||||||||||||||||||
| 65 | 1 | ▶ | return $p if -e $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | my $parent = $p->parent; | |||||||||||||||||||||||||||
| 67 | 1 | my $existing = $nearest_existing->($parent); | |||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $existing_realpath = realpath($existing) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | or die "Cannot resolve path: $existing\n"; | ||||||||||||||||||||||||||||
| 70 | 1 | my $real_existing = path($existing_realpath)->absolute; | |||||||||||||||||||||||||||
| 71 | 1 | $assert_in_root->($real_existing); | |||||||||||||||||||||||||||
| 72 | 1 | return $p; | |||||||||||||||||||||||||||
| 73 | 299 | }; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 299 | my $server = MCP::Server->new(name => 'raider-files', version => '1.0'); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | $server->tool( | ||||||||||||||||||||||||||||
| 78 | name => 'list_files', | ||||||||||||||||||||||||||||
| 79 | description => 'List entries in a directory. Directories are suffixed with "/".', | ||||||||||||||||||||||||||||
| 80 | input_schema => { | ||||||||||||||||||||||||||||
| 81 | type => 'object', | ||||||||||||||||||||||||||||
| 82 | properties => { path => { type => 'string', description => 'Directory path' } }, | ||||||||||||||||||||||||||||
| 83 | required => ['path'], | ||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 86 | 1 | my ($tool, $in) = @_; | |||||||||||||||||||||||||||
| 87 | 1 | my $p = eval { $resolve->($in->{path}) }; | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | return $tool->text_result("Error: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | return $tool->text_result("Error: not a directory: $p", 1) unless -d $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my @entries = sort map { -d $_ ? $_->basename . '/' : $_->basename } $p->children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | return $tool->text_result(join("\n", @entries)); | |||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | 299 | ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | $server->tool( | ||||||||||||||||||||||||||||
| 96 | name => 'read_file', | ||||||||||||||||||||||||||||
| 97 | description => 'Read the full contents of a text file.', | ||||||||||||||||||||||||||||
| 98 | input_schema => { | ||||||||||||||||||||||||||||
| 99 | type => 'object', | ||||||||||||||||||||||||||||
| 100 | properties => { path => { type => 'string', description => 'File path' } }, | ||||||||||||||||||||||||||||
| 101 | required => ['path'], | ||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||
| 103 | 3 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 104 | 3 | my ($tool, $in) = @_; | |||||||||||||||||||||||||||
| 105 | 3 | my $p = eval { $resolve->($in->{path}) }; | |||||||||||||||||||||||||||
| 106 | 3 | ▶ | return $tool->text_result("Error: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | return $tool->text_result("Error: not a file: $p", 1) unless -f $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | my $content = eval { $p->slurp_utf8 }; | |||||||||||||||||||||||||||
| 109 | 1 | ▶ | return $tool->text_result("Error reading $p: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | return $tool->text_result($content); | |||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | 299 | ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | $server->tool( | ||||||||||||||||||||||||||||
| 115 | name => 'write_file', | ||||||||||||||||||||||||||||
| 116 | description => 'Write contents to a file. Creates parent directories, overwrites existing files.', | ||||||||||||||||||||||||||||
| 117 | input_schema => { | ||||||||||||||||||||||||||||
| 118 | type => 'object', | ||||||||||||||||||||||||||||
| 119 | properties => { | ||||||||||||||||||||||||||||
| 120 | path => { type => 'string', description => 'File path' }, | ||||||||||||||||||||||||||||
| 121 | content => { type => 'string', description => 'Full file contents' }, | ||||||||||||||||||||||||||||
| 122 | }, | ||||||||||||||||||||||||||||
| 123 | required => ['path', 'content'], | ||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||
| 125 | 2 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 126 | 2 | my ($tool, $in) = @_; | |||||||||||||||||||||||||||
| 127 | 2 | my $p = eval { $resolve_for_write->($in->{path}) }; | |||||||||||||||||||||||||||
| 128 | 2 | ▶ | return $tool->text_result("Error: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | eval { | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | $p->parent->mkpath unless -d $p->parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $p->spew_utf8($in->{content}); | |||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | return $tool->text_result("Error writing $p: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | return $tool->text_result("Wrote " . length($in->{content}) . " bytes to $p"); | |||||||||||||||||||||||||||
| 135 | }, | ||||||||||||||||||||||||||||
| 136 | 299 | ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $server->tool( | ||||||||||||||||||||||||||||
| 139 | name => 'edit_file', | ||||||||||||||||||||||||||||
| 140 | description => 'Replace an exact unique substring in a file (old_string must match exactly once).', | ||||||||||||||||||||||||||||
| 141 | input_schema => { | ||||||||||||||||||||||||||||
| 142 | type => 'object', | ||||||||||||||||||||||||||||
| 143 | properties => { | ||||||||||||||||||||||||||||
| 144 | path => { type => 'string', description => 'File path' }, | ||||||||||||||||||||||||||||
| 145 | old_string => { type => 'string', description => 'Exact text to replace' }, | ||||||||||||||||||||||||||||
| 146 | new_string => { type => 'string', description => 'Replacement text' }, | ||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||
| 148 | required => ['path', 'old_string', 'new_string'], | ||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | 2 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 151 | 2 | my ($tool, $in) = @_; | |||||||||||||||||||||||||||
| 152 | 2 | my $p = eval { $resolve->($in->{path}) }; | |||||||||||||||||||||||||||
| 153 | 2 | ▶ | return $tool->text_result("Error: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | return $tool->text_result("Error: not a file: $p", 1) unless -f $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 2 | my $content = eval { $p->slurp_utf8 }; | |||||||||||||||||||||||||||
| 156 | 2 | ▶ | return $tool->text_result("Error reading $p: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 2 | my $old = $in->{old_string}; | |||||||||||||||||||||||||||
| 158 | 2 | my $count = () = $content =~ /\Q$old\E/g; | |||||||||||||||||||||||||||
| 159 | 2 | ▶ | return $tool->text_result("Error: old_string not found", 1) if $count == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | ▶ | return $tool->text_result("Error: old_string matches $count times, must be unique", 1) if $count > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 1 | $content =~ s/\Q$old\E/$in->{new_string}/; | |||||||||||||||||||||||||||
| 162 | 1 | eval { $p->spew_utf8($content) }; | |||||||||||||||||||||||||||
| 163 | 1 | ▶ | return $tool->text_result("Error writing $p: $@", 1) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | return $tool->text_result("Edited $p"); | |||||||||||||||||||||||||||
| 165 | }, | ||||||||||||||||||||||||||||
| 166 | 299 | ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 299 | return $server; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 76 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =pod | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 NAME | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Langertha::Raider::FileTools - MCP::Server factory with local filesystem tools (list/read/write/edit) | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 VERSION | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | version 0.503 | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The file tools of L<raider>: an L<MCP::Server> with C<list_files(path)>, | ||||||||||||||||||||||||||||
| 190 | C<read_file(path)>, C<write_file(path, content)> and | ||||||||||||||||||||||||||||
| 191 | C<edit_file(path, old_string, new_string)>, confined to one directory. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 build_file_tools_server | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | my $server = Langertha::Raider::FileTools::build_file_tools_server( | ||||||||||||||||||||||||||||
| 196 | root => '/some/dir', # optional confinement root | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Returns an L<MCP::Server> instance with the tools C<list_files>, C<read_file>, | ||||||||||||||||||||||||||||
| 200 | C<write_file>, and C<edit_file> registered. When C<root> is set, all path | ||||||||||||||||||||||||||||
| 201 | arguments are confined to that directory. Existing paths and nearest existing | ||||||||||||||||||||||||||||
| 202 | parents are resolved with C<realpath>, so symlinks cannot be used to escape the | ||||||||||||||||||||||||||||
| 203 | root. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =over | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =item * L<MCP::Server> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =item * L<Langertha::Raider::CLI> | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =back | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head2 Issues | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 220 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 IRC | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 239 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||