App-Raider

 view release on metacpan or  search on metacpan

lib/App/Raider/FileTools.pm  view on Meta::CPAN

sub build_file_tools_server {
  my %args = @_;
  my $root = defined $args{root} ? path($args{root})->absolute : undef;

  my $resolve = sub {
    my ($path) = @_;
    my $p = path($path);
    if ($root) {
      $p = $p->is_absolute ? $p : $root->child($path);
      $p = $p->absolute;
      die "Path escapes root: $p\n" unless $root->subsumes($p);
    }
    return $p;
  };

  my $server = MCP::Server->new(name => 'app-raider-files', version => '1.0');

  $server->tool(
    name         => 'list_files',
    description  => 'List entries in a directory. Directories are suffixed with "/".',
    input_schema => {

t/10_filetools.t  view on Meta::CPAN

  });
  like($edit->{content}[0]{text}, qr/Edited/, 'edit reports success');
  is(path($dir, 'new.txt')->slurp_utf8, "one\nTWO\n", 'content edited');

  my $miss = call_tool('edit_file', {
    path => 'new.txt', old_string => 'nope', new_string => 'x',
  });
  ok($miss->{isError}, 'missing old_string is an error');
};

subtest root_escape => sub {
  my $res = call_tool('read_file', { path => '../outside' });
  ok($res->{isError}, 'path escape rejected');
};

done_testing;



( run in 1.959 second using v1.01-cache-2.11-cpan-39bf76dae61 )