File Coverage

blib/lib/MCP/Resource.pm
Criterion Covered Total %
statement 36 36 100.0
branch 3 4 75.0
condition 4 6 66.6
subroutine 8 8 100.0
pod 3 3 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             package MCP::Resource;
2 4     4   21 use Mojo::Base 'MCP::Primitive', -signatures;
  4         6  
  4         23  
3              
4 4     4   963 use Mojo::Util qw(b64_encode);
  4         6  
  4         166  
5 4     4   28 use Scalar::Util qw(blessed);
  4         4  
  4         2977  
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 335 sub binary_resource ($self, $data) {
  1         1  
  1         3  
  1         1  
14 1         9 my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, blob => b64_encode($data, '')}]};
15 1         32 return $result;
16             }
17              
18 8     8 1 11 sub call ($self, $context) {
  8         11  
  8         38  
  8         12  
19 8         24 local $self->{context} = $context;
20 8         35 my $result = $self->code->($self);
21 8 100 66 1   203 return $result->then(sub { $self->_type_check($_[0]) }) if blessed($result) && $result->isa('Mojo::Promise');
  1         500429  
22 7         26 return $self->_type_check($result);
23             }
24              
25 7     7 1 10 sub text_resource ($self, $text) {
  7         10  
  7         10  
  7         9  
26 7         19 my $result = {contents => [{uri => $self->uri, mimeType => $self->mime_type, text => $text}]};
27 7         90 return $result;
28             }
29              
30 8     8   15 sub _type_check ($self, $result) {
  8         12  
  8         14  
  8         8  
31 8 50 66     113 return $result if ref $result eq 'HASH' && exists $result->{contents};
32 7         25 return $self->text_resource($result);
33             }
34              
35             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 is a container for resources.
52              
53             =head1 ATTRIBUTES
54              
55             L 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 inherits all methods from L 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, L, L.
117              
118             =cut