| 1 | package MCP::Server::KnowledgeStore::Command::agent; | ||
| 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: Sync agent definitions between the store and .claude/agents | ||
| 8 | |||
| 9 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 10 | use Mojo::File qw(curfile); | ||
| 11 | |||
| 12 | has description => 'Sync agent definitions with .claude/agents'; | ||
| 13 | has usage => <<~'USAGE'; | ||
| 14 | Usage: APPLICATION agent list [--project NAME] | ||
| 15 | APPLICATION agent pull <name> [--dir PATH] | ||
| 16 | APPLICATION agent pull --project NAME [--dir PATH] | ||
| 17 | APPLICATION agent push <name> [--dir PATH] [--project NAME] [--author NAME] | ||
| 18 | |||
| 19 | ./bin/mcp-ks agent pull react-native-programming | ||
| 20 | ./bin/mcp-ks agent pull --project abto | ||
| 21 | ./bin/mcp-ks agent push react-native-programming --project abto | ||
| 22 | |||
| 23 | Claude Code resolves subagent_type from .claude/agents/*.md before any | ||
| 24 | tool call can run, so pulling an agent from the store is what actually | ||
| 25 | makes it usable in a session - reading it over MCP alone does not. | ||
| 26 | --dir defaults to ./.claude/agents (relative to the current directory). | ||
| 27 | USAGE | ||
| 28 | |||
| 29 | sub run ($self, $action = undef, @args) { | ||
| 30 | my $method = { | ||
| 31 | list => \&_list, | ||
| 32 | pull => \&_pull, | ||
| 33 | push => \&_push, | ||
| 34 | }->{ $action // '' }; | ||
| 35 | die $self->usage unless $method; | ||
| 36 | |||
| 37 | unless (eval { $method->($self, @args); 1 }) { | ||
| 38 | my $err = $@ // 'unknown error'; | ||
| 39 | $err =~ s/ at \S+ line \d+\.?\s*\z//; | ||
| 40 | say STDERR $err; | ||
| 41 | exit 1; | ||
| 42 | } | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _dir_for (@args) { | ||
| 47 | my $dir = '.claude/agents'; | ||
| 48 | GetOptionsFromArray(\@args, 'dir=s' => \$dir); | ||
| 49 | return (Mojo::File->new($dir), @args); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _list ($self, @args) { | ||
| 53 | my $project; | ||
| 54 | GetOptionsFromArray(\@args, 'project=s' => \$project); | ||
| 55 | my $store = $self->app->store; | ||
| 56 | my $agents = $store->list_agents($project); | ||
| 57 | return say 'No agents in the store.' unless @$agents; | ||
| 58 | printf "%-30s %-20s %s\n", 'NAME', 'AUTHOR', 'PROJECTS'; | ||
| 59 | for my $agent (@$agents) { | ||
| 60 | printf "%-30s %-20s %s\n", $agent->{name}, $agent->{author} // '', | ||
| 61 | join(', ', @{ $agent->{projects} // [] }); | ||
| 62 | } | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _pull ($self, @args) { | ||
| 67 | my $project; | ||
| 68 | GetOptionsFromArray(\@args, 'project=s' => \$project); | ||
| 69 | my ($dir, $name) = _dir_for(@args); | ||
| 70 | die $self->usage unless defined $project || defined $name; | ||
| 71 | |||
| 72 | my $store = $self->app->store; | ||
| 73 | my @names | ||
| 74 | = defined $project | ||
| 75 | ? map { $_->{name} } @{ $store->list_agents($project) } | ||
| 76 | : ($name); | ||
| 77 | die "No agents tagged for project '$project'.\n" | ||
| 78 | if defined $project && !@names; | ||
| 79 | |||
| 80 | $dir->make_path unless -d $dir; | ||
| 81 | for my $agent_name (@names) { | ||
| 82 | my $agent = $store->get_agent($agent_name); | ||
| 83 | die "No agent named '$agent_name'.\n" unless $agent; | ||
| 84 | my $file = $dir->child("$agent_name.md"); | ||
| 85 | $file->spurt($agent->{body}); | ||
| 86 | say "Pulled $agent_name -> $file"; | ||
| 87 | } | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _push ($self, @args) { | ||
| 92 | my ($project, $author); | ||
| 93 | GetOptionsFromArray( | ||
| 94 | \@args, | ||
| 95 | 'project=s' => \$project, | ||
| 96 | 'author=s' => \$author | ||
| 97 | ); | ||
| 98 | my ($dir, $name) = _dir_for(@args); | ||
| 99 | die $self->usage unless defined $name; | ||
| 100 | |||
| 101 | my $file = $dir->child("$name.md"); | ||
| 102 | die "No such file: $file\n" unless -e $file; | ||
| 103 | |||
| 104 | my $store = $self->app->store; | ||
| 105 | $store->save_agent($name, $file->slurp, author => $author); | ||
| 106 | say "Pushed $file -> $name"; | ||
| 107 | |||
| 108 | if (defined $project) { | ||
| 109 | $store->tag_agent($name, $project); | ||
| 110 | say "Tagged $name for project '$project'"; | ||
| 111 | } | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ | ||