| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
AnyEvent::MCP::Client::Request - a request for client input |
| 4 |
|
|
|
| 5 |
|
|
=head1 DESCRIPTION |
| 6 |
|
|
|
| 7 |
root |
1.2 |
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 |
root |
1.1 |
|
| 16 |
|
|
=head1 METHODS |
| 17 |
|
|
|
| 18 |
|
|
=head2 $req->reply ($result) |
| 19 |
|
|
|
| 20 |
|
|
Completes the input request with a result hash reference. Duplicate replies |
| 21 |
|
|
die. Protocol field spelling, such as C<resultType>, is preserved. |
| 22 |
|
|
|
| 23 |
|
|
=head2 $req->error ($code, $message, $data) |
| 24 |
|
|
|
| 25 |
|
|
Reports a JSON-RPC error. C<$data> is optional. For modern input this fails |
| 26 |
|
|
the local operation; for legacy requests it sends an error response. |
| 27 |
|
|
|
| 28 |
|
|
=head2 $req->cancel |
| 29 |
|
|
|
| 30 |
|
|
Marks the request finished and cancelled, releases its completion callback |
| 31 |
|
|
and calls C<< $req->{on_cancel}->($req) >> when present. Repeated cancellation |
| 32 |
|
|
is harmless. Applications can use C<on_cancel> to close an outstanding prompt. |
| 33 |
|
|
|
| 34 |
|
|
=cut |
| 35 |
|
|
|
| 36 |
|
|
package AnyEvent::MCP::Client::Request; |
| 37 |
|
|
|
| 38 |
|
|
use common::sense; |
| 39 |
|
|
use AnyEvent::MCP (); |
| 40 |
|
|
use Scalar::Util qw(weaken); |
| 41 |
|
|
|
| 42 |
|
|
our @ISA = qw(AnyEvent::MCP::Base); |
| 43 |
|
|
|
| 44 |
|
|
sub new { |
| 45 |
|
|
my $class = shift; |
| 46 |
|
|
my $self = $class->SUPER::new (@_); |
| 47 |
|
|
weaken $self->{client}; |
| 48 |
|
|
$self |
| 49 |
|
|
} |
| 50 |
|
|
|
| 51 |
|
|
sub _finish { |
| 52 |
|
|
my ($self, $success, $result) = @_; |
| 53 |
|
|
|
| 54 |
|
|
die "Request already finished\n" if $self->{finished}; |
| 55 |
|
|
$self->{finished} = 1; |
| 56 |
|
|
delete $self->{on_cancel}; |
| 57 |
|
|
(delete $self->{on_reply})->($success, $result); |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
sub reply { |
| 61 |
|
|
my ($self, $result) = @_; |
| 62 |
|
|
$result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026"; |
| 63 |
|
|
$self->_finish (1, $result); |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
sub error { |
| 67 |
|
|
my ($self, $code, $message, $data) = @_; |
| 68 |
|
|
$self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) }); |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
|
sub cancel { |
| 72 |
|
|
my ($self) = @_; |
| 73 |
|
|
return if $self->{finished}; |
| 74 |
|
|
$self->{finished} = $self->{cancelled} = 1; |
| 75 |
|
|
delete $self->{on_reply}; |
| 76 |
|
|
(delete $self->{on_cancel} || sub {})->($self); |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
1 |