| 1 | package MCP::Prompt; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Mojo::Base 'MCP::Primitive', -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | 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 | 4 | has description => 'Generic MCP prompt'; | |||||||||||||||||||||||||||
| 9 | 4 | has name => 'prompt'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ▶ | sub call ($self, $args, $context) { | |||||||||||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 9 | local $self->{context} = $context; | |||||||||||||||||||||||||||
| 13 | 9 | my $result = $self->code->($self, $args); | |||||||||||||||||||||||||||
| 14 | 9 | ▶ | 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
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
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")
| |||||||||||||||||||||||||||||
| 15 | 7 | return $self->_type_check($result); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | sub text_prompt ($self, $text, $role = 'user', $description = undef) { | ||||||||||||||||||||||||||
|
Subroutine
text_prompt: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 9 | my $result = {messages => [{role => $role, content => {type => 'text', text => "$text"}}]}; | |||||||||||||||||||||||||||
| 20 | 9 | ▶ | $result->{description} = $description if defined $description; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 9 | return $result; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ▶ | sub validate_input ($self, $args) { | |||||||||||||||||||||||||||
|
Subroutine
validate_input: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 10 | for my $arg (@{$self->arguments}) { | |||||||||||||||||||||||||||
| 26 | 4 | ▶ | next unless $arg->{required}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | return 1 unless exists $args->{$arg->{name}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 9 | return 0; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ▶ | sub _type_check ($self, $result) { | |||||||||||||||||||||||||||
|
Subroutine
_type_check: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 33 | 9 | ▶ | return $result if ref $result eq 'HASH' && exists $result->{messages}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $result eq "HASH" and exists $result->{"messages"}
MC/DC50%1/2
ref $result eq "HASH" and exists $result->{"messages"}
ref $result eq "HASH"
exists $result->{"messages"}
Truth table66%2/3
A and B
A= ref $result eq "HASH"B= exists $result->{"messages"}
| |||||||||||||||||||||||||||||
| 34 | 8 | return $self->text_prompt($result); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | 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 | ||||||||||||||||||||||||||||