| 1 |
root |
1.1 |
use common::sense; |
| 2 |
|
|
# the HTTP child uses a private loopback listener. |
| 3 |
|
|
BEGIN { delete $ENV{http_proxy} } |
| 4 |
|
|
use Test::More; |
| 5 |
|
|
BEGIN { |
| 6 |
|
|
plan skip_all => "extended local forms test (PERL_ANYEVENT_MCP_TESTS=1)" |
| 7 |
|
|
unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1; |
| 8 |
|
|
} |
| 9 |
|
|
|
| 10 |
|
|
use AnyEvent (); |
| 11 |
|
|
use AnyEvent::Util (); |
| 12 |
|
|
use AnyEvent::MCP::Client::JSONL (); |
| 13 |
|
|
use File::Temp (); |
| 14 |
|
|
use File::Path (); |
| 15 |
|
|
use IO::Socket::INET (); |
| 16 |
|
|
use JSON::XS (); |
| 17 |
|
|
use Time::HiRes (); |
| 18 |
|
|
|
| 19 |
|
|
my $dir = File::Temp::tempdir (CLEANUP => 1); |
| 20 |
|
|
File::Path::mkpath (["$dir/home", "$dir/config"], 0, 0700); |
| 21 |
|
|
my $helper = "$dir/form-helper"; |
| 22 |
|
|
sub write_file { |
| 23 |
|
|
my ($path, $text) = @_; |
| 24 |
|
|
open my $fh, ">", $path or die "$path: $!"; |
| 25 |
|
|
print {$fh} $text; |
| 26 |
|
|
close $fh or die "$path: $!"; |
| 27 |
|
|
} |
| 28 |
|
|
write_file $helper, "#!$^X\n" . q{ |
| 29 |
|
|
use common::sense; |
| 30 |
|
|
use JSON::XS (); |
| 31 |
|
|
local $/; |
| 32 |
|
|
my $request = JSON::XS::decode_json <STDIN>; |
| 33 |
|
|
open my $log, ">>", "$ENV{MCP_TEST_FORM_DIR}/requests" or die $!; |
| 34 |
|
|
print {$log} JSON::XS::encode_json ($request), "\n"; |
| 35 |
|
|
close $log; |
| 36 |
|
|
if ($request->{message} =~ /Wait for cancellation/) { |
| 37 |
|
|
open my $pid, ">", "$ENV{MCP_TEST_FORM_DIR}/waiting-pid" or die $!; |
| 38 |
|
|
print {$pid} "$$"; |
| 39 |
|
|
close $pid; |
| 40 |
|
|
sleep 60; |
| 41 |
|
|
} |
| 42 |
|
|
open my $reply, "<", "$ENV{MCP_TEST_FORM_DIR}/reply" or die $!; |
| 43 |
|
|
print scalar <$reply>; |
| 44 |
|
|
}; |
| 45 |
|
|
chmod 0700, $helper or die $!; |
| 46 |
|
|
write_file "$dir/anyevent-mcp-form-elicitation", "#!$^X\n" . q{ |
| 47 |
|
|
use common::sense; |
| 48 |
|
|
use JSON::XS (); |
| 49 |
|
|
open my $log, ">>", "$ENV{MCP_TEST_FORM_DIR}/modes" or die $!; |
| 50 |
|
|
print {$log} JSON::XS::encode_json (\@ARGV), "\n"; |
| 51 |
|
|
close $log or die $!; |
| 52 |
|
|
exec $^X, "$ENV{MCP_TEST_FORM_DIR}/form-helper"; |
| 53 |
|
|
die "exec: $!"; |
| 54 |
|
|
}; |
| 55 |
|
|
chmod 0700, "$dir/anyevent-mcp-form-elicitation" or die $!; |
| 56 |
|
|
my %env = ( |
| 57 |
|
|
HOME => "$dir/home", XDG_CONFIG_HOME => "$dir/config", |
| 58 |
|
|
PERL_ANYEVENT_MCP_ELICITATION_HELPER => $helper, |
| 59 |
|
|
MCP_TEST_FORM_DIR => $dir, PATH => "$dir:$ENV{PATH}", |
| 60 |
|
|
); |
| 61 |
|
|
write_file "$dir/reply", '{"action":"accept","content":{"choice":"Keep"}}'; |
| 62 |
|
|
my $args = { question => "Keep or replace?", options => [{ label => "Keep" }, { label => "Replace" }] }; |
| 63 |
|
|
|
| 64 |
|
|
sub wait_for { |
| 65 |
|
|
my ($start) = @_; |
| 66 |
|
|
my $cv = AE::cv; |
| 67 |
|
|
my $timer = AE::timer 30, 0, sub { $cv->croak ("Local form test timeout\n") }; |
| 68 |
|
|
$start->(sub { $cv->send ([@_]) }); |
| 69 |
|
|
@{$cv->recv} |
| 70 |
|
|
} |
| 71 |
|
|
sub question { |
| 72 |
|
|
my ($client, $name) = @_; |
| 73 |
|
|
my ($success, $result) = wait_for (sub { $client->call ($name || "ask-user", $args, $_[0]) }); |
| 74 |
|
|
ok $success, "question transport completed"; |
| 75 |
|
|
$result |
| 76 |
|
|
} |
| 77 |
|
|
sub requests { |
| 78 |
|
|
open my $fh, "<", "$dir/requests" or die $!; |
| 79 |
|
|
my @requests = map JSON::XS::decode_json ($_), <$fh>; |
| 80 |
|
|
close $fh; |
| 81 |
|
|
@requests |
| 82 |
|
|
} |
| 83 |
|
|
sub standalone { |
| 84 |
|
|
my ($mode, $caps, $legacy, $on_request) = @_; |
| 85 |
|
|
AnyEvent::MCP::Client::JSONL->new ( |
| 86 |
|
|
command => [$^X, "-Ilib", "bin/mcp-ask-user", "--stdio", "--ask", $mode], |
| 87 |
|
|
env => \%env, client_capabilities => $caps, |
| 88 |
|
|
($legacy ? (versions => ["2025-11-25"]) : ()), |
| 89 |
|
|
on_request => $on_request || sub { die "Local question reached upstream elicitation\n" }, |
| 90 |
|
|
) |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
my $client = standalone ("helper", {}, 1); |
| 94 |
|
|
my $result = question $client; |
| 95 |
|
|
is $result->{structuredContent}{answer}, "Keep", "standalone local helper works for a legacy client"; |
| 96 |
|
|
write_file "$dir/reply", '{"action":"decline"}'; |
| 97 |
|
|
$result = question $client; |
| 98 |
|
|
is $result->{structuredContent}{status}, "declined", "local decline is retained"; |
| 99 |
|
|
write_file "$dir/reply", '{"action":"cancel"}'; |
| 100 |
|
|
$result = question $client; |
| 101 |
|
|
is $result->{structuredContent}{status}, "cancelled", "local cancel is distinct"; |
| 102 |
|
|
write_file "$dir/reply", '{"action":"accept","content":{"choice":"not offered"}}'; |
| 103 |
|
|
$result = question $client; |
| 104 |
|
|
ok $result->{isError}, "standalone validates even a custom helper's choice"; |
| 105 |
|
|
|
| 106 |
|
|
my $form_args = { |
| 107 |
|
|
message => "Configure locally", |
| 108 |
|
|
requestedSchema => { |
| 109 |
|
|
type => "object", required => ["enabled", "count"], |
| 110 |
|
|
properties => { |
| 111 |
|
|
enabled => { type => "boolean" }, |
| 112 |
|
|
count => { type => "integer", minimum => 1 }, |
| 113 |
|
|
}, |
| 114 |
|
|
}, |
| 115 |
|
|
}; |
| 116 |
|
|
write_file "$dir/reply", '{"action":"accept","content":{"enabled":true,"count":2}}'; |
| 117 |
|
|
my ($form_ok, $form_result) = wait_for (sub { |
| 118 |
|
|
$client->call ("ask-form", $form_args, $_[0]) |
| 119 |
|
|
}); |
| 120 |
|
|
ok $form_ok, "generic local form works for a legacy client"; |
| 121 |
|
|
is_deeply $form_result->{structuredContent}, |
| 122 |
|
|
{ action => "accept", content => { enabled => JSON::XS::true, count => 2 } }, |
| 123 |
|
|
"local form returns standard typed response"; |
| 124 |
|
|
my @form_requests = requests; |
| 125 |
|
|
is_deeply $form_requests[-1], { mode => "form", %$form_args }, |
| 126 |
|
|
"generic parameters reach the helper unchanged"; |
| 127 |
|
|
for my $action (qw(decline cancel)) { |
| 128 |
|
|
write_file "$dir/reply", JSON::XS::encode_json { action => $action }; |
| 129 |
|
|
($form_ok, $form_result) = wait_for (sub { |
| 130 |
|
|
$client->call ("ask-form", $form_args, $_[0]) |
| 131 |
|
|
}); |
| 132 |
|
|
is_deeply $form_result->{structuredContent}, { action => $action }, |
| 133 |
|
|
"local generic form retains $action"; |
| 134 |
|
|
} |
| 135 |
|
|
write_file "$dir/reply", '{"action":"accept","content":[]}'; |
| 136 |
|
|
($form_ok, $form_result) = wait_for (sub { |
| 137 |
|
|
$client->call ("ask-form", $form_args, $_[0]) |
| 138 |
|
|
}); |
| 139 |
|
|
ok $form_result->{isError}, "generic tool rejects a malformed custom helper response"; |
| 140 |
|
|
$client->close; |
| 141 |
|
|
|
| 142 |
|
|
write_file "$dir/reply", '{"action":"accept","content":{"choice":"Keep"}}'; |
| 143 |
|
|
$client = standalone ("auto", {}); |
| 144 |
|
|
$result = question $client; |
| 145 |
|
|
is $result->{structuredContent}{answer}, "Keep", "standalone auto falls back when no forms are advertised"; |
| 146 |
|
|
$client->close; |
| 147 |
|
|
|
| 148 |
|
|
my @before = requests; |
| 149 |
|
|
my $elicited; |
| 150 |
|
|
$client = standalone ("auto", { elicitation => { form => {} } }, 0, sub { |
| 151 |
|
|
++$elicited; |
| 152 |
|
|
$_[1]->reply ({ action => "decline" }); |
| 153 |
|
|
}); |
| 154 |
|
|
$result = question $client; |
| 155 |
|
|
my @after = requests; |
| 156 |
|
|
ok $elicited == 1 && $result->{structuredContent}{status} eq "declined" && @before == @after, |
| 157 |
|
|
"auto native decline never falls back to a local helper"; |
| 158 |
|
|
$client->close; |
| 159 |
|
|
|
| 160 |
|
|
sub broker { |
| 161 |
|
|
my ($mode, $caps, $legacy, $url) = @_; |
| 162 |
|
|
AnyEvent::MCP::Client::JSONL->new ( |
| 163 |
|
|
command => [$^X, "-Ilib", "bin/mcp-multibroker", "--stdio", "--ask", $mode, "questions", |
| 164 |
|
|
$url || "stdio:$^X -Ilib bin/mcp-ask-user"], |
| 165 |
|
|
env => \%env, client_capabilities => $caps, |
| 166 |
|
|
($legacy ? (versions => ["2025-11-25"]) : ()), |
| 167 |
|
|
on_request => sub { die "Broker local question reached upstream elicitation\n" }, |
| 168 |
|
|
) |
| 169 |
|
|
} |
| 170 |
|
|
for my $case ( |
| 171 |
|
|
["helper", { elicitation => { form => {} } }, 0], |
| 172 |
|
|
["helper", {}, 0], |
| 173 |
|
|
["helper", {}, 1], |
| 174 |
|
|
["auto", {}, 0], |
| 175 |
|
|
) { |
| 176 |
|
|
$client = broker (@$case); |
| 177 |
|
|
$result = question $client, "questions.ask-user"; |
| 178 |
|
|
is $result->{structuredContent}{answer}, "Keep", |
| 179 |
|
|
"broker $case->[0] handles the child form locally despite upstream capability differences"; |
| 180 |
|
|
my @requests = requests; |
| 181 |
|
|
like $requests[-1]{message}, qr/\AServer: questions\n\n/, "broker identifies the requesting child"; |
| 182 |
|
|
$client->close; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
|
for my $mode (qw(tty yad)) { |
| 186 |
|
|
local $env{PERL_ANYEVENT_MCP_ELICITATION_HELPER} = "$dir/not-a-custom-helper"; |
| 187 |
|
|
for my $through_broker (0, 1) { |
| 188 |
|
|
$client = $through_broker ? broker ($mode, {}) : standalone ($mode, {}); |
| 189 |
|
|
$result = question $client, $through_broker ? "questions.ask-user" : "ask-user"; |
| 190 |
|
|
is $result->{structuredContent}{answer}, "Keep", "$mode works through the selected built-in form helper"; |
| 191 |
|
|
open my $log, "<", "$dir/modes" or die $!; |
| 192 |
|
|
my @modes = map JSON::XS::decode_json ($_), <$log>; |
| 193 |
|
|
close $log; |
| 194 |
|
|
is_deeply $modes[-1], ["--$mode"], "$mode is forced without consulting the custom helper"; |
| 195 |
|
|
$client->close; |
| 196 |
|
|
} |
| 197 |
|
|
} |
| 198 |
|
|
|
| 199 |
|
|
SKIP: { |
| 200 |
|
|
skip "extended form helper cancellation tests (PERL_ANYEVENT_MCP_TESTS=1)", 3 unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1; |
| 201 |
|
|
|
| 202 |
|
|
$client = broker ("helper", {}); |
| 203 |
|
|
$args->{question} = "Wait for cancellation"; |
| 204 |
|
|
my $cancelled = AE::cv; |
| 205 |
|
|
my $operation = $client->call ("questions.ask-user", $args, sub { $cancelled->send ([@_]) }); |
| 206 |
|
|
my $poll; |
| 207 |
|
|
wait_for (sub { |
| 208 |
|
|
my $done = shift; |
| 209 |
|
|
$poll = AE::timer 0, 0.01, sub { |
| 210 |
|
|
return unless -s "$dir/waiting-pid"; |
| 211 |
|
|
undef $poll; |
| 212 |
|
|
$done->(1); |
| 213 |
|
|
}; |
| 214 |
|
|
}); |
| 215 |
|
|
open my $pidfile, "<", "$dir/waiting-pid" or die $!; |
| 216 |
|
|
my $helper_pid = <$pidfile>; |
| 217 |
|
|
close $pidfile; |
| 218 |
|
|
$client->cancel ($operation); |
| 219 |
|
|
my $cancel = $cancelled->recv; |
| 220 |
|
|
ok !$cancel->[0] && $cancel->[1]{cancelled}, "broker question cancellation reaches the caller"; |
| 221 |
|
|
wait_for (sub { |
| 222 |
|
|
my $done = shift; |
| 223 |
|
|
$poll = AE::timer 0, 0.01, sub { |
| 224 |
|
|
return if kill 0, $helper_pid; |
| 225 |
|
|
undef $poll; |
| 226 |
|
|
$done->(1); |
| 227 |
|
|
}; |
| 228 |
|
|
}); |
| 229 |
|
|
ok !kill (0, $helper_pid), "broker cancels and reaps the active form helper"; |
| 230 |
|
|
$args->{question} = "Keep or replace?"; |
| 231 |
|
|
$result = question $client, "questions.ask-user"; |
| 232 |
|
|
is $result->{structuredContent}{answer}, "Keep", "prompt queue remains usable after cancellation"; |
| 233 |
|
|
$client->close; |
| 234 |
|
|
|
| 235 |
|
|
} |
| 236 |
|
|
|
| 237 |
|
|
# Independently running HTTP servers delegate to the broker too. |
| 238 |
|
|
my $listener = IO::Socket::INET->new ( |
| 239 |
|
|
LocalAddr => "127.0.0.1", LocalPort => 0, Proto => "tcp", Listen => 1, |
| 240 |
|
|
) or die $!; |
| 241 |
|
|
my $port = $listener->sockport; |
| 242 |
|
|
close $listener; |
| 243 |
|
|
my ($pid, $stdout, $stderr); |
| 244 |
root |
1.2 |
my $server; |
| 245 |
|
|
{ |
| 246 |
|
|
local %ENV = (%ENV, %env); |
| 247 |
|
|
$server = AnyEvent::Util::run_cmd ( |
| 248 |
|
|
[$^X, "-Ilib", "bin/mcp-ask-user", "--ask", "mcp", "--bind", "127.0.0.1:$port", "--token", "form-test"], |
| 249 |
|
|
"<" => "/dev/null", ">" => \$stdout, "2>" => \$stderr, '$$' => \$pid, |
| 250 |
|
|
); |
| 251 |
|
|
} |
| 252 |
root |
1.1 |
my $ready; |
| 253 |
|
|
for (1 .. 1500) { |
| 254 |
|
|
$ready = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp"); |
| 255 |
|
|
last if $ready; |
| 256 |
|
|
Time::HiRes::sleep (0.02); |
| 257 |
|
|
} |
| 258 |
|
|
ok $ready, "standalone HTTP question server is listening"; |
| 259 |
|
|
close $ready if $ready; |
| 260 |
|
|
$client = broker ("helper", {}, 0, "http://127.0.0.1:$port/form-test/"); |
| 261 |
|
|
$result = question $client, "questions.ask-user"; |
| 262 |
|
|
is $result->{structuredContent}{answer}, "Keep", "broker handles an independent HTTP child's form locally"; |
| 263 |
|
|
$client->close; |
| 264 |
|
|
kill TERM => $pid if $pid; |
| 265 |
|
|
$server->recv; |
| 266 |
|
|
|
| 267 |
|
|
done_testing; |