| 1 | package MCP::Tool; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use Mojo::Base 'MCP::Primitive', -signatures; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use JSON::Schema::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Mojo::JSON qw(false to_json true); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Mojo::Util qw(b64_encode); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use constant DIALECTS => { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 'http://json-schema.org/draft-07/schema#' => 'draft7', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 'https://json-schema.org/draft/2019-09/schema' => 'draft2019-09', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 'https://json-schema.org/draft/2020-12/schema' => 'draft2020-12' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | ▶ | has annotations => sub { {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | has code => sub { die 'Tool code not implemented' }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | has description => 'Generic MCP tool'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 17 | ▶ | has input_schema => sub { {type => 'object'} }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 12 | has max_schema_depth => 50; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | has name => 'tool'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | has 'output_schema'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub audio_result ($self, $audio, $options = {}, $is_error = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
audio_result: called CC=5 SCAR=17.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | content => [{type => 'audio', data => b64_encode($audio, ''), mimeType => $options->{mime_type} // 'audio/wav'}], | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$options{"mime_type"} // "audio/wav"
MC/DC0%0/1
$$options{"mime_type"} // "audio/wav"
$$options{"mime_type"}
Truth table50%1/2
A
A= $$options{"mime_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | isError => $is_error ? true : false | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 48 | ▶ | sub call ($self, $args, $context) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 48 | local $self->{context} = $context; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 48 | my $result = $self->code->($self, $args); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 46 | ▶ | 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
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | return $self->_type_check($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 56 | ▶ | sub header_params ($self) { return $self->{header_params} //= _header_params($self->input_schema) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
header_params: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
$$self{"header_params"} //= _header_params($self->input_schema)
MC/DC0%0/2
$$self{"header_params"} //= _header_params($self->input_schema)
$$self{"header_params"}
_header_params($self->input_schema)
Truth table66%2/3
A //= B
A= $$self{"header_params"}B= _header_params($self->input_schema)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub image_result ($self, $image, $options = {}, $is_error = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
image_result: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | content => [{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | type => 'image', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | data => b64_encode($image, ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | mimeType => $options->{mime_type} // 'image/png', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | annotations => $options->{annotations} // {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | }], | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$$options{"mime_type"} // "image/png"
$$options{"annotations"} // {}
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"}
Truth table50%1/2
A
A= $$options{"annotations"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | isError => $is_error ? true : false | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub resource_link_result ($self, $uri, $options = {}, $is_error = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resource_link_result: called CC=8 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | content => [{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | type => 'resource_link', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | uri => $uri, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | name => $options->{name} // '', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | description => $options->{description} // '', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | mimeType => $options->{mime_type} // 'text/plain', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | annotations => $options->{annotations} // {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | }], | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
$$options{"name"} // ""
$$options{"description"} // ""
$$options{"mime_type"} // "text/plain"
$$options{"annotations"} // {}
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"}
Truth table50%1/2
A
A= $$options{"description"}
Truth table50%1/2
A
A= $$options{"mime_type"}
Truth table50%1/2
A
A= $$options{"annotations"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | isError => $is_error ? true : false | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | sub structured_result ($self, $data, $is_error = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
structured_result: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | return $self->text_result('Structured content does not match the output schema', 1) if $self->validate_output($data); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | my $result = $self->text_result(to_json($data), $is_error); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | $result->{structuredContent} = $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 37 | ▶ | sub text_result ($self, $text, $is_error = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text_result: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 37 | ▶ | return {content => [{type => 'text', text => "$text"}], isError => $is_error ? true : false}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 68 | ▶ | sub validate_input ($self, $args) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_input: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 68 | my $schema = $self->input_schema; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 68 | ▶ | my $validator = $self->{input_validator} //= $self->_validator($schema); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$self{"input_validator"} //= $self->_validator($schema)
MC/DC0%0/2
$$self{"input_validator"} //= $self->_validator($schema)
$$self{"input_validator"}
$self->_validator($schema)
Truth table66%2/3
A //= B
A= $$self{"input_validator"}B= $self->_validator($schema)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 66 | ▶ | return $validator->evaluate($args, $schema) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | sub validate_output ($self, $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_output: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | ▶ | return 0 unless my $schema = $self->output_schema; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | my $validator = $self->{output_validator} //= $self->_validator($schema); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$self{"output_validator"} //= $self->_validator($schema)
MC/DC0%0/2
$$self{"output_validator"} //= $self->_validator($schema)
$$self{"output_validator"}
$self->_validator($schema)
Truth table66%2/3
A //= B
A= $$self{"output_validator"}B= $self->_validator($schema)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | ▶ | return $validator->evaluate($data, $schema) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 44 | ▶ | sub _header_params ($schema, $path = []) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_header_params: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 44 | ▶ | return [] unless ref $schema eq 'HASH' && ref(my $properties = $schema->{properties}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $schema eq "HASH" and ref(my $properties = $$schema{"properties"}) eq "HASH"
MC/DC50%1/2
ref $schema eq "HASH" and ref(my $properties = $$schema{"properties"}) eq "HASH"
ref $schema eq "HASH"
ref(my $properties = $$schema{"properties"}) eq "HASH"
Truth table66%2/3
A and B
A= ref $schema eq "HASH"B= ref(my $properties = $$schema{"properties"}) eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 12 | my @params; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 12 | for my $key (sort keys %$properties) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 16 | ▶ | next unless ref(my $property = $properties->{$key}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 16 | my @next = (@$path, $key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | push @params, {name => $property->{'x-mcp-header'}, path => \@next, type => $property->{type} // ''} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 16 | ▶ | if defined $property->{'x-mcp-header'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$property{"type"} // ""
MC/DC0%0/1
$$property{"type"} // ""
$$property{"type"}
Truth table50%1/2
A
A= $$property{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 16 | push @params, @{_header_params($property, \@next)}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 12 | return \@params; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 44 | ▶ | sub _type_check ($self, $result) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_type_check: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 44 | ▶ | return $result if ref $result eq 'HASH' && (exists $result->{content} || exists $result->{resultType}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $result eq "HASH" and exists $$result{"content"} || exists $$result{"resultType"}
exists $$result{"content"} or exists $$result{"resultType"}
MC/DC33%1/3
ref $result eq "HASH" and exists $$result{"content"} || exists $$result{"resultType"}
ref $result eq "HASH"
exists $$result{"content"}
exists $$result{"resultType"}
Truth table75%3/4
A and B or C
A= ref $result eq "HASH"B= exists $$result{"content"}C= exists $$result{"resultType"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 31 | return $self->text_result($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 40 | ▶ | sub _validator ($self, $schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validator: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 40 | my %options = (boolean_result => 1, max_depth => $self->max_schema_depth); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 40 | ▶ | if (my $dialect = ref $schema eq 'HASH' ? $schema->{'$schema'} : undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | croak qq{Unsupported JSON Schema dialect "$dialect"} unless DIALECTS->{$dialect}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 37 | else { $options{specification_version} = 'draft2020-12' } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 38 | return JSON::Schema::Tiny->new(%options); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | MCP::Tool - Tool container | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | use MCP::Tool; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | my $tool = MCP::Tool->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | L<MCP::Tool> is a container for tools to be called. Arguments and structured content are validated with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | L<JSON::Schema::Tiny>, which covers JSON Schema 2020-12, 2019-09, and draft-07, but only partially implements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | C<$id>, C<$dynamicRef>, and C<$dynamicAnchor>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | L<MCP::Tool> implements the following attributes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =head2 annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | my $annotations = $tool->annotations; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | $tool = $tool->annotations({title => '...'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Optional annotations for the tool which provide additional metadata about the tool behavior. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =head2 code | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | my $code = $tool->code; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | $tool = $tool->code(sub { ... }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | Tool code. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 description | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | my $description = $tool->description; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | $tool = $tool->description('A brief description of the tool'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | Description of the tool. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 input_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | my $schema = $tool->input_schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | $tool = $tool->input_schema({type => 'object', properties => {foo => {type => 'string'}}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | JSON schema for validating input arguments. Schemas without a C<$schema> keyword are interpreted as JSON Schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 2020-12, the default dialect of the Model Context Protocol; schemas with one are interpreted as the dialect it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | names, which has to be one of 2020-12, 2019-09, or draft-07. Any other dialect is an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | Properties may carry an C<x-mcp-header> keyword, naming the HTTP header a client has to mirror the argument in, for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | the benefit of gateways that route on headers alone. See L</"header_params">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =head2 max_schema_depth | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | my $depth = $tool->max_schema_depth; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | $tool = $tool->max_schema_depth(10); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | How many levels deep validation may descend into a schema before it is aborted, which bounds the work a deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | nested or recursive schema can cause. Defaults to C<50>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =head2 name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | my $name = $tool->name; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | $tool = $tool->name('my_tool'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | Name of the tool. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | =head2 output_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | my $schema = $tool->output_schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | $tool = $tool->output_schema({type => 'object', properties => {foo => {type => 'string'}}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | JSON schema for validating output results, interpreted with the same dialect rules as L</"input_schema">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | L<MCP::Tool> inherits all methods from L<MCP::Primitive> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | =head2 audio_result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | my $result = $tool->audio_result($bytes, $options, $is_error); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | Returns an audio result in the expected format, optionally marking it as an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | These options are currently available: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =item mime_type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | mime_type => 'audio/wav' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | Specifies the MIME type of the audio, defaults to C<audio/wav>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | =head2 call | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | my $result = $tool->call($args, $context); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | Calls the tool with the given arguments and context, returning a result. The result can be a promise or a direct value. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | =head2 header_params | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | my $params = $tool->header_params; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | All properties of L</"input_schema"> annotated with C<x-mcp-header>, as an array reference of hash references with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | C<name>, C<path> and C<type> keys. Only properties reachable from the schema root through a chain of C<properties> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | keys are considered, since the protocol forbids C<x-mcp-header> anywhere else. The result is cached after the first | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | call. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | =head2 image_result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | my $result = $tool->image_result($bytes, $options, $is_error); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | Returns an image result in the expected format, optionally marking it as an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | These options are currently available: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =item annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | annotations => {audience => ['user']} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | Annotations for the image. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | =item mime_type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | mime_type => 'image/png' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | Specifies the MIME type of the image, defaults to C<image/png>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =head2 resource_link_result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | my $result = $tool->resource_link_result($uri, $options, $is_error); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | Returns a resource link result in the expected format, optionally marking it as an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | These options are currently available: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | =item annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | annotations => {audience => ['user']} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | Annotations for the resource link. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | =item description | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | description => 'A brief description of the resource' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | Description of the resource. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | =item mime_type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | mime_type => 'text/x-perl' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | Specifies the MIME type of the resource, defaults to C<text/plain>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =item name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | name => 'Resource Name' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | Name of the resource. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | =head2 structured_result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | my $result = $tool->structured_result({foo => 'bar'}, $is_error); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | Returns a structured result in the format of L</"output_schema">, optionally marking it as an error. Data that does | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | not match L</"output_schema"> is turned into an error result instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | =head2 text_result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | my $result = $tool->text_result('Some text', $is_error); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | Returns a text result in the expected format, optionally marking it as an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =head2 validate_input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | my $bool = $tool->validate_input($args); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | Validates the input arguments against L</"input_schema">. Returns true if validation failed. References are only | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | resolved as JSON pointers into the schema itself, so a C<$ref> to a remote schema is never fetched over the network | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | and simply fails validation. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | =head2 validate_output | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | my $bool = $tool->validate_output($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | Validates structured content against L</"output_schema">. Returns true if validation failed, and false if the tool | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | has no output schema. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||