| 1 | package MCP::Resource; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use Mojo::Base 'MCP::Primitive', -signatures; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Mojo::Util qw(b64_encode); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 0 | ▶ | has code => sub { die 'Resource code not implemented' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | has description => 'Generic MCP resource'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | has mime_type => 'text/plain'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | has name => 'resource'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | has uri => 'file://unknown'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | sub binary_resource ($self, $data) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
binary_resource: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, blob => b64_encode($data, '')}]}; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | sub call ($self, $context) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 12 | local $self->{context} = $context; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 12 | my $result = $self->code->($self); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 11 | ▶ | 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")
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | return $self->_type_check($result); | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 10 | ▶ | sub text_resource ($self, $text) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text_resource: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 10 | my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, text => $text}]}; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 11 | ▶ | sub _type_check ($self, $result) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_type_check: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | ▶ | return $result if ref $result eq 'HASH' && (exists $result->{contents} || exists $result->{resultType}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $result eq "HASH" and exists $result->{"contents"} || exists $result->{"resultType"}
exists $result->{"contents"} or exists $result->{"resultType"}
MC/DC33%1/3
ref $result eq "HASH" and exists $result->{"contents"} || exists $result->{"resultType"}
ref $result eq "HASH"
exists $result->{"contents"}
exists $result->{"resultType"}
Truth table50%2/4
A and B or C
A= ref $result eq "HASH"B= exists $result->{"contents"}C= exists $result->{"resultType"}
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | return $self->text_resource($result); | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | MCP::Resource - Resource container | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | use MCP::Resource; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | my $resource = MCP::Resource->new; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | L<MCP::Resource> is a container for resources. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | L<MCP::Resource> implements the following attributes. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head2 code | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | my $code = $resource->code; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | $resource = $resource->code(sub { ... }); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | Resource code. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head2 description | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | my $description = $resource->description; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | $resource = $resource->description('A brief description of the resource'); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Description of the resource. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head2 mime_type | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | my $mime_type = $resource->mime_type; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | $resource = $resource->mime_type('text/plain'); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | MIME type of the resource. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head2 name | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | my $name = $resource->name; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | $resource = $resource->name('my_resource'); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Name of the resource. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head2 uri | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | my $uri = $resource->uri; | ||||||||||||||||||||||||||||||||||||||||||
| 88 | $resource = $resource->uri('file:///path/to/resource.txt'); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | URI of the resource. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | L<MCP::Resource> inherits all methods from L<MCP::Primitive> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 binary_resource | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | my $result = $resource->binary_resource($data); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | Returns a binary resource in the expected format. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =head2 call | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | my $result = $resource->call($context); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | Calls the resource with context, returning a result. The result can be a promise or a direct value. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head2 text_resource | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | my $result = $resource->text_resource('Some text'); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | Returns a text resource in the expected format. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||||||||||||||||