File Coverage

blib/lib/MCP/Resource.pm
Criterion Covered Total %
statement 36 40 90.0
branch 3 6 50.0
condition 4 6 66.6
subroutine 8 9 88.8
pod 4 4 100.0
total 55 65 84.6


line stmt bran cond sub pod time code
1             package MCP::Resource;
2 2     2   13 use Mojo::Base -base, -signatures;
  2         4  
  2         15  
3              
4 2     2   614 use Mojo::Util qw(b64_encode);
  2         4  
  2         117  
5 2     2   10 use Scalar::Util qw(blessed);
  2         3  
  2         1198  
6              
7             has code => sub { die 'Resource code not implemented' };
8             has description => 'Generic MCP resource';
9             has mime_type => 'text/plain';
10             has name => 'resource';
11             has uri => 'file://unknown';
12              
13 1     1 1 299 sub binary_resource ($self, $data) {
  1         2  
  1         2  
  1         2  
14 1         5 my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, blob => b64_encode($data, '')}]};
15 1         29 return $result;
16             }
17              
18 6     6 1 9 sub call ($self, $context) {
  6         11  
  6         8  
  6         11  
19 6         37 local $self->{context} = $context;
20 6         27 my $result = $self->code->($self);
21 6 100 66 1   202 return $result->then(sub { $self->_type_check($_[0]) }) if blessed($result) && $result->isa('Mojo::Promise');
  1         500934  
22 5         17 return $self->_type_check($result);
23             }
24              
25 0 0   0 1 0 sub context ($self) { $self->{context} || {} }
  0         0  
  0         0  
  0         0  
26              
27 5     5 1 11 sub text_resource ($self, $text) {
  5         10  
  5         9  
  5         7  
28 5         22 my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, text => $text}]};
29 5         102 return $result;
30             }
31              
32 6     6   14 sub _type_check ($self, $result) {
  6         10  
  6         16  
  6         8  
33 6 50 66     30 return $result if ref $result eq 'HASH' && exists $result->{contents};
34 5         48 return $self->text_resource($result);
35             }
36              
37             1;
38              
39             =encoding utf8
40              
41             =head1 NAME
42              
43             MCP::Resource - Resource container
44              
45             =head1 SYNOPSIS
46              
47             use MCP::Resource;
48              
49             my $resource = MCP::Resource->new;
50              
51             =head1 DESCRIPTION
52              
53             L is a container for resources.
54              
55             =head1 ATTRIBUTES
56              
57             L implements the following attributes.
58              
59             =head2 code
60              
61             my $code = $resource->code;
62             $resource = $resource->code(sub { ... });
63              
64             Resource code.
65              
66             =head2 description
67              
68             my $description = $resource->description;
69             $resource = $resource->description('A brief description of the resource');
70              
71             Description of the resource.
72              
73             =head2 mime_type
74              
75             my $mime_type = $resource->mime_type;
76             $resource = $resource->mime_type('text/plain');
77              
78             MIME type of the resource.
79              
80             =head2 name
81              
82             my $name = $resource->name;
83             $resource = $resource->name('my_resource');
84              
85             Name of the resource.
86              
87             =head2 uri
88              
89             my $uri = $resource->uri;
90             $resource = $resource->uri('file:///path/to/resource.txt');
91              
92             URI of the resource.
93              
94             =head1 METHODS
95              
96             L inherits all methods from L and implements the following new ones.
97              
98             =head2 binary_resource
99              
100             my $result = $resource->binary_resource($data);
101              
102             Returns a binary resource in the expected format.
103              
104             =head2 call
105              
106             my $result = $resource->call($context);
107              
108             Calls the resource with context, returning a result. The result can be a promise or a direct value.
109              
110             =head2 context
111              
112             my $context = $resource->context;
113              
114             Returns the context in which the resouce is executed.
115              
116             # Get controller for requests using the HTTP transport
117             my $c = $resource->context->{controller};
118              
119             =head2 text_resource
120              
121             my $result = $resource->text_resource('Some text');
122              
123             Returns a text resource in the expected format.
124              
125             =head1 SEE ALSO
126              
127             L, L, L.
128              
129             =cut