AnyEvent-MCP
view release on metacpan or search on metacpan
write_file "$dir/work/numbered-lines", "alpha\n\nomega";
$result = call "read-lines" => {
path => "numbered-lines", ranges => ["2,3", "1", "4"], linenums => JSON::XS::true,
};
is_deeply [map $_->{text}, @{$result->{content}}], [
"[range=2,3]", "2:\n3:omega", "[range=1]", "1:alpha\n", "[range=4]", "",
], "linenums handles blank lines, missing final newline and out-of-range requests";
ok join (",", @{$tool{"read-lines"}{inputSchema}{properties}{encoding}{enum}})
eq "utf-8,latin1,base64"
&& $tool{"read-lines"}{inputSchema}{properties}{encoding}{default} eq "utf-8",
"read-lines advertises three explicit byte representations with UTF-8 default";
ok $tool{"read-lines"}{inputSchema}{properties}{linenums}{type} eq "boolean"
&& !$tool{"read-lines"}{inputSchema}{properties}{linenums}{default},
"linenums is an optional boolean defaulting to false";
my $binary_lines = "A\xff\x00\nB\xe4\nC\x80";
write_file "$dir/work/binary-lines", $binary_lines;
$result = call "read-lines" => { path => "binary-lines", ranges => ["1,3"] };
ok $result->{isError} && $result->{content}[0]{text} =~ /UTF-8|utf-8|UTF8/,
"default UTF-8 rejects invalid input instead of guessing an encoding";
$result = call "read-lines" => { path => "binary-lines", ranges => ["1"], encoding => "utf-8" };
ok $result->{isError}, "explicit UTF-8 also rejects invalid bytes";
$result = call "read-lines" => {
path => "binary-lines", ranges => ["2,3", "1"], encoding => "latin1",
};
ok !$result->{isError} && @{$result->{content}} == 4
&& $result->{content}[0]{text} eq "[range=2,3]"
&& $result->{content}[1]{text} eq "B\xe4\nC\x80"
&& $result->{content}[2]{text} eq "[range=1]"
&& $result->{content}[3]{text} eq "A\xff\x00\n",
"Latin-1 keeps exact LF/NUL bytes and preserves missing final LF in the source entry";
$result = call "read-lines" => {
path => "binary-lines", ranges => ["2,3", "1"], encoding => "base64",
};
my $b64_first = MIME::Base64::encode_base64 ("B\xe4\nC\x80", "");
my $b64_second = MIME::Base64::encode_base64 ("A\xff\x00\n", "");
ok !$result->{isError} && @{$result->{content}} == 4
&& $result->{content}[0]{text} eq "[range=2,3]"
&& $result->{content}[1]{text} eq $b64_first
&& $result->{content}[2]{text} eq "[range=1]"
&& $result->{content}[3]{text} eq $b64_second,
"base64 independently encodes exact raw bytes in separate metadata and source entries";
$result = call "read-lines" => {
path => "binary-lines", ranges => ["3"], encoding => "base64",
};
ok @{$result->{content}} == 2
&& $result->{content}[0]{text} eq "[range=3]"
&& $result->{content}[1]{text} eq MIME::Base64::encode_base64 ("C\x80", ""),
"base64 preserves absence of the final newline inside the encoded bytes";
$result = call "read-lines" => {
path => "binary-lines", ranges => ["2,3"], encoding => "base64", linenums => JSON::XS::true,
};
ok !$result->{isError}
&& MIME::Base64::decode_base64 ($result->{content}[1]{text}) eq "2:B\xe4\n3:C\x80",
"base64 encodes numbered lines while retaining raw bytes and unterminated final line";
write_file "$dir/work/utf8-lines", "\xc3\xa4\n";
$result = call "read-lines" => { path => "utf8-lines", ranges => ["1"] };
ok !$result->{isError} && @{$result->{content}} == 2
&& $result->{content}[0]{text} eq "[range=1]"
&& $result->{content}[1]{text} eq chr (0xe4) . "\n",
"valid UTF-8 is decoded into a separate source entry with default behavior unchanged";
$result = call "read-lines" => { path => "utf8-lines", ranges => ["1"], encoding => "latin1" };
ok !$result->{isError} && @{$result->{content}} == 2
&& $result->{content}[0]{text} eq "[range=1]"
&& $result->{content}[1]{text} eq "\xc3\xa4\n",
"Latin-1 does not reinterpret valid UTF-8 byte sequences";
$result = call "read-lines" => { path => "read", ranges => ["1"], encoding => "utf-16" };
ok $result->{isError}, "unsupported read-lines encoding is rejected";
my $large_bytes = "X" x ((1 << 20) + 100);
write_file "$dir/work/large-lines", $large_bytes;
$result = call "read-lines" => { path => "large-lines", ranges => ["1"], encoding => "base64" };
my $large_b64 = MIME::Base64::encode_base64 (substr ($large_bytes, 0, 1 << 20), "");
ok !$result->{isError} && @{$result->{content}} == 2
&& $result->{content}[0]{text} eq "[range=1 truncated=1]"
&& $result->{content}[1]{text} eq $large_b64,
"base64 uses raw byte budget and puts truncated=1 only in range metadata";
$result = call "read-lines" => {
path => "large-lines", ranges => ["1"], encoding => "base64", linenums => JSON::XS::true,
};
ok !$result->{isError}
&& $result->{content}[0]{text} eq "[range=1 truncated=1]"
&& MIME::Base64::decode_base64 ($result->{content}[1]{text})
eq "1:" . ("X" x ((1 << 20) - 2)),
"numbering counts prefix bytes against the 1 MiB limit";
}
$result = call "file-write" => { path => "new", data => MIME::Base64::encode_base64 ("hello\n", "") };
ok $result->{structuredContent}{bytes} == 6 && read_file ("$dir/work/new") eq "hello\n",
"write receives module result";
# the write worker's different grants evict the cached read worker.
my $evicted = AnyEvent->condvar;
my $eviction_deadline = AnyEvent->timer (after => 30, cb => sub { die "Worker eviction timeout\n" });
my $eviction_poll = AnyEvent->timer (after => 0, interval => 0.01, cb => sub {
$evicted->send unless kill 0 => $cached_pid;
});
$evicted->recv;
undef $eviction_poll;
undef $eviction_deadline;
ok !(kill 0 => $cached_pid), "evicting an incompatible cached worker terminates and reaps it";
SKIP: {
skip "extended GNU find test (PERL_ANYEVENT_MCP_TESTS=1)", 3
unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
skip "GNU find is unavailable", 3 unless $utility{find};
$result = call list => {};
ok $result->{content}[0]{text} =~ /\tnew\n/, "compact listing preserved";
$result = call list => { path => "" };
ok !$result->{isError} && $result->{content}[0]{text} =~ /\tnew\n/,
"empty list path selects workdir";
mkdir "$dir/work/0" or die $!;
write_file "$dir/work/0/zero", "zero\n";
$result = call list => { path => "0" };
ok !$result->{isError} && $result->{content}[0]{text} =~ /\tzero\n/
&& $result->{content}[0]{text} !~ /\tnew\n/,
"directory named 0 is not treated as empty";
}
SKIP: {
skip "extended GNU find/grep tests (PERL_ANYEVENT_MCP_TESTS=1)", 9
for my $backend (qw(git svn cvs hg)) {
$result = call "vcs-diff" => { backend => $backend, files => ["-strange"] };
ok !$result->{isError} && $result->{structuredContent}{exit} == 1
&& $result->{structuredContent}{stdout} eq "header\nkeep: $backend\ntrailer\n", "$backend diff preserves differences status and output";
ok $prompts[-1]{action} eq "vcs-diff" && $prompts[-1]{dimensions}{subject}{value} eq $backend,
"$backend diff uses a fixed-operation permission instead of arbitrary exec";
ok join (",", sort @{$prompts[-1]{sandbox_grants}}) eq
($backend eq "cvs" ? "ssh-agent,workdir-read" : "workdir-read"),
"$backend diff requests its fixed sandbox grants";
my @log = split /\n/, read_file "$dir/vcs.log";
my $argv = JSON::XS::decode_json $log[-1];
ok $argv->[-1] eq ($backend eq "svn" ? "./-strange@" : "./-strange")
&& grep ($_ eq "--", @$argv), "$backend filenames cannot become options";
$result = call "vcs-status" => { backend => $backend, max_lines => 1 };
ok !$result->{isError} && $result->{structuredContent}{exit} == 0
&& $result->{structuredContent}{stdout} eq "header\n", "$backend status shares output limits";
$result = call "vcs-log" => { backend => $backend, files => ["-strange"], max_lines => 2 };
ok !$result->{isError} && $result->{structuredContent}{exit} == 0
&& $result->{structuredContent}{stdout} eq "header\nkeep: $backend\n"
&& $result->{structuredContent}{stdout_truncated}, "$backend log shares bounded output";
ok $prompts[-1]{action} eq "vcs-log"
&& $prompts[-1]{dimensions}{subject}{value} eq $backend,
"$backend log has its own backend-scoped permission action";
ok join (",", sort @{$prompts[-1]{sandbox_grants}}) eq
(($backend eq "cvs" || $backend eq "svn") ? "ssh-agent,workdir-read" : "workdir-read"),
"$backend log requests appropriate network/SSH authority";
@log = split /\n/, read_file "$dir/vcs.log";
$argv = JSON::XS::decode_json $log[-1];
ok $argv->[-1] eq ($backend eq "svn" ? "./-strange@" : "./-strange")
&& grep ($_ eq "--", @$argv), "$backend log file path remains a literal argument";
ok (($backend eq "git" && grep ($_ eq "--no-decorate", @$argv))
|| ($backend eq "cvs" && grep ($_ eq "-N", @$argv))
|| ($backend eq "svn" && grep ($_ eq "--no-auth-cache", @$argv))
|| ($backend eq "hg" && grep ($_ eq "log", @$argv))),
"$backend log uses fixed backend options";
SKIP: {
skip "extended GNU grep VCS test (PERL_ANYEVENT_MCP_TESTS=1)", 1
unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
skip "GNU grep is unavailable", 1 unless $utility{grep};
$result = call "vcs-diff" => { backend => $backend, grep => "^keep" };
ok !$result->{isError} && $result->{structuredContent}{exit} == 1
&& $result->{structuredContent}{stdout} eq "keep: $backend\n", "$backend diff shares postfiltering";
}
}
File::Path::mkpath ("$dir/work/sub", 0, 0700);
for my $backend (qw(git svn cvs hg)) {
$result = call "vcs-diff" => { backend => $backend, cwd => "sub", files => ["../-strange"] };
my @log = split /\n/, read_file "$dir/vcs.log";
my $argv = JSON::XS::decode_json $log[-1];
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/sub"
&& $argv->[-1] eq ($backend eq "svn" ? "./../-strange@" : "./../-strange")
&& $prompts[-1]{reason} =~ /\Q$dir\/work\/sub\E/,
"$backend runs in cwd with relative filenames and displays command directory";
}
my $unicode_cwd = "dir-\x{e4}\x{1f431}";
my $unicode_file = "file-\x{e4}\x{1f431}";
my ($cwd_bytes, $file_bytes) = ($unicode_cwd, "./$unicode_file");
utf8::encode $_ for $cwd_bytes, $file_bytes;
mkdir "$dir/work/$cwd_bytes" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => $unicode_cwd, files => [$unicode_file] };
my @unicode_log = split /\n/, read_file "$dir/vcs.log";
my $unicode_argv = JSON::XS::decode_json $unicode_log[-1];
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/$cwd_bytes",
"VCS wrapper encodes Unicode working directory before chdir";
ok $unicode_argv->[-1] eq $file_bytes,
"VCS pathspec is passed as UTF-8 bytes alongside binary executable path";
ok $tool{"vcs-diff"}{inputSchema}{properties}{cwd}{default} eq "."
&& $tool{"vcs-status"}{inputSchema}{properties}{cwd}{default} eq "."
&& $tool{"vcs-log"}{inputSchema}{properties}{cwd}{default} eq ".",
"all VCS tools advertise cwd default";
$result = call "vcs-status" => { backend => "git", cwd => "" };
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work",
"empty VCS cwd selects workdir";
my $before_vcs = read_file "$dir/vcs.log";
$result = call "vcs-status" => { backend => "git", files => [""] };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
"empty VCS filename remains invalid";
$result = call "vcs-status" => { backend => "git", cwd => "../home" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
"lexical cwd escape is rejected before execution";
symlink "$dir/home", "$dir/work/external-cwd" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => "external-cwd" };
ok $result->{structuredContent}{exit} != 0 && read_file ("$dir/vcs.log") eq $before_vcs
&& $result->{structuredContent}{stderr} =~ /cwd must resolve within/,
"worker rejects resolved cwd escape before starting VCS";
symlink "sub", "$dir/work/internal-cwd" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => "internal-cwd" };
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/sub",
"internal cwd symlink is allowed";
$before_vcs = read_file "$dir/vcs.log";
$result = call "vcs-status" => { backend => "git", cwd => "missing-cwd" };
ok $result->{structuredContent}{exit} != 0 && read_file ("$dir/vcs.log") eq $before_vcs,
"missing cwd cannot start VCS";
$result = call "vcs-diff" => { backend => "cvs", files => ["../outside"] };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs, "VCS paths cannot escape the selected workdir";
$reduce = 1;
$result = call "vcs-status" => { backend => "cvs" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs, "reduced approval cannot omit CVS agent/network authority";
$result = call "vcs-log" => { backend => "svn" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
"reduced approval cannot omit SVN log agent/network authority";
$reduce = 0;
$result = call exec => { argv => [$^X, "-e", 'print "worker\\n"'], reason => "test worker" };
ok $result->{structuredContent}{stdout} eq "worker\n" && !$result->{isError}, "arbitrary exec uses module permissions";
ok !exists $prompts[-1]{details}, "exec without stdin offers no viewer details";
ok $tool{exec}{inputSchema}{properties}{stdin_encoding}{enum}[2] eq "base64", "exec advertises stdin encoding choices";
my $short_stdin = "print qq(hello from stdin\n);\n";
$result = call exec => { argv => [$^X, "-"], stdin => $short_stdin,
reason => "inspect stdin test" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "hello from stdin\n"
&& $prompts[-1]{details}{"View stdin"} eq $short_stdin,
"real exec passes original source to display-only details and runs it unchanged";
my $large_stdin = "x" x 65537;
$result = call exec => { argv => [$^X, "-e", 'local $/; my $s = <STDIN>; print length $s'],
stdin => $large_stdin, reason => "inspect long input" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "65537"
( run in 0.545 second using v1.01-cache-2.11-cpan-234fa220139 )