| 1 |
root |
1.1 |
use common::sense; |
| 2 |
|
|
use Test::More; |
| 3 |
|
|
use File::Temp (); |
| 4 |
|
|
use File::Path (); |
| 5 |
|
|
use IPC::Open3 (); |
| 6 |
|
|
use Symbol (); |
| 7 |
|
|
use Socket (); |
| 8 |
|
|
|
| 9 |
|
|
use AnyEvent::MCP::Config (); |
| 10 |
|
|
use AnyEvent::MCP::Server::Permissions (); |
| 11 |
|
|
use AnyEvent::MCP::Server (); |
| 12 |
|
|
|
| 13 |
|
|
my $dir = File::Temp::tempdir (CLEANUP => 1); |
| 14 |
|
|
local $ENV{XDG_CONFIG_HOME} = $dir; |
| 15 |
|
|
my $file = "$dir/anyevent-mcp/global.ini"; |
| 16 |
root |
1.3 |
my $config = AnyEvent::MCP::Config->new (file => $file)->load->ensure_secret; |
| 17 |
root |
1.1 |
|
| 18 |
|
|
sub run { |
| 19 |
|
|
my @cmd = @_; |
| 20 |
|
|
my $err = Symbol::gensym; |
| 21 |
|
|
my $pid = IPC::Open3::open3 (undef, my $out, $err, $^X, "-Ilib", @cmd); |
| 22 |
|
|
local $/; |
| 23 |
|
|
my $stdout = <$out> // ""; |
| 24 |
|
|
my $stderr = <$err> // ""; |
| 25 |
|
|
waitpid $pid, 0; |
| 26 |
|
|
($? >> 8, $stdout, $stderr) |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
|
sub cli { |
| 30 |
|
|
run "bin/anyevent-mcp", "--config", $file, @_ |
| 31 |
|
|
} |
| 32 |
|
|
|
| 33 |
|
|
my ($help_status, $help_text, $help_errors) = cli "--help"; |
| 34 |
|
|
is $help_status, 0, "user management help succeeds: $help_errors"; |
| 35 |
|
|
like $help_text, qr/--program NAME.*select \[program NAME\]/, |
| 36 |
|
|
"help explains program scope"; |
| 37 |
|
|
like $help_text, qr/--instance NAME.*select \[instance NAME\]/, |
| 38 |
|
|
"help explains instance scope"; |
| 39 |
|
|
like $help_text, qr/anyevent-mcp --program mcp-fs create-user backup/, |
| 40 |
|
|
"help includes scoped user creation example"; |
| 41 |
|
|
($help_status, $help_text, $help_errors) = run "bin/mcp-multibroker", "--help"; |
| 42 |
|
|
is $help_status, 0, "multibroker help succeeds: $help_errors"; |
| 43 |
|
|
like $help_text, qr/--print-urls.*list URLs for all enabled users/, |
| 44 |
|
|
"multibroker help advertises plural URL printing"; |
| 45 |
|
|
like $help_text, qr/--print-url=chatgpt/, "multibroker help shows exact user selection"; |
| 46 |
|
|
|
| 47 |
|
|
my $users = $config->users ("test", "mcp-test"); |
| 48 |
|
|
is_deeply $users, { default => { scope => "global", generation => 1 } }, |
| 49 |
|
|
"implicit default is an ordinary global generation-1 user"; |
| 50 |
|
|
my $original = $config->user_credentials ("test", "mcp-test")->[0][2]; |
| 51 |
|
|
like $original, qr/\A[A-Za-z0-9_-]{43}\z/, "default has a URL-safe token"; |
| 52 |
|
|
is $config->connect_user ("test", "mcp-test"), "default", "default connects"; |
| 53 |
|
|
|
| 54 |
root |
1.2 |
{ |
| 55 |
|
|
my $unicode_file = "$dir/anyevent-mcp/unicode.ini"; |
| 56 |
|
|
my $comment = "# \x{e4}\x{1f431} comment\n"; |
| 57 |
|
|
my $lock = AnyEvent::MCP::file_lock $unicode_file; |
| 58 |
root |
1.4 |
AnyEvent::MCP::file_save $unicode_file, $comment . AnyEvent::MCP::file_load ($file) |
| 59 |
|
|
. "[extra]\nlabel = \x{e4}\x{1f431}\n"; |
| 60 |
root |
1.2 |
close $lock; |
| 61 |
|
|
my ($status, $out, $err) = run "bin/anyevent-mcp", "--config", $unicode_file, "create-user", "carol"; |
| 62 |
|
|
is $status, 0, "user editor accepts Unicode configuration comments: $err"; |
| 63 |
|
|
my $saved = AnyEvent::MCP::file_load $unicode_file; |
| 64 |
root |
1.4 |
unlike $saved, qr/\Q$comment\E/, "canonical config rewrite drops comments"; |
| 65 |
|
|
like $saved, qr/label = \x{e4}\x{1f431}/, "canonical config rewrite retains Unicode values"; |
| 66 |
root |
1.2 |
like $saved, qr/user\.carol\s*=\s*1/, "user editor still updates selected INI key"; |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
root |
1.1 |
my ($exit, $out, $err) = cli "create-user", "default"; |
| 70 |
|
|
isnt $exit, 0, "implicit default counts as an existing enabled user"; |
| 71 |
|
|
($exit, $out, $err) = cli "create-user", "alice"; |
| 72 |
|
|
is $exit, 0, "create global alice succeeds: $err"; |
| 73 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 74 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{scope}, "global", "global user inherited"; |
| 75 |
|
|
my $alice = $config->user_credentials ("test", "mcp-test")->[0][2]; |
| 76 |
|
|
my @creds = @{$config->user_credentials ("test", "mcp-test")}; |
| 77 |
|
|
is scalar @creds, 2, "both default and alice accepted"; |
| 78 |
|
|
is $creds[0][0], "default", "default comes first"; |
| 79 |
|
|
isnt $creds[1][2], $original, "user tokens differ"; |
| 80 |
|
|
isnt $creds[1][2], $config->user_credentials ("test", "other-program")->[1][2], |
| 81 |
|
|
"programs derive distinct credentials"; |
| 82 |
|
|
isnt $creds[1][2], $config->user_credentials ("other", "mcp-test")->[1][2], |
| 83 |
|
|
"instances derive distinct credentials"; |
| 84 |
|
|
|
| 85 |
|
|
($exit, $out, $err) = cli "create-user", "alice"; |
| 86 |
|
|
isnt $exit, 0, "duplicate create rejected"; |
| 87 |
|
|
($exit, $out, $err) = cli "revoke-user", "alice"; |
| 88 |
|
|
is $exit, 0, "revoke succeeds: $err"; |
| 89 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 90 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{generation}, -1, "revoke preserves generation"; |
| 91 |
|
|
is scalar @{$config->user_credentials ("test", "mcp-test")}, 1, "revoked token no longer accepted"; |
| 92 |
|
|
($exit, $out, $err) = cli "create-user", "alice"; |
| 93 |
|
|
is $exit, 0, "recreate revoked user succeeds: $err"; |
| 94 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 95 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{generation}, 2, "counter increased"; |
| 96 |
|
|
isnt $config->user_credentials ("test", "mcp-test")->[1][2], $creds[1][2], |
| 97 |
|
|
"revoked credential does not reappear"; |
| 98 |
|
|
|
| 99 |
|
|
($exit, $out, $err) = cli "--program", "mcp-test", "create-user", "alice"; |
| 100 |
|
|
is $exit, 0, "program override created: $err"; |
| 101 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 102 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{scope}, "program", "program takes precedence"; |
| 103 |
|
|
is $config->users ("test", "other-program")->{alice}{scope}, "global", |
| 104 |
|
|
"other program still inherits global"; |
| 105 |
|
|
($exit, $out, $err) = cli "--instance", "test", "revoke-user", "alice"; |
| 106 |
|
|
is $exit, 0, "local disable shadows inherited alice: $err"; |
| 107 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 108 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{generation}, -1, "instance deny shadows program"; |
| 109 |
|
|
is scalar @{$config->user_credentials ("test", "mcp-test")}, 1, "shadowed user not accepted"; |
| 110 |
|
|
($exit, $out, $err) = cli "--instance", "test", "create-user", "alice"; |
| 111 |
|
|
is $exit, 0, "instance re-enable works"; |
| 112 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 113 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{alice}{scope}, "instance", "instance takes precedence"; |
| 114 |
|
|
is $config->users ("test", "mcp-test")->{alice}{generation}, 2, "instance restarts at generation 2"; |
| 115 |
|
|
|
| 116 |
|
|
($exit, $out, $err) = cli "revoke-user", "default"; |
| 117 |
|
|
is $exit, 0, "implicit default can be revoked"; |
| 118 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 119 |
root |
1.1 |
is_deeply $config->user_credentials ("another", "mcp-test")->[0][0], "alice", |
| 120 |
|
|
"other users work without default"; |
| 121 |
|
|
my %test_http = $config->http_settings ("test"); |
| 122 |
|
|
is scalar @{$test_http{connect_tokens}}, 0, |
| 123 |
|
|
"revoked default cannot register auxiliary connections"; |
| 124 |
|
|
($exit, $out, $err) = cli "create-user", "default"; |
| 125 |
|
|
is $exit, 0, "default can be rotated back into existence"; |
| 126 |
root |
1.3 |
$config = AnyEvent::MCP::Config->new (file => $file)->load; |
| 127 |
root |
1.1 |
is $config->users ("test", "mcp-test")->{default}{generation}, 2, "default rotated"; |
| 128 |
|
|
|
| 129 |
|
|
my $original_request = { action => "read", dimensions => { |
| 130 |
|
|
user => { label => "Fake", value => "instance:admin", choices => [] }, |
| 131 |
|
|
} }; |
| 132 |
|
|
my $bound = AnyEvent::MCP::Server::Permissions::_user_request ( |
| 133 |
|
|
{ user => "global:alice" }, $original_request); |
| 134 |
|
|
is $bound->{action}, "read", "permission binding preserves original request"; |
| 135 |
|
|
is $bound->{dimensions}{user}{value}, "global:alice", "tool-supplied user is replaced"; |
| 136 |
|
|
is $bound->{dimensions}{user}{default}, "global:alice", |
| 137 |
|
|
"exact user scope uses the permission engine's literal matcher convention"; |
| 138 |
|
|
my $exact = AnyEvent::MCP::Server::Permissions::_regex ($bound->{dimensions}{user}{default}); |
| 139 |
|
|
ok "global:alice" =~ $exact, "exact user matcher accepts its own identity through permission regex rules"; |
| 140 |
|
|
ok "global:alice2" !~ $exact, "exact user matcher rejects a different identity"; |
| 141 |
|
|
ok "global:bob" !~ $exact, "exact user matcher rejects another user"; |
| 142 |
|
|
my $all = AnyEvent::MCP::Server::Permissions::_regex ($bound->{dimensions}{user}{choices}[1]{match}); |
| 143 |
|
|
ok "global:alice" =~ $all, "all-users matcher accepts current identity"; |
| 144 |
|
|
my $scope = AnyEvent::MCP::Server::Permissions->new; |
| 145 |
|
|
is $scope->check ({ %$bound, reason => "scope test", sandbox_grants => [] })->{decision}, |
| 146 |
|
|
"ask", "managed user scope passes actual permission request validation"; |
| 147 |
|
|
$bound = AnyEvent::MCP::Server::Permissions::_user_request ({}, $original_request); |
| 148 |
|
|
ok !exists $bound->{dimensions}{user}, "unidentified request gets no fabricated user"; |
| 149 |
|
|
|
| 150 |
|
|
{ |
| 151 |
|
|
my $graph = { read => [], write => ["read"], host => ["write"] }; |
| 152 |
|
|
my $request = { action => "exec", reason => "test", sandbox_grants => ["host"] }; |
| 153 |
|
|
my $permissions = AnyEvent::MCP::Server::Permissions->new ( |
| 154 |
|
|
sandbox_grants => $graph, |
| 155 |
|
|
rules => [{ decision => "deny", action => '^exec\z', "sandbox-grants" => "write" }], |
| 156 |
|
|
); |
| 157 |
|
|
is $permissions->check ($request)->{decision}, "deny", |
| 158 |
|
|
"saved workdir-write denial also denies stronger host permission"; |
| 159 |
|
|
|
| 160 |
|
|
$permissions = AnyEvent::MCP::Server::Permissions->new ( |
| 161 |
|
|
sandbox_grants => $graph, |
| 162 |
|
|
rules => [{ decision => "allow", action => '^exec\z', "sandbox-grants" => "write" }], |
| 163 |
|
|
); |
| 164 |
|
|
is $permissions->check ($request)->{decision}, "ask", |
| 165 |
|
|
"saved workdir-write allowance cannot authorize stronger host permission"; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
|
|
|
| 169 |
|
|
($exit, $out, $err) = run "bin/mcp-fs", "--print-url=alice"; |
| 170 |
|
|
is $exit, 0, "print-url for named user succeeds: $err"; |
| 171 |
|
|
like $out, qr/\Ahttps?:\/\/\S+\/\s+# user global:alice/m, "printed URL names its principal"; |
| 172 |
|
|
($exit, $out, $err) = run "bin/mcp-fs", "--print-url=unknown"; |
| 173 |
|
|
isnt $exit, 0, "unknown user selector fails"; |
| 174 |
|
|
|
| 175 |
|
|
{ |
| 176 |
|
|
socket my $listener, Socket::AF_INET (), Socket::SOCK_STREAM (), 0 |
| 177 |
|
|
or die "reserve TCP port: $!"; |
| 178 |
|
|
bind $listener, Socket::pack_sockaddr_in (0, Socket::inet_aton ("127.0.0.1")) |
| 179 |
|
|
or die "bind reserved port: $!"; |
| 180 |
|
|
listen $listener, 1 or die "listen reserved port: $!"; |
| 181 |
|
|
my ($port) = Socket::unpack_sockaddr_in (getsockname $listener); |
| 182 |
|
|
|
| 183 |
|
|
($exit, $out, $err) = run "bin/mcp-multibroker", |
| 184 |
|
|
"--bind", "127.0.0.1:$port", "--print-urls=alice"; |
| 185 |
|
|
is $exit, 0, "plural URL option succeeds even with the address already listening: $err"; |
| 186 |
|
|
like $out, qr{\Ahttp://127\.0\.0\.1:$port/[A-Za-z0-9_-]{43}/ \# user global:alice\n\z}, |
| 187 |
|
|
"URL for occupied port is derived without attempting to bind"; |
| 188 |
|
|
|
| 189 |
|
|
($exit, $out, $err) = run "bin/mcp-multibroker", |
| 190 |
|
|
"--bind", "127.0.0.1:$port", "--print-url"; |
| 191 |
|
|
is $exit, 0, "original singular option also prints without binding: $err"; |
| 192 |
|
|
like $out, qr/\# user global:default \(connect\)/, "unfiltered listing identifies connection user"; |
| 193 |
|
|
close $listener; |
| 194 |
|
|
} |
| 195 |
|
|
|
| 196 |
|
|
my $meta = { |
| 197 |
|
|
"io.modelcontextprotocol/protocolVersion" => "2026-07-28", |
| 198 |
|
|
"io.modelcontextprotocol/clientCapabilities" => { elicitation => { form => {} } }, |
| 199 |
|
|
}; |
| 200 |
|
|
sub asserted_user { |
| 201 |
|
|
my ($ask, $value, $sent) = @_; |
| 202 |
|
|
my $server = AnyEvent::MCP::Server->new ( |
| 203 |
|
|
permissions => AnyEvent::MCP::Server::Permissions->new (ask => $ask), |
| 204 |
|
|
on_request => sub { |
| 205 |
|
|
my ($srv, $req) = @_; |
| 206 |
|
|
$req->reply ({ user => $req->{user} }); |
| 207 |
|
|
}, |
| 208 |
|
|
); |
| 209 |
|
|
my $reply; |
| 210 |
|
|
$server->rpc ({ |
| 211 |
|
|
jsonrpc => "2.0", id => 1, method => "tools/call", |
| 212 |
|
|
params => { |
| 213 |
|
|
name => "example", arguments => {}, |
| 214 |
|
|
_meta => { %$meta, "anyevent-mcp/user" => $sent }, |
| 215 |
|
|
}, |
| 216 |
|
|
}, sub { $reply = $_[0] }, |
| 217 |
|
|
peer => {}, user => $value, protocol_version => "2026-07-28"); |
| 218 |
|
|
$reply |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
is asserted_user ("mcp", "global:default", "global:alice")->{result}{user}, |
| 222 |
|
|
"global:alice", "trusted MCP client can assert original user"; |
| 223 |
|
|
is asserted_user ("helper", "global:default", "global:alice")->{result}{user}, |
| 224 |
|
|
"global:default", "default helper mode ignores asserted user"; |
| 225 |
|
|
is asserted_user ("auto", "global:default", "global:alice")->{result}{user}, |
| 226 |
|
|
"global:alice", "auto with native forms trusts the peer"; |
| 227 |
|
|
ok !defined asserted_user ("mcp", "global:default", undef)->{result}{user}, |
| 228 |
|
|
"trusted proxy can assert an explicitly anonymous caller"; |
| 229 |
|
|
like asserted_user ("mcp", "global:default", "admin!")->{error}{message}, |
| 230 |
|
|
qr/Invalid delegated user/, "malformed trusted identity rejected"; |
| 231 |
|
|
|
| 232 |
|
|
done_testing; |