blib/lib/AnyEvent/MCP/Client/Request.pm

filter: Statementstmt 93.5% 29 / 31 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 75.0% 3 / 4 totaltotal 86.7% 46 / 53 CC 6 SCAR 9.3
CC
6
Cov
88%
error
6.0
cancel
3.0
_finish
2.0
CRAP 2.5
« blib/lib/AnyEvent/MCP/Server/Request.pm ↑ Summary blib/lib/AnyEvent/MCP/Server/Toolset.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Client::Request - a request for client input
4  
5 =head1 DESCRIPTION
6  
7 An MCP server may need input from the client before it can finish a tool call.
8 In that case C<on_request> receives this object. You can retain it while a
9 human answers a dialog, then call C<reply> or C<error>. A legacy incoming
10 request uses the same interface.
11  
12 The fields C<client>, C<id>, C<method>, C<params>, C<operation> (the original
13 local call, when known), C<protocol_version>, C<finished> and C<cancelled>
14 are accessible directly. Do not reply after cancellation.
15  
16 Usually you encounter this object in an
17 L<AnyEvent::MCP::Client/on_request> callback, rather than constructing it:
18  
19 on_request => sub {
20 my ($client, $req) = @_;
21 if ($req->{method} eq "elicitation/create") {
22 $req->reply ({ action => "decline" });
23 } else {
24 $req->error (-32601, "Unsupported input method");
25 }
26 },
27  
28 For real human prompting, see
29 L<AnyEvent::MCP::Permissions::Ask> instead.
30  
31 =head1 METHODS
32  
33 =over
34  
35 =item $req->reply ($result)
36  
37 Completes the input request with a result hash reference. Duplicate replies
38 die. Protocol field spelling, such as C<resultType>, is preserved.
39  
40 =item $req->error ($code, $message, $data)
41  
42 Reports a JSON-RPC error. C<$data> is optional. For modern input this fails
43 the local operation; for legacy requests it sends an error response.
44  
45 =item $req->cancel
46  
47 Marks the request finished and cancelled, releases its completion callback
48 and calls C<< $req->{on_cancel}->($req) >> when present. Repeated cancellation
49 is harmless. Applications can use C<on_cancel> to close an outstanding prompt.
50  
51 =back
52  
53 =cut
54  
55 package AnyEvent::MCP::Client::Request;
56  
57 37 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 37 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 37 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 37 our @ISA = qw(AnyEvent::MCP::Base);
62  
63 31 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
64 31 my $class = shift;
65  
66 31 my $self = $class->SUPER::new (@_);
67 31 weaken $self->{client};
68 31 $self
69 }
70  
71 30 ▶sub _finish {
Subroutine _finish: called CC=2 SCAR=6.9
72 30 my ($self, $success, $result) = @_;
73  
74 30 ▶ die "Request already finished\n" if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 29 30
75 29 $self->{finished} = 1;
76 29 delete $self->{on_cancel};
77 29 (delete $self->{on_reply})->($success, $result);
78 }
79  
80 30 ▶sub reply {
Subroutine reply: called CC=2 SCAR=6.9
Pod documented
81 30 my ($self, $result) = @_;
82  
83 30 ▶ $result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"protocol_version"} ge 2026 28 2 30
84 30 $self->_finish (1, $result);
85 }
86  
87 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
88 0 my ($self, $code, $message, $data) = @_;
89  
90 0 ▶ $self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) });
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 0 0
91 }
92  
93 3 ▶sub cancel {
Subroutine cancel: called CC=3 SCAR=11.0
Pod documented
94 3 my ($self) = @_;
95  
96 3 ▶ return if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 2 3
97 2 $self->{finished} = $self->{cancelled} = 1;
98 2 delete $self->{on_reply};
99 2 my $cancel = delete $self->{on_cancel};
100 2 ▶ $cancel->($self) if $cancel;
Branch: 1 branch
BranchTrueFalseTotal
if $cancel 2 0 2
101 }
102  
103 37 1
« blib/lib/AnyEvent/MCP/Server/Request.pm ↑ Summary blib/lib/AnyEvent/MCP/Server/Toolset.pm »