ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Client/Request.pm
Revision: 1.4
Committed: Tue Oct 13 22:54:22 2026 UTC (4 weeks, 1 day ago) by root
Branch: MAIN
Changes since 1.3: +7 -3 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     are accessible directly. Do not reply after cancellation.
15 root 1.1
16     =head1 METHODS
17    
18 root 1.4 =over
19    
20     =item $req->reply ($result)
21 root 1.1
22     Completes the input request with a result hash reference. Duplicate replies
23     die. Protocol field spelling, such as C<resultType>, is preserved.
24    
25 root 1.4 =item $req->error ($code, $message, $data)
26 root 1.1
27     Reports a JSON-RPC error. C<$data> is optional. For modern input this fails
28     the local operation; for legacy requests it sends an error response.
29    
30 root 1.4 =item $req->cancel
31 root 1.1
32     Marks the request finished and cancelled, releases its completion callback
33     and calls C<< $req->{on_cancel}->($req) >> when present. Repeated cancellation
34     is harmless. Applications can use C<on_cancel> to close an outstanding prompt.
35    
36 root 1.4 =back
37    
38 root 1.1 =cut
39    
40     package AnyEvent::MCP::Client::Request;
41    
42     use common::sense;
43     use AnyEvent::MCP ();
44     use Scalar::Util qw(weaken);
45    
46     our @ISA = qw(AnyEvent::MCP::Base);
47    
48     sub new {
49     my $class = shift;
50 root 1.3
51 root 1.1 my $self = $class->SUPER::new (@_);
52     weaken $self->{client};
53     $self
54     }
55    
56     sub _finish {
57     my ($self, $success, $result) = @_;
58    
59     die "Request already finished\n" if $self->{finished};
60     $self->{finished} = 1;
61     delete $self->{on_cancel};
62     (delete $self->{on_reply})->($success, $result);
63     }
64    
65     sub reply {
66     my ($self, $result) = @_;
67 root 1.3
68 root 1.1 $result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026";
69     $self->_finish (1, $result);
70     }
71    
72     sub error {
73     my ($self, $code, $message, $data) = @_;
74 root 1.3
75 root 1.1 $self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) });
76     }
77    
78     sub cancel {
79     my ($self) = @_;
80 root 1.3
81 root 1.1 return if $self->{finished};
82     $self->{finished} = $self->{cancelled} = 1;
83     delete $self->{on_reply};
84 root 1.3 my $cancel = delete $self->{on_cancel};
85     $cancel->($self) if $cancel;
86 root 1.1 }
87    
88     1