| 1 |
root |
1.1 |
use common::sense; |
| 2 |
|
|
use AnyEvent (); |
| 3 |
|
|
use AnyEvent::Handle (); |
| 4 |
|
|
use AnyEvent::MCP::Server::Simple; |
| 5 |
|
|
use AnyEvent::MCP::Server::HTTP (); |
| 6 |
|
|
use AnyEvent::MCP::Server::Permissions (); |
| 7 |
|
|
use AnyEvent::MCP::Config (); |
| 8 |
|
|
use File::Temp (); |
| 9 |
|
|
use IPC::Open3 (); |
| 10 |
|
|
use Symbol (); |
| 11 |
|
|
use JSON::XS (); |
| 12 |
|
|
use MIME::Base64 (); |
| 13 |
|
|
use Socket; |
| 14 |
|
|
|
| 15 |
|
|
print "1..53\n"; |
| 16 |
|
|
|
| 17 |
|
|
my $test; |
| 18 |
|
|
sub ok($$) { |
| 19 |
|
|
my ($condition, $name) = @_; |
| 20 |
|
|
|
| 21 |
|
|
print (($condition ? "ok " : "not ok "), ++$test, " - $name\n"); |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
|
|
sub post { |
| 25 |
|
|
my ($server, $path, $authorization) = @_; |
| 26 |
|
|
|
| 27 |
|
|
socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 28 |
|
|
$server->accept ($a); |
| 29 |
|
|
my $client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 30 |
|
|
my $cv = AnyEvent->condvar; |
| 31 |
|
|
my $timer = AnyEvent->timer (after => 30, cb => sub { die "HTTP timeout\n" }); |
| 32 |
|
|
my $body = '{"jsonrpc":"2.0","id":1,"method":"ping"}'; |
| 33 |
|
|
$client->push_write ( |
| 34 |
|
|
"POST $path HTTP/1.1\r\nContent-Type: application/json\r\n" |
| 35 |
|
|
. "Content-Length: " . length ($body) . "\r\n" |
| 36 |
|
|
. (defined $authorization ? "Authorization: $authorization\r\n" : "") |
| 37 |
|
|
. "\r\n$body" |
| 38 |
|
|
); |
| 39 |
|
|
|
| 40 |
|
|
my ($header, $response); |
| 41 |
|
|
$client->push_read (line => "\r\n\r\n", sub { |
| 42 |
|
|
my ($handle, $text) = @_; |
| 43 |
|
|
$header = $text; |
| 44 |
|
|
my ($length) = $header =~ /Content-Length: ([0-9]+)/; |
| 45 |
|
|
$handle->push_read (chunk => $length, sub { |
| 46 |
|
|
$response = $_[1]; |
| 47 |
|
|
$cv->send; |
| 48 |
|
|
}); |
| 49 |
|
|
}); |
| 50 |
|
|
$cv->recv; |
| 51 |
|
|
$client->destroy; |
| 52 |
|
|
$server->close; |
| 53 |
|
|
|
| 54 |
|
|
($header, $response) |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
my $server = AnyEvent::MCP::Server::HTTP->new ( |
| 58 |
|
|
port => 31740, path => "/", tokens => ["secret", "alternate"], |
| 59 |
|
|
); |
| 60 |
|
|
ok $server->url eq "http://127.0.0.1:31740/secret/", "cookie URL"; |
| 61 |
|
|
ok $server->url (0) eq "http://127.0.0.1:31740/", "clean URL"; |
| 62 |
|
|
|
| 63 |
|
|
my ($header, $body) = post ($server, "/secret/"); |
| 64 |
|
|
ok $header =~ m{HTTP/1.1 200}, "cookie authenticates"; |
| 65 |
|
|
ok JSON::XS::decode_json ($body)->{id} == 1, "authenticated RPC reply"; |
| 66 |
|
|
($header, $body) = post ($server, "/", "Bearer secret"); |
| 67 |
|
|
ok $header =~ m{HTTP/1.1 200}, "same token authenticates bearer"; |
| 68 |
|
|
($header, $body) = post ($server, "/", "bEaReR secret"); |
| 69 |
|
|
ok $header =~ m{HTTP/1.1 200}, "authentication scheme is case insensitive"; |
| 70 |
|
|
($header, $body) = post ($server, "/"); |
| 71 |
|
|
ok $header =~ m{HTTP/1.1 401}, "missing credential rejected"; |
| 72 |
|
|
ok $header =~ /WWW-Authenticate: Bearer/, "bearer challenge"; |
| 73 |
|
|
($header, $body) = post ($server, "/", "Bearer wrong"); |
| 74 |
|
|
ok $header =~ m{HTTP/1.1 401}, "wrong bearer rejected"; |
| 75 |
|
|
($header, $body) = post ($server, "/wrong/"); |
| 76 |
|
|
ok $header =~ m{HTTP/1.1 404}, "wrong cookie path rejected"; |
| 77 |
|
|
|
| 78 |
|
|
$server = AnyEvent::MCP::Server::HTTP->new (tokens => ["first", "second"]); |
| 79 |
|
|
($header, $body) = post ($server, "/second/mcp/"); |
| 80 |
|
|
ok $header =~ m{HTTP/1.1 200}, "second token works in URL"; |
| 81 |
|
|
($header, $body) = post ($server, "/mcp/", "Bearer first"); |
| 82 |
|
|
ok $header =~ m{HTTP/1.1 200}, "first token works as bearer"; |
| 83 |
|
|
($header, $body) = post ($server, "/mcp/", "Basic YTpi"); |
| 84 |
|
|
ok $header =~ m{HTTP/1.1 401} && $header !~ /WWW-Authenticate: Basic/, "Basic authentication is not supported"; |
| 85 |
|
|
|
| 86 |
|
|
$server = AnyEvent::MCP::Server::HTTP->new ( |
| 87 |
|
|
path => "/", tokens => ["first", "second"], |
| 88 |
|
|
token_users => { first => "global:alice", second => "global:bob" }, |
| 89 |
|
|
on_request => sub { $_[1]->reply ({ user => $_[1]{user} }) }, |
| 90 |
|
|
); |
| 91 |
|
|
($header, $body) = post ($server, "/first/"); |
| 92 |
|
|
ok JSON::XS::decode_json ($body)->{result}{user} eq "global:alice", "path token identifies user"; |
| 93 |
|
|
($header, $body) = post ($server, "/", "Bearer second"); |
| 94 |
|
|
ok JSON::XS::decode_json ($body)->{result}{user} eq "global:bob", "Bearer identifies distinct user"; |
| 95 |
|
|
($header, $body) = post ($server, "/first/", "Bearer second"); |
| 96 |
|
|
ok $header =~ m{HTTP/1.1 401}, "conflicting path and Bearer identities rejected"; |
| 97 |
|
|
|
| 98 |
|
|
$server = AnyEvent::MCP::Server::HTTP->new (tokens => ["standalone"]); |
| 99 |
|
|
($header, $body) = post ($server, "/mcp/"); |
| 100 |
|
|
ok $header =~ m{HTTP/1.1 401}, "embedded server with tokens rejects unauthenticated requests"; |
| 101 |
|
|
$server = AnyEvent::MCP::Server::HTTP->new (auth => "none", |
| 102 |
|
|
host => "::1", port => 31740, handle => { tls => "accept" }, |
| 103 |
|
|
); |
| 104 |
|
|
ok $server->url eq "https://[::1]:31740/mcp/", "IPv6 and TLS URL"; |
| 105 |
|
|
$server->{url_host} = "[::1]"; |
| 106 |
|
|
ok $server->url eq "https://[::1]:31740/mcp/", "prebracketed IPv6 URL host is not bracketed twice"; |
| 107 |
|
|
$server->{url_host} = "example.net"; |
| 108 |
|
|
$server->{url_scheme} = "http"; |
| 109 |
|
|
ok $server->url eq "http://example.net:31740/mcp/", "advertised URL overrides"; |
| 110 |
|
|
delete $server->{url_host}; |
| 111 |
|
|
$server->{host} = "0.0.0.0"; |
| 112 |
|
|
ok $server->url eq "http://" . (POSIX::uname)[1] . ":31740/mcp/", "wildcard nodename"; |
| 113 |
|
|
$server->{port} = 0; |
| 114 |
|
|
eval { $server->url }; |
| 115 |
|
|
ok $@ =~ /port is not known/, "unknown port has no URL"; |
| 116 |
|
|
|
| 117 |
|
|
my $dir = File::Temp::tempdir (CLEANUP => 1); |
| 118 |
|
|
local $ENV{XDG_CONFIG_HOME} = $dir; |
| 119 |
|
|
local $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL}; |
| 120 |
|
|
my $printed = ""; |
| 121 |
|
|
{ |
| 122 |
|
|
local *STDOUT; |
| 123 |
|
|
open STDOUT, ">", \$printed or die $!; |
| 124 |
|
|
my $timer; |
| 125 |
|
|
mcp_start name => "example", server_version => 0.61, on_start => sub { |
| 126 |
|
|
$server = $_[0]; |
| 127 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 128 |
|
|
}; |
| 129 |
|
|
} |
| 130 |
|
|
ok $server->isa ("AnyEvent::MCP::Server::HTTP"), "normal startup uses HTTP"; |
| 131 |
|
|
ok $server->{permissions} && $server->{permissions}->isa ("AnyEvent::MCP::Server::Permissions"), |
| 132 |
|
|
"Simple supplies a default permissions object even without explicit configuration"; |
| 133 |
|
|
ok $server->{permissions}{file} eq "$dir/anyevent-mcp/rules/" |
| 134 |
|
|
. AnyEvent::MCP::Config::program_name () . "-example.ini", |
| 135 |
|
|
"default permissions use program and instance in the policy filename"; |
| 136 |
|
|
ok $server->{port} && $printed eq "", "HTTP startup leaves stdout empty"; |
| 137 |
root |
1.2 |
my $config = AnyEvent::MCP::Config::global_config (); |
| 138 |
root |
1.1 |
ok $server->{tokens}[0] eq $config->user_credentials ("example")->[0][2], |
| 139 |
|
|
"startup token matches derived utility"; |
| 140 |
|
|
ok $server->{server_info}{name} eq "AnyEvent::MCP", "instance name is not the server implementation name"; |
| 141 |
|
|
ok JSON::XS::encode_json ($server->{server_info}) =~ /"version":"0\.61"/, |
| 142 |
|
|
"Simple stringifies numeric module versions"; |
| 143 |
|
|
$server->close; |
| 144 |
|
|
|
| 145 |
|
|
my $err = Symbol::gensym; |
| 146 |
|
|
my $pid = IPC::Open3::open3 (my $in, my $out, $err, $^X, "-Ilib", "-e", ' |
| 147 |
|
|
use AnyEvent::MCP::Server::Simple; |
| 148 |
|
|
$ENV{XDG_CONFIG_HOME} = $ARGV[0]; |
| 149 |
|
|
$ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} = "stdio"; |
| 150 |
|
|
mcp_start; |
| 151 |
|
|
', "$dir/stdio"); |
| 152 |
|
|
print {$in} "{\"jsonrpc\":\"2.0\",\"id\":42,\"method\":\"ping\"}\n"; |
| 153 |
|
|
$in->flush; |
| 154 |
|
|
close $in; |
| 155 |
|
|
local $/; |
| 156 |
|
|
my $output = <$out>; |
| 157 |
|
|
my $errors = <$err>; |
| 158 |
|
|
waitpid $pid, 0; |
| 159 |
|
|
ok !$? && !$errors, "multibroker startup succeeds"; |
| 160 |
|
|
ok eval { JSON::XS::decode_json ($output)->{id} == 42 }, "stdio output contains only RPC"; |
| 161 |
|
|
ok !-e "$dir/stdio", "stdio does not access global configuration"; |
| 162 |
|
|
|
| 163 |
|
|
$err = Symbol::gensym; |
| 164 |
|
|
$pid = do { |
| 165 |
|
|
local $ENV{PERL_ANYEVENT_LOG} = ""; # inspect the normal activity log. |
| 166 |
|
|
IPC::Open3::open3 ($in, $out, $err, $^X, "-Ilib", "-e", ' |
| 167 |
|
|
use AnyEvent::MCP::Server::Simple; |
| 168 |
|
|
my $timer; |
| 169 |
|
|
mcp_tool delayed => "delayed", {}, sub { |
| 170 |
|
|
my ($req) = @_; |
| 171 |
|
|
$timer = AnyEvent->timer (after => 0.03, cb => sub { |
| 172 |
|
|
$req->reply ({ content => [{ type => "text", text => "x" x 200000 }] }); |
| 173 |
|
|
}); |
| 174 |
|
|
}; |
| 175 |
|
|
mcp_stdio; |
| 176 |
|
|
print STDERR "stopped\\n"; |
| 177 |
|
|
'); |
| 178 |
|
|
}; |
| 179 |
|
|
print {$in} JSON::XS::encode_json ({ |
| 180 |
|
|
jsonrpc => "2.0", id => 1, method => "initialize", params => { |
| 181 |
|
|
protocolVersion => "2025-11-25", clientInfo => {}, capabilities => {}, |
| 182 |
|
|
}, |
| 183 |
|
|
}), "\n"; |
| 184 |
|
|
print {$in} JSON::XS::encode_json ({ |
| 185 |
|
|
jsonrpc => "2.0", id => 2, method => "tools/call", params => { name => "delayed" }, |
| 186 |
|
|
}), "\n"; |
| 187 |
|
|
close $in; |
| 188 |
|
|
$output = <$out>; |
| 189 |
|
|
$errors = <$err>; |
| 190 |
|
|
waitpid $pid, 0; |
| 191 |
|
|
ok !$? && $errors =~ /\bdelayed\b.*\nstopped\n\z/s, |
| 192 |
|
|
"mcp_stdio logs tool activity and returns after input EOF"; |
| 193 |
|
|
my @messages = map JSON::XS::decode_json ($_), split /\n/, $output; |
| 194 |
|
|
ok @messages == 2 && $messages[1]{id} == 2 |
| 195 |
|
|
&& length $messages[1]{result}{content}[0]{text} == 200000, |
| 196 |
|
|
"EOF waits for an asynchronous reply and drains large output"; |
| 197 |
|
|
|
| 198 |
|
|
my ($started, $closed, $timer); |
| 199 |
|
|
mcp_http auth => "none", port => 0, name => "local-instance", |
| 200 |
|
|
server_name => "advertised", server_version => 1.2, |
| 201 |
|
|
on_start => sub { |
| 202 |
|
|
$server = $_[0]; |
| 203 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 204 |
|
|
}; |
| 205 |
|
|
ok $server->{server_info}{name} eq "advertised", |
| 206 |
|
|
"Simple server name is independent of instance name"; |
| 207 |
|
|
ok JSON::XS::encode_json ($server->{server_info}) =~ /"version":"1\.2"/, |
| 208 |
|
|
"flat Simple version is sent as a JSON string"; |
| 209 |
|
|
my $raw = { name => "raw", version => "7" }; |
| 210 |
|
|
mcp_http auth => "none", port => 0, name => "local-instance", |
| 211 |
|
|
server_info => $raw, |
| 212 |
|
|
on_start => sub { |
| 213 |
|
|
$server = $_[0]; |
| 214 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 215 |
|
|
}; |
| 216 |
|
|
ok $server->{server_info} == $raw, |
| 217 |
|
|
"explicit raw server_info is passed through unchanged"; |
| 218 |
|
|
eval { mcp_http auth => "none", port => 0, |
| 219 |
|
|
server_name => "advertised", server_info => $raw }; |
| 220 |
|
|
ok $@ =~ /Cannot combine server_info with server_name/, |
| 221 |
|
|
"Simple rejects mixing raw and flat identity settings"; |
| 222 |
|
|
|
| 223 |
|
|
mcp_http auth => "none", port => 0, name => "another-instance", |
| 224 |
|
|
on_start => sub { |
| 225 |
|
|
$server = $_[0]; |
| 226 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 227 |
|
|
}; |
| 228 |
|
|
ok $server->{server_info}{name} eq "AnyEvent::MCP", |
| 229 |
|
|
"Simple defaults to the library implementation name"; |
| 230 |
|
|
ok JSON::XS::encode_json ($server->{server_info}) =~ /"version":"\Q$AnyEvent::MCP::VERSION\E"/, |
| 231 |
|
|
"Simple defaults to the library version as a JSON string"; |
| 232 |
|
|
|
| 233 |
|
|
mcp_http auth => "none", port => 0, on_start => sub { |
| 234 |
|
|
my ($server) = @_; |
| 235 |
|
|
++$started; |
| 236 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 237 |
|
|
}, on_close => sub { ++$closed }; |
| 238 |
|
|
ok $started == 1 && $closed == 1, "mcp_http blocks through startup and returns on close"; |
| 239 |
|
|
|
| 240 |
|
|
eval { mcp_http auth => "none", port => 0, on_start => sub { die "startup failed\n" } }; |
| 241 |
|
|
ok $@ =~ /startup failed/, "startup hook exception closes the server and propagates"; |
| 242 |
|
|
|
| 243 |
|
|
{ |
| 244 |
|
|
local @ARGV = ("--auto-deny=*,write", "--auto-allow=read_lines,read", |
| 245 |
root |
1.3 |
"--auto-deny=*,*,*", "--auto-allow=*,*,"); |
| 246 |
root |
1.1 |
my $options = mcp_getoptions ({ name => "auto-test" }); |
| 247 |
|
|
ok JSON::XS::encode_json ($options->{auto_rules}) eq JSON::XS::encode_json ([ |
| 248 |
root |
1.3 |
["deny", "*,write"], ["allow", "read_lines,read"], ["deny", "*,*,*"], ["allow", "*,*,"], |
| 249 |
root |
1.1 |
]), "CLI collects repeatable allow/deny rules in original order"; |
| 250 |
|
|
|
| 251 |
|
|
my $perm = AnyEvent::MCP::Server::Permissions->new ( |
| 252 |
|
|
rules => [{ decision => "deny", action => "^" }], |
| 253 |
|
|
); |
| 254 |
|
|
my $timer; |
| 255 |
|
|
mcp_http %$options, auth => "none", port => 0, permissions => $perm, |
| 256 |
|
|
on_start => sub { |
| 257 |
|
|
my ($server) = @_; |
| 258 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 259 |
|
|
}; |
| 260 |
|
|
my $call = { action => "read", sandbox_grants => [], reason => "test" }; |
| 261 |
|
|
ok $perm->check ($call, "read-lines")->{decision} eq "allow", |
| 262 |
|
|
"Simple starter installs ordered allow rule"; |
| 263 |
|
|
ok $perm->check ({ %$call, action => "write" }, "read-lines")->{decision} eq "deny", |
| 264 |
|
|
"earlier auto-deny takes precedence over later allow"; |
| 265 |
|
|
ok $perm->check ($call, "exec")->{decision} eq "deny", |
| 266 |
|
|
"later wildcard deny catches unmatched tools"; |
| 267 |
|
|
|
| 268 |
|
|
my $created; |
| 269 |
|
|
mcp_http auth => "none", port => 0, auto_rules => [["deny", "*"]], |
| 270 |
|
|
on_start => sub { |
| 271 |
|
|
my ($server) = @_; |
| 272 |
|
|
$created = $server->{permissions}; |
| 273 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $server->close }); |
| 274 |
|
|
}; |
| 275 |
|
|
ok $created && $created->check ($call, "anything")->{decision} eq "deny", |
| 276 |
|
|
"Simple starter creates a permission engine for automatic denial"; |
| 277 |
|
|
} |
| 278 |
|
|
|
| 279 |
|
|
my $own = AnyEvent::MCP::Server::Permissions->new ( |
| 280 |
|
|
name => "explicit-test", |
| 281 |
|
|
auto_rules => [["deny", "*,write"], ["deny", "*,read"]], |
| 282 |
|
|
); |
| 283 |
|
|
mcp_permissions $own; |
| 284 |
|
|
my $observed; |
| 285 |
|
|
mcp_http auth => "none", port => 0, ask => "none", |
| 286 |
|
|
auto_rules => [["allow", "*,write"]], |
| 287 |
|
|
on_start => sub { |
| 288 |
|
|
my ($instance) = @_; |
| 289 |
|
|
$observed = $instance->{permissions}; |
| 290 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $instance->close }); |
| 291 |
|
|
}; |
| 292 |
|
|
ok $observed == $own, "mcp_permissions supplies the exact object to Simple"; |
| 293 |
|
|
ok $observed->{ask} eq "none", "common ask option overrides the supplied object's default"; |
| 294 |
|
|
my $operation = { action => "write", sandbox_grants => [], reason => "test" }; |
| 295 |
|
|
ok $observed->check ($operation, "probe")->{decision} eq "allow", |
| 296 |
|
|
"common automatic rule takes precedence over the object's own rules"; |
| 297 |
|
|
ok $observed->check ({ %$operation, action => "read" }, "probe")->{decision} eq "deny", |
| 298 |
|
|
"app-supplied automatic rules survive common-option processing"; |
| 299 |
|
|
|
| 300 |
|
|
mcp_permissions undef; |
| 301 |
|
|
my $disabled; |
| 302 |
|
|
mcp_http auth => "none", port => 0, ask => "none", |
| 303 |
|
|
auto_rules => [["deny", "*"]], |
| 304 |
|
|
on_start => sub { |
| 305 |
|
|
my ($instance) = @_; |
| 306 |
|
|
$disabled = $instance->{permissions}; |
| 307 |
|
|
$timer = AnyEvent->timer (after => 0.01, cb => sub { $instance->close }); |
| 308 |
|
|
}; |
| 309 |
|
|
ok !defined $disabled, "mcp_permissions undef disables implicit engine despite --ask and automatic rules"; |
| 310 |
|
|
eval { mcp_permissions () }; |
| 311 |
|
|
ok $@ =~ /expects one argument/, |
| 312 |
|
|
"a missing argument cannot accidentally disable the permission engine"; |