| 1 | package MCP::Server::KnowledgeStore::Command::token; | ||
| 2 | 1 | ▶ | use v5.38; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Mojo::Base 'Mojolicious::Command', -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | our $VERSION = '0.001'; | ||
| 6 | |||
| 7 | # ABSTRACT: Manage the agent tokens that authenticate /mcp | ||
| 8 | |||
| 9 | has description => 'Manage agent tokens'; | ||
| 10 | has usage => <<~'USAGE'; | ||
| 11 | Usage: APPLICATION token list | ||
| 12 | APPLICATION token add <name> | ||
| 13 | APPLICATION token revoke <name> | ||
| 14 | |||
| 15 | ./bin/mcp-ks token add 'claude-code machine-a' | ||
| 16 | |||
| 17 | A token is shown once, when it is created, and cannot be recovered | ||
| 18 | afterwards - only its hash is stored. Mint a new one instead. | ||
| 19 | USAGE | ||
| 20 | |||
| 21 | sub run ($self, $action = undef, @args) { | ||
| 22 | my $store = $self->app->store; | ||
| 23 | my $method = { | ||
| 24 | list => \&_list, | ||
| 25 | add => \&_add, | ||
| 26 | revoke => \&_revoke, | ||
| 27 | }->{ $action // '' }; | ||
| 28 | die $self->usage unless $method; | ||
| 29 | |||
| 30 | # A croak from the store is a message for the operator, not a stack | ||
| 31 | # trace: strip the "at ... line N" Perl appends and exit non-zero. | ||
| 32 | unless (eval { $method->($self, $store, @args); 1 }) { | ||
| 33 | my $err = $@ // 'unknown error'; | ||
| 34 | $err =~ s/ at \S+ line \d+\.?\s*\z//; | ||
| 35 | say STDERR $err; | ||
| 36 | exit 1; | ||
| 37 | } | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _list ($self, $store, @) { | ||
| 42 | my $tokens = $store->list_tokens; | ||
| 43 | return say 'No tokens yet.' unless @$tokens; | ||
| 44 | printf "%-30s %-12s %s\n", 'NAME', 'STATE', 'LAST USED'; | ||
| 45 | for my $token (@$tokens) { | ||
| 46 | printf "%-30s %-12s %s\n", $token->{name}, | ||
| 47 | ($token->{active} ? 'active' : 'revoked'), | ||
| 48 | $token->{last_used_at} // 'never'; | ||
| 49 | } | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _add ($self, $store, $name = undef, @) { | ||
| 54 | die $self->usage unless defined $name && length $name; | ||
| 55 | my $token = $store->create_token($name); | ||
| 56 | say "Token for $token->{name}:"; | ||
| 57 | say ""; | ||
| 58 | say " $token->{token}"; | ||
| 59 | say ""; | ||
| 60 | say 'Shown once - store it now. Only its hash is kept.'; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _revoke ($self, $store, $name = undef, @) { | ||
| 65 | die $self->usage unless defined $name && length $name; | ||
| 66 | my $revoked = $store->revoke_token($name); | ||
| 67 | return say "No live token named '$name'." unless $revoked; | ||
| 68 | say "Revoked $revoked->{name} at $revoked->{revoked_at}."; | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ | ||