MCP/Tool.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 75.0% 12 / 16 Conditioncond 55.0% 11 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 94.1% 16 / 17 Podpod 100% 7 / 7 totaltotal 80.4% 99 / 123 CC 26 SCAR 16.3
CC
26
Cov
84%
resource_link_result
11.6
image_result
7.3
audio_result
5.6
CRAP 5.1
« MCP/Server.pm ↑ Summary MCP/Server/Transport/HTTP.pm »
1 package MCP::Tool;
2 4 ▶use Mojo::Base 'MCP::Primitive', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::JSON qw(false to_json true);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::Util qw(b64_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶has annotations => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
10 0 ▶has code => sub { die 'Tool code not implemented' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 4 has description => 'Generic MCP tool';
12 10 ▶has input_schema => sub { {type => 'object'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 4 has name => 'tool';
14 4 has 'output_schema';
15  
16 1 ▶sub audio_result ($self, $audio, $options = {}, $is_error = 0) {
Subroutine audio_result: called CC=5 SCAR=17.2
Pod documented
17 return {
18 1 ▶ content => [{type => 'audio', data => b64_encode($audio, ''), mimeType => $options->{mime_type} // 'audio/wav'}],
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 0 1 1
Condition50%1/2
$options->{"mime_type"} // "audio/wav"
l!l
01
MC/DC0%0/1
$options->{"mime_type"} // "audio/wav"
$options->{"mime_type"}
Truth table50%1/2
A
A= $options->{"mime_type"}
Aresult
11
00
19 isError => $is_error ? true : false
20 };
21 }
22  
23 ▶sub call ($self, $args, $context) {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
24 22 local $self->{context} = $context;
25 22 my $result = $self->code->($self, $args);
26 22 ▶ return $result->then(sub { $self->_type_check($_[0]) }) if blessed($result) && $result->isa('Mojo::Promise');
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Mojo::Promise") 3 19 22
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
1903
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Mojo::Promise")
builtin::blessed($result) $result->isa("Mojo::Promise")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Mojo::Promise")
ABresult
0X0
100
111
27 19 return $self->_type_check($result);
28 }
29  
30 1 ▶sub image_result ($self, $image, $options = {}, $is_error = 0) {
Subroutine image_result: called CC=6 SCAR=19.9
Pod documented
31 return {
32 content => [{
33 type => 'image',
34 data => b64_encode($image, ''),
35 mimeType => $options->{mime_type} // 'image/png',
36 annotations => $options->{annotations} // {}
37 1 ▶ }],
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 0 1 1
Condition50%2/4
$options->{"mime_type"} // "image/png"
l!l
01
$options->{"annotations"} // {}
l!l
10
MC/DC0%0/1
$options->{"mime_type"} // "image/png"
$options->{"mime_type"}
MC/DC0%0/1
$options->{"annotations"} // {}
$options->{"annotations"}
Truth table50%1/2
A
A= $options->{"mime_type"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"annotations"}
Aresult
11
00
38 isError => $is_error ? true : false
39 };
40 }
41  
42 1 ▶sub resource_link_result ($self, $uri, $options = {}, $is_error = 0) {
Subroutine resource_link_result: called CC=8 SCAR=24.5
Pod documented
43 return {
44 content => [{
45 type => 'resource_link',
46 uri => $uri,
47 name => $options->{name} // '',
48 description => $options->{description} // '',
49 mimeType => $options->{mime_type} // 'text/plain',
50 annotations => $options->{annotations} // {}
51 1 ▶ }],
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 0 1 1
Condition50%4/8
$options->{"name"} // ""
l!l
10
$options->{"description"} // ""
l!l
10
$options->{"mime_type"} // "text/plain"
l!l
01
$options->{"annotations"} // {}
l!l
01
MC/DC0%0/1
$options->{"name"} // ""
$options->{"name"}
MC/DC0%0/1
$options->{"description"} // ""
$options->{"description"}
MC/DC0%0/1
$options->{"mime_type"} // "text/plain"
$options->{"mime_type"}
MC/DC0%0/1
$options->{"annotations"} // {}
$options->{"annotations"}
Truth table50%1/2
A
A= $options->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"description"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"mime_type"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"annotations"}
Aresult
11
00
52 isError => $is_error ? true : false
53 };
54 }
55  
56 2 ▶sub structured_result ($self, $data, $is_error = 0) {
Subroutine structured_result: called CC=2 SCAR=6.9
Pod documented
57 2 my $result = $self->text_result(to_json($data), $is_error);
58 2 $result->{structuredContent} = $data;
59 2 return $result;
60 }
61  
62 19 ▶sub text_result ($self, $text, $is_error = 0) {
Subroutine text_result: called CC=3 SCAR=11.4
Pod documented
63 19 ▶ return {content => [{type => 'text', text => "$text"}], isError => $is_error ? true : false};
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 0 19 19
64 }
65  
66 ▶sub validate_input ($self, $args) {
Subroutine validate_input: called CC=3 SCAR=11.0
Pod documented
67 23 ▶ unless ($self->{validator}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"validator"}) 17 6 23
68 17 my $validator = $self->{validator} = JSON::Validator->new;
69 17 $validator->schema($self->input_schema);
70 }
71  
72 23 my @errors = $self->{validator}->validate($args);
73 23 ▶ return @errors ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 1 22 23
74 }
75  
76 ▶sub _type_check ($self, $result) {
Subroutine _type_check: called CC=3 SCAR=11.1
77 22 ▶ return $result if ref $result eq 'HASH' && exists $result->{content};
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" and exists $result->{"content"} 5 17 22
Condition66%2/3
ref $result eq "HASH" and exists $result->{"content"}
!ll&&!rl&&r
1705
MC/DC50%1/2
ref $result eq "HASH" and exists $result->{"content"}
ref $result eq "HASH" exists $result->{"content"}
Truth table66%2/3
A and B
A= ref $result eq "HASH"B= exists $result->{"content"}
ABresult
0X0
100
111
78 17 return $self->text_result($result);
79 }
80  
81 4 1;
82  
83 =encoding utf8
84  
85 =head1 NAME
86  
87 MCP::Tool - Tool container
88  
89 =head1 SYNOPSIS
90  
91 use MCP::Tool;
92  
93 my $tool = MCP::Tool->new;
94  
95 =head1 DESCRIPTION
96  
97 L<MCP::Tool> is a container for tools to be called.
98  
99 =head1 ATTRIBUTES
100  
101 L<MCP::Tool> implements the following attributes.
102  
103 =head2 annotations
104  
105 my $annotations = $tool->annotations;
106 $tool = $tool->annotations({title => '...'});
107  
108 Optional annotations for the tool which provide additional metadata about the tool behavior.
109  
110 =head2 code
111  
112 my $code = $tool->code;
113 $tool = $tool->code(sub { ... });
114  
115 Tool code.
116  
117 =head2 description
118  
119 my $description = $tool->description;
120 $tool = $tool->description('A brief description of the tool');
121  
122 Description of the tool.
123  
124 =head2 input_schema
125  
126 my $schema = $tool->input_schema;
127 $tool = $tool->input_schema({type => 'object', properties => {foo => {type => 'string'}}});
128  
129 JSON schema for validating input arguments.
130  
131 =head2 name
132  
133 my $name = $tool->name;
134 $tool = $tool->name('my_tool');
135  
136 Name of the tool.
137  
138 =head2 output_schema
139  
140 my $schema = $tool->output_schema;
141 $tool = $tool->output_schema({type => 'object', properties => {foo => {type => 'string'}}});
142  
143 JSON schema for validating output results.
144  
145 =head1 METHODS
146  
147 L<MCP::Tool> inherits all methods from L<MCP::Primitive> and implements the following new ones.
148  
149 =head2 audio_result
150  
151 my $result = $tool->audio_result($bytes, $options, $is_error);
152  
153 Returns an audio result in the expected format, optionally marking it as an error.
154  
155 These options are currently available:
156  
157 =over 2
158  
159 =item mime_type
160  
161 mime_type => 'audio/wav'
162  
163 Specifies the MIME type of the audio, defaults to C<audio/wav>.
164  
165 =back
166  
167 =head2 call
168  
169 my $result = $tool->call($args, $context);
170  
171 Calls the tool with the given arguments and context, returning a result. The result can be a promise or a direct value.
172  
173 =head2 image_result
174  
175 my $result = $tool->image_result($bytes, $options, $is_error);
176  
177 Returns an image result in the expected format, optionally marking it as an error.
178  
179 These options are currently available:
180  
181 =over 2
182  
183 =item annotations
184  
185 annotations => {audience => ['user']}
186  
187 Annotations for the image.
188  
189 =item mime_type
190  
191 mime_type => 'image/png'
192  
193 Specifies the MIME type of the image, defaults to C<image/png>.
194  
195 =back
196  
197 =head2 resource_link_result
198  
199 my $result = $tool->resource_link_result($uri, $options, $is_error);
200  
201 Returns a resource link result in the expected format, optionally marking it as an error.
202  
203 These options are currently available:
204  
205 =over 2
206  
207 =item annotations
208  
209 annotations => {audience => ['user']}
210  
211 Annotations for the resource link.
212  
213 =item description
214  
215 description => 'A brief description of the resource'
216  
217 Description of the resource.
218  
219 =item mime_type
220  
221 mime_type => 'text/x-perl'
222  
223 Specifies the MIME type of the resource, defaults to C<text/plain>.
224  
225 =item name
226  
227 name => 'Resource Name'
228  
229 Name of the resource.
230  
231 =back
232  
233 =head2 structured_result
234  
235 my $result = $tool->structured_result({foo => 'bar'}, $is_error);
236  
237 Returns a structured result in the format of L</"output_schema">, optionally marking it as an error.
238  
239 =head2 text_result
240  
241 my $result = $tool->text_result('Some text', $is_error);
242  
243 Returns a text result in the expected format, optionally marking it as an error.
244  
245 =head2 validate_input
246  
247 my $bool = $tool->validate_input($args);
248  
249 Validates the input arguments against the tool's input schema. Returns true if validation failed.
250  
251 =head1 SEE ALSO
252  
253 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
254  
255 =cut
« MCP/Server.pm ↑ Summary MCP/Server/Transport/HTTP.pm »