AnyEvent-MCP
view release on metacpan or search on metacpan
},
sub {
my ($req, $args) = @_;
my ($workdir, $identity) = fs_workdir $args->{workdir};
ARRAY:: eq ref $args->{paths} and @{$args->{paths}} and @{$args->{paths}} <= 100
or die "stat needs 1..100 paths\n";
my @paths = map { fs_path $workdir, $_ } @{$args->{paths}};
die "stat paths must be inside the selected workdir\n"
if grep { !fs_inside ($_, $workdir) } @paths;
my $granted = fs_require_paths $req, $workdir, $identity, \@paths, "read", $args->{reason};
my @log = @paths;
splice @log, 3, @log - 3, "..." if @log > 3;
AE::log debug => join " ", "fs stat", @log;
perl_eval $req, $workdir, $granted, $stat, [map { AnyEvent::MCP::utf8enc $_ } @paths],
30, sub {
my ($success, $result) = @_;
return $req->error ($result)
unless $success;
@$result == @paths
or return $req->error ("invalid stat response\n");
my @result;
for my $i (0 .. $#paths) {
my ($ok, @s) = unpack "(w/a*)*", $result->[$i];
if ($ok) {
my $type = shift @s;
push @result, +{
path => $paths[$i],
type => $type,
size => 0 + $s[7],
mode => (sprintf "%04o", $s[2] & 07777),
uid => 0 + $s[4],
gid => 0 + $s[5],
mtime => 0 + $s[9],
};
} else {
push @result, +{
path => $paths[$i],
error => AnyEvent::MCP::utf8dec $s[0],
};
}
}
$req->reply (fs_result { result => \@result });
};
}
;
mcp_tool exec =>
"Run argv " . ($bwrap ? "in bubblewrap. " : "UNSANDBOXED with full host-user privileges. ")
. ($bwrap ? "Private /tmp by default (gone after exec); use for temp files/IPC. shared-tmp exposes HOST /tmp (sockets/services). Request ONLY for HOST /tmp, NOT ordinary temp/IPC. " : "")
. "stdin_encoding: utf-8 (default), latin1 or base64; max 4 MiB decoded. "
. "stdout/stderr UTF-8, captured to 1 MiB each; max_lines/max_bytes bound response. Optional grep: PCRE stdout filter after capture; stderr unfiltered. Reports capture truncation. "
. "Explicit sandbox_grants replace default workdir-read. Exit/signal/timeout are normal results, not tool errors. No command-prefix safety classification. Timeout >300 s requires "
. "long-running grant; max six hours.",
{
type => "object",
properties => {
%output_schema,
workdir => $workdir_schema,
argv => {
type => "array",
items => { type => "string" },
minItems => 1,
},
sandbox_grants => {
type => "array",
description => "Providing sandbox_grants (even []) replaces this default entirely; otherwise workdir-read under bubblewrap. Without bubblewrap: full host authority. host-proc unavailable under bubblewrap.",
items => {
type => "string",
enum => [sort keys %sandbox_grant],
},
},
reason => {
type => "string",
description => "Why command needs authority.",
},
stdin => { type => "string" },
stdin_encoding => {
type => "string",
default => "utf-8",
enum => [qw(utf-8 latin1 base64)],
description => "stdin bytes: UTF-8 (default), Latin-1 code points U+0000..U+00FF, or canonical Base64; max 4 MiB decoded.",
},
timeout => {
type => "integer",
minimum => 1,
maximum => 21600,
default => 30,
description => "Timeout seconds; >300 also requires long-running grant.",
},
},
required => [qw(argv reason)],
additionalProperties => JSON::XS::false,
},
sub {
my ($req, $args) = @_;
my $output = fs_output_options $args;
ARRAY:: eq ref $args->{argv} and @{$args->{argv}}
or die "exec needs argv strings without NUL\n";
for my $arg (@{$args->{argv}}) {
defined $arg and !ref $arg and $arg !~ /\0/
or die "exec needs argv strings without NUL\n";
}
length $args->{argv}[0] or die "exec needs an executable\n";
my ($workdir, $identity) = fs_workdir $args->{workdir};
my $timeout = $args->{timeout} // 30;
$timeout >= 1 && $timeout <= 21600 && $timeout == int $timeout
or die "invalid timeout\n";
my $sandbox_grants = [exists $args->{sandbox_grants} ? @{$args->{sandbox_grants}} : @exec_default_sandbox_grants];
push @$sandbox_grants, "long-running" if $timeout > 300;
$sandbox_grants = [fs_effective_grants @$sandbox_grants];
my $requested = $permissions->closure ($sandbox_grants);
die "host-proc sandbox authority is unresolved and unavailable in v1\n"
if $bwrap && $requested->{"host-proc"};
my $stdin_text = $args->{stdin} // "";
my $stdin_encoding = $args->{stdin_encoding} // "utf-8";
$stdin_encoding =~ /\A(?:utf-8|latin1|base64)\z/
or die "invalid stdin_encoding\n";
my $input;
if ($stdin_encoding eq "utf-8") {
$input = AnyEvent::MCP::utf8enc $stdin_text;
} elsif ($stdin_encoding eq "latin1") {
$stdin_text !~ /[^\x00-\xff]/
or die "latin1 stdin contains code points above U+00FF\n";
$input = pack "C*", unpack "U*", $stdin_text;
} else {
$input = MIME::Base64::decode_base64 $stdin_text;
MIME::Base64::encode_base64 ($input, "") eq $stdin_text
or die "invalid base64 stdin\n";
}
length $input <= 4 * 1024 * 1024 or die "stdin exceeds 4 MiB\n";
my $stdin_detail = $stdin_text;
if (length $input && $stdin_encoding ne "utf-8") {
my $preview = substr $input, 0, 4096;
$preview =~ s/([^\x20-\x5b\x5d-\x7e])/sprintf "\\x{%02X}", ord $1/ge;
$stdin_detail = "$stdin_encoding stdin: " . length ($input) . " bytes (escaped preview)\n\n" . $preview;
$stdin_detail .= "\n... (preview limited to 4096 bytes)"
if length $input > 4096;
}
my $coder = JSON::XS->new->canonical;
my $subject = $coder->encode ($args->{argv});
my $executable = $coder->encode ($args->{argv}[0]);
my $command_label = length $subject > 100 ? substr ($subject, 0, 97) . "..." : $subject;
( run in 1.166 second using v1.01-cache-2.11-cpan-234fa220139 )