blib/lib/AnyEvent/MCP/Client.pm

filter: Statementstmt 94.1% 224 / 238 Branchbran 70.7% 92 / 130 Conditioncond 62.2% 56 / 90 MC/DCmcdc 42.8% 21 / 49 Subroutinesub 92.5% 37 / 40 Podpod 100% 8 / 8 totaltotal 78.9% 438 / 555 CC 107 SCAR 27.5
CC
107
Cov
81%
_receive
82.4
_input
17.1
close
13.0
CRAP 15.6
« blib/lib/AnyEvent/MCP/Client/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/File.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Client - asynchronous MCP client protocol
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::MCP::Client::HTTP;
8  
9 my $client = AnyEvent::MCP::Client::HTTP->new (
10 url => $url, bearer => $token,
11 );
12  
13 $client->call (echo => { text => "hello" }, sub {
14 my ($success, $result) = @_;
15 ...
16 });
17  
18 =head1 DESCRIPTION
19  
20 This is the common protocol implementation used by
21 L<AnyEvent::MCP::Client::HTTP> and L<AnyEvent::MCP::Client::JSONL>. Choose a
22 transport, keep the client object alive, and run an AnyEvent event loop.
23 You can call a tool without connecting explicitly: the client connects and
24 discovers or initializes the server as needed. Concurrent requests share that
25 work instead of starting independent connections.
26  
27 An interrupted connection does not mean the server did nothing. A tool might
28 have completed its side effects just before the connection failed. For that
29 reason, the client never replays a tool call automatically. Applications that
30 want retries must decide whether the operation is safe to repeat.
31  
32 Modern clients use discovery and per-request metadata. Legacy clients use
33 initialization and ordinary request/response. Legacy server-initiated requests
34 are accepted on JSONL; legacy HTTP GET streams are not implemented.
35  
36 For a runnable call, see L<AnyEvent::MCP::Client::HTTP/FIRST CALL> when
37 connecting to an HTTP endpoint or
38 L<AnyEvent::MCP::Client::JSONL/FIRST CALL> when launching a subprocess.
39 Both use C<call> from this module. In its callback, C<$success> reports
40 JSON-RPC/transport success; a tool can still return C<isError> true.
41 Keep the client alive while its callbacks are pending.
42  
43 =head1 METHODS
44  
45 =over
46  
47 =item $client = AnyEvent::MCP::Client->new (%config)
48  
49 Normally construct a transport subclass. Its constructor accepts these
50 common options as well as the transport-specific options:
51  
52 =over
53  
54 =item client_info => { name => $name, version => $version }
55  
56 Advertised client identity. Defaults to C<AnyEvent::MCP> and this
57 distribution's version.
58  
59 =item client_capabilities => { %capabilities }
60  
61 Supported client interactions, defaulting to an empty hash. Advertising a
62 capability does not install a handler; supply C<on_request> when needed.
63  
64 =item versions => [$version...]
65  
66 Supported protocol revisions in preference order. Defaults to
67 C<2026-07-28>, C<2025-11-25>, C<2025-06-18> and C<2025-03-26>.
68 Here, modern means the 2026 discovery/input-challenge protocol; legacy
69 means the 2025 initialization protocol.
70  
71 =item request_timeout => $seconds
72  
73 Optional overall deadline for C<rpc> and C<call>, including connection
74 and user input. Omitted or zero means no overall deadline. This is separate
75 from HTTP's inactivity timeout.
76  
77 =item max_rounds => $count
78  
79 Maximum input-challenge rounds handled by C<call>, defaulting to 32.
80  
81 =item name => $name
82  
83 Optional local configuration name, independent of C<client_info>.
84 HTTP loads C<[client NAME]> defaults only when this is supplied;
85 JSONL does not load transport defaults.
86  
87 =back
88  
89 Event callbacks are also constructor options; see L</EVENTS>.
90  
91 =item $client->connect ($done->($success, $result))
92  
93 Discovers or initializes the server. On success C<$result> is undef.
94 Any JSON-RPC error from discovery falls back to legacy initialization.
95 Transport failures do not trigger fallback.
96 After connecting, C<protocol_version>, C<server_info>, C<server_capabilities>
97 and C<instructions> are available directly in the client hash.
98  
99 =item $id = $client->rpc ($method, $params, $done->($success, $result))
100  
101 Sends a request. C<$params> is a hash reference and is not modified.
102 C<$id> identifies the local operation for cancellation. Callbacks may run
103 before this method returns.
104  
105 On success C<$result> is the entire decoded result, including C<isError>,
106 C<_meta> and C<resultType>. A result with C<resultType> set to
107 C<input_required> is returned unchanged. C<$success> reports whether the
108 JSON-RPC request succeeded, not whether the tool succeeded: a tool result
109 with C<isError> true still has C<$success> true.
110 On failure C<$result> is an error hash: JSON-RPC errors retain their fields,
111 while local errors have C<message> and C<transport> true. Local cancellation
112 also has C<cancelled> true. Each completion callback runs once.
113  
114 =item $id = $client->call ($name, $args, $done->($success, $result), %params)
115  
116 Calls a tool and handles modern C<input_required> results via C<on_request>.
117 C<$args> is a hash reference of tool arguments. C<%params> supplies
118 additional protocol parameters, such as C<_meta>. Set
119 C<< $params{_meta}{"io.modelcontextprotocol/clientCapabilities"} >>
120 to override the constructor capabilities for this call, allowing
121 brokers to advertise the upstream capabilities without changing shared state.
122 The original method and arguments are retained; only server-requested answers
123 and opaque C<requestState> are added for continuation. No connection failure
124 causes a retry. Without C<on_request>, an interaction fails locally.
125  
126 Permission challenges are input requests handled by C<on_request>. To ask
127 locally through the external permission helper:
128  
129 client_capabilities => { elicitation => { form => {} } },
130 on_request => sub {
131 AnyEvent::MCP::Permissions::Ask->elicitation ($_[1]);
132 },
133  
134 Load L<AnyEvent::MCP::Permissions::Ask> before using that callback. It handles
135 permission-tagged elicitation only; applications supporting other elicitation
136 requests dispatch those separately. The client does not silently approve
137 permissions. C<rpc> returns input challenges intact for broker forwarding.
138  
139 =item $client->tools ($done->($success, $tools))
140  
141 Collects all pages of tool definitions into an array reference. Names and
142 definitions are preserved. Repeated pagination cursors are rejected.
143  
144 =item $client->notify ($method, $params, $done->($success, $result))
145  
146 Sends a notification after connecting. C<$done> is optional. Success means
147 the transport accepted it, not that the server acted on it.
148  
149 =item $client->cancel ($id, $reason)
150  
151 Cancels a pending local operation, completes its callback with an error and
152 sends best-effort protocol cancellation for an outstanding wire request.
153 C<$reason> is optional. Late responses are ignored.
154  
155 =item $client->close
156  
157 Closes transport resources, cancels input handlers and fails pending callbacks.
158 New operations can reconnect. Previously sent calls are not repeated.
159  
160 =back
161  
162 =head1 EVENTS
163  
164 =over
165  
166 =item on_request => $callback->($client, $req)
167  
168 Handles an input request asynchronously. C<$req> is a
169 L<AnyEvent::MCP::Client::Request> with C<method> and C<params>.
170 Declare the corresponding capabilities explicitly in C<client_capabilities>.
171 For a broker, C<rpc> can instead return the complete input challenge upstream.
172  
173 =item on_notification => $callback->($client, $method, $params)
174  
175 Receives server notifications, including progress and tool-list changes.
176  
177 =item on_ready => $callback->($client)
178  
179 Called once after a successful MCP discovery or initialization, including
180 reconnections. It does not fire for repeated C<connect> calls on an already
181 connected client.
182  
183 =item on_close => $callback->($client, $error)
184  
185 Called when an established connection closes unexpectedly, with a transport
186 error hash. C<pending> counts operations interrupted by the close, before
187 their callbacks were failed. An explicit C<close> without a reason does not
188 trigger this event.
189  
190 =back
191  
192 =cut
193  
194 package AnyEvent::MCP::Client;
195  
196 60 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 60 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
198 60 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
199 60 ▶use AnyEvent::MCP::Client::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
200 60 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 60 our @ISA = qw(AnyEvent::MCP::Base);
203  
204 183 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
205 183 my $class = shift;
206  
207 183 $class->SUPER::new (
208 client_info => { name => "AnyEvent::MCP", version => "$AnyEvent::MCP::VERSION" },
209 client_capabilities => {},
210 versions => [qw(2026-07-28 2025-11-25 2025-06-18 2025-03-26)],
211 @_,
212 )
213 }
214  
215 663 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.1
Pod documented
216 663 my ($self, $done) = @_;
217  
218 663 ▶ return $done->(1, undef) if $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connected"} 482 181 663
219 181 ▶ if ($self->{connecting}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"connecting"}) 6 175 181
220 6 push @{$self->{connecting}}, $done;
221 return
222 6 }
223  
224 175 $self->{connecting} = [$done];
225 175 weaken $self;
226 175 ▶ $self->_open (sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
227 175 my ($success, $error) = @_;
228  
229 175 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 175 175
230 175 ▶ return $self->_connected (0, $error) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 175 175
231  
232 175 my ($modern) = grep $_ ge "2026", @{$self->{versions}};
233 175 ▶ return $self->_initialize unless $modern;
Branch: 1 branch
BranchTrueFalseTotal
unless $modern 7 168 175
234  
235 168 $self->{protocol_version} = $modern;
236 168 ▶ $self->_request ("server/discover", {}, sub {
Subroutine __ANON__: called CC=9 SCAR=22.4
237 168 my ($success, $result) = @_;
238  
239 168 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 168 168
240  
241 168 ▶ return $self->_initialize if !$success && !$result->{transport};
Branch: 1 branch
BranchTrueFalseTotal
unless $success or $result->{"transport"} 5 163 168
Condition100%3/3
$success or $result->{"transport"}
l!l&&r!l&&!r
153105
MC/DC100%2/2
$success or $result->{"transport"}
$success $result->{"transport"}
Truth table100%3/3
A or B
A= $successB= $result->{"transport"}
ABresult
1X1
011
000
242 163 ▶ return $self->_connected (0, $result) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 10 153 163
243  
244 return $self->_connected (0, { transport => 1, message => "Invalid discovery result" })
245 153 ▶ unless ref $result->{supportedVersions} eq "ARRAY" && ref $result->{capabilities} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result->{"supportedVersions"} eq "ARRAY" and ref $result->{"capabilities"} eq "HASH" 0 153 153
Condition33%1/3
ref $result->{"supportedVersions"} eq "ARRAY" and ref $result->{"capabilities"} eq "HASH"
!ll&&!rl&&r
00153
MC/DC0%0/2
ref $result->{"supportedVersions"} eq "ARRAY" and ref $result->{"capabilities"} eq "HASH"
ref $result->{"supportedVersions"} eq "ARRAY" ref $result->{"capabilities"} eq "HASH"
Truth table33%1/3
A and B
A= ref $result->{"supportedVersions"} eq "ARRAY"B= ref $result->{"capabilities"} eq "HASH"
ABresult
0X0
100
111
246  
247 153 my %supported = map { $_ => 1 } @{$result->{supportedVersions}};
248 153 my ($version) = grep $supported{$_}, @{$self->{versions}};
249 153 ▶ return $self->_connected (0, { transport => 1, message => "No supported protocol version" })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 153 153
250 unless defined $version;
251 153 ▶ return $self->_initialize if $version lt "2026";
Branch: 1 branch
BranchTrueFalseTotal
if $version lt 2026 0 153 153
252  
253 153 $self->{protocol_version} = $version;
254 153 $self->{server_info} = $result->{_meta}{"io.modelcontextprotocol/serverInfo"};
255 153 $self->{server_capabilities} = $result->{capabilities};
256 153 $self->{instructions} = $result->{instructions};
257 153 $self->_connected (1, undef);
258 168 });
259 175 });
260 }
261  
262 12 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.1
263 12 my ($self) = @_;
264  
265 12 my ($version) = grep $_ lt "2026", @{$self->{versions}};
266 12 ▶ return $self->_connected (0, { transport => 1, message => "Server requires unsupported legacy initialization" })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 12 12
267 unless defined $version;
268  
269 12 $self->{protocol_version} = $version;
270 12 weaken $self;
271 $self->_request ("initialize", {
272 protocolVersion => $version,
273 clientInfo => $self->{client_info},
274 capabilities => $self->{client_capabilities},
275 12 ▶ }, sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
276 12 my ($success, $result) = @_;
277  
278 12 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 12 12
279 12 ▶ return $self->_connected (0, $result) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 12 12
280  
281 12 my $version = $result->{protocolVersion};
282 return $self->_connected (0, { transport => 1, message => "Unsupported negotiated protocol version" })
283 12 ▶ unless grep $_ eq $version && $_ lt "2026", @{$self->{versions}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $version && $_ lt 2026), @{$$self{"versions"};}) 0 12 12
Condition66%2/3
$_ eq $version && $_ lt '2026'
!ll&&!rl&&r
15012
MC/DC50%1/2
$_ eq $version && $_ lt '2026'
$_ eq $version $_ lt '2026'
Truth table66%2/3
A && B
A= $_ eq $versionB= $_ lt '2026'
ABresult
0X0
100
111
284  
285 12 $self->{protocol_version} = $version;
286 12 $self->{server_info} = $result->{serverInfo};
287 12 $self->{server_capabilities} = $result->{capabilities};
288 12 $self->{instructions} = $result->{instructions};
289 $self->_send ({ jsonrpc => "2.0", method => "notifications/initialized" },
290 12 ▶ sub { $self->_connected (@_) if $self });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $self 12 0 12
291 12 });
292 }
293  
294 175 ▶sub _connected {
Subroutine _connected: called CC=4 SCAR=13.9
295 175 my ($self, $success, $error) = @_;
296  
297 175 ▶ my $waiters = delete $self->{connecting} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $waiters = delete $self->{"connecting"} or return 170 5 175
298 170 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 165 5 170
299 165 $self->{connected} = 1;
300 165 $self->event ("ready");
301 } else {
302 5 $self->_shutdown;
303 5 delete @$self{qw(connected protocol_version server_info server_capabilities instructions)};
304 }
305 170 $_->($success, $error) for @$waiters;
306 }
307  
308 922 ▶sub _request {
Subroutine _request: called CC=5 SCAR=16.1
309 922 my ($self, $method, $params, $done, $operation) = @_;
310  
311 922 my $id = ++$self->{wire_id};
312 922 my %params = %$params;
313  
314 922 ▶ if ($self->{protocol_version} ge "2026") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"protocol_version"} ge 2026) 889 33 922
315 $params{_meta} = {
316 889 ▶ %{$params{_meta} || {}},
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'_meta'}{'io.modelcontextprotocol/clientCapabilities'} ? : 129 760 889
Condition100%2/2
$params{'_meta'} || {}
l!l
131758
MC/DC100%1/1
$params{'_meta'} || {}
$params{'_meta'}
Truth table100%2/2
A
A= $params{'_meta'}
Aresult
11
00
317 "io.modelcontextprotocol/protocolVersion" => $self->{protocol_version},
318 "io.modelcontextprotocol/clientInfo" => $self->{client_info},
319 "io.modelcontextprotocol/clientCapabilities" => exists $params{_meta}{"io.modelcontextprotocol/clientCapabilities"}
320 ? $params{_meta}{"io.modelcontextprotocol/clientCapabilities"} : $self->{client_capabilities},
321 889 };
322 }
323  
324 922 $self->{pending}{$id} = $done;
325 922 ▶ $operation->{wire_id} = $id if $operation;
Branch: 1 branch
BranchTrueFalseTotal
if $operation 742 180 922
326 922 my $generation = $self->{generation};
327 922 weaken $self;
328 920 ▶ $self->_send ({ jsonrpc => "2.0", id => $id, method => $method, params => \%params }, sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
329 920 my ($success, $error) = @_;
330  
331 920 ▶ return if !$self || $self->{generation} != $generation || $success;
Branch: 1 branch
BranchTrueFalseTotal
if not $self or $self->{"generation"} != $generation or $success 907 13 920
Condition50%3/6
not $self or $self->{"generation"} != $generation or $success
l!l&&r!l&&!r
090713
not $self or $self->{"generation"} != $generation
l!l&&r!l&&!r
00920
MC/DC33%1/3
not $self or $self->{"generation"} != $generation or $success
not $self $self->{"generation"} != $generation $success
Truth table50%2/4
not(A) or B or C
A= not $selfB= $self->{"generation"} != $generationC= $success
ABCresult
1XX1
01X1
0011
0000
332 13 ▶ my $done = delete $self->{pending}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $done = delete $self->{"pending"}{$id} or return 7 6 13
333 7 $done->(0, $error);
334 922 });
335 922 $id
336 }
337  
338 633 ▶sub _operation {
Subroutine _operation: called CC=2 SCAR=6.9
339 633 my ($self, $method, $params, $done, $interactions) = @_;
340  
341 633 my $id = ++$self->{operation_id};
342 633 my $op = $self->{operations}{$id} = {
343 method => $method,
344 params => $params,
345 done => $done,
346 interactions => $interactions,
347 };
348  
349 633 weaken $self;
350 633 ▶ if ($self->{request_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"request_timeout"}) 1 632 633
351 1 ▶ $op->{timer} = AnyEvent->timer (after => $self->{request_timeout}, cb => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
352 1 ▶ $self->cancel ($id, "Request timed out") if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 1 0 1
353 1 });
354 }
355  
356 633 ▶ $self->connect (sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
357 633 my ($success, $error) = @_;
358  
359 633 ▶ return unless $self && $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $self->{"operations"}{$id} 3 630 633
Condition66%2/3
$self and $self->{"operations"}{$id}
!ll&&!rl&&r
03630
MC/DC50%1/2
$self and $self->{"operations"}{$id}
$self $self->{"operations"}{$id}
Truth table66%2/3
A and B
A= $selfB= $self->{"operations"}{$id}
ABresult
0X0
100
111
360 630 ▶ return $self->_finish ($id, 0, $error) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 5 625 630
361 625 $self->_continue ($id, $params);
362 633 });
363 633 $id
364 }
365  
366 193 ▶sub rpc {
Subroutine rpc: called CC=1 SCAR=0.0
Pod documented
367 193 my ($self, $method, $params, $done) = @_;
368  
369 193 $self->_operation ($method, $params, $done, 0)
370 }
371  
372 440 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
373 440 my ($self, $name, $args, $done, %params) = @_;
374  
375 440 $self->_operation ("tools/call", { %params, name => $name, arguments => $args }, $done, 1)
376 }
377  
378 742 ▶sub _continue {
Subroutine _continue: called CC=2 SCAR=6.9
379 742 my ($self, $id, $params) = @_;
380  
381 742 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 742 0 742
382  
383 742 weaken $self;
384 735 ▶ $self->_request ($op->{method}, $params, sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
385 735 my ($success, $result) = @_;
386  
387 735 ▶ return unless $self && $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $self->{"operations"}{$id} 6 729 735
Condition66%2/3
$self and $self->{"operations"}{$id}
!ll&&!rl&&r
06729
MC/DC50%1/2
$self and $self->{"operations"}{$id}
$self $self->{"operations"}{$id}
Truth table66%2/3
A and B
A= $selfB= $self->{"operations"}{$id}
ABresult
0X0
100
111
388 729 delete $op->{wire_id};
389  
390 729 ▶ if ($success && $op->{interactions} && $result->{resultType} eq "input_required") {
Branch: 1 branch
BranchTrueFalseTotal
if ($success and $op->{"interactions"} and $result->{"resultType"} eq "input_required") 119 610 729
Condition100%6/6
$success and $op->{"interactions"} and $result->{"resultType"} eq "input_required"
!ll&&!rl&&r
181429119
$success and $op->{"interactions"}
!ll&&!rl&&r
3178548
MC/DC100%3/3
$success and $op->{"interactions"} and $result->{"resultType"} eq "input_required"
$success $op->{"interactions"} $result->{"resultType"} eq "input_required"
Truth table100%4/4
A and B and C
A= $successB= $op->{"interactions"}C= $result->{"resultType"} eq "input_required"
ABCresult
0XX0
10X0
1100
1111
391 119 return $self->_input ($id, $result)
392 }
393 610 $self->_finish ($id, $success, $result);
394 742 }, $op);
395 }
396  
397 119 ▶sub _input {
Subroutine _input: called CC=14 SCAR=28.4
398 119 my ($self, $id, $result) = @_;
399  
400 119 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 119 0 119
401 119 my $input = $result->{inputRequests};
402  
403 return $self->_finish ($id, 0, { transport => 1, message => "Input required without a client handler" })
404 119 ▶ unless $self->{on_request} && ref $input eq "HASH" && keys %$input;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"on_request"} and ref $input eq "HASH" and keys %$input 1 118 119
Condition66%4/6
$self->{"on_request"} and ref $input eq "HASH" and keys %$input
!ll&&!rl&&r
10118
$self->{"on_request"} and ref $input eq "HASH"
!ll&&!rl&&r
10118
MC/DC33%1/3
$self->{"on_request"} and ref $input eq "HASH" and keys %$input
$self->{"on_request"} ref $input eq "HASH" keys %$input
Truth table50%2/4
A and B and C
A= $self->{"on_request"}B= ref $input eq "HASH"C= keys %$input
ABCresult
0XX0
10X0
1100
1111
405 return $self->_finish ($id, 0, { transport => 1, message => "Too many input rounds" })
406 118 ▶ if ++$op->{rounds} > ($self->{max_rounds} || 32);
Branch: 1 branch
BranchTrueFalseTotal
if ++$op->{"rounds"} > ($self->{"max_rounds"} || 32) 0 118 118
Condition50%1/2
$self->{'max_rounds'} || 32
l!l
0118
MC/DC0%0/1
$self->{'max_rounds'} || 32
$self->{'max_rounds'}
Truth table50%1/2
A
A= $self->{'max_rounds'}
Aresult
11
00
407  
408 118 weaken $self;
409 118 my %responses;
410 118 my $remaining = keys %$input;
411 118 $op->{input} = [];
412 118 for my $key (sort keys %$input) {
413 119 ▶ last unless $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operations"}{$id} 0 119 119
414 119 my $request = $input->{$key};
415 119 ▶ unless (ref $request eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request eq "HASH" and defined $request->{"method"} and ref($request->{"params"} || {}) eq "HASH") 0 119 119
Condition37%3/8
ref $request eq "HASH" and defined $request->{"method"} and ref($request->{"params"} || {}) eq "HASH"
!ll&&!rl&&r
00119
ref $request eq "HASH" and defined $request->{"method"}
!ll&&!rl&&r
00119
$request->{'params'} || {}
l!l
1190
MC/DC0%0/3
ref $request eq "HASH" and defined $request->{"method"} and ref($request->{"params"} || {}) eq "HASH"
ref $request eq "HASH" defined $request->{"method"} $request->{'params'}
Truth table25%1/4
A and B and C
A= ref $request eq "HASH"B= defined $request->{"method"}C= $request->{'params'}
ABCresult
0XX0
10X0
1100
1111
416 && defined $request->{method}
417 && ref ($request->{params} || {}) eq "HASH") {
418 0 return $self->_finish ($id, 0, { transport => 1, message => "Invalid input request" })
419 }
420  
421 my $req = AnyEvent::MCP::Client::Request->new (
422 client => $self,
423 id => $key,
424 method => $request->{method},
425 params => $request->{params} || {},
426 protocol_version => $self->{protocol_version},
427 operation => $id,
428 118 ▶ on_reply => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
429 118 my ($success, $response) = @_;
430  
431 118 ▶ return unless $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operations"}{$id} 0 118 118
432 118 ▶ return $self->_finish ($id, 0, $response) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 118 118
433 118 $responses{$key} = $response;
434 118 ▶ return if --$remaining;
Branch: 1 branch
BranchTrueFalseTotal
if --$remaining 1 117 118
435  
436 117 delete $op->{input};
437 117 my %params = (%{$op->{params}}, inputResponses => \%responses);
438 117 ▶ $params{requestState} = $result->{requestState} if exists $result->{requestState};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"requestState"} 116 1 117
439 117 $self->_continue ($id, \%params);
440 },
441 119 ▶ );
Condition50%1/2
$request->{'params'} || {}
l!l
1190
MC/DC0%0/1
$request->{'params'} || {}
$request->{'params'}
Truth table50%1/2
A
A= $request->{'params'}
Aresult
11
00
442 119 push @{$op->{input}}, $req;
443 119 $self->event (request => $req);
444 }
445 }
446  
447 633 ▶sub _finish {
Subroutine _finish: called CC=5 SCAR=16.1
448 633 my ($self, $id, $success, $result) = @_;
449  
450 633 ▶ my $op = delete $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = delete $self->{"operations"}{$id} or return 633 0 633
451 633 ▶ delete $self->{pending}{$op->{wire_id}} if exists $op->{wire_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $op->{"wire_id"} 13 620 633
452 633 ▶ $_->cancel for @{$op->{input} || []};
Condition100%2/2
$op->{'input'} || []
l!l
1632
MC/DC100%1/1
$op->{'input'} || []
$op->{'input'}
Truth table100%2/2
A
A= $op->{'input'}
Aresult
11
00
453 633 delete $op->{timer};
454 633 $op->{done}->($success, $result);
455 }
456  
457 9 ▶sub cancel {
Subroutine cancel: called CC=5 SCAR=16.1
Pod documented
458 9 my ($self, $id, $reason) = @_;
459  
460 9 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 8 1 9
461 8 my $wire = $op->{wire_id};
462 8 ▶ $reason = "Request cancelled" unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 4 4 8
463  
464 8 $self->_finish ($id, 0, { transport => 1, cancelled => 1, message => $reason });
465 8 ▶ if (defined $wire && $self->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wire and $self->{"connected"}) 7 1 8
Condition66%2/3
defined $wire and $self->{"connected"}
!ll&&!rl&&r
107
MC/DC50%1/2
defined $wire and $self->{"connected"}
defined $wire $self->{"connected"}
Truth table66%2/3
A and B
A= defined $wireB= $self->{"connected"}
ABresult
0X0
100
111
466 $self->_send ({
467 jsonrpc => "2.0", method => "notifications/cancelled",
468 params => { requestId => $wire, reason => $reason },
469 7 ▶ }, sub {});
Subroutine __ANON__: called CC=1 SCAR=0.0
470 7 $self->_abort ($wire);
471 }
472 }
473  
474 54 ▶sub tools {
Subroutine tools: called CC=1 SCAR=0.0
Pod documented
475 54 my ($self, $done) = @_;
476  
477 54 my (@tools, %seen);
478 54 weaken $self;
479 54 my $page;
480 56 ▶ $page = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
481 56 my ($cursor) = @_;
482  
483 56 ▶ $self->rpc ("tools/list", defined $cursor ? { cursor => $cursor } : {}, sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
484 56 my ($success, $result) = @_;
485  
486 56 ▶ unless ($success && ref $result->{tools} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success and ref $result->{"tools"} eq "ARRAY") 3 53 56
Condition66%2/3
$success and ref $result->{"tools"} eq "ARRAY"
!ll&&!rl&&r
3053
MC/DC50%1/2
$success and ref $result->{"tools"} eq "ARRAY"
$success ref $result->{"tools"} eq "ARRAY"
Truth table66%2/3
A and B
A= $successB= ref $result->{"tools"} eq "ARRAY"
ABresult
0X0
100
111
487 3 undef $page;
488 3 ▶ return $done->(0, $success ? { transport => 1, message => "Invalid tools/list result" } : $result)
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 3 3
489 }
490  
491 53 push @tools, @{$result->{tools}};
492 53 ▶ if (defined $result->{nextCursor} && length $result->{nextCursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"nextCursor"} and length $result->{"nextCursor"}) 3 50 53
Condition66%2/3
defined $result->{"nextCursor"} and length $result->{"nextCursor"}
!ll&&!rl&&r
5003
MC/DC50%1/2
defined $result->{"nextCursor"} and length $result->{"nextCursor"}
defined $result->{"nextCursor"} length $result->{"nextCursor"}
Truth table66%2/3
A and B
A= defined $result->{"nextCursor"}B= length $result->{"nextCursor"}
ABresult
0X0
100
111
493 3 ▶ if ($seen{$result->{nextCursor}}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$result->{"nextCursor"}}++) 1 2 3
494 1 undef $page;
495 1 return $done->(0, { transport => 1, message => "Repeated tools/list cursor" })
496 }
497 return $page->($result->{nextCursor})
498 2 }
499  
500 50 undef $page;
501 50 $done->(1, \@tools);
502 56 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined $cursor ? : 2 54 56
503 54 };
504 54 $page->();
505 }
506  
507 1 ▶sub notify {
Subroutine notify: called CC=2 SCAR=7.3
Pod documented
508 1 my ($self, $method, $params, $done) = @_;
509  
510 1 ▶ $done ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$done ||= sub { }
l!l&&r!l&&!r
100
MC/DC0%0/2
$done ||= sub { }
$done sub { }
Truth table33%1/3
A ||= B
A= $doneB= sub { }
ABresult
1X1
011
000
511 1 weaken $self;
512  
513 1 ▶ $self->connect (sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
514 1 my ($success, $error) = @_;
515  
516 1 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 1 1
517 1 ▶ return $done->(0, $error) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 1 1
518 1 $self->_send ({ jsonrpc => "2.0", method => $method, params => $params }, $done);
519 1 });
520 }
521  
522 899 ▶sub _receive {
Subroutine _receive: called CC=17 SCAR=44.1
523 899 my ($self, $message) = @_;
524  
525 die "Invalid JSON-RPC message\n"
526 899 ▶ unless ref $message eq "HASH" && $message->{jsonrpc} eq "2.0";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" 0 899 899
Condition33%1/3
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0"
!ll&&!rl&&r
00899
MC/DC0%0/2
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0"
ref $message eq "HASH" $message->{"jsonrpc"} eq "2.0"
Truth table33%1/3
A and B
A= ref $message eq "HASH"B= $message->{"jsonrpc"} eq "2.0"
ABresult
0X0
100
111
527  
528 899 ▶ if (exists $message->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $message->{"method"}) 2 897 899
529 2 ▶ die "Invalid server method\n" if ref $message->{method};
Branch: 1 branch
BranchTrueFalseTotal
if ref $message->{"method"} 0 2 2
530 2 ▶ if (!exists $message->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $message->{"id"}) 2 0 2
531 2 ▶ $self->event (notification => $message->{method}, $message->{params} || {});
Condition50%1/2
$message->{'params'} || {}
l!l
20
MC/DC0%0/1
$message->{'params'} || {}
$message->{'params'}
Truth table50%1/2
A
A= $message->{'params'}
Aresult
11
00
532 return
533 2 }
534  
535 0 ▶ die "Server-initiated request on modern connection\n" if $self->{protocol_version} ge "2026";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"protocol_version"} ge 2026 0 0 0
536 0 weaken $self;
537 my $req = AnyEvent::MCP::Client::Request->new (
538 client => $self,
539 id => $message->{id},
540 method => $message->{method},
541 params => $message->{params} || {},
542 0 ▶ on_reply => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
543 0 my ($success, $result) = @_;
544  
545 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
546 0 delete $self->{incoming}{$message->{id}};
547 $self->_send ({
548 jsonrpc => "2.0", id => $message->{id},
549 ($success ? "result" : "error") => $result,
550 0 ▶ }, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
551 },
552 0 ▶ );
Condition0%0/2
$message->{'params'} || {}
l!l
00
MC/DC0%0/1
$message->{'params'} || {}
$message->{'params'}
Truth table0%0/2
A
A= $message->{'params'}
Aresult
11
00
553 0 $self->{incoming}{$req->{id}} = $req;
554 0 ▶ if ($req->{method} eq "ping") {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{'method'} eq 'ping') { } 0 0 0
elsif ($self->{'on_request'}) { } 0 0 0
555 0 $req->reply ({});
556 } elsif ($self->{on_request}) {
557 0 $self->event (request => $req);
558 } else {
559 0 $req->error (-32601, "Method not supported");
560 }
561 return
562 0 }
563  
564 897 ▶ my $field = exists $message->{result} ? "result" : "error";
Branch: 1 branch
BranchTrueFalseTotal
exists $message->{'result'} ? : 891 6 897
565  
566 defined $message->{id} && !ref $message->{id}
567 && (exists $message->{result}) != (exists $message->{error})
568 897 ▶ && ref $message->{$field} eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
defined $message->{"id"} and not ref $message->{"id"} and exists $message->{"result"} != exists $message->{"error"} and ref $message->{$field} eq "HASH" or die "Invalid JSON-RPC response\n" 897 0 897
Condition33%3/9
defined $message->{"id"} and not ref $message->{"id"} and exists $message->{"result"} != exists $message->{"error"} and ref $message->{$field} eq "HASH"
!ll&&!rl&&r
00897
defined $message->{"id"} and not ref $message->{"id"} and exists $message->{"result"} != exists $message->{"error"}
!ll&&!rl&&r
00897
defined $message->{"id"} and not ref $message->{"id"}
!ll&&!rl&&r
00897
MC/DC0%0/4
defined $message->{"id"} and not ref $message->{"id"} and exists $message->{"result"} != exists $message->{"error"} and ref $message->{$field} eq "HASH"
defined $message->{"id"} not ref $message->{"id"} exists $message->{"result"} != exists $message->{"error"} ref $message->{$field} eq "HASH"
Truth table20%1/5
A and not(B) and C and D
A= defined $message->{"id"}B= not ref $message->{"id"}C= exists $message->{"result"} != exists $message->{"error"}D= ref $message->{$field} eq "HASH"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
569 or die "Invalid JSON-RPC response\n";
570  
571 897 ▶ my $done = delete $self->{pending}{$message->{id}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $done = delete $self->{"pending"}{$message->{"id"}} or return 897 0 897
572 897 $done->(exists $message->{result}, $message->{$field});
573 }
574  
575 58 ▶sub _open {
Subroutine _open: called CC=1 SCAR=0.0
576 58 $_[1]->(1, undef)
577 }
578  
579 7 ▶sub _abort {
Subroutine _abort: called CC=1 SCAR=0.0
580 }
581  
582 319 ▶sub close {
Subroutine close: called CC=13 SCAR=25.7
Pod documented
583 319 my ($self, $message) = @_;
584  
585 319 my $was_connected = $self->{connected};
586 # the close event runs after outstanding operations have been failed.
587 319 ▶ my $outstanding = scalar keys %{$self->{operations} || {}};
Condition100%2/2
$self->{'operations'} || {}
l!l
30415
MC/DC100%1/1
$self->{'operations'} || {}
$self->{'operations'}
Truth table100%2/2
A
A= $self->{'operations'}
Aresult
11
00
588 319 ++$self->{generation};
589 319 $self->_shutdown;
590 319 delete @$self{qw(connected protocol_version server_info server_capabilities instructions)};
591  
592 319 ▶ my $error = { transport => 1, pending => $outstanding,
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 13 306 319
593 message => defined $message ? $message : "Client closed" };
594 319 ▶ my $connecting = delete $self->{connecting} || [];
Condition100%2/2
delete $self->{'connecting'} || []
l!l
5314
MC/DC100%1/1
delete $self->{'connecting'} || []
delete $self->{'connecting'}
Truth table100%2/2
A
A= delete $self->{'connecting'}
Aresult
11
00
595 319 ▶ my $pending = delete $self->{pending} || {};
Condition100%2/2
delete $self->{'pending'} || {}
l!l
182137
MC/DC100%1/1
delete $self->{'pending'} || {}
delete $self->{'pending'}
Truth table100%2/2
A
A= delete $self->{'pending'}
Aresult
11
00
596 319 ▶ my $incoming = delete $self->{incoming} || {};
Condition50%1/2
delete $self->{'incoming'} || {}
l!l
0319
MC/DC0%0/1
delete $self->{'incoming'} || {}
delete $self->{'incoming'}
Truth table50%1/2
A
A= delete $self->{'incoming'}
Aresult
11
00
597 319 $_->cancel for values %$incoming;
598 319 ▶ $self->_finish ($_, 0, $error) for keys %{$self->{operations} || {}};
Condition100%2/2
$self->{'operations'} || {}
l!l
30415
MC/DC100%1/1
$self->{'operations'} || {}
$self->{'operations'}
Truth table100%2/2
A
A= $self->{'operations'}
Aresult
11
00
599 319 $_->(0, $error) for values %$pending;
600 319 $_->(0, $error) for @$connecting;
601 318 ▶ $self->event (close => $error) if $was_connected && defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if $was_connected and defined $message 8 310 318
Condition100%3/3
$was_connected and defined $message
!ll&&!rl&&r
1501608
MC/DC100%2/2
$was_connected and defined $message
$was_connected defined $message
Truth table100%3/3
A and B
A= $was_connectedB= defined $message
ABresult
0X0
100
111
602 }
603  
604 129 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
605 129 $_[0]->close
606 }
607  
608 60 1
« blib/lib/AnyEvent/MCP/Client/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/File.pm »