blib/lib/Net/Async/MCP.pm

filter: Statementstmt 98.7% 395 / 400 Branchbran 85.8% 158 / 184 Conditioncond 69.0% 105 / 152 MC/DCmcdc 41.4% 34 / 82 Subroutinesub 100% 64 / 64 Podpod 100% 22 / 22 totaltotal 86.0% 778 / 904 CC 164 SCAR 21.4
CC
164
Cov
89%
_probe_handshake
20.1
_request
18.3
_renegotiated_version
14.6
CRAP 8.5
« t/bin/legacy_mcp_server.pl ↑ Summary blib/lib/Net/Async/MCP/Transport/HTTP.pm »
1 package Net::Async::MCP;
2 # ABSTRACT: Async MCP (Model Context Protocol) client for IO::Async
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Scalar::Util qw( blessed looks_like_number weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '0.007';
13  
14  
15 # The modern protocol revisions this client speaks, newest first: the first of
16 # them is what a client that was not told otherwise sends, and they are the
17 # only ones a refused request may be renegotiated into.
18 #
19 # What belongs in here is decided by the code below and by nothing else: an
20 # entry means that what this file builds is what that revision defines - today
21 # a server/discover handshake with no initialize/initialized pair, the
22 # io.modelcontextprotocol/* keys in _meta, and input_required results answered
23 # with inputResponses. So adding one means first teaching this client to build
24 # that revision's request shapes. A revision some dependency happens to know
25 # about is not one this client can speak, and switching to it on that strength
26 # would put a new version string on requests that revision never had - trading
27 # a clear "unsupported protocol version" for a confusing "method not found".
28 #
29 # Deliberately not MCP::Constants::SUPPORTED_VERSIONS: that list says what the
30 # installed server library accepts, which is a different question whose answer
31 # only happens to be the same one today.
32 10 my @MODERN_PROTOCOL_VERSIONS = ('2026-07-28');
33  
34 # The legacy revisions this client speaks, newest first: the ones with an
35 # initialize/initialized handshake and no protocol keys in _meta, which is what
36 # every third-party server speaks today. The same rule as above decides what
37 # goes in here - what this file builds for them, the handshake, the plain
38 # requests and the absent _meta, is the same in all four, and a revision whose
39 # requests differ from that is not one to add without teaching the client the
40 # difference first.
41 10 my @LEGACY_PROTOCOL_VERSIONS = ('2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05');
42  
43 # What this client sends when the caller named no protocol_version: the newest
44 # revision it speaks. Deliberately not MCP::Constants::PROTOCOL_VERSION - that
45 # is the revision the installed server library implements, and following it
46 # onto a newer one would put that version string on requests this file still
47 # builds in the old one, for the reason spelled out above.
48 10 my $DEFAULT_PROTOCOL_VERSION = $MODERN_PROTOCOL_VERSIONS[0];
49  
50 # The code a server answers a request made in a revision it does not speak
51 # with. The number the specification gives it, written out rather than read
52 # from L<MCP::Constants>: it is compared against what a remote server sent, so
53 # a locally installed library has no say in it, and a client on stdio or HTTP
54 # has no reason to have L<MCP> installed at all.
55 10 my $UNSUPPORTED_PROTOCOL_VERSION = -32022;
56  
57 # The errors only a modern server answers with - HeaderMismatch,
58 # MissingRequiredClientCapability and UnsupportedProtocolVersion. A probe
59 # refused with one of them was refused by a modern server, and must not fall
60 # back to initialize; any other error says the server is legacy.
61 10 my %MODERN_ERROR_CODES = map { $_ => 1 } ( -32020, -32021, $UNSUPPORTED_PROTOCOL_VERSION );
62  
63 # How long the server/discover probe of a stdio server may stay unanswered
64 # before the server is taken to be legacy. Generous because a slow start - an
65 # npx that first has to fetch its package - looks exactly like a server that
66 # will never answer.
67 10 my $DEFAULT_DISCOVER_TIMEOUT = 10;
68  
69 # The settings this client keeps to itself, whichever transport it ends up
70 # with.
71 10 my @CLIENT_KEYS = qw( server command url protocol_version client_capabilities
72 on_input_request discover_timeout );
73  
74 # The settings handed on to the transport instead, each with the class a
75 # transport has to be for it to reach it. Kept apart from @CLIENT_KEYS because
76 # they are passed on only when the caller actually gave them: the transport's
77 # own default for stall_timeout has to survive a client that was never asked
78 # about it.
79 #
80 # Which transport takes what is one decision per key rather than one for the
81 # lot. headers and stall_timeout describe an HTTP request, and handing one to
82 # the Stdio transport is not a setting it ignores but an "Unrecognised
83 # configuration keys" croak; timeout, the limit on waiting for an answer, and
84 # on_notification, an event, are what both wire transports have.
85 # on_subscription_end has only the HTTP transport to go to, a stream being the
86 # one thing a subscription can end on. The InProcess transport is no
87 # L<IO::Async::Notifier>, has no C<configure> and no events at all, so it
88 # matches nothing here and is handed nothing. env, cwd and on_stderr describe
89 # a subprocess, which only the Stdio transport has.
90 10 my %TRANSPORT_KEYS = (
91 headers => 'Net::Async::MCP::Transport::HTTP',
92 timeout => 'IO::Async::Notifier',
93 stall_timeout => 'Net::Async::MCP::Transport::HTTP',
94 on_notification => 'IO::Async::Notifier',
95 on_subscription_end => 'Net::Async::MCP::Transport::HTTP',
96 env => 'Net::Async::MCP::Transport::Stdio',
97 cwd => 'Net::Async::MCP::Transport::Stdio',
98 on_stderr => 'Net::Async::MCP::Transport::Stdio',
99 );
100  
101 # The keys of %TRANSPORT_KEYS that are events of this client rather than
102 # settings. They travel to the transport wrapped - see _event_handler - and
103 # are asked for by event rather than by key, because a subclass method of that
104 # name is a handler just as much as a configured code ref is.
105 10 my %EVENT_KEYS = map { $_ => 1 } qw( on_notification on_subscription_end on_stderr );
106  
107 69 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.1
108 69 my ( $self, $params ) = @_;
109 69 $self->{protocol_version_pinned} = defined $params->{protocol_version};
110 69 for my $key (@CLIENT_KEYS, keys %TRANSPORT_KEYS) {
111 1035 ▶ $self->{$key} = delete $params->{$key} if exists $params->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{$key} 122 913 1035
112 }
113 69 ▶ $self->{protocol_version} //= $DEFAULT_PROTOCOL_VERSION;
Condition66%2/3
$self->{"protocol_version"} //= $DEFAULT_PROTOCOL_VERSION
l!l&&r!l&&!r
8610
MC/DC0%0/2
$self->{"protocol_version"} //= $DEFAULT_PROTOCOL_VERSION
$self->{"protocol_version"} $DEFAULT_PROTOCOL_VERSION
Truth table66%2/3
A //= B
A= $self->{"protocol_version"}B= $DEFAULT_PROTOCOL_VERSION
ABresult
1X1
011
000
114 69 ▶ $self->{client_capabilities} //= {};
Condition100%2/2
$self->{"client_capabilities"} //= {}
l!l
762
MC/DC100%1/1
$self->{"client_capabilities"} //= {}
$self->{"client_capabilities"}
Truth table100%2/2
A
A= $self->{"client_capabilities"}
Aresult
11
00
115 69 $self->SUPER::_init($params);
116 }
117  
118 79 ▶sub configure {
Subroutine configure: called CC=8 SCAR=20.8
Pod documented
119 79 my ( $self, %params ) = @_;
120 79 my @changed = grep { exists $params{$_} } sort keys %TRANSPORT_KEYS;
121  
122 # A protocol_version the caller names is a pin, which decides the handshake
123 # - see initialize. One left to its default is not, and neither is the
124 # version a handshake agreed on, which is set without passing through here.
125 $self->{protocol_version_pinned} = defined $params{protocol_version}
126 79 ▶ if exists $params{protocol_version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"protocol_version"} 1 78 79
127  
128 79 for my $key (@CLIENT_KEYS, keys %TRANSPORT_KEYS) {
129 1185 ▶ $self->{$key} = delete $params{$key} if exists $params{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 10 1175 1185
130 }
131 79 ▶ $self->{protocol_version} //= $DEFAULT_PROTOCOL_VERSION;
Condition66%2/3
$self->{"protocol_version"} //= $DEFAULT_PROTOCOL_VERSION
l!l&&r!l&&!r
7810
MC/DC0%0/2
$self->{"protocol_version"} //= $DEFAULT_PROTOCOL_VERSION
$self->{"protocol_version"} $DEFAULT_PROTOCOL_VERSION
Truth table66%2/3
A //= B
A= $self->{"protocol_version"}B= $DEFAULT_PROTOCOL_VERSION
ABresult
1X1
011
000
132 79 ▶ $self->{client_capabilities} //= {};
Condition100%2/2
$self->{"client_capabilities"} //= {}
l!l
781
MC/DC100%1/1
$self->{"client_capabilities"} //= {}
$self->{"client_capabilities"}
Truth table100%2/2
A
A= $self->{"client_capabilities"}
Aresult
11
00
133  
134 # A transport that already exists takes the change too: the transport is
135 # built when this client joins a loop, and a bearer token that has to be
136 # rotated, or a handler for the notifications of a call about to be made,
137 # arrives long after that. Only the keys that transport takes, though - see
138 # %TRANSPORT_KEYS for why that is asked per key and not once for the lot.
139 79 ▶ if ( my $transport = $self->{transport} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $transport = $self->{"transport"}) 10 69 79
140 10 my @keys = grep { $transport->isa($TRANSPORT_KEYS{$_}) } @changed;
141 10 ▶ $transport->configure($self->_transport_params(@keys)) if @keys;
Branch: 1 branch
BranchTrueFalseTotal
if @keys 6 4 10
142 }
143  
144 79 $self->SUPER::configure(%params);
145 }
146  
147 # Private: the named settings as the transport takes them. All of them travel
148 # as they stand except the events among them, which the transport would
149 # otherwise invoke with itself - see _event_handler.
150 43 ▶sub _transport_params {
Subroutine _transport_params: called CC=2 SCAR=6.9
151 43 my ( $self, @keys ) = @_;
152 43 my %params = map { $_ => $self->{$_} } @keys;
153 43 for my $event (grep { $EVENT_KEYS{$_} } keys %params) {
154 11 $params{$event} = $self->_event_handler($event);
155 }
156 43 return %params;
157 }
158  
159 # Private: the handler handed to the transport for an event of this client's
160 # own. It is the transport that receives the underlying thing - a notification,
161 # an ended subscription - and invokes the event, so a handler passed on as it
162 # stands would be called with the transport, while every other event of this
163 # client is called with the client. This wrapper puts the client back in front,
164 # and leaves the choice of handler to the client's own event dispatch, so a
165 # subclass method serves as well as a configured code ref.
166 #
167 # The client holds the transport, so what the transport holds must not hold the
168 # client: a strong reference in here would close the cycle. It is never undef
169 # where it matters - a transport that can still deliver anything is in a loop,
170 # and the loop holds the client that holds it.
171 11 ▶sub _event_handler {
Subroutine _event_handler: called CC=2 SCAR=7.1
172 11 my ( $self, $event ) = @_;
173 11 ▶ return undef unless $self->can_event($event);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_event($event) 0 11 11
174  
175 11 weaken( my $weak_self = $self );
176 34 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
177 34 my ( undef, @args ) = @_;
178 34 ▶ my $client = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $weak_self or return 34 0 34
179 34 return $client->maybe_invoke_event($event => @args);
180 11 };
181 }
182  
183 60 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=1 SCAR=0.0
184 60 my ( $self, $loop ) = @_;
185 60 $self->SUPER::_add_to_loop($loop);
186 60 $self->_ensure_transport;
187 }
188  
189 98 ▶sub _ensure_transport {
Subroutine _ensure_transport: called CC=7 SCAR=19.8
190 98 my ( $self ) = @_;
191 98 ▶ return if $self->{transport};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport"} 38 60 98
192  
193 60 ▶ if ($self->{server}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'server'}) { } 23 37 60
elsif ($self->{'command'}) { } 24 13 37
elsif ($self->{'url'}) { } 13 0 13
194 23 require Net::Async::MCP::Transport::InProcess;
195 $self->{transport} = Net::Async::MCP::Transport::InProcess->new(
196 server => $self->{server},
197 23 );
198 }
199 elsif ($self->{command}) {
200 24 ▶ croak "Stdio transport requires being added to an IO::Async::Loop"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loop 0 24 24
201 unless $self->loop;
202 24 require Net::Async::MCP::Transport::Stdio;
203 $self->_build_transport('Net::Async::MCP::Transport::Stdio',
204 command => $self->{command},
205 24 on_request => $self->_server_request_handler);
206 }
207 elsif ($self->{url}) {
208 13 ▶ croak "HTTP transport requires being added to an IO::Async::Loop"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loop 0 13 13
209 unless $self->loop;
210 13 require Net::Async::MCP::Transport::HTTP;
211 $self->_build_transport('Net::Async::MCP::Transport::HTTP',
212 url => $self->{url},
213 13 on_request => $self->_server_request_handler);
214 }
215 else {
216 0 croak "Must provide server, command, or url";
217 }
218 }
219  
220 # Private: a transport of $class, built with the setting that selected it and
221 # whichever of %TRANSPORT_KEYS that class takes, and made a child of this
222 # client so that it joins and leaves the loop along with it.
223 37 ▶sub _build_transport {
Subroutine _build_transport: called CC=2 SCAR=6.9
224 37 my ( $self, $class, %params ) = @_;
225  
226 # Only the keys the caller actually gave: handing over a stall_timeout of
227 # undef for one that was never set would switch off the transport's default
228 # instead of leaving it alone. Events are asked for by name rather than by
229 # key, because a subclass method of that name is a handler just as much as a
230 # configured code ref is.
231 28 my @keys = grep { $class->isa($TRANSPORT_KEYS{$_}) }
232 37 ▶ grep { $EVENT_KEYS{$_} ? $self->can_event($_) : exists $self->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
$EVENT_KEYS{$_} ? : 111 185 296
233 sort keys %TRANSPORT_KEYS;
234  
235 37 my $transport = $class->new(%params, $self->_transport_params(@keys));
236 37 $self->{transport} = $transport;
237 37 $self->add_child($transport);
238 37 return $transport;
239 }
240  
241 # Private: the handler the wire transports hand the requests a server sends
242 # to the client - off stdout over stdio, off a response stream over HTTP. Only a legacy server sends any: the modern revision has no
243 # server-to-client requests - a server asks through input_required results
244 # instead - so on a modern connection they stay unanswered, as they always
245 # were. A legacy server gets an empty result for ping, which every revision
246 # requires a client to answer, and METHOD_NOT_FOUND for everything else -
247 # roots/list, sampling, elicitation - since the legacy handshake declares no
248 # capability that would invite those. Weak for the same reason as
249 # _event_handler: the client holds the transport that holds this.
250 37 ▶sub _server_request_handler {
Subroutine _server_request_handler: called CC=1 SCAR=0.0
251 37 my ( $self ) = @_;
252 37 weaken( my $weak_self = $self );
253 4 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
254 4 my ( undef, $request ) = @_;
255 4 ▶ my $client = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $weak_self or return 4 0 4
256 4 ▶ return unless $client->is_legacy;
Branch: 1 branch
BranchTrueFalseTotal
unless $client->is_legacy 0 4 4
257  
258 4 my $method = $request->{method};
259 4 ▶ return { result => {} } if $method eq 'ping';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "ping" 2 2 4
260 2 return { error => {
261 code => -32601,
262 message => "Method not found: $method",
263 } };
264 37 };
265 }
266  
267 13 ▶sub protocol_version { $_[0]->{protocol_version} }
Subroutine protocol_version: called CC=1 SCAR=0.0
Pod documented
268  
269  
270 327 ▶sub is_legacy { ( $_[0]->{era} // '' ) eq 'legacy' ? 1 : 0 }
Subroutine is_legacy: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
($_[0]{'era'} // '') eq 'legacy' ? : 117 210 327
Condition100%2/2
$_[0]{"era"} // ""
l!l
149178
MC/DC100%1/1
$_[0]{"era"} // ""
$_[0]{"era"}
Truth table100%2/2
A
A= $_[0]{"era"}
Aresult
11
00
271  
272  
273 15 ▶sub discover_timeout { $_[0]->{discover_timeout} // $DEFAULT_DISCOVER_TIMEOUT }
Subroutine discover_timeout: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"discover_timeout"} // $DEFAULT_DISCOVER_TIMEOUT
l!l&&r!l&&!r
4110
MC/DC0%0/2
$_[0]{"discover_timeout"} // $DEFAULT_DISCOVER_TIMEOUT
$_[0]{"discover_timeout"} $DEFAULT_DISCOVER_TIMEOUT
Truth table66%2/3
A // B
A= $_[0]{"discover_timeout"}B= $DEFAULT_DISCOVER_TIMEOUT
ABresult
1X1
011
000
274  
275  
276 1 ▶sub instructions { $_[0]->{instructions} }
Subroutine instructions: called CC=1 SCAR=0.0
Pod documented
277  
278  
279 3 ▶sub client_capabilities { $_[0]->{client_capabilities} }
Subroutine client_capabilities: called CC=1 SCAR=0.0
Pod documented
280  
281  
282 2 ▶sub headers { $_[0]->{headers} }
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
283  
284  
285 1 ▶sub timeout { $_[0]->{timeout} }
Subroutine timeout: called CC=1 SCAR=0.0
Pod documented
286  
287  
288 1 ▶sub stall_timeout { $_[0]->{stall_timeout} }
Subroutine stall_timeout: called CC=1 SCAR=0.0
Pod documented
289  
290  
291  
292  
293  
294 # Private: the C<_meta> fields carried on every JSON-RPC request, as required
295 # by the current MCP revision.
296 102 ▶sub _meta {
Subroutine _meta: called CC=1 SCAR=0.0
297 102 my ( $self ) = @_;
298 return {
299 'io.modelcontextprotocol/protocolVersion' => $self->{protocol_version},
300 'io.modelcontextprotocol/clientCapabilities' => $self->{client_capabilities},
301 102 'io.modelcontextprotocol/clientInfo' => $self->_client_info,
302 };
303 }
304  
305 # Private: who this client is, as both eras' handshakes tell the server.
306 126 ▶sub _client_info { return { name => 'Net::Async::MCP', version => $VERSION } }
Subroutine _client_info: called CC=1 SCAR=0.0
307  
308 # Private: merge a caller's C<_meta> (if any) into the standard one, returning
309 # params that carry C<_meta> on every request. A legacy revision has no
310 # standard _meta: its requests carry the caller's own, if any, and otherwise
311 # none at all.
312 154 ▶sub _with_meta {
Subroutine _with_meta: called CC=4 SCAR=14.0
313 154 my ( $self, $params ) = @_;
314 154 ▶ my %params = %{ $params // {} };
Condition50%1/2
$params // {}
l!l
1540
MC/DC0%0/1
$params // {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
315 154 ▶ return \%params if $self->is_legacy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_legacy 52 102 154
316 102 ▶ my %meta = (%{ $self->_meta }, %{ $params{_meta} // {} });
Condition50%1/2
$params{"_meta"} // {}
l!l
0102
MC/DC0%0/1
$params{"_meta"} // {}
$params{"_meta"}
Truth table50%1/2
A
A= $params{"_meta"}
Aresult
11
00
317 102 return { %params, _meta => \%meta };
318 }
319  
320 # Private: how many input_required results one request may collect before the
321 # client gives up on it. A server is allowed to ask again after being answered
322 # - a confirmation can lead to a second question - but one that never arrives
323 # at a final result would keep this going for as long as it feels like.
324 10 my $MAX_INPUT_ROUNDS = 8;
325  
326 # Private: the protocol revision to send a refused request again with, or undef
327 # if there is none. A server that does not speak the revision a request was
328 # made with answers UNSUPPORTED_PROTOCOL_VERSION and lists the ones it does
329 # speak in error.data.supported, newest first; the first of those this client
330 # speaks too is the one to switch to.
331 30 ▶sub _renegotiated_version {
Subroutine _renegotiated_version: called CC=13 SCAR=26.8
332 30 my ( $self, $failed ) = @_;
333  
334 30 my ( undef, $category, $error ) = $failed->failure;
335 30 ▶ return undef unless ( $category // '' ) eq 'mcp' && ref $error eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ($category // "") eq "mcp" and ref $error eq "HASH" 8 22 30
Condition80%4/5
($category // "") eq "mcp" and ref $error eq "HASH"
!ll&&!rl&&r
8022
$category // ""
l!l
255
MC/DC50%1/2
($category // "") eq "mcp" and ref $error eq "HASH"
$category ref $error eq "HASH"
Truth table66%2/3
A and B
A= $categoryB= ref $error eq "HASH"
ABresult
0X0
100
111
336 22 ▶ return undef unless ( $error->{code} // 0 ) == $UNSUPPORTED_PROTOCOL_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->{"code"} // 0) == $UNSUPPORTED_PROTOCOL_VERSION 15 7 22
Condition50%1/2
$error->{"code"} // 0
l!l
220
MC/DC0%0/1
$error->{"code"} // 0
$error->{"code"}
Truth table50%1/2
A
A= $error->{"code"}
Aresult
11
00
337  
338 7 ▶ my $supported = ref $error->{data} eq 'HASH' ? $error->{data}{supported} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $error->{'data'} eq 'HASH' ? : 7 0 7
339 7 ▶ return undef unless ref $supported eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $supported eq "ARRAY" 0 7 7
340  
341 7 my %usable = map { $_ => 1 } @MODERN_PROTOCOL_VERSIONS;
342 7 for my $version (@$supported) {
343 8 ▶ next unless defined $version && !ref $version && $usable{$version};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and not ref $version and $usable{$version} 5 3 8
Condition50%3/6
defined $version and not ref $version and $usable{$version}
!ll&&!rl&&r
053
defined $version and not ref $version
!ll&&!rl&&r
008
MC/DC33%1/3
defined $version and not ref $version and $usable{$version}
defined $version not ref $version $usable{$version}
Truth table50%2/4
A and not(B) and C
A= defined $versionB= not ref $versionC= $usable{$version}
ABCresult
0XX0
10X0
1100
1111
344  
345 # The one offer that is no answer: the request that was just refused
346 # carried this very version, so sending it again would be sending the same
347 # request twice and getting the same refusal back.
348 3 ▶ return undef if $version eq $self->{protocol_version};
Branch: 1 branch
BranchTrueFalseTotal
if $version eq $self->{"protocol_version"} 0 3 3
349 3 return $version;
350 }
351  
352 4 return undef;
353 }
354  
355 # Private: one MCP request, from a method's own params to the final result.
356 # Every method goes through here, because this is the one place that holds what
357 # is true of all of them: the _meta every request carries, the input_required
358 # round trips a result may take before it is one, and the one retry a refused
359 # protocol version is worth.
360 134 ▶async sub _request {
Subroutine _request: called CC=18 SCAR=29.1
361 134 my ( $self, $method, $params, %options ) = @_;
362  
363 134 my ( %retry, $rounds, $renegotiated, $renewed );
364 134 while ( 1 ) {
365 # A request made while a new legacy session is being started waits for
366 # it, rather than going out in no session at all - all but the initialize
367 # that starts it. Without cancel, so that a caller giving up on its own
368 # request does not take the session from everybody else waiting on it.
369 154 ▶ if ( ( my $renewal = $self->{session_renewal} ) && !$options{start_session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $renewal = $self->{"session_renewal"} and not $options{"start_session"}) 0 154 154
Condition33%1/3
my $renewal = $self->{"session_renewal"} and not $options{"start_session"}
!ll&&!rl&&r
15400
MC/DC0%0/2
my $renewal = $self->{"session_renewal"} and not $options{"start_session"}
my $renewal = $self->{"session_renewal"} not $options{"start_session"}
Truth table33%1/3
A and not(B)
A= my $renewal = $self->{"session_renewal"}B= not $options{"start_session"}
ABresult
0X0
100
111
370 0 await $renewal->without_cancel;
371 }
372  
373 # Awaited as a completed Future rather than for its value: a failure has
374 # more to it than its message, and the server's error object - which is
375 # where a refused version names the ones it would accept instead - travels
376 # as the failure's details.
377 my $answered = await $self->{transport}->send_request($method,
378 154 ▶ $self->_with_meta({ %{ $params // {} }, %retry }), %options)
Condition100%2/2
$params // {}
l!l
9262
MC/DC100%1/1
$params // {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
379 154 ▶ ->followed_by(sub { Future->done( $_[0] ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
380  
381 150 ▶ my ( $why, $category, $expired ) = $answered->is_failed ? $answered->failure : ();
Branch: 1 branch
BranchTrueFalseTotal
$answered->is_failed ? : 40 110 150
382 150 ▶ if ( ( $category // '' ) eq 'mcp_session_expired' && $self->is_legacy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($category // "") eq "mcp_session_expired" and $self->is_legacy) 6 144 150
Condition80%4/5
($category // "") eq "mcp_session_expired" and $self->is_legacy
!ll&&!rl&&r
14406
$category // ""
l!l
33117
MC/DC50%1/2
($category // "") eq "mcp_session_expired" and $self->is_legacy
$category $self->is_legacy
Truth table66%2/3
A and B
A= $categoryB= $self->is_legacy
ABresult
0X0
100
111
383 # The server ended the session. The specification has the client start
384 # a new one and nothing else, and the request goes out again in it -
385 # once: a server that ends the new session as well is not one more
386 # session away from answering.
387 6 ▶ return await Future->fail("MCP $method: the server ended the new "
Branch: 1 branch
BranchTrueFalseTotal
if $renewed++ 1 5 6
388 . "session as well ($why)", $category, $expired)
389 if $renewed++;
390 5 await $self->_renew_session($expired)->without_cancel;
391 5 next;
392 }
393  
394 144 ▶ if ( $answered->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answered->is_failed) 34 110 144
395 # Renegotiation is the modern revision's: a legacy server agreed on its
396 # version in the handshake and has no -32022 to refuse one with.
397 34 ▶ my $version = $renegotiated || $self->is_legacy
Branch: 1 branch
BranchTrueFalseTotal
$renegotiated || $self->is_legacy ? : 4 30 34
Condition100%3/3
$renegotiated or $self->is_legacy
l!l&&r!l&&!r
1330
MC/DC100%2/2
$renegotiated or $self->is_legacy
$renegotiated $self->is_legacy
Truth table100%3/3
A or B
A= $renegotiatedB= $self->is_legacy
ABresult
1X1
011
000
398 ? undef : $self->_renegotiated_version($answered);
399  
400 # Nothing to switch to, or one switch already made - which makes a second
401 # refusal the server's problem and not one more version away from being
402 # solved. Either way the server's own error is what the caller gets, code
403 # and data and all, rather than a summary of it written here.
404 34 ▶ return await $answered unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 31 3 34
405  
406 # Kept on the client, not just used for the retry: every following
407 # request would otherwise pay for the same refusal again. The _meta at
408 # the top of this loop is built afresh, so the retry carries the version
409 # agreed on here and the answers of any input_required round already
410 # walked.
411 3 $renegotiated = 1;
412 3 $self->{protocol_version} = $version;
413 3 next;
414 }
415  
416 110 my $result = $answered->get;
417  
418 # So is the input_required round trip: a legacy result is final whatever
419 # it carries, and one without a resultType is complete in every revision.
420 110 ▶ return $result if $self->is_legacy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_legacy 42 68 110
421 return $result
422 unless ref $result eq 'HASH'
423 68 ▶ && ( $result->{resultType} // '' ) eq 'input_required';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" and ($result->{"resultType"} // "") eq "input_required" 51 17 68
Condition80%4/5
ref $result eq "HASH" and ($result->{"resultType"} // "") eq "input_required"
!ll&&!rl&&r
05117
$result->{"resultType"} // ""
l!l
3533
MC/DC50%1/2
ref $result eq "HASH" and ($result->{"resultType"} // "") eq "input_required"
ref $result eq "HASH" $result->{"resultType"}
Truth table66%2/3
A and B
A= ref $result eq "HASH"B= $result->{"resultType"}
ABresult
0X0
100
111
424  
425 17 ▶ croak "MCP $method: server answered with input_required more than "
Branch: 1 branch
BranchTrueFalseTotal
if ++$rounds > $MAX_INPUT_ROUNDS 1 16 17
426 . "$MAX_INPUT_ROUNDS times without returning a result"
427 if ++$rounds > $MAX_INPUT_ROUNDS;
428  
429 16 %retry = await $self->_input_required($method, $result);
430 }
431 10 }
432  
433 # Private: the top-level params that turn a request into the retry an
434 # input_required result asks for. Both of them are optional and each is sent
435 # only when the result actually carried it.
436 16 ▶async sub _input_required {
Subroutine _input_required: called CC=5 SCAR=16.1
437 16 my ( $self, $method, $result ) = @_;
438  
439 16 my %retry;
440  
441 # Mirrored back exactly as it arrived. The server sealed it against its own
442 # secret and bound it to this caller and this primitive, so there is nothing
443 # here to read and everything to break by touching it.
444 16 ▶ $retry{requestState} = $result->{requestState} if defined $result->{requestState};
Branch: 1 branch
BranchTrueFalseTotal
if defined $result->{"requestState"} 11 5 16
445  
446 16 my $requests = $result->{inputRequests};
447 16 ▶ if ( ref $requests eq 'HASH' && keys %$requests ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $requests eq 'HASH' and keys %$requests) { } 6 10 16
elsif (not %retry) { } 1 9 10
Condition66%2/3
ref $requests eq "HASH" and keys %$requests
!ll&&!rl&&r
1006
MC/DC50%1/2
ref $requests eq "HASH" and keys %$requests
ref $requests eq "HASH" keys %$requests
Truth table66%2/3
A and B
A= ref $requests eq "HASH"B= keys %$requests
ABresult
0X0
100
111
448 6 $retry{inputResponses} = await $self->_input_responses($method, $requests);
449 }
450 elsif ( !%retry ) {
451 # Neither half: no question to answer, and nothing to hand back that would
452 # make the second attempt any different from the first.
453 1 croak "MCP $method: server sent an input_required result with neither "
454 . "inputRequests nor requestState";
455 }
456  
457 12 return %retry;
458 10 }
459  
460 # Private: the answers to a server's input requests, under the keys it asked
461 # them by - the same keys it reads its own responses back from.
462 6 ▶async sub _input_responses {
Subroutine _input_responses: called CC=13 SCAR=26.3
463 6 my ( $self, $method, $requests ) = @_;
464  
465 # Checked over the whole ask before a single answer is gathered: a server
466 # that asks for an undeclared capability has broken its side of the
467 # capability promise, and no part of that ask should be acted on.
468 6 ▶ my $capabilities = $self->{client_capabilities} // {};
Condition50%1/2
$self->{"client_capabilities"} // {}
l!l
60
MC/DC0%0/1
$self->{"client_capabilities"} // {}
$self->{"client_capabilities"}
Truth table50%1/2
A
A= $self->{"client_capabilities"}
Aresult
11
00
469 6 for my $key (sort keys %$requests) {
470 7 my $request = $requests->{$key};
471 7 ▶ my $wanted = ref $request eq 'HASH' ? $request->{method} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $request eq 'HASH' ? : 7 0 7
472  
473 7 ▶ croak "MCP $method: server sent an input request '$key' without a method"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wanted and length $wanted 0 7 7
Condition33%1/3
defined $wanted and length $wanted
!ll&&!rl&&r
007
MC/DC0%0/2
defined $wanted and length $wanted
defined $wanted length $wanted
Truth table33%1/3
A and B
A= defined $wantedB= length $wanted
ABresult
0X0
100
111
474 unless defined $wanted && length $wanted;
475  
476 # The capability is the namespace of the method asked for:
477 # elicitation/create needs elicitation, sampling/createMessage needs
478 # sampling, roots/list needs roots.
479 7 my $capability = ( split m{/}, $wanted, 2 )[0];
480 croak "MCP $method: server sent input request '$key' for '$wanted', "
481 . "a capability this client did not declare in client_capabilities"
482 7 ▶ unless exists $capabilities->{$capability};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $capabilities->{$capability} 1 6 7
483 }
484  
485 5 ▶ croak "MCP $method: server sent input requests ("
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_event("on_input_request") 1 4 5
486 . join(', ', sort keys %$requests)
487 . ") but no on_input_request handler is set to answer them"
488 unless $self->can_event('on_input_request');
489  
490 4 my %responses;
491 4 for my $key (sort keys %$requests) {
492 5 my $request = $requests->{$key};
493 my $response = $self->invoke_event(on_input_request =>
494 5 ▶ $request->{method}, $request->{params} // {});
Condition50%1/2
$request->{"params"} // {}
l!l
50
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
495  
496 # A handler that has to ask a human answers with a Future instead of an
497 # answer, and the retry waits for it.
498 5 ▶ $response = await $response if blessed($response) && $response->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($response) and $response->isa("Future") 1 4 5
Condition66%2/3
builtin::blessed($response) and $response->isa("Future")
!ll&&!rl&&r
401
MC/DC50%1/2
builtin::blessed($response) and $response->isa("Future")
builtin::blessed($response) $response->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($response)B= $response->isa("Future")
ABresult
0X0
100
111
499  
500 5 ▶ croak "MCP $method: on_input_request answered input request '$key' with "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response eq "HASH" 1 4 5
501 . "something that is not a HashRef"
502 unless ref $response eq 'HASH';
503  
504 4 $responses{$key} = $response;
505 }
506  
507 3 return \%responses;
508 10 }
509  
510 6 ▶sub server_info { $_[0]->{server_info} }
Subroutine server_info: called CC=1 SCAR=0.0
Pod documented
511  
512  
513 1 ▶sub server_capabilities { $_[0]->{server_capabilities} }
Subroutine server_capabilities: called CC=1 SCAR=0.0
Pod documented
514  
515  
516 30 ▶async sub initialize {
Subroutine initialize: called CC=5 SCAR=16.1
Pod documented
517 30 my ( $self ) = @_;
518 30 $self->_ensure_transport;
519  
520 # The era is the server's, so once a handshake has found it out it is not
521 # asked again; before that, a pin decides it, and without one the probe does.
522 30 ▶ my $era = $self->{era} // $self->_pinned_era // '';
Condition80%4/5
($self->{"era"} // $self->_pinned_era) // ""
l!l
426
$self->{"era"} // $self->_pinned_era
l!l&&r!l&&!r
0426
MC/DC50%1/2
($self->{"era"} // $self->_pinned_era) // ""
$self->{"era"} $self->_pinned_era
Truth table66%2/3
A // B
A= $self->{"era"}B= $self->_pinned_era
ABresult
1X1
011
000
523 30 ▶ return await $self->_legacy_handshake($self->{protocol_version}) if $era eq 'legacy';
Branch: 1 branch
BranchTrueFalseTotal
if $era eq "legacy" 3 27 30
524 27 ▶ return await $self->_modern_handshake if $era eq 'modern';
Branch: 1 branch
BranchTrueFalseTotal
if $era eq "modern" 1 26 27
525 26 return await $self->_probe_handshake;
526 10 }
527  
528  
529 # Private: the era a pinned protocol_version asks for, or nothing for a client
530 # that pinned none.
531 30 ▶sub _pinned_era {
Subroutine _pinned_era: called CC=3 SCAR=11.0
532 30 my ( $self ) = @_;
533 30 ▶ return unless $self->{protocol_version_pinned};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"protocol_version_pinned"} 26 4 30
534 4 my $version = $self->{protocol_version};
535 4 ▶ return ( grep { $_ eq $version } @LEGACY_PROTOCOL_VERSIONS ) ? 'legacy' : 'modern';
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $version;} @LEGACY_PROTOCOL_VERSIONS) ? : 3 1 4
536 }
537  
538 # Private: the modern handshake, a server/discover and nothing else.
539 1 ▶async sub _modern_handshake {
Subroutine _modern_handshake: called CC=1 SCAR=0.2
540 1 my ( $self ) = @_;
541 1 my $result = await $self->_request('server/discover');
542 0 return $self->_modern_result($result);
543 10 }
544  
545 # Private: what a DiscoverResult tells about the server, kept on the client.
546 3 ▶sub _modern_result {
Subroutine _modern_result: called CC=4 SCAR=13.9
547 3 my ( $self, $result ) = @_;
548 3 $self->{era} = 'modern';
549  
550 # Read without autovivifying an _meta key into the result we hand back.
551 3 ▶ my $meta = $result->{_meta} // {};
Condition100%2/2
$result->{"_meta"} // {}
l!l
21
MC/DC100%1/1
$result->{"_meta"} // {}
$result->{"_meta"}
Truth table100%2/2
A
A= $result->{"_meta"}
Aresult
11
00
552 3 ▶ $self->{server_info} = $meta->{'io.modelcontextprotocol/serverInfo'} // {};
Condition100%2/2
$meta->{"io.modelcontextprotocol/serverInfo"} // {}
l!l
21
MC/DC100%1/1
$meta->{"io.modelcontextprotocol/serverInfo"} // {}
$meta->{"io.modelcontextprotocol/serverInfo"}
Truth table100%2/2
A
A= $meta->{"io.modelcontextprotocol/serverInfo"}
Aresult
11
00
553 3 ▶ $self->{server_capabilities} = $result->{capabilities} // {};
Condition50%1/2
$result->{"capabilities"} // {}
l!l
30
MC/DC0%0/1
$result->{"capabilities"} // {}
$result->{"capabilities"}
Truth table50%1/2
A
A= $result->{"capabilities"}
Aresult
11
00
554 3 $self->{instructions} = $result->{instructions};
555  
556 3 return $result;
557 }
558  
559 # Private: the handshake of a client that speaks both eras and was not told
560 # which one the server speaks - see initialize for the three outcomes.
561 26 ▶async sub _probe_handshake {
Subroutine _probe_handshake: called CC=20 SCAR=30.0
562 26 my ( $self ) = @_;
563  
564 26 my $probe = $self->_request('server/discover');
565  
566 # Only a stdio server may answer an unknown request with nothing at all;
567 # over HTTP every request is answered with a status, and in process on the
568 # spot. Losing the race cancels the probe, which drops it from the
569 # transport, so an answer that still arrives finds nothing to settle.
570 26 ▶ my $timeout = $self->{transport}->isa('Net::Async::MCP::Transport::Stdio')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'transport'}->isa('Net::Async::MCP::Transport::Stdio') ? : 15 11 26
571 ? $self->discover_timeout : 0;
572 26 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 15 11 26
573 $probe = Future->wait_any(
574 $probe,
575 2 ▶ $self->loop->delay_future(after => $timeout)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
576 2 Future->fail("server/discover got no answer within ${timeout}s",
577 'mcp_probe_timeout');
578 15 }),
579 );
580 }
581  
582 26 ▶ my $answered = await $probe->followed_by(sub { Future->done( $_[0] ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
583 26 ▶ return $self->_modern_result($answered->get) if $answered->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $answered->is_done 3 23 26
584  
585 23 my ( $why, $category, $error ) = $answered->failure;
586 23 ▶ $category //= '';
Condition100%2/2
$category //= ""
l!l
203
MC/DC100%1/1
$category //= ""
$category
Truth table100%2/2
A
A= $category
Aresult
11
00
587 23 ▶ my $code = $category eq 'mcp' && ref $error eq 'HASH' ? $error->{code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$category eq 'mcp' && ref $error eq 'HASH' ? : 15 8 23
Condition66%2/3
$category eq "mcp" and ref $error eq "HASH"
!ll&&!rl&&r
8015
MC/DC50%1/2
$category eq "mcp" and ref $error eq "HASH"
$category eq "mcp" ref $error eq "HASH"
Truth table66%2/3
A and B
A= $category eq "mcp"B= ref $error eq "HASH"
ABresult
0X0
100
111
588  
589 # A modern server that turned the request down. Falling back to initialize
590 # is what the specification rules out here - unless the versions it offers
591 # instead are all legacy ones, and taking one of them up is doing what it
592 # asks.
593 23 ▶ if (defined $code && $MODERN_ERROR_CODES{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and $MODERN_ERROR_CODES{$code}) 3 20 23
Condition100%3/3
defined $code and $MODERN_ERROR_CODES{$code}
!ll&&!rl&&r
8123
MC/DC100%2/2
defined $code and $MODERN_ERROR_CODES{$code}
defined $code $MODERN_ERROR_CODES{$code}
Truth table100%3/3
A and B
A= defined $codeB= $MODERN_ERROR_CODES{$code}
ABresult
0X0
100
111
594 3 ▶ my $version = $code == $UNSUPPORTED_PROTOCOL_VERSION
Branch: 1 branch
BranchTrueFalseTotal
$code == $UNSUPPORTED_PROTOCOL_VERSION ? : 3 0 3
595 ? $self->_legacy_offer($error) : undef;
596 3 ▶ return await $answered unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 2 1 3
597 1 return await $self->_legacy_handshake($version);
598 }
599  
600 # A failure the server did not send is no sign of a legacy server, and an
601 # initialize sent after it would only fail with a less telling message. Over
602 # stdio that is a process gone; over HTTP a connection that could not be
603 # made or broke off, which the HTTP transport reports in a category of its
604 # own (connect, http, timeout, ...). What is left
605 # is an answer: a JSON-RPC error, a probe that got none in time, and an HTTP
606 # status with no JSON-RPC error in its body, which the HTTP transport reports
607 # without a category - the 400 or 404 a legacy server answers a request it
608 # does not know before initialize with, above all. A stdio probe that ran
609 # out of the request timeout got no answer in time too, only within a
610 # shorter limit than discover_timeout - unlike an HTTP timeout, which is a
611 # connection that never delivered a status.
612 my $stdio_timeout = $category eq 'timeout'
613 20 ▶ && $self->{transport}->isa('Net::Async::MCP::Transport::Stdio');
Condition66%2/3
$category eq 'timeout' && $self->{'transport'}->isa('Net::Async::MCP::Transport::Stdio')
!ll&&!rl&&r
1802
MC/DC50%1/2
$category eq 'timeout' && $self->{'transport'}->isa('Net::Async::MCP::Transport::Stdio')
$category eq 'timeout' $self->{'transport'}->isa('Net::Async::MCP::Transport::Stdio')
Truth table66%2/3
A && B
A= $category eq 'timeout'B= $self->{'transport'}->isa('Net::Async::MCP::Transport::Stdio')
ABresult
0X0
100
111
614 return await $answered
615 unless $category eq 'mcp' || $category eq 'mcp_probe_timeout' || $stdio_timeout
616 20 ▶ || ( $category eq '' && $self->{transport}->is_alive );
Branch: 1 branch
BranchTrueFalseTotal
unless $category eq "mcp" or $category eq "mcp_probe_timeout" or $stdio_timeout or $category eq "" and $self->{"transport"}->is_alive 3 17 20
Condition100%12/12
$category eq "mcp" or $category eq "mcp_probe_timeout" or $stdio_timeout or $category eq "" and $self->{"transport"}->is_alive
l!l&&r!l&&!r
1613
$category eq "mcp" or $category eq "mcp_probe_timeout" or $stdio_timeout
l!l&&r!l&&!r
1424
$category eq "mcp" or $category eq "mcp_probe_timeout"
l!l&&r!l&&!r
1226
$category eq "" and $self->{"transport"}->is_alive
!ll&&!rl&&r
121
MC/DC100%5/5
$category eq "mcp" or $category eq "mcp_probe_timeout" or $stdio_timeout or $category eq "" and $self->{"transport"}->is_alive
$category eq "mcp" $category eq "mcp_probe_timeout" $stdio_timeout $category eq "" $self->{"transport"}->is_alive
Truth table100%6/6
A or B or C or D and E
A= $category eq "mcp"B= $category eq "mcp_probe_timeout"C= $stdio_timeoutD= $category eq ""E= $self->{"transport"}->is_alive
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
617  
618 my $fallback = await $self->_legacy_handshake($LEGACY_PROTOCOL_VERSIONS[0])
619 17 ▶ ->followed_by(sub { Future->done( $_[0] ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
620 17 ▶ return $fallback->get if $fallback->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $fallback->is_done 15 2 17
621  
622 # A server that refused both handshakes speaks neither era, and only both
623 # refusals together say so. Anything else - an unspoken version, a process
624 # that died - is told best by its own message.
625 2 my ( $failure, $fallback_category, @details ) = $fallback->failure;
626 2 ▶ return await $fallback unless ( $fallback_category // '' ) eq 'mcp';
Branch: 1 branch
BranchTrueFalseTotal
unless ($fallback_category // "") eq "mcp" 2 0 2
Condition100%2/2
$fallback_category // ""
l!l
11
MC/DC100%1/1
$fallback_category // ""
$fallback_category
Truth table100%2/2
A
A= $fallback_category
Aresult
11
00
627 0 return await Future->fail("MCP handshake failed: server/discover ($why) and "
628 . "initialize ($failure) were both refused", $fallback_category, @details);
629 10 }
630  
631 # Private: the legacy revision to take up from a -32022 that lists no modern
632 # revision this client speaks - the newest legacy one both sides speak - or
633 # nothing.
634 3 ▶sub _legacy_offer {
Subroutine _legacy_offer: called CC=6 SCAR=18.6
635 3 my ( $self, $error ) = @_;
636 3 ▶ my $supported = ref $error->{data} eq 'HASH' ? $error->{data}{supported} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $error->{'data'} eq 'HASH' ? : 3 0 3
637 3 ▶ return unless ref $supported eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $supported eq "ARRAY" 0 3 3
638  
639 3 ▶ my %offered = map { $_ => 1 } grep { defined && !ref } @$supported;
Condition33%1/3
defined $_ && !ref($_)
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table33%1/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
640 3 for my $version (@LEGACY_PROTOCOL_VERSIONS) {
641 10 ▶ return $version if $offered{$version};
Branch: 1 branch
BranchTrueFalseTotal
if $offered{$version} 1 9 10
642 }
643 2 return;
644 }
645  
646 # Private: the legacy handshake, asking for $version: initialize, its answer
647 # checked and kept, then notifications/initialized. With renewal set it starts
648 # a new session of a connection already legacy, and a failure leaves the era
649 # as it is.
650 24 ▶async sub _legacy_handshake {
Subroutine _legacy_handshake: called CC=13 SCAR=26.4
651 24 my ( $self, $version, %args ) = @_;
652 24 my $transport = $self->{transport};
653  
654 # Set before the request goes out rather than once it is answered: the
655 # initialize is a legacy request itself, with no protocol keys in _meta, and
656 # a server may ping its client before it answers. The transport switches
657 # with it, to a binding where a session is opened by this very request.
658 24 $self->{era} = 'legacy';
659 24 ▶ $transport->start_legacy_session if $transport->can('start_legacy_session');
Branch: 1 branch
BranchTrueFalseTotal
if $transport->can("start_legacy_session") 11 13 24
660  
661 my $answered = await $self->_request('initialize', {
662 protocolVersion => $version,
663 capabilities => {},
664 clientInfo => $self->_client_info,
665 24 ▶ }, start_session => 1)->followed_by(sub { Future->done( $_[0] ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
666  
667 24 ▶ unless ( $answered->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($answered->is_done) 1 23 24
668 1 ▶ $self->_abandon_legacy unless $args{renewal};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"renewal"} 1 0 1
669 1 return await $answered;
670 }
671  
672 23 my $result = $answered->get;
673 23 ▶ my $agreed = ref $result eq 'HASH' ? $result->{protocolVersion} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 23 0 23
674  
675 # The specification leaves a client that does not speak the answered
676 # revision one thing to do, which is to stop. The transport is left alone
677 # for the caller to shut down.
678 23 ▶ unless ( defined $agreed && !ref $agreed
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $agreed and not ref $agreed and grep {$_ eq $agreed;} @LEGACY_PROTOCOL_VERSIONS) 1 22 23
Condition50%3/6
defined $agreed and not ref $agreed and grep {$_ eq $agreed;} @LEGACY_PROTOCOL_VERSIONS
!ll&&!rl&&r
0122
defined $agreed and not ref $agreed
!ll&&!rl&&r
0023
MC/DC33%1/3
defined $agreed and not ref $agreed and grep {$_ eq $agreed;} @LEGACY_PROTOCOL_VERSIONS
defined $agreed not ref $agreed grep {$_ eq $agreed;} @LEGACY_PROTOCOL_VERSIONS
Truth table50%2/4
A and not(B) and C
A= defined $agreedB= not ref $agreedC= grep {$_ eq $agreed;} @LEGACY_PROTOCOL_VERSIONS
ABCresult
0XX0
10X0
1100
1111
679 92 && grep { $_ eq $agreed } @LEGACY_PROTOCOL_VERSIONS ) {
680 1 ▶ $self->_abandon_legacy unless $args{renewal};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"renewal"} 1 0 1
681 1 ▶ croak "MCP server answered initialize with protocol version '"
Condition50%1/2
$agreed // "(none)"
l!l
10
MC/DC0%0/1
$agreed // "(none)"
$agreed
Truth table50%1/2
A
A= $agreed
Aresult
11
00
682 . ( $agreed // '(none)' ) . "', which this client does not speak (it "
683 . "speaks " . join(', ', @LEGACY_PROTOCOL_VERSIONS) . " over initialize)";
684 }
685  
686 22 $self->{protocol_version} = $agreed;
687 22 ▶ $self->{server_info} = $result->{serverInfo} // {};
Condition50%1/2
$result->{"serverInfo"} // {}
l!l
220
MC/DC0%0/1
$result->{"serverInfo"} // {}
$result->{"serverInfo"}
Truth table50%1/2
A
A= $result->{"serverInfo"}
Aresult
11
00
688 22 ▶ $self->{server_capabilities} = $result->{capabilities} // {};
Condition50%1/2
$result->{"capabilities"} // {}
l!l
220
MC/DC0%0/1
$result->{"capabilities"} // {}
$result->{"capabilities"}
Truth table50%1/2
A
A= $result->{"capabilities"}
Aresult
11
00
689 22 $self->{instructions} = $result->{instructions};
690  
691 # Before the notification rather than after it: everything after the
692 # initialize answer carries the agreed revision, notifications/initialized
693 # included.
694 22 ▶ $transport->set_legacy_protocol_version($agreed)
Branch: 1 branch
BranchTrueFalseTotal
if $transport->can("set_legacy_protocol_version") 11 11 22
695 if $transport->can('set_legacy_protocol_version');
696  
697 22 await $transport->send_notification('notifications/initialized');
698  
699 22 return $result;
700 10 }
701  
702 # Private: back out of a legacy handshake that did not come off, on the client
703 # and on a transport that switched binding for it.
704 2 ▶sub _abandon_legacy {
Subroutine _abandon_legacy: called CC=2 SCAR=7.0
705 2 my ( $self ) = @_;
706 2 delete $self->{era};
707 2 my $transport = $self->{transport};
708 2 ▶ $transport->end_legacy_session if $transport->can('end_legacy_session');
Branch: 1 branch
BranchTrueFalseTotal
if $transport->can("end_legacy_session") 0 2 2
709 2 return;
710 }
711  
712 # Private: a Future for a new legacy session, after a request that went out in
713 # $expired was answered with the end of that session. Shared by every request
714 # that needs it, so requests that all ran into the same expired session start
715 # one new session between them rather than one each; and one that ran into a
716 # session already replaced needs no new session at all, only to be sent again.
717 5 ▶sub _renew_session {
Subroutine _renew_session: called CC=5 SCAR=16.6
718 5 my ( $self, $expired ) = @_;
719 5 ▶ return $self->{session_renewal} if $self->{session_renewal};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_renewal"} 2 3 5
720  
721 3 my $transport = $self->{transport};
722 3 ▶ my $current = $transport->can('session_id') ? $transport->session_id : undef;
Branch: 1 branch
BranchTrueFalseTotal
$transport->can('session_id') ? : 3 0 3
723 3 ▶ return Future->done if defined $current && $current ne $expired;
Branch: 1 branch
BranchTrueFalseTotal
if defined $current and $current ne $expired 0 3 3
Condition33%1/3
defined $current and $current ne $expired
!ll&&!rl&&r
300
MC/DC0%0/2
defined $current and $current ne $expired
defined $current $current ne $expired
Truth table33%1/3
A and B
A= defined $currentB= $current ne $expired
ABresult
0X0
100
111
724  
725 3 my $renewal = $self->_legacy_handshake($self->{protocol_version}, renewal => 1);
726 3 $self->{session_renewal} = $renewal;
727  
728 3 weaken( my $weak_self = $self );
729 3 ▶ $renewal->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
730 3 ▶ my $self = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak_self or return 3 0 3
731 3 delete $self->{session_renewal};
732 3 });
733  
734 3 return $renewal;
735 }
736  
737 # Private: how many pages a list method walks before giving up. Only a cursor
738 # that comes round again proves a server is looping; one that keeps changing
739 # while never running out cannot be told apart from a genuinely long list, so
740 # there has to be an end to it somewhere.
741 10 my $MAX_LIST_PAGES = 100;
742  
743 # Private: every entry of a paginated list method, following nextCursor until
744 # the server stops handing one out. $key is the result key holding the entries,
745 # and each page is appended in the order the server sent it.
746 #
747 # Anything short of the full list is failed rather than returned. Handing back
748 # the pages collected so far would be indistinguishable from a server that
749 # really has that many entries, which is exactly the bug this walk exists to
750 # fix - it would just move from the first page to the hundredth.
751 30 ▶async sub _list_all {
Subroutine _list_all: called CC=6 SCAR=18.0
752 30 my ( $self, $method, $key ) = @_;
753  
754 30 my ( @entries, %seen, $cursor );
755  
756 30 for my $page ( 1 .. $MAX_LIST_PAGES ) {
757 34 ▶ my $result = await $self->_request($method,
Branch: 1 branch
BranchTrueFalseTotal
defined $cursor ? : 4 30 34
758 defined $cursor ? { cursor => $cursor } : undef);
759  
760 32 ▶ push @entries, @{ $result->{$key} // [] };
Condition50%1/2
$result->{$key} // []
l!l
320
MC/DC0%0/1
$result->{$key} // []
$result->{$key}
Truth table50%1/2
A
A= $result->{$key}
Aresult
11
00
761  
762 32 $cursor = $result->{nextCursor};
763 32 ▶ return \@entries unless defined $cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cursor 27 5 32
764  
765 # A cursor is a position in the list, so being handed one back that was
766 # already followed means the server is not moving. Left alone that spins
767 # for as long as the server keeps answering.
768 croak "MCP $method pagination: server repeated cursor '$cursor'"
769 5 ▶ if $seen{$cursor}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$cursor}++ 1 4 5
770 }
771  
772 0 croak "MCP $method pagination: server offered more than $MAX_LIST_PAGES pages";
773 10 }
774  
775 26 ▶async sub list_tools {
Subroutine list_tools: called CC=2 SCAR=7.1
Pod documented
776 26 my ( $self ) = @_;
777 26 my $tools = await $self->_list_all('tools/list', 'tools');
778  
779 # A fresh listing is the whole truth about the server's tools, so it replaces
780 # the cache rather than adding to it - and the whole truth is every page,
781 # which is why this runs on the merged list once the walk is through. A walk
782 # that failed part way leaves the cache as it was instead of replacing it with
783 # what happens to have arrived.
784 $self->{tool_header_params} = {
785 53 map { $_->{name} => _header_params($_->{inputSchema}) }
786 23 ▶ grep { ref $_ eq 'HASH' && defined $_->{name} } @$tools
Condition33%1/3
ref $_ eq 'HASH' && defined $_->{'name'}
!ll&&!rl&&r
0053
MC/DC0%0/2
ref $_ eq 'HASH' && defined $_->{'name'}
ref $_ eq 'HASH' defined $_->{'name'}
Truth table33%1/3
A && B
A= ref $_ eq 'HASH'B= defined $_->{'name'}
ABresult
0X0
100
111
787 };
788  
789 23 return $tools;
790 10 }
791  
792  
793 # Private: the arguments a server expects mirrored into Mcp-Param-{Name}
794 # headers - every property annotated with x-mcp-header, reachable from the
795 # schema root through a chain of "properties" keys and nothing else. Same walk
796 # as MCP::Tool::_header_params, whose result the server checks the headers
797 # against, down to sorting by key so both sides agree on order.
798 105 ▶sub _header_params {
Subroutine _header_params: called CC=8 SCAR=20.8
799 105 my ( $schema, $path ) = @_;
800 105 ▶ $path //= [];
Condition100%2/2
$path //= []
l!l
4857
MC/DC100%1/1
$path //= []
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
801  
802 105 ▶ return [] unless ref $schema eq 'HASH' && ref $schema->{properties} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" and ref $schema->{"properties"} eq "HASH" 82 23 105
Condition100%3/3
ref $schema eq "HASH" and ref $schema->{"properties"} eq "HASH"
!ll&&!rl&&r
18123
MC/DC100%2/2
ref $schema eq "HASH" and ref $schema->{"properties"} eq "HASH"
ref $schema eq "HASH" ref $schema->{"properties"} eq "HASH"
Truth table100%3/3
A and B
A= ref $schema eq "HASH"B= ref $schema->{"properties"} eq "HASH"
ABresult
0X0
100
111
803  
804 23 my $properties = $schema->{properties};
805 23 my @params;
806 23 for my $key (sort keys %$properties) {
807 48 my $property = $properties->{$key};
808 48 ▶ next unless ref $property eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $property eq "HASH" 0 48 48
809  
810 48 my @next = ( @$path, $key );
811 push @params, {
812 name => $property->{'x-mcp-header'},
813 path => \@next,
814 type => $property->{type} // '',
815 48 ▶ } if defined $property->{'x-mcp-header'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $property->{"x-mcp-header"} 22 26 48
Condition50%1/2
$property->{"type"} // ""
l!l
220
MC/DC0%0/1
$property->{"type"} // ""
$property->{"type"}
Truth table50%1/2
A
A= $property->{"type"}
Aresult
11
00
816 48 push @params, @{ _header_params($property, \@next) };
817 }
818  
819 23 return \@params;
820 }
821  
822 # Private: the argument a header parameter points at, or undef if the caller
823 # passed none. Same walk as MCP::Server::Transport::HTTP::_arg_value, which is
824 # what the server compares the header against.
825 20 ▶sub _arg_value {
Subroutine _arg_value: called CC=3 SCAR=11.0
826 20 my ( $arguments, $path ) = @_;
827  
828 20 my $value = $arguments;
829 20 for my $key (@$path) {
830 25 ▶ return undef unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 4 21 25
831 21 $value = $value->{$key};
832 }
833 16 return $value;
834 }
835  
836 # Private: an argument value in the form the server's _match_value compares it
837 # in. Getting this wrong is not a degradation but a rejection: the server
838 # answers -32020 (HEADER_MISMATCH) for a header that disagrees with the body.
839 8 ▶sub _header_value {
Subroutine _header_value: called CC=8 SCAR=21.0
840 8 my ( $type, $value ) = @_;
841  
842 8 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 3 5 8
843 # A JSON false reaches us either as \0, which JSON::MaybeXS encodes as
844 # false, or as a JSON::PP::Boolean. The latter knows it is false, but \0 is
845 # a reference and so a *true* Perl value: asking it directly would put
846 # "true" in the header while the body says false. Unwrap plain scalar
847 # references first, and let an object's own boolean overload speak.
848 3 ▶ $value = $$value while ref $value eq 'SCALAR' || ref $value eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "SCALAR" || ref $value eq "REF" and do { $value = $$value } 2 3 5
Condition66%2/3
ref $value eq "SCALAR" or ref $value eq "REF"
l!l&&r!l&&!r
203
MC/DC50%1/2
ref $value eq "SCALAR" or ref $value eq "REF"
ref $value eq "SCALAR" ref $value eq "REF"
Truth table66%2/3
A or B
A= ref $value eq "SCALAR"B= ref $value eq "REF"
ABresult
1X1
011
000
849 3 ▶ return $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 2 3
850 }
851  
852 # Compared numerically by the server, so the number decides, not its
853 # spelling. Anything that is not a number at all is passed through as text
854 # for the server to reject.
855 5 ▶ return 0 + $value if $type eq 'integer' && !ref $value && looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "integer" and not ref $value and &looks_like_number($value) 1 4 5
Condition66%4/6
$type eq "integer" and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
401
$type eq "integer" and not ref $value
!ll&&!rl&&r
401
MC/DC33%1/3
$type eq "integer" and not ref $value and &looks_like_number($value)
$type eq "integer" not ref $value &looks_like_number($value)
Truth table50%2/4
A and not(B) and C
A= $type eq "integer"B= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
856  
857 4 return "$value";
858 }
859  
860 # Private: the Mcp-Param-{Name} bindings for a tools/call, as a list of
861 # name/value pairs with each value already formatted the way the server
862 # compares it.
863 41 ▶async sub _tool_header_params {
Subroutine _tool_header_params: called CC=7 SCAR=19.5
864 41 my ( $self, $name, $arguments ) = @_;
865  
866 41 my $transport = $self->{transport};
867 41 ▶ return () unless $transport->can('mirrors_header_params')
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can("mirrors_header_params") and $transport->mirrors_header_params 35 6 41
Condition66%2/3
$transport->can("mirrors_header_params") and $transport->mirrors_header_params
!ll&&!rl&&r
0356
MC/DC50%1/2
$transport->can("mirrors_header_params") and $transport->mirrors_header_params
$transport->can("mirrors_header_params") $transport->mirrors_header_params
Truth table66%2/3
A and B
A= $transport->can("mirrors_header_params")B= $transport->mirrors_header_params
ABresult
0X0
100
111
868 && $transport->mirrors_header_params;
869  
870 6 ▶ unless (exists $self->{tool_header_params}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"tool_header_params"}{$name}) 5 1 6
871 # Calling a tool whose schema this client has never seen is not safe over a
872 # binding that mirrors arguments into headers: an annotated argument
873 # without its header is rejected outright, so the schema has to be fetched
874 # before the call goes out. A failure is deliberately swallowed - the tool
875 # may well have no annotated argument at all, and must not become
876 # uncallable because an unrelated tools/list failed. The request then goes
877 # out bare and the server decides.
878 5 ▶ await $self->list_tools->else(sub { Future->done });
Subroutine __ANON__: called CC=1 SCAR=0.0
879 }
880  
881 6 my @params;
882 6 ▶ for my $param (@{ $self->{tool_header_params}{$name} // [] }) {
Condition100%2/2
$self->{"tool_header_params"}{$name} // []
l!l
51
MC/DC100%1/1
$self->{"tool_header_params"}{$name} // []
$self->{"tool_header_params"}{$name}
Truth table100%2/2
A
A= $self->{"tool_header_params"}{$name}
Aresult
11
00
883 20 my $value = _arg_value($arguments, $param->{path});
884  
885 # A header the server does not expect is rejected exactly like a missing
886 # one, so an argument the caller left out gets no header.
887 20 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 12 8 20
888  
889 push @params, {
890 name => $param->{name},
891 8 value => _header_value($param->{type}, $value),
892 };
893 }
894  
895 6 return @params;
896 10 }
897  
898 41 ▶async sub call_tool {
Subroutine call_tool: called CC=3 SCAR=11.0
Pod documented
899 41 my ( $self, $name, $arguments ) = @_;
900 41 ▶ $arguments //= {};
Condition50%1/2
$arguments //= {}
l!l
410
MC/DC0%0/1
$arguments //= {}
$arguments
Truth table50%1/2
A
A= $arguments
Aresult
11
00
901  
902 41 my @header_params = await $self->_tool_header_params($name, $arguments);
903  
904 41 ▶ my $result = await $self->_request('tools/call',
Branch: 1 branch
BranchTrueFalseTotal
@header_params ? : 5 36 41
905 {
906 name => $name,
907 arguments => $arguments,
908 },
909 @header_params ? ( header_params => \@header_params ) : (),
910 );
911 30 return $result;
912 10 }
913  
914  
915 2 ▶async sub list_prompts {
Subroutine list_prompts: called CC=1 SCAR=0.0
Pod documented
916 2 my ( $self ) = @_;
917 2 return await $self->_list_all('prompts/list', 'prompts');
918 10 }
919  
920  
921 1 ▶async sub get_prompt {
Subroutine get_prompt: called CC=2 SCAR=7.0
Pod documented
922 1 my ( $self, $name, $arguments ) = @_;
923 1 ▶ my $result = await $self->_request('prompts/get', {
Condition50%1/2
$arguments // {}
l!l
10
MC/DC0%0/1
$arguments // {}
$arguments
Truth table50%1/2
A
A= $arguments
Aresult
11
00
924 name => $name,
925 arguments => $arguments // {},
926 });
927 1 return $result;
928 10 }
929  
930  
931 2 ▶async sub list_resources {
Subroutine list_resources: called CC=1 SCAR=0.0
Pod documented
932 2 my ( $self ) = @_;
933 2 return await $self->_list_all('resources/list', 'resources');
934 10 }
935  
936  
937 2 ▶async sub read_resource {
Subroutine read_resource: called CC=1 SCAR=0.0
Pod documented
938 2 my ( $self, $uri ) = @_;
939 2 my $result = await $self->_request('resources/read', {
940 uri => $uri,
941 });
942 1 return $result;
943 10 }
944  
945  
946 # Private: the notification a server answers a subscriptions/listen with in
947 # place of a response. Named to the transport rather than recognised by it -
948 # see subscriptions_listen.
949 10 my $SUBSCRIPTION_ACKNOWLEDGED = 'notifications/subscriptions/acknowledged';
950  
951 6 ▶async sub subscriptions_listen {
Subroutine subscriptions_listen: called CC=3 SCAR=11.0
Pod documented
952 6 my ( $self, $notifications ) = @_;
953  
954 # A legacy server sends its list_changed notifications without being asked,
955 # and has no subscriptions/listen to ask for them with.
956 6 ▶ croak "MCP subscriptions/listen needs protocol revision "
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_legacy 1 5 6
957 . "$DEFAULT_PROTOCOL_VERSION; this server speaks legacy revision "
958 . "$self->{protocol_version}, which has no subscriptions"
959 if $self->is_legacy;
960  
961 5 ▶ my $result = await $self->_request('subscriptions/listen',
Condition50%1/2
$notifications // {}
l!l
50
MC/DC0%0/1
$notifications // {}
$notifications
Truth table50%1/2
A
A= $notifications
Aresult
11
00
962 { notifications => $notifications // {} },
963 resolve_on_notification => $SUBSCRIPTION_ACKNOWLEDGED);
964 3 return $result;
965 10 }
966  
967  
968 5 ▶async sub subscriptions_stop {
Subroutine subscriptions_stop: called CC=2 SCAR=6.9
Pod documented
969 5 my ( $self, $subscription_id ) = @_;
970 5 $self->_ensure_transport;
971  
972 5 my $transport = $self->{transport};
973 5 ▶ return 0 unless $transport->can('stop_subscription');
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can("stop_subscription") 1 4 5
974 4 return $transport->stop_subscription($subscription_id);
975 10 }
976  
977  
978 3 ▶async sub ping {
Subroutine ping: called CC=2 SCAR=6.9
Pod documented
979 3 my ( $self ) = @_;
980 # The current MCP revision moved liveness to the transport level and has no
981 # client-addressable JSON-RPC "ping" request. Sending one would fail against
982 # MCP::Server >= 0.15 (InProcess errors with -32601); stdio only "succeeded"
983 # via an accidental legacy latch. Ask the transport whether it is still
984 # usable instead of reporting success unconditionally.
985 3 $self->_ensure_transport;
986 3 ▶ croak "MCP transport is not alive" unless $self->{transport}->is_alive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transport"}->is_alive 1 2 3
987 2 return 1;
988 10 }
989  
990  
991 25 ▶async sub shutdown {
Subroutine shutdown: called CC=3 SCAR=11.8
Pod documented
992 25 my ( $self ) = @_;
993 25 ▶ if ($self->{transport} && $self->{transport}->can('close')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transport"} and $self->{"transport"}->can("close")) 25 0 25
Condition33%1/3
$self->{"transport"} and $self->{"transport"}->can("close")
!ll&&!rl&&r
0025
MC/DC0%0/2
$self->{"transport"} and $self->{"transport"}->can("close")
$self->{"transport"} $self->{"transport"}->can("close")
Truth table33%1/3
A and B
A= $self->{"transport"}B= $self->{"transport"}->can("close")
ABresult
0X0
100
111
994 25 await $self->{transport}->close;
995 }
996 24 return 1;
997 10 }
998  
999  
1000  
1001 10 1;
1002  
1003 __END__
« t/bin/legacy_mcp_server.pl ↑ Summary blib/lib/Net/Async/MCP/Transport/HTTP.pm »