| 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 |
root |
1.5 |
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 |
root |
1.1 |
=head1 METHODS |
| 32 |
|
|
|
| 33 |
root |
1.4 |
=over |
| 34 |
|
|
|
| 35 |
|
|
=item $req->reply ($result) |
| 36 |
root |
1.1 |
|
| 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 |
root |
1.4 |
=item $req->error ($code, $message, $data) |
| 41 |
root |
1.1 |
|
| 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 |
root |
1.4 |
=item $req->cancel |
| 46 |
root |
1.1 |
|
| 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 |
root |
1.4 |
=back |
| 52 |
|
|
|
| 53 |
root |
1.1 |
=cut |
| 54 |
|
|
|
| 55 |
|
|
package AnyEvent::MCP::Client::Request; |
| 56 |
|
|
|
| 57 |
|
|
use common::sense; |
| 58 |
|
|
use AnyEvent::MCP (); |
| 59 |
|
|
use Scalar::Util qw(weaken); |
| 60 |
|
|
|
| 61 |
|
|
our @ISA = qw(AnyEvent::MCP::Base); |
| 62 |
|
|
|
| 63 |
|
|
sub new { |
| 64 |
|
|
my $class = shift; |
| 65 |
root |
1.3 |
|
| 66 |
root |
1.1 |
my $self = $class->SUPER::new (@_); |
| 67 |
|
|
weaken $self->{client}; |
| 68 |
|
|
$self |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
|
sub _finish { |
| 72 |
|
|
my ($self, $success, $result) = @_; |
| 73 |
|
|
|
| 74 |
|
|
die "Request already finished\n" if $self->{finished}; |
| 75 |
|
|
$self->{finished} = 1; |
| 76 |
|
|
delete $self->{on_cancel}; |
| 77 |
|
|
(delete $self->{on_reply})->($success, $result); |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
sub reply { |
| 81 |
|
|
my ($self, $result) = @_; |
| 82 |
root |
1.3 |
|
| 83 |
root |
1.1 |
$result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026"; |
| 84 |
|
|
$self->_finish (1, $result); |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
sub error { |
| 88 |
|
|
my ($self, $code, $message, $data) = @_; |
| 89 |
root |
1.3 |
|
| 90 |
root |
1.1 |
$self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) }); |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
sub cancel { |
| 94 |
|
|
my ($self) = @_; |
| 95 |
root |
1.3 |
|
| 96 |
root |
1.1 |
return if $self->{finished}; |
| 97 |
|
|
$self->{finished} = $self->{cancelled} = 1; |
| 98 |
|
|
delete $self->{on_reply}; |
| 99 |
root |
1.3 |
my $cancel = delete $self->{on_cancel}; |
| 100 |
|
|
$cancel->($self) if $cancel; |
| 101 |
root |
1.1 |
} |
| 102 |
|
|
|
| 103 |
|
|
1 |