blib/lib/MCP/Server/KnowledgeStore/Tools/Spec.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package MCP::Server::KnowledgeStore::Tools::Spec;
2 1 ▶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 spec MCP tools, as an MCP::Server::KnowledgeStore::Tools plugin
8  
9 use Mojo::JSON qw(true false);
10 use MCP::Server::KnowledgeStore::Tools::Support qw(
11 name_pattern summary author revision_property author_property
12 project_property
13 );
14  
15 my $NAME_PATTERN = name_pattern;
16  
17 sub register ($class, $server, $store, %opts) {
18 $server->tool(
19 name => 'list_specs',
20 description =>
21 'List spec entries for a project - the write-up others can read to '
22 . 'see what a project is meant to do',
23 input_schema => {
24 type => 'object',
25 properties => project_property(),
26 additionalProperties => false,
27 },
28 code => sub ($tool, $args) {
29 my $entries = $store->list('spec', $args->{project});
30 return $tool->structured_result(
31 { specs => [map { summary($_) } @$entries] });
32 },
33 );
34  
35 # Postgres tsvector picks the candidates, TF-IDF corpus vectors rank
36 # them, and the matching lines come back with the hit. One tool: the
37 # two this replaced ran the same query through the same ranking.
38 $server->tool(
39 name => 'search_specs',
40 description =>
41 'Search across spec frontmatter and bodies. Postgres tsvector '
42 . 'picks the candidates, TF-IDF corpus vectors rank them.',
43 input_schema => {
44 type => 'object',
45 properties => {
46 query => { type => 'string', description => 'Text to search for' },
47 limit => {
48 type => 'integer',
49 minimum => 1,
50 maximum => 100,
51 default => 10,
52 description => 'Maximum number of results (default: 10)'
53 },
54 project_property()->%*,
55 },
56 required => ['query'],
57 additionalProperties => false,
58 },
59 code => sub ($tool, $args) {
60 my $hits = $store->search('spec', $args->{query}, $args->{project},
61 $args->{limit} // 10);
62 return $tool->structured_result(
63 {
64 specs => [
65 map {
66 {
67 summary($_)->%*,
68 matches => $_->{matches},
69 rank => $_->{rank},
70 ts_rank => $_->{ts_rank}
71 }
72 } @$hits
73 ]
74 }
75 );
76 },
77 );
78  
79 $server->tool(
80 name => 'get_spec',
81 description =>
82 'Fetch the full content of one spec, at its current or an older revision',
83 input_schema => {
84 type => 'object',
85 properties => {
86 name => { type => 'string', pattern => $NAME_PATTERN },
87 revision_property()->%*,
88 },
89 required => ['name'],
90 additionalProperties => false,
91 },
92 code => sub ($tool, $args) {
93 my $entry = $store->get('spec', $args->{name}, $args->{revision});
94 return $tool->text_result(
95 "No spec named '$args->{name}'"
96 . (
97 defined $args->{revision} ? " at revision $args->{revision}" : ''
98 ),
99 1
100 ) unless $entry;
101 return $entry->{content};
102 },
103 );
104  
105 $server->tool(
106 name => 'save_spec',
107 description => 'Save a spec as a new revision in the shared store',
108 input_schema => {
109 type => 'object',
110 properties => {
111 name => { type => 'string', pattern => $NAME_PATTERN },
112 content => { type => 'string' },
113 description => { type => 'string' },
114 project_property()->%*,
115 author_property()->%*,
116 },
117 required => [qw(name content project)],
118 additionalProperties => true,
119 },
120 code => sub ($tool, $args) {
121 my $entry = $store->save(
122 'spec', $args->{name}, $args->{content},
123 author => author($tool, $args),
124 map { defined $args->{$_} ? ($_ => $args->{$_}) : () }
125 qw(description project)
126 );
127 return $tool->structured_result(
128 { name => $entry->{name}, revision => $entry->{revision} });
129 },
130 );
131  
132 $server->tool(
133 name => 'list_spec_revisions',
134 description => 'List every saved revision of one spec, newest first',
135 input_schema => {
136 type => 'object',
137 properties => { name => { type => 'string', pattern => $NAME_PATTERN } },
138 required => ['name'],
139 additionalProperties => false,
140 },
141 code => sub ($tool, $args) {
142 my $history = $store->history('spec', $args->{name});
143 return $tool->text_result("No spec named '$args->{name}'", 1)
144 unless @$history;
145 my @revisions;
146 for my $row (@$history) {
147 push @revisions,
148 {
149 revision => $row->{revision},
150 description => $row->{description},
151 created_at => "$row->{created_at}",
152 is_head => $row->{is_head} ? true : false,
153 map { defined $row->{$_} ? ($_ => $row->{$_}) : () }
154 qw(author type project),
155 };
156 }
157 return $tool->structured_result({ revisions => \@revisions });
158 },
159 );
160  
161 $server->tool(
162 name => 'mark_spec_useful',
163 description =>
164 'Record that a spec was useful, incrementing its useful_count',
165 input_schema => {
166 type => 'object',
167 properties => { name => { type => 'string', pattern => $NAME_PATTERN } },
168 required => ['name'],
169 additionalProperties => false,
170 },
171 code => sub ($tool, $args) {
172 my $count = eval { $store->mark_useful('spec', $args->{name}) };
173 return $tool->text_result("No spec named '$args->{name}'", 1)
174 unless defined $count;
175 return $tool->structured_result(
176 { name => $args->{name}, useful_count => $count });
177 },
178 );
179  
180 # Lifecycle management tools - we need to handle allow_purge here since
181 # Spec doesn't use Revisioned. Get it from the server stash if passed.
182 my $allow_purge = $opts{allow_purge} // 1;
183  
184 $server->tool(
185 name => 'archive_spec',
186 description =>
187 'Archive (soft delete) a spec. Hides it from normal operations '
188 . 'but keeps all data and revision history.',
189 input_schema => {
190 type => 'object',
191 properties => {
192 name => { type => 'string', pattern => $NAME_PATTERN },
193 expected_revision => {
194 type => 'integer',
195 minimum => 1,
196 description => 'Optional: verify this revision before archiving',
197 },
198 author => {
199 type => 'string',
200 description => 'Who to record as performing the archive'
201 },
202 reason =>
203 { type => 'string', description => 'Optional reason for archiving' },
204 },
205 required => ['name'],
206 additionalProperties => false,
207 },
208 code => sub ($tool, $args) {
209 my $author = author($tool, $args);
210 my $entry = eval {
211 $store->archive(
212 'spec', $args->{name},
213 author => $author,
214 expected_revision => $args->{expected_revision},
215 reason => $args->{reason},
216 );
217 };
218 return $tool->text_result(
219 "No spec named '$args->{name}' or already archived", 1)
220 if $@ && $@ =~ /no spec named/;
221 return $tool->text_result($@, 1) if $@;
222 return $tool->text_result("Already archived: $args->{name}", 1)
223 unless $entry;
224 return $tool->structured_result(
225 {
226 name => $entry->{name},
227 revision => $entry->{revision},
228 archived_at => "$entry->{archived_at}",
229 }
230 );
231 },
232 );
233  
234 $server->tool(
235 name => 'restore_spec',
236 description => 'Restore a previously archived spec',
237 input_schema => {
238 type => 'object',
239 properties => {
240 name => { type => 'string', pattern => $NAME_PATTERN },
241 author => {
242 type => 'string',
243 description => 'Who to record as performing the restore'
244 },
245 reason =>
246 { type => 'string', description => 'Optional reason for restoring' },
247 },
248 required => ['name'],
249 additionalProperties => false,
250 },
251 code => sub ($tool, $args) {
252 my $author = author($tool, $args);
253 my $entry = eval {
254 $store->restore(
255 'spec', $args->{name},
256 author => $author,
257 reason => $args->{reason}
258 );
259 };
260 return $tool->text_result($@, 1) if $@;
261 return $tool->text_result("No archived spec named '$args->{name}'", 1)
262 unless $entry;
263 return $tool->structured_result(
264 {
265 name => $entry->{name},
266 revision => $entry->{revision},
267 }
268 );
269 },
270 );
271  
272 $server->tool(
273 name => 'purge_spec',
274 description => "Permanently delete a spec and ALL its revision history. "
275 . (
276 $allow_purge
277 ? 'This is destructive and cannot be undone.'
278 : 'DISABLED on this server'
279 ),
280 input_schema => {
281 type => 'object',
282 properties => {
283 name => { type => 'string', pattern => $NAME_PATTERN },
284 expected_revision => {
285 type => 'integer',
286 minimum => 1,
287 description => 'Optional: verify this revision before purging',
288 },
289 author => {
290 type => 'string',
291 description => 'Who to record as performing the purge'
292 },
293 reason => {
294 type => 'string',
295 description => 'Optional reason for permanent deletion'
296 },
297 },
298 required => ['name'],
299 additionalProperties => false,
300 },
301 code => sub ($tool, $args) {
302 return $tool->text_result("purge_spec is disabled on this server", 1)
303 unless $allow_purge;
304  
305 my $author = author($tool, $args);
306 my $result = eval {
307 $store->purge(
308 'spec', $args->{name},
309 author => $author,
310 expected_revision => $args->{expected_revision},
311 reason => $args->{reason},
312 );
313 };
314 return $tool->text_result($@, 1) if $@;
315 return $tool->structured_result($result);
316 },
317 );
318  
319 $server->tool(
320 name => 'list_archived_specs',
321 description =>
322 'List archived spec entries, optionally filtered by project',
323 input_schema => {
324 type => 'object',
325 properties => project_property(),
326 additionalProperties => false,
327 },
328 code => sub ($tool, $args) {
329 my $entries = $store->list_archived('spec', $args->{project});
330 return $tool->structured_result(
331 { specs => [map { summary($_) } @$entries] });
332 },
333 );
334  
335 # Add MCP Resource Templates (if server supports them)
336 # These provide addressable reads via URIs as an alternative to get_spec
337 if ($server->can('add_template')) {
338 $server->add_template(
339 uri_template => 'spec://specs/{name}',
340 name => 'spec_read',
341 description => 'Read the current revision of a spec by name',
342 mime_type => 'text/markdown',
343 code => sub ($template, $params, $context) {
344 my $name = $params->{name};
345 my $entry = $store->get('spec', $name);
346 return undef unless $entry;
347 return $entry->{content};
348 },
349 );
350  
351 $server->add_template(
352 uri_template => 'spec://specs/{name}/revisions/{revision}',
353 name => 'spec_read_revision',
354 description =>
355 'Read a specific revision of a spec by name and revision number',
356 mime_type => 'text/markdown',
357 code => sub ($template, $params, $context) {
358 my $name = $params->{name};
359 my $revision = $params->{revision};
360 my $entry = $store->get('spec', $name, $revision);
361 return undef unless $entry;
362 return $entry->{content};
363 },
364 );
365 }
366  
367 return;
368 }
369  
370 1;
371  
372 __END__
↑ Summary