ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MCP/lib/AnyEvent/MCP/Client/Request.pm
Revision: 1.6
Committed: Fri Nov 27 03:37:26 2026 UTC (7 days, 8 hours ago) by root
Branch: MAIN
CVS Tags: rel-0_92, HEAD
Changes since 1.5: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.6 are accessible directly. For embedded input requests, C<input_result_meta>
15     contains the enclosing input-required result's C<_meta>, if supplied.
16     Do not reply after cancellation.
17 root 1.1
18 root 1.5 Usually you encounter this object in an
19     L<AnyEvent::MCP::Client/on_request> callback, rather than constructing it:
20    
21     on_request => sub {
22     my ($client, $req) = @_;
23     if ($req->{method} eq "elicitation/create") {
24     $req->reply ({ action => "decline" });
25     } else {
26     $req->error (-32601, "Unsupported input method");
27     }
28     },
29    
30     For real human prompting, see
31     L<AnyEvent::MCP::Permissions::Ask> instead.
32    
33 root 1.1 =head1 METHODS
34    
35 root 1.4 =over
36    
37     =item $req->reply ($result)
38 root 1.1
39     Completes the input request with a result hash reference. Duplicate replies
40     die. Protocol field spelling, such as C<resultType>, is preserved.
41    
42 root 1.4 =item $req->error ($code, $message, $data)
43 root 1.1
44     Reports a JSON-RPC error. C<$data> is optional. For modern input this fails
45     the local operation; for legacy requests it sends an error response.
46    
47 root 1.4 =item $req->cancel
48 root 1.1
49     Marks the request finished and cancelled, releases its completion callback
50     and calls C<< $req->{on_cancel}->($req) >> when present. Repeated cancellation
51     is harmless. Applications can use C<on_cancel> to close an outstanding prompt.
52    
53 root 1.4 =back
54    
55 root 1.1 =cut
56    
57     package AnyEvent::MCP::Client::Request;
58    
59     use common::sense;
60     use AnyEvent::MCP ();
61     use Scalar::Util qw(weaken);
62    
63     our @ISA = qw(AnyEvent::MCP::Base);
64    
65     sub new {
66     my $class = shift;
67 root 1.3
68 root 1.1 my $self = $class->SUPER::new (@_);
69     weaken $self->{client};
70     $self
71     }
72    
73     sub _finish {
74     my ($self, $success, $result) = @_;
75    
76     die "Request already finished\n" if $self->{finished};
77     $self->{finished} = 1;
78     delete $self->{on_cancel};
79     (delete $self->{on_reply})->($success, $result);
80     }
81    
82     sub reply {
83     my ($self, $result) = @_;
84 root 1.3
85 root 1.1 $result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026";
86     $self->_finish (1, $result);
87     }
88    
89     sub error {
90     my ($self, $code, $message, $data) = @_;
91 root 1.3
92 root 1.1 $self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) });
93     }
94    
95     sub cancel {
96     my ($self) = @_;
97 root 1.3
98 root 1.1 return if $self->{finished};
99     $self->{finished} = $self->{cancelled} = 1;
100     delete $self->{on_reply};
101 root 1.3 my $cancel = delete $self->{on_cancel};
102     $cancel->($self) if $cancel;
103 root 1.1 }
104    
105     1