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
« bin/anyevent-mcp ↑ Summary blib/lib/AnyEvent/MCP/Client/HTTP.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 36 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 36 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
198 36 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
199 36 ▶use AnyEvent::MCP::Client::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
200 36 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 36 our @ISA = qw(AnyEvent::MCP::Base);
203  
204 112 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
205 112 my $class = shift;
206  
207 112 $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 345 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.1
Pod documented
216 345 my ($self, $done) = @_;
217  
218 345 ▶ return $done->(1, undef) if $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connected"} 237 108 345
219 108 ▶ if ($self->{connecting}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"connecting"}) 3 105 108
220 3 push @{$self->{connecting}}, $done;
221 return
222 3 }
223  
224 105 $self->{connecting} = [$done];
225 105 weaken $self;
226 105 ▶ $self->_open (sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
227 105 my ($success, $error) = @_;
228  
229 105 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 105 105
230 105 ▶ return $self->_connected (0, $error) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 105 105
231  
232 105 my ($modern) = grep $_ ge "2026", @{$self->{versions}};
233 105 ▶ return $self->_initialize unless $modern;
Branch: 1 branch
BranchTrueFalseTotal
unless $modern 4 101 105
234  
235 101 $self->{protocol_version} = $modern;
236 101 ▶ $self->_request ("server/discover", {}, sub {
Subroutine __ANON__: called CC=9 SCAR=22.4
237 101 my ($success, $result) = @_;
238  
239 101 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 101 101
240  
241 101 ▶ return $self->_initialize if !$success && !$result->{transport};
Branch: 1 branch
BranchTrueFalseTotal
unless $success or $result->{"transport"} 5 96 101
Condition100%3/3
$success or $result->{"transport"}
l!l&&r!l&&!r
8975
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 96 ▶ return $self->_connected (0, $result) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 7 89 96
243  
244 return $self->_connected (0, { transport => 1, message => "Invalid discovery result" })
245 89 ▶ 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 89 89
Condition33%1/3
ref $result->{"supportedVersions"} eq "ARRAY" and ref $result->{"capabilities"} eq "HASH"
!ll&&!rl&&r
0089
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 89 my %supported = map { $_ => 1 } @{$result->{supportedVersions}};
248 89 my ($version) = grep $supported{$_}, @{$self->{versions}};
249 89 ▶ return $self->_connected (0, { transport => 1, message => "No supported protocol version" })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 89 89
250 unless defined $version;
251 89 ▶ return $self->_initialize if $version lt "2026";
Branch: 1 branch
BranchTrueFalseTotal
if $version lt 2026 0 89 89
252  
253 89 $self->{protocol_version} = $version;
254 89 $self->{server_info} = $result->{_meta}{"io.modelcontextprotocol/serverInfo"};
255 89 $self->{server_capabilities} = $result->{capabilities};
256 89 $self->{instructions} = $result->{instructions};
257 89 $self->_connected (1, undef);
258 101 });
259 105 });
260 }
261  
262 9 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.1
263 9 my ($self) = @_;
264  
265 9 my ($version) = grep $_ lt "2026", @{$self->{versions}};
266 9 ▶ return $self->_connected (0, { transport => 1, message => "Server requires unsupported legacy initialization" })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 9 9
267 unless defined $version;
268  
269 9 $self->{protocol_version} = $version;
270 9 weaken $self;
271 $self->_request ("initialize", {
272 protocolVersion => $version,
273 clientInfo => $self->{client_info},
274 capabilities => $self->{client_capabilities},
275 9 ▶ }, sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
276 9 my ($success, $result) = @_;
277  
278 9 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 9 9
279 9 ▶ return $self->_connected (0, $result) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 9 9
280  
281 9 my $version = $result->{protocolVersion};
282 return $self->_connected (0, { transport => 1, message => "Unsupported negotiated protocol version" })
283 9 ▶ unless grep $_ eq $version && $_ lt "2026", @{$self->{versions}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $version && $_ lt 2026), @{$$self{"versions"};}) 0 9 9
Condition66%2/3
$_ eq $version && $_ lt '2026'
!ll&&!rl&&r
1509
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 9 $self->{protocol_version} = $version;
286 9 $self->{server_info} = $result->{serverInfo};
287 9 $self->{server_capabilities} = $result->{capabilities};
288 9 $self->{instructions} = $result->{instructions};
289 $self->_send ({ jsonrpc => "2.0", method => "notifications/initialized" },
290 9 ▶ sub { $self->_connected (@_) if $self });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $self 9 0 9
291 9 });
292 }
293  
294 105 ▶sub _connected {
Subroutine _connected: called CC=4 SCAR=13.9
295 105 my ($self, $success, $error) = @_;
296  
297 105 ▶ my $waiters = delete $self->{connecting} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $waiters = delete $self->{"connecting"} or return 103 2 105
298 103 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 98 5 103
299 98 $self->{connected} = 1;
300 98 $self->event ("ready");
301 } else {
302 5 $self->_shutdown;
303 5 delete @$self{qw(connected protocol_version server_info server_capabilities instructions)};
304 }
305 103 $_->($success, $error) for @$waiters;
306 }
307  
308 456 ▶sub _request {
Subroutine _request: called CC=5 SCAR=16.1
309 456 my ($self, $method, $params, $done, $operation) = @_;
310  
311 456 my $id = ++$self->{wire_id};
312 456 my %params = %$params;
313  
314 456 ▶ if ($self->{protocol_version} ge "2026") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"protocol_version"} ge 2026) 436 20 456
315 $params{_meta} = {
316 436 ▶ %{$params{_meta} || {}},
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'_meta'}{'io.modelcontextprotocol/clientCapabilities'} ? : 107 329 436
Condition100%2/2
$params{'_meta'} || {}
l!l
111325
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 436 };
322 }
323  
324 456 $self->{pending}{$id} = $done;
325 456 ▶ $operation->{wire_id} = $id if $operation;
Branch: 1 branch
BranchTrueFalseTotal
if $operation 346 110 456
326 456 my $generation = $self->{generation};
327 456 weaken $self;
328 454 ▶ $self->_send ({ jsonrpc => "2.0", id => $id, method => $method, params => \%params }, sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
329 454 my ($success, $error) = @_;
330  
331 454 ▶ return if !$self || $self->{generation} != $generation || $success;
Branch: 1 branch
BranchTrueFalseTotal
if not $self or $self->{"generation"} != $generation or $success 441 13 454
Condition50%3/6
not $self or $self->{"generation"} != $generation or $success
l!l&&r!l&&!r
044113
not $self or $self->{"generation"} != $generation
l!l&&r!l&&!r
00454
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 456 });
335 456 $id
336 }
337  
338 325 ▶sub _operation {
Subroutine _operation: called CC=2 SCAR=6.9
339 325 my ($self, $method, $params, $done, $interactions) = @_;
340  
341 325 my $id = ++$self->{operation_id};
342 325 my $op = $self->{operations}{$id} = {
343 method => $method,
344 params => $params,
345 done => $done,
346 interactions => $interactions,
347 };
348  
349 325 weaken $self;
350 325 ▶ if ($self->{request_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"request_timeout"}) 1 324 325
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 325 ▶ $self->connect (sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
357 325 my ($success, $error) = @_;
358  
359 325 ▶ return unless $self && $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $self->{"operations"}{$id} 1 324 325
Condition66%2/3
$self and $self->{"operations"}{$id}
!ll&&!rl&&r
01324
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 324 ▶ return $self->_finish ($id, 0, $error) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 5 319 324
361 319 $self->_continue ($id, $params);
362 325 });
363 325 $id
364 }
365  
366 175 ▶sub rpc {
Subroutine rpc: called CC=1 SCAR=0.0
Pod documented
367 175 my ($self, $method, $params, $done) = @_;
368  
369 175 $self->_operation ($method, $params, $done, 0)
370 }
371  
372 150 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
373 150 my ($self, $name, $args, $done, %params) = @_;
374  
375 150 $self->_operation ("tools/call", { %params, name => $name, arguments => $args }, $done, 1)
376 }
377  
378 346 ▶sub _continue {
Subroutine _continue: called CC=2 SCAR=6.9
379 346 my ($self, $id, $params) = @_;
380  
381 346 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 346 0 346
382  
383 346 weaken $self;
384 342 ▶ $self->_request ($op->{method}, $params, sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
385 342 my ($success, $result) = @_;
386  
387 342 ▶ return unless $self && $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $self->{"operations"}{$id} 5 337 342
Condition66%2/3
$self and $self->{"operations"}{$id}
!ll&&!rl&&r
05337
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 337 delete $op->{wire_id};
389  
390 337 ▶ if ($success && $op->{interactions} && $result->{resultType} eq "input_required") {
Branch: 1 branch
BranchTrueFalseTotal
if ($success and $op->{"interactions"} and $result->{"resultType"} eq "input_required") 29 308 337
Condition100%6/6
$success and $op->{"interactions"} and $result->{"resultType"} eq "input_required"
!ll&&!rl&&r
16714129
$success and $op->{"interactions"}
!ll&&!rl&&r
3164170
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 29 return $self->_input ($id, $result)
392 }
393 308 $self->_finish ($id, $success, $result);
394 346 }, $op);
395 }
396  
397 29 ▶sub _input {
Subroutine _input: called CC=14 SCAR=28.4
398 29 my ($self, $id, $result) = @_;
399  
400 29 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 29 0 29
401 29 my $input = $result->{inputRequests};
402  
403 return $self->_finish ($id, 0, { transport => 1, message => "Input required without a client handler" })
404 29 ▶ 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 28 29
Condition66%4/6
$self->{"on_request"} and ref $input eq "HASH" and keys %$input
!ll&&!rl&&r
1028
$self->{"on_request"} and ref $input eq "HASH"
!ll&&!rl&&r
1028
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 28 ▶ if ++$op->{rounds} > ($self->{max_rounds} || 32);
Branch: 1 branch
BranchTrueFalseTotal
if ++$op->{"rounds"} > ($self->{"max_rounds"} || 32) 0 28 28
Condition50%1/2
$self->{'max_rounds'} || 32
l!l
028
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 28 weaken $self;
409 28 my %responses;
410 28 my $remaining = keys %$input;
411 28 $op->{input} = [];
412 28 for my $key (sort keys %$input) {
413 29 ▶ last unless $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operations"}{$id} 0 29 29
414 29 my $request = $input->{$key};
415 29 ▶ 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 29 29
Condition37%3/8
ref $request eq "HASH" and defined $request->{"method"} and ref($request->{"params"} || {}) eq "HASH"
!ll&&!rl&&r
0029
ref $request eq "HASH" and defined $request->{"method"}
!ll&&!rl&&r
0029
$request->{'params'} || {}
l!l
290
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 28 ▶ on_reply => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
429 28 my ($success, $response) = @_;
430  
431 28 ▶ return unless $self->{operations}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operations"}{$id} 0 28 28
432 28 ▶ return $self->_finish ($id, 0, $response) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 28 28
433 28 $responses{$key} = $response;
434 28 ▶ return if --$remaining;
Branch: 1 branch
BranchTrueFalseTotal
if --$remaining 1 27 28
435  
436 27 delete $op->{input};
437 27 my %params = (%{$op->{params}}, inputResponses => \%responses);
438 27 ▶ $params{requestState} = $result->{requestState} if exists $result->{requestState};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"requestState"} 26 1 27
439 27 $self->_continue ($id, \%params);
440 },
441 29 ▶ );
Condition50%1/2
$request->{'params'} || {}
l!l
290
MC/DC0%0/1
$request->{'params'} || {}
$request->{'params'}
Truth table50%1/2
A
A= $request->{'params'}
Aresult
11
00
442 29 push @{$op->{input}}, $req;
443 29 $self->event (request => $req);
444 }
445 }
446  
447 325 ▶sub _finish {
Subroutine _finish: called CC=5 SCAR=16.1
448 325 my ($self, $id, $success, $result) = @_;
449  
450 325 ▶ my $op = delete $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = delete $self->{"operations"}{$id} or return 325 0 325
451 325 ▶ delete $self->{pending}{$op->{wire_id}} if exists $op->{wire_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $op->{"wire_id"} 9 316 325
452 325 ▶ $_->cancel for @{$op->{input} || []};
Condition100%2/2
$op->{'input'} || []
l!l
1324
MC/DC100%1/1
$op->{'input'} || []
$op->{'input'}
Truth table100%2/2
A
A= $op->{'input'}
Aresult
11
00
453 325 delete $op->{timer};
454 325 $op->{done}->($success, $result);
455 }
456  
457 6 ▶sub cancel {
Subroutine cancel: called CC=5 SCAR=16.1
Pod documented
458 6 my ($self, $id, $reason) = @_;
459  
460 6 ▶ my $op = $self->{operations}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $self->{"operations"}{$id} or return 5 1 6
461 5 my $wire = $op->{wire_id};
462 5 ▶ $reason = "Request cancelled" unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 3 2 5
463  
464 5 $self->_finish ($id, 0, { transport => 1, cancelled => 1, message => $reason });
465 5 ▶ if (defined $wire && $self->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wire and $self->{"connected"}) 4 1 5
Condition66%2/3
defined $wire and $self->{"connected"}
!ll&&!rl&&r
104
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 4 ▶ }, sub {});
Subroutine __ANON__: called CC=1 SCAR=0.0
470 4 $self->_abort ($wire);
471 }
472 }
473  
474 43 ▶sub tools {
Subroutine tools: called CC=1 SCAR=0.0
Pod documented
475 43 my ($self, $done) = @_;
476  
477 43 my (@tools, %seen);
478 43 weaken $self;
479 43 my $page;
480 45 ▶ $page = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
481 45 my ($cursor) = @_;
482  
483 45 ▶ $self->rpc ("tools/list", defined $cursor ? { cursor => $cursor } : {}, sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
484 45 my ($success, $result) = @_;
485  
486 45 ▶ unless ($success && ref $result->{tools} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success and ref $result->{"tools"} eq "ARRAY") 1 44 45
Condition66%2/3
$success and ref $result->{"tools"} eq "ARRAY"
!ll&&!rl&&r
1044
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 1 undef $page;
488 1 ▶ return $done->(0, $success ? { transport => 1, message => "Invalid tools/list result" } : $result)
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 1 1
489 }
490  
491 44 push @tools, @{$result->{tools}};
492 44 ▶ if (defined $result->{nextCursor} && length $result->{nextCursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"nextCursor"} and length $result->{"nextCursor"}) 3 41 44
Condition66%2/3
defined $result->{"nextCursor"} and length $result->{"nextCursor"}
!ll&&!rl&&r
4103
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 41 undef $page;
501 41 $done->(1, \@tools);
502 45 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined $cursor ? : 2 43 45
503 43 };
504 43 $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 443 ▶sub _receive {
Subroutine _receive: called CC=17 SCAR=44.1
523 443 my ($self, $message) = @_;
524  
525 die "Invalid JSON-RPC message\n"
526 443 ▶ 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 443 443
Condition33%1/3
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0"
!ll&&!rl&&r
00443
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 443 ▶ if (exists $message->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $message->{"method"}) 5 438 443
529 5 ▶ die "Invalid server method\n" if ref $message->{method};
Branch: 1 branch
BranchTrueFalseTotal
if ref $message->{"method"} 0 5 5
530 5 ▶ if (!exists $message->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $message->{"id"}) 5 0 5
531 5 ▶ $self->event (notification => $message->{method}, $message->{params} || {});
Condition50%1/2
$message->{'params'} || {}
l!l
50
MC/DC0%0/1
$message->{'params'} || {}
$message->{'params'}
Truth table50%1/2
A
A= $message->{'params'}
Aresult
11
00
532 return
533 5 }
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 438 ▶ my $field = exists $message->{result} ? "result" : "error";
Branch: 1 branch
BranchTrueFalseTotal
exists $message->{'result'} ? : 432 6 438
565  
566 defined $message->{id} && !ref $message->{id}
567 && (exists $message->{result}) != (exists $message->{error})
568 438 ▶ && 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" 438 0 438
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
00438
defined $message->{"id"} and not ref $message->{"id"} and exists $message->{"result"} != exists $message->{"error"}
!ll&&!rl&&r
00438
defined $message->{"id"} and not ref $message->{"id"}
!ll&&!rl&&r
00438
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 438 ▶ my $done = delete $self->{pending}{$message->{id}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $done = delete $self->{"pending"}{$message->{"id"}} or return 438 0 438
572 438 $done->(exists $message->{result}, $message->{$field});
573 }
574  
575 56 ▶sub _open {
Subroutine _open: called CC=1 SCAR=0.0
576 56 $_[1]->(1, undef)
577 }
578  
579 4 ▶sub _abort {
Subroutine _abort: called CC=1 SCAR=0.0
580 }
581  
582 187 ▶sub close {
Subroutine close: called CC=13 SCAR=25.7
Pod documented
583 187 my ($self, $message) = @_;
584  
585 187 my $was_connected = $self->{connected};
586 # the close event runs after outstanding operations have been failed.
587 187 ▶ my $outstanding = scalar keys %{$self->{operations} || {}};
Condition100%2/2
$self->{'operations'} || {}
l!l
17710
MC/DC100%1/1
$self->{'operations'} || {}
$self->{'operations'}
Truth table100%2/2
A
A= $self->{'operations'}
Aresult
11
00
588 187 ++$self->{generation};
589 187 $self->_shutdown;
590 187 delete @$self{qw(connected protocol_version server_info server_capabilities instructions)};
591  
592 187 ▶ my $error = { transport => 1, pending => $outstanding,
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 5 182 187
593 message => defined $message ? $message : "Client closed" };
594 187 ▶ my $connecting = delete $self->{connecting} || [];
Condition100%2/2
delete $self->{'connecting'} || []
l!l
2185
MC/DC100%1/1
delete $self->{'connecting'} || []
delete $self->{'connecting'}
Truth table100%2/2
A
A= delete $self->{'connecting'}
Aresult
11
00
595 187 ▶ my $pending = delete $self->{pending} || {};
Condition100%2/2
delete $self->{'pending'} || {}
l!l
11176
MC/DC100%1/1
delete $self->{'pending'} || {}
delete $self->{'pending'}
Truth table100%2/2
A
A= delete $self->{'pending'}
Aresult
11
00
596 187 ▶ my $incoming = delete $self->{incoming} || {};
Condition50%1/2
delete $self->{'incoming'} || {}
l!l
0187
MC/DC0%0/1
delete $self->{'incoming'} || {}
delete $self->{'incoming'}
Truth table50%1/2
A
A= delete $self->{'incoming'}
Aresult
11
00
597 187 $_->cancel for values %$incoming;
598 187 ▶ $self->_finish ($_, 0, $error) for keys %{$self->{operations} || {}};
Condition100%2/2
$self->{'operations'} || {}
l!l
17710
MC/DC100%1/1
$self->{'operations'} || {}
$self->{'operations'}
Truth table100%2/2
A
A= $self->{'operations'}
Aresult
11
00
599 187 $_->(0, $error) for values %$pending;
600 187 $_->(0, $error) for @$connecting;
601 187 ▶ $self->event (close => $error) if $was_connected && defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if $was_connected and defined $message 3 184 187
Condition100%3/3
$was_connected and defined $message
!ll&&!rl&&r
86983
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 72 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
605 72 $_[0]->close
606 }
607  
608 36 1
« bin/anyevent-mcp ↑ Summary blib/lib/AnyEvent/MCP/Client/HTTP.pm »