File Coverage

blib/lib/MCP/Prompt.pm
Criterion Covered Total %
statement 44 44 100.0
branch 9 12 75.0
condition 4 6 66.6
subroutine 8 8 100.0
pod 4 4 100.0
total 69 74 93.2


line stmt bran cond sub pod time code
1             package MCP::Prompt;
2 2     2   14 use Mojo::Base -base, -signatures;
  2         4  
  2         17  
3              
4 2     2   666 use Scalar::Util qw(blessed);
  2         3  
  2         1725  
5              
6             has arguments => sub { [] };
7             has code => sub { die 'Prompt code not implemented' };
8             has description => 'Generic MCP prompt';
9             has name => 'prompt';
10              
11 7     7 1 14 sub call ($self, $args, $context) {
  7         13  
  7         12  
  7         11  
  7         10  
12 7         25 local $self->{context} = $context;
13 7         31 my $result = $self->code->($self, $args);
14 7 100 66 2   557 return $result->then(sub { $self->_type_check($_[0]) }) if blessed($result) && $result->isa('Mojo::Promise');
  2         997009  
15 5         16 return $self->_type_check($result);
16             }
17              
18 1 50   1 1 11 sub context ($self) { $self->{context} || {} }
  1         3  
  1         1  
  1         30  
19              
20 7     7 1 42 sub text_prompt ($self, $text, $role = 'user', $description = undef) {
  7         31  
  7         14  
  7         14  
  7         31  
  7         10  
21 7         91 my $result = {messages => [{role => $role, content => {type => 'text', text => "$text"}}]};
22 7 100       29 $result->{description} = $description if defined $description;
23 7         30 return $result;
24             }
25              
26 8     8 1 15 sub validate_input ($self, $args) {
  8         19  
  8         13  
  8         13  
27 8         13 for my $arg (@{$self->arguments}) {
  8         46  
28 4 50       28 next unless $arg->{required};
29 4 100       21 return 1 unless exists $args->{$arg->{name}};
30             }
31 7         44 return 0;
32             }
33              
34 7     7   17 sub _type_check ($self, $result) {
  7         14  
  7         12  
  7         12  
35 7 50 66     52 return $result if ref $result eq 'HASH' && exists $result->{messages};
36 6         27 return $self->text_prompt($result);
37             }
38              
39             1;
40              
41             =encoding utf8
42              
43             =head1 NAME
44              
45             MCP::Prompt - Prompt container
46              
47             =head1 SYNOPSIS
48              
49             use MCP::Prompt;
50              
51             my $prompt = MCP::Prompt->new;
52              
53             =head1 DESCRIPTION
54              
55             L is a container for prompts.
56              
57             =head1 ATTRIBUTES
58              
59             L implements the following attributes.
60              
61             =head2 arguments
62              
63             my $args = $prompt->arguments;
64             $prompt = $prompt->arguments([{name => 'foo', description => 'Whatever', required => 1}]);
65              
66             Arguments for the prompt.
67              
68             =head2 code
69              
70             my $code = $prompt->code;
71             $prompt = $prompt->code(sub { ... });
72              
73             Prompt code.
74              
75             =head2 description
76              
77             my $description = $prompt->description;
78             $prompt = $prompt->description('A brief description of the prompt');
79              
80             Description of the prompt.
81              
82             =head2 name
83              
84             my $name = $prompt->name;
85             $prompt = $prompt->name('my_prompt');
86              
87             Name of the Prompt.
88              
89             =head1 METHODS
90              
91             L inherits all methods from L and implements the following new ones.
92              
93             =head2 call
94              
95             my $result = $prompt->call($args, $context);
96              
97             Calls the prompt with the given arguments and context, returning a result. The result can be a promise or a direct
98             value.
99              
100             =head2 context
101              
102             my $context = $prompt->context;
103              
104             Returns the context in which the prompt is executed.
105              
106             # Get controller for requests using the HTTP transport
107             my $c = $prompt->context->{controller};
108              
109             =head2 text_prompt
110              
111             my $result = $prompt->text_prompt('Some text');
112             my $result = $prompt->text_prompt('Some text', $role);
113             my $result = $prompt->text_prompt('Some text', $role, $description);
114              
115             Returns a text prompt in the expected format.
116              
117             =head2 validate_input
118              
119             my $bool = $prompt->validate_input($args);
120              
121             Validates the input arguments. Returns true if validation failed.
122              
123             =head1 SEE ALSO
124              
125             L, L, L.
126              
127             =cut