ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Client/Request.pm
Revision: 1.3
Committed: Tue Oct 13 10:17:31 2026 UTC (4 weeks, 2 days ago) by root
Branch: MAIN
Changes since 1.2: +6 -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     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 root 1.3
47 root 1.1 my $self = $class->SUPER::new (@_);
48     weaken $self->{client};
49     $self
50     }
51    
52     sub _finish {
53     my ($self, $success, $result) = @_;
54    
55     die "Request already finished\n" if $self->{finished};
56     $self->{finished} = 1;
57     delete $self->{on_cancel};
58     (delete $self->{on_reply})->($success, $result);
59     }
60    
61     sub reply {
62     my ($self, $result) = @_;
63 root 1.3
64 root 1.1 $result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026";
65     $self->_finish (1, $result);
66     }
67    
68     sub error {
69     my ($self, $code, $message, $data) = @_;
70 root 1.3
71 root 1.1 $self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) });
72     }
73    
74     sub cancel {
75     my ($self) = @_;
76 root 1.3
77 root 1.1 return if $self->{finished};
78     $self->{finished} = $self->{cancelled} = 1;
79     delete $self->{on_reply};
80 root 1.3 my $cancel = delete $self->{on_cancel};
81     $cancel->($self) if $cancel;
82 root 1.1 }
83    
84     1