ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Client/Request.pm
Revision: 1.5
Committed: Wed Oct 21 17:28:16 2026 UTC (3 weeks ago) by root
Branch: MAIN
CVS Tags: rel-0_81, rel-0_82, rel-0_71, rel-0_61, rel-0_65, rel-0_6, rel-0_7, rel-0_8, rel-0_9, rel-0_62, HEAD
Changes since 1.4: +15 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 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
66 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
83 $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
90 $self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) });
91 }
92
93 sub cancel {
94 my ($self) = @_;
95
96 return if $self->{finished};
97 $self->{finished} = $self->{cancelled} = 1;
98 delete $self->{on_reply};
99 my $cancel = delete $self->{on_cancel};
100 $cancel->($self) if $cancel;
101 }
102
103 1