MCP/Resource.pm

filter: Statementstmt 96.3% 26 / 27 Branchbran 100% 4 / 4 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 88.6% 47 / 53 CC 5 SCAR 6.6
CC
5
Cov
92%
_type_check
3.0
call
3.0
__ANON__
1.1
CRAP 1.9
« MCP.pm ↑ Summary MCP/Constants.pm »
1 package MCP::Resource;
2 4 ▶use Mojo::Base 'MCP::Primitive', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Mojo::Util qw(b64_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶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 4 has description => 'Generic MCP resource';
9 4 has mime_type => 'text/plain';
10 4 has name => 'resource';
11 4 has uri => 'file://unknown';
12  
13 ▶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 ▶sub call ($self, $context) {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
19 8 local $self->{context} = $context;
20 8 my $result = $self->code->($self);
21 8 ▶ 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") 1 7 8
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
701
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
22 7 return $self->_type_check($result);
23 }
24  
25 ▶sub text_resource ($self, $text) {
Subroutine text_resource: called CC=1 SCAR=0.0
Pod documented
26 7 my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, text => $text}]};
27 7 return $result;
28 }
29  
30 ▶sub _type_check ($self, $result) {
Subroutine _type_check: called CC=3 SCAR=11.1
31 8 ▶ return $result if ref $result eq 'HASH' && exists $result->{contents};
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" and exists $result->{"contents"} 1 7 8
Condition66%2/3
ref $result eq "HASH" and exists $result->{"contents"}
!ll&&!rl&&r
701
MC/DC50%1/2
ref $result eq "HASH" and exists $result->{"contents"}
ref $result eq "HASH" exists $result->{"contents"}
Truth table66%2/3
A and B
A= ref $result eq "HASH"B= exists $result->{"contents"}
ABresult
0X0
100
111
32 7 return $self->text_resource($result);
33 }
34  
35 4 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
« MCP.pm ↑ Summary MCP/Constants.pm »