| 1 | package MCP::Server::KnowledgeStore::Tools::Agent; | ||
| 2 | 2 | ▶ | use v5.38; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Mojo::Base -strict, -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | our $VERSION = '0.001'; | ||
| 6 | |||
| 7 | # ABSTRACT: The agent MCP tools, as an MCP::Server::KnowledgeStore::Tools plugin | ||
| 8 | |||
| 9 | use Mojo::JSON qw(false); | ||
| 10 | use MCP::Server::KnowledgeStore::Tools::Support | ||
| 11 | qw(name_pattern author author_property); | ||
| 12 | |||
| 13 | my $NAME_PATTERN = name_pattern; | ||
| 14 | |||
| 15 | sub _project_property () { | ||
| 16 | return { | ||
| 17 | project => { | ||
| 18 | type => 'string', | ||
| 19 | description => 'Only agents tagged as tailored for this project', | ||
| 20 | }, | ||
| 21 | }; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub register ($class, $server, $store, %opts) { | ||
| 25 | $server->tool( | ||
| 26 | name => 'list_agents', | ||
| 27 | description => | ||
| 28 | 'List agent definitions held in the store - global, one per name ' | ||
| 29 | . '(there is one "analyse", not a per-project copy). project filters ' | ||
| 30 | . 'to agents tailored for that project; omit it to list every agent.', | ||
| 31 | input_schema => { | ||
| 32 | type => 'object', | ||
| 33 | properties => _project_property(), | ||
| 34 | additionalProperties => false, | ||
| 35 | }, | ||
| 36 | code => sub ($tool, $args) { | ||
| 37 | return $tool->structured_result( | ||
| 38 | { agents => $store->list_agents($args->{project}) }); | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | $server->tool( | ||
| 43 | name => 'get_agent', | ||
| 44 | description => | ||
| 45 | 'Fetch the full content of one agent definition (.claude/agents/*.md), ' | ||
| 46 | . 'verbatim - frontmatter and all, unparsed', | ||
| 47 | input_schema => { | ||
| 48 | type => 'object', | ||
| 49 | properties => { name => { type => 'string', pattern => $NAME_PATTERN } }, | ||
| 50 | required => ['name'], | ||
| 51 | additionalProperties => false, | ||
| 52 | }, | ||
| 53 | code => sub ($tool, $args) { | ||
| 54 | my $agent = $store->get_agent($args->{name}); | ||
| 55 | return $tool->text_result("No agent named '$args->{name}'", 1) | ||
| 56 | unless $agent; | ||
| 57 | return $agent->{body}; | ||
| 58 | }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | $server->tool( | ||
| 62 | name => 'save_agent', | ||
| 63 | description => | ||
| 64 | 'Save an agent definition verbatim, overwriting whatever was there for ' | ||
| 65 | . 'that name - no revision history, no frontmatter parsing. Agents are ' | ||
| 66 | . 'global; use tag_agent to say which projects it fits.', | ||
| 67 | input_schema => { | ||
| 68 | type => 'object', | ||
| 69 | properties => { | ||
| 70 | name => { type => 'string', pattern => $NAME_PATTERN }, | ||
| 71 | content => { type => 'string' }, | ||
| 72 | author_property()->%*, | ||
| 73 | }, | ||
| 74 | required => [qw(name content)], | ||
| 75 | additionalProperties => false, | ||
| 76 | }, | ||
| 77 | code => sub ($tool, $args) { | ||
| 78 | my $agent = $store->save_agent($args->{name}, $args->{content}, | ||
| 79 | author => author($tool, $args)); | ||
| 80 | return $tool->structured_result( | ||
| 81 | { name => $agent->{name}, updated_at => "$agent->{updated_at}" }); | ||
| 82 | }, | ||
| 83 | ); | ||
| 84 | |||
| 85 | $server->tool( | ||
| 86 | name => 'tag_agent', | ||
| 87 | description => | ||
| 88 | 'Tag an agent as tailored for a project (additive - does not remove ' | ||
| 89 | . 'other tags)', | ||
| 90 | input_schema => { | ||
| 91 | type => 'object', | ||
| 92 | properties => { | ||
| 93 | name => { type => 'string', pattern => $NAME_PATTERN }, | ||
| 94 | project => { type => 'string' }, | ||
| 95 | }, | ||
| 96 | required => [qw(name project)], | ||
| 97 | additionalProperties => false, | ||
| 98 | }, | ||
| 99 | code => sub ($tool, $args) { | ||
| 100 | eval { $store->tag_agent($args->{name}, $args->{project}) }; | ||
| 101 | return $tool->text_result("No agent named '$args->{name}'", 1) if $@; | ||
| 102 | return $tool->structured_result( | ||
| 103 | { name => $args->{name}, project => $args->{project} }); | ||
| 104 | }, | ||
| 105 | ); | ||
| 106 | |||
| 107 | $server->tool( | ||
| 108 | name => 'untag_agent', | ||
| 109 | description => "Remove an agent's tag for a project", | ||
| 110 | input_schema => { | ||
| 111 | type => 'object', | ||
| 112 | properties => { | ||
| 113 | name => { type => 'string', pattern => $NAME_PATTERN }, | ||
| 114 | project => { type => 'string' }, | ||
| 115 | }, | ||
| 116 | required => [qw(name project)], | ||
| 117 | additionalProperties => false, | ||
| 118 | }, | ||
| 119 | code => sub ($tool, $args) { | ||
| 120 | $store->untag_agent($args->{name}, $args->{project}); | ||
| 121 | return $tool->structured_result( | ||
| 122 | { name => $args->{name}, project => $args->{project} }); | ||
| 123 | }, | ||
| 124 | ); | ||
| 125 | |||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | __END__ | ||