MCP/Prompt.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 86.3% 57 / 66 CC 12 SCAR 11.4
CC
12
Cov
90%
_type_check
4.1
validate_input
4.0
text_prompt
4.0
CRAP 3.1
« MCP/Primitive.pm ↑ Summary MCP/Resource.pm »
1 package MCP::Prompt;
2 10 ▶use Mojo::Base 'MCP::Primitive', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶has arguments => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
7 0 ▶has code => sub { die 'Prompt code not implemented' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
8 10 has description => 'Generic MCP prompt';
9 10 has name => 'prompt';
10  
11 14 ▶sub call ($self, $args, $context) {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
12 14 local $self->{context} = $context;
13 14 my $result = $self->code->($self, $args);
14 12 ▶ 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") 2 10 12
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
1002
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
15 10 return $self->_type_check($result);
16 }
17  
18 11 ▶sub text_prompt ($self, $text, $role = 'user', $description = undef) {
Subroutine text_prompt: called CC=4 SCAR=13.9
Pod documented
19 11 my $result = {messages => [{role => $role, content => {type => 'text', text => "$text"}}]};
20 11 ▶ $result->{description} = $description if defined $description;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 1 10 11
21 11 return $result;
22 }
23  
24 15 ▶sub validate_input ($self, $args) {
Subroutine validate_input: called CC=4 SCAR=13.9
Pod documented
25 15 for my $arg (@{$self->arguments}) {
26 5 ▶ next unless $arg->{required};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"required"} 0 5 5
27 5 ▶ return 1 unless exists $args->{$arg->{name}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$args{$arg->{"name"}} 1 4 5
28 }
29 14 return 0;
30 }
31  
32 12 ▶sub _type_check ($self, $result) {
Subroutine _type_check: called CC=4 SCAR=14.2
33 12 ▶ return $result if ref $result eq 'HASH' && (exists $result->{messages} || exists $result->{resultType});
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" and exists $result->{"messages"} || exists $result->{"resultType"} 3 9 12
Condition66%4/6
ref $result eq "HASH" and exists $result->{"messages"} || exists $result->{"resultType"}
!ll&&!rl&&r
903
exists $result->{"messages"} or exists $result->{"resultType"}
l!l&&r!l&&!r
210
MC/DC33%1/3
ref $result eq "HASH" and exists $result->{"messages"} || exists $result->{"resultType"}
ref $result eq "HASH" exists $result->{"messages"} exists $result->{"resultType"}
Truth table75%3/4
A and B or C
A= ref $result eq "HASH"B= exists $result->{"messages"}C= exists $result->{"resultType"}
ABCresult
0XX0
1000
11X1
1011
34 9 return $self->text_prompt($result);
35 }
36  
37 10 1;
38  
39 =encoding utf8
40  
41 =head1 NAME
42  
43 MCP::Prompt - Prompt container
44  
45 =head1 SYNOPSIS
46  
47 use MCP::Prompt;
48  
49 my $prompt = MCP::Prompt->new;
50  
51 =head1 DESCRIPTION
52  
53 L<MCP::Prompt> is a container for prompts.
54  
55 =head1 ATTRIBUTES
56  
57 L<MCP::Prompt> implements the following attributes.
58  
59 =head2 arguments
60  
61 my $args = $prompt->arguments;
62 $prompt = $prompt->arguments([{name => 'foo', description => 'Whatever', required => 1}]);
63  
64 Arguments for the prompt.
65  
66 =head2 code
67  
68 my $code = $prompt->code;
69 $prompt = $prompt->code(sub { ... });
70  
71 Prompt code.
72  
73 =head2 description
74  
75 my $description = $prompt->description;
76 $prompt = $prompt->description('A brief description of the prompt');
77  
78 Description of the prompt.
79  
80 =head2 name
81  
82 my $name = $prompt->name;
83 $prompt = $prompt->name('my_prompt');
84  
85 Name of the Prompt.
86  
87 =head1 METHODS
88  
89 L<MCP::Prompt> inherits all methods from L<MCP::Primitive> and implements the following new ones.
90  
91 =head2 call
92  
93 my $result = $prompt->call($args, $context);
94  
95 Calls the prompt with the given arguments and context, returning a result. The result can be a promise or a direct
96 value.
97  
98 =head2 text_prompt
99  
100 my $result = $prompt->text_prompt('Some text');
101 my $result = $prompt->text_prompt('Some text', $role);
102 my $result = $prompt->text_prompt('Some text', $role, $description);
103  
104 Returns a text prompt in the expected format.
105  
106 =head2 validate_input
107  
108 my $bool = $prompt->validate_input($args);
109  
110 Validates the input arguments. Returns true if validation failed.
111  
112 =head1 SEE ALSO
113  
114 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
115  
116 =cut
« MCP/Primitive.pm ↑ Summary MCP/Resource.pm »