| 1 | package Net::Async::MCP::Transport::HTTP; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Streamable HTTP MCP transport via Net::Async::HTTP | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use parent 'IO::Async::Notifier'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Encode qw( encode is_utf8 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use MIME::Base64 qw( encode_base64 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Scalar::Util qw( blessed weaken ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # The methods whose name-ish parameter is mirrored into Mcp-Name, and the | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # parameter it is taken from. Same table as MCP::Client and MCP::Server's HTTP | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # transport, which compares the header against exactly this field of the body. | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | my %NAME_PARAM = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 'prompts/get' => 'name', | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 'resources/read' => 'uri', | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 'tools/call' => 'name', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | my $DEFAULT_STALL_TIMEOUT = 60; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # Where a subscription's acknowledgement carries the id every later message on | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # that stream is tagged with, and the only handle there is on a running | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # subscription. Written out for the same reason the protocol version key above | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # is: it is a name on the wire, not something a locally installed L<MCP> has | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # any say in. | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | my $SUBSCRIPTION_ID_META = 'io.modelcontextprotocol/subscriptionId'; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 37 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 37 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | $self->{url} = delete $params->{url} | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 37 | ▶ | or croak "url is required"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # exists, not defined: an explicit undef is a caller switching a timeout off, | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # and has to survive the default applied below. | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 37 | for my $key (qw( headers timeout stall_timeout )) { | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 111 | ▶ | $self->{$key} = delete $params->{$key} if exists $params->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | $self->{stall_timeout} = $DEFAULT_STALL_TIMEOUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 37 | ▶ | unless exists $self->{stall_timeout}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 37 | $self->{next_id} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 37 | $self->{json} = JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # Every request that has not finished, by JSON-RPC id: the Future the caller | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # holds, the exchange it is being answered over, and the subscription it | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # opened where it opened one. Both halves are needed here rather than only | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # the one the caller sees, because leaving the loop has to settle the Future | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # and close the stream, and neither can be reached from the other. | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 37 | $self->{pending} = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Subscription id to the request id of the exchange carrying it, which is | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # what turns the one handle a caller has on a running subscription into the | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # stream to close. | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 37 | $self->{subscriptions} = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 37 | $self->SUPER::_init($params); | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 42 | ▶ | sub configure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
configure: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 42 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 42 | ▶ | if (exists $params{url}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->{url} = delete $params{url}; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 42 | ▶ | $self->{headers} = delete $params{headers} if exists $params{headers}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | $self->{on_notification} = delete $params{on_notification} | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 42 | ▶ | if exists $params{on_notification}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $self->{on_subscription_end} = delete $params{on_subscription_end} | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 42 | ▶ | if exists $params{on_subscription_end}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 42 | for my $key (qw( timeout stall_timeout )) { | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 84 | ▶ | next unless exists $params{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | $self->{$key} = delete $params{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | # Only once this transport has joined a loop is there an HTTP client to | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # reconfigure; before that _add_to_loop picks the values up itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $self->{http}->configure($key => $self->{$key}) if $self->{http}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 42 | $self->SUPER::configure(%params); | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | ▶ | sub _add_to_loop { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | my ( $self, $loop ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | $self->SUPER::_add_to_loop($loop); | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | require Net::Async::HTTP; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | my $http = Net::Async::HTTP->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | max_connections_per_host => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | timeout => $self->{timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | stall_timeout => $self->{stall_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | $self->{http} = $http; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | $self->add_child($http); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | ▶ | sub _remove_from_loop { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_remove_from_loop: called CC=8 SCAR=21.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | my ( $self, $loop ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # Leaving the loop takes the HTTP client along, it being a child of this | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # transport, and with it every connection an answer could still arrive over. | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # So nothing still open can ever be settled from outside again, and it has to | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # be settled here or it stays pending for the rest of the program - the same | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # lesson as L<Net::Async::MCP::Transport::Stdio>'s own _remove_from_loop. | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # The caller's Future is failed rather than completed: nothing here saw a | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # response go missing, what it knows is that none can reach it any more. Its | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # exchange is cancelled afterwards, which closes the stream; doing it the | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # other way round would settle the Future as cancelled and lose the reason | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # with it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | for my $id (keys %{ $self->{pending} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | my $pending = delete $self->{pending}{$id} or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | delete $self->{subscriptions}{ $pending->{subscription} } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | if defined $pending->{subscription}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | my $outcome = $pending->{outcome}; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | $outcome->fail("MCP HTTP transport left the loop before the request was answered") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$outcome and not $outcome->is_ready
MC/DC50%1/2
$outcome and not $outcome->is_ready
$outcome
not $outcome->is_ready
Truth table66%2/3
A and not(B)
A= $outcomeB= not $outcome->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | if $outcome && !$outcome->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | my $exchange = $pending->{exchange}; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | ▶ | $exchange->cancel if $exchange && !$exchange->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$exchange and not $exchange->is_ready
MC/DC0%0/2
$exchange and not $exchange->is_ready
$exchange
not $exchange->is_ready
Truth table33%1/3
A and not(B)
A= $exchangeB= not $exchange->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 6 | $self->SUPER::_remove_from_loop($loop); | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # The Future a caller is handed. An IO::Async::Future wherever there is a loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # to take one from, which is every transport that has an HTTP client to send | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # with: only the plain Future knows no ->await, and a caller's ->get on one | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # that is not ready yet dies instead of running the loop until it is. | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 40 | ▶ | sub _new_future { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_future: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 40 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 40 | ▶ | my $loop = $self->loop or return Future->new; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | return $loop->new_future; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 29 | ▶ | sub send_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_request: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 29 | my ( $self, $method, $params, %options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 29 | my $id = ++$self->{next_id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 29 | ▶ | my $request = { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | id => $id, | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | defined $params ? ( params => $params ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 29 | my $body = $self->{json}->encode($request); | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 29 | require HTTP::Request; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | my $http_req = HTTP::Request->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | POST => $self->{url}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 29 | [ $self->_standard_headers($method, $params, %options) ], | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | $body, | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # The Future the caller holds, and deliberately not the one the exchange | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # below ends in. What answers a request is its message, and the end of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # body that message arrived in is a different moment: a server is free to | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # hold an event stream open after its response, and for a subscription it | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # always does - that request is answered by a notification and the stream | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # then runs for as long as the subscription does. Waiting for the body to | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # end would mean waiting for the stall timeout in the first case and forever | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # in the second. | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 29 | my $outcome = $self->_new_future; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 29 | my $pending = $self->{pending}{$id} = { outcome => $outcome }; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | my $exchange = $self->{http}->do_request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | request => $http_req, | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 28 | ▶ | on_header => sub { $self->_response_reader($outcome, $id, \%options, @_) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 19 | ▶ | )->then(sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 19 | my ( $result ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # Both body readers end in the Future for the JSON-RPC outcome, and | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # Net::Async::HTTP passes whatever they return through as the result of | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # its own. A response object instead means the body never reached them: | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # a redirect, which it consumes itself rather than handing over - it does | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # not follow one for a POST, so this is where a redirected endpoint ends | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # up, and the status line is all there is to report. | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 19 | ▶ | return $result if blessed($result) && $result->isa('Future'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($result) and $result->isa("Future")
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Future")
builtin::blessed($result)
$result->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Future")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | return $self->_handle_response($result); | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 29 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Held on the transport because the caller no longer holds it: a Future | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # nothing refers to is collected mid-flight, and this one is the request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 29 | $pending->{exchange} = $exchange; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # These callbacks live on the two Futures the pending entry holds, so | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # capturing the transport strongly would close a cycle no refcount breaks. | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # It is never undef where it matters: a transport with a request in flight is | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # in a loop, and the loop holds it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 29 | weaken( my $weak_self = $self ); | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # Cancelling the answer cancels the request, which over Streamable HTTP | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # means closing the stream it would have been answered on - there is no | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # notifications/cancelled in this binding. Reached through the pending table | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # rather than by capturing the exchange, which would be the other half of | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # the same cycle. | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | $outcome->on_cancel(sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | my $self = $weak_self or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $exchange = ( $self->{pending}{$id} // {} )->{exchange} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{"pending"}{$id} // {}
MC/DC0%0/1
$self->{"pending"}{$id} // {}
$self->{"pending"}{$id}
Truth table0%0/2
A
A= $self->{"pending"}{$id}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | $exchange->cancel unless $exchange->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 29 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 28 | ▶ | $exchange->on_ready(sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 28 | my ( $finished ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 28 | ▶ | if ( my $self = $weak_self ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 28 | my $ended = delete $self->{pending}{$id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # Whether the subscription is still registered tells who ended it: | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # stop_subscription and close take the entry out before they close the | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # stream, so an end that came from the server's side - a stream the | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # server closed, a connection that failed - reaches here with the entry | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # standing, and that is the end on_subscription_end reports. An end this | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # client caused itself has nobody waiting to be told. | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 28 | ▶ | if ($ended && defined $ended->{subscription} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$ended and defined $ended->{"subscription"} and delete $self->{"subscriptions"}{$ended->{"subscription"}}
$ended and defined $ended->{"subscription"}
MC/DC100%3/3
$ended and defined $ended->{"subscription"} and delete $self->{"subscriptions"}{$ended->{"subscription"}}
$ended
defined $ended->{"subscription"}
delete $self->{"subscriptions"}{$ended->{"subscription"}}
Truth table100%4/4
A and B and C
A= $endedB= defined $ended->{"subscription"}C= delete $self->{"subscriptions"}{$ended->{"subscription"}}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | && delete $self->{subscriptions}{ $ended->{subscription} }) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | $self->maybe_invoke_event(on_subscription_end => $ended->{subscription}); | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # An exchange that ends after the caller was answered has nothing left to | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # tell it. This is every subscription: it was settled by its | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # acknowledgement, and the stream ending afterwards - closed by | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # L</stop_subscription>, by the server, or by the connection failing - is | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # reported through L</on_subscription_end> above rather than through this | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # Future, which was settled long before. | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 28 | ▶ | return if $outcome->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Otherwise the exchange is where the answer comes from: an HTTP error, a | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # body that could not be read, a stream that ended without saying | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # anything, or a plain JSON response that was only complete at the end. | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 15 | ▶ | return $outcome->cancel if $finished->is_cancelled; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 15 | ▶ | return $outcome->fail($finished->failure) if $finished->is_failed; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 11 | return $outcome->done($finished->get); | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 29 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 29 | return $outcome; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub send_notification { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_notification: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my ( $self, $method, $params ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | my $request = { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | defined $params ? ( params => $params ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my $body = $self->{json}->encode($request); | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | require HTTP::Request; | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | my $http_req = HTTP::Request->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | POST => $self->{url}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | [ $self->_standard_headers($method, $params) ], | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | $body, | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | return $self->{http}->do_request(request => $http_req)->then(sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my ( $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | return $self->_handle_notification_response($response); | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | $self->stop_subscription($_) for keys %{ $self->{subscriptions} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | return Future->done; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 12 | ▶ | sub stop_subscription { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop_subscription: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 12 | my ( $self, $subscription_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 12 | ▶ | return 0 unless defined $subscription_id; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 12 | my $id = delete $self->{subscriptions}{$subscription_id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | 12 | ▶ | return 0 unless defined $id; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 5 | $self->_close_stream($id); | |||||||||||||||||||||||||||||||||||||||||||||
| 286 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | ▶ | sub is_alive { 1 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_alive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 7 | ▶ | sub mirrors_header_params { 1 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mirrors_header_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # Net::Async::HTTP hands the response header over as soon as it has it and | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # takes the callback for the body in return, so this is where the content type | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # decides how the body is read: an event stream carries notifications the | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # server sends before its response and has to be read as it arrives, while | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # every other body says nothing until it is complete and is judged whole. | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 28 | ▶ | sub _response_reader { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_response_reader: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 28 | my ( $self, $outcome, $id, $options, $header ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 28 | ▶ | return $self->_sse_reader($outcome, $id, $options) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$header->is_success and ($header->content_type // "") =~ m[^text/event-stream]i
$header->content_type // ""
MC/DC50%1/2
$header->is_success and ($header->content_type // "") =~ m[^text/event-stream]i
$header->is_success
$header->content_type
Truth table66%2/3
A and B
A= $header->is_successB= $header->content_type
| |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | if $header->is_success | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | && ($header->content_type // '') =~ m{^text/event-stream}i; | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 27 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 27 | ▶ | return $header->add_content(@_) if @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 13 | return $self->_handle_response($header); | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # Reads an SSE body as it arrives. Returns the callback Net::Async::HTTP feeds | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # the body to: once per chunk of bytes as it lands, and once with no arguments | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # at the end of the stream, where whatever it returns becomes the result of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # request's Future - here the Future the JSON-RPC outcome is reported through. | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # A chunk ends wherever the network put it, mid-line and mid-character | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # included, so nothing leaves the buffer before its newline has arrived and | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # nothing is decoded before its event is complete. | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 26 | ▶ | sub _sse_reader { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sse_reader: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 26 | my ( $self, $outcome, $id, $options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # A reader driven directly rather than from send_request answers nobody, but | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # still has to have something to settle: _handle_sse_response reads a whole | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # body through here and hands back what this ends up with. | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 26 | ▶ | $outcome //= $self->_new_future; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$outcome //= $self->_new_future
MC/DC0%0/2
$outcome //= $self->_new_future
$outcome
$self->_new_future
Truth table66%2/3
A //= B
A= $outcomeB= $self->_new_future
| |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # What the client said settles this request in place of a response. Named by | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # the client and not worked out here: which method that is is MCP semantics, | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # and a transport reading the request's own method and deciding for itself | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # would be guessing where it was told. | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 26 | ▶ | my $resolve_on = ( $options // {} )->{resolve_on_notification}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options // {}
MC/DC100%1/1
$options // {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 26 | my $buffer = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | 26 | my @data; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # One line of the stream, its newline already taken off. A blank line ends | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # an event, a line opening with a colon is a comment - the shape of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | # keep-alives a server sends to hold an idle stream open - and every other | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # line is a field, of which this client reads only "data". | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 91 | ▶ | my $line = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 91 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 91 | $text =~ s/\r\z//; | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 91 | ▶ | if (length $text) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 42 | ▶ | return if $text =~ /^:/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 40 | my ( $field, $value ) = split /:/, $text, 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | 40 | ▶ | return unless defined $field && $field eq 'data'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $field and $field eq "data"
MC/DC50%1/2
defined $field and $field eq "data"
defined $field
$field eq "data"
Truth table66%2/3
A and B
A= defined $fieldB= $field eq "data"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 35 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 35 | $value =~ s/^ //; | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | 35 | push @data, $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 35 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 49 | my $event = join "\n", @data; | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | 49 | @data = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | 49 | ▶ | return unless length $event; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | # Not every event is JSON-RPC this client can use, and one that is not is | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # no reason to abandon a stream that still owes it a response. | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 34 | my $decoded = eval { $self->{json}->decode($event) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | 34 | ▶ | return unless ref $decoded eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # An event that answers no request is a notification - unless it is the | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # one the client named as this request's answer, which is how a | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # subscription is acknowledged: it settles the request rather than being | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # delivered as an event of its own, since it is what the caller asked for | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # and not something that happened while it waited. | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 34 | ▶ | unless (exists $decoded->{id}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | return $self->_open_subscription($outcome, $id, $decoded) | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | if defined $resolve_on | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | && !$outcome->is_ready | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 24 | ▶ | && ( $decoded->{method} // '' ) eq $resolve_on; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%6/8
defined $resolve_on and not $outcome->is_ready and ($decoded->{"method"} // "") eq $resolve_on
defined $resolve_on and not $outcome->is_ready
$decoded->{"method"} // ""
MC/DC66%2/3
defined $resolve_on and not $outcome->is_ready and ($decoded->{"method"} // "") eq $resolve_on
defined $resolve_on
not $outcome->is_ready
$decoded->{"method"}
Truth table75%3/4
A and not(B) and C
A= defined $resolve_onB= not $outcome->is_readyC= $decoded->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 14 | return $self->maybe_invoke_event(on_notification => $decoded); | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # An event that does answer settles the request the moment it lands rather | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # than at the end of the body: a stream holds the answer to exactly one | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # request, so the first one is kept and what comes after cannot make an | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # earlier answer untrue. An id without a result or an error is a | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # server-initiated request, which this client does not answer, so it is | ||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # dropped. | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | $self->_settle_response($outcome, $decoded) | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | if !$outcome->is_ready | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 10 | ▶ | && (exists $decoded->{result} || exists $decoded->{error}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not $outcome->is_ready and exists $decoded->{"result"} || exists $decoded->{"error"}
exists $decoded->{"result"} or exists $decoded->{"error"}
MC/DC0%0/3
not $outcome->is_ready and exists $decoded->{"result"} || exists $decoded->{"error"}
not $outcome->is_ready
exists $decoded->{"result"}
exists $decoded->{"error"}
Truth table50%2/4
not(A) and B or C
A= not $outcome->is_readyB= exists $decoded->{"result"}C= exists $decoded->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 26 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 248 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 248 | ▶ | unless (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | # The end of the stream terminates whatever it interrupted: a server | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # that closed right behind its last data line still said it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 14 | ▶ | $line->($buffer) if length $buffer; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 14 | $buffer = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | 14 | $line->(''); | |||||||||||||||||||||||||||||||||||||||||||||
| 402 | 14 | return $self->_sse_result($outcome, $resolve_on); | |||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 234 | my ( $chunk ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | 234 | ▶ | return unless defined $chunk; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # The JSON decoder has utf8 => 1 and wants bytes, which is what | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # Net::Async::HTTP hands over. Characters would be decoded a second time | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | # and every non-ASCII event lost to the failed decode above. | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 234 | ▶ | $chunk = encode('UTF-8', $chunk) if is_utf8($chunk); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 234 | $buffer .= $chunk; | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | 234 | ▶ | $line->($1) while $buffer =~ s/^([^\n]*)\n//; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 234 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | 26 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # The answer an event carrying one settles the request with, taken as it lands | ||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # rather than held until the stream ends. | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 10 | ▶ | sub _settle_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_settle_response: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 10 | my ( $self, $outcome, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 10 | ▶ | if (defined(my $err = $data->{error})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 3 | my @failure = $self->_jsonrpc_error_failure($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 427 | 3 | ▶ | return $outcome->fail(@failure) if @failure; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | return $outcome->fail($self->_foreign_error_message($err)); | |||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 7 | return $outcome->done($data->{result}); | |||||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # The acknowledgement of a subscription, which is the answer to the request | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # that opened it: its params are what the caller is handed - the subscription | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # id and the notification types the server honoured - and the id is what the | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # stream is filed under, since closing that stream is the only way to end the | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # subscription again. | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 10 | ▶ | sub _open_subscription { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open_subscription: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 10 | my ( $self, $outcome, $id, $acknowledgement ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 10 | ▶ | my $params = ref $acknowledgement->{params} eq 'HASH' ? $acknowledgement->{params} : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 10 | ▶ | my $meta = ref $params->{_meta} eq 'HASH' ? $params->{_meta} : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 10 | my $subscription_id = $meta->{$SUBSCRIPTION_ID_META}; | |||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # A subscription with no id is one nothing can address: it cannot be stopped | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | # and a close cannot reach it either, so handing it to a caller would be | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | # handing over a stream with no way of ending it. The stream goes with the | ||||||||||||||||||||||||||||||||||||||||||||||
| 449 | # refusal, which is what ends it on this side. | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 10 | ▶ | unless (defined $subscription_id && length $subscription_id) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $subscription_id and length $subscription_id
MC/DC50%1/2
defined $subscription_id and length $subscription_id
defined $subscription_id
length $subscription_id
Truth table66%2/3
A and B
A= defined $subscription_idB= length $subscription_id
| |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | $outcome->fail("MCP HTTP subscription acknowledged without a subscription id"); | |||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | $self->_close_stream($id); | |||||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 9 | $self->{subscriptions}{$subscription_id} = $id; | |||||||||||||||||||||||||||||||||||||||||||||
| 457 | 9 | ▶ | $self->{pending}{$id}{subscription} = $subscription_id if $self->{pending}{$id}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 9 | return $outcome->done($params); | |||||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # Closing the stream a request is running on, which is what both cancelling a | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | # request and unsubscribing come down to over this binding: Net::Async::HTTP | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | # closes the connection when its request Future is cancelled. | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 6 | ▶ | sub _close_stream { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_close_stream: called CC=5 SCAR=17.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 6 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 6 | ▶ | return 0 unless defined $id; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 6 | ▶ | my $exchange = ( $self->{pending}{$id} // {} )->{exchange} or return 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"pending"}{$id} // {}
MC/DC0%0/1
$self->{"pending"}{$id} // {}
$self->{"pending"}{$id}
Truth table50%1/2
A
A= $self->{"pending"}{$id}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 6 | ▶ | return 0 if $exchange->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 6 | $exchange->cancel; | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # What a finished SSE stream leaves the exchange with. The answer it carried is | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # already the caller's, so handing the same Future back keeps the exchange | ||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # resolving with exactly what the request resolved with; only a stream that | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | # said nothing has anything of its own to report. | ||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 14 | ▶ | sub _sse_result { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sse_result: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 14 | my ( $self, $outcome, $resolve_on ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 14 | ▶ | return $outcome if $outcome->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | # A subscription is never answered with a response, so reporting a missing | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | # one would send a caller looking for something the server was never going | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | # to send. | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 3 | ▶ | return Future->fail("MCP HTTP stream ended before the subscription was acknowledged") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 489 | if defined $resolve_on; | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 2 | return Future->fail("MCP HTTP no JSON-RPC response in SSE stream"); | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # The metadata headers every POST carries. They are read back out of the body | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # instead of out of transport state so that the two cannot drift apart: the | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # server compares header against body and answers -32020 when they differ. | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 45 | ▶ | sub _standard_headers { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_standard_headers: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 45 | my ( $self, $method, $params, %options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 45 | ▶ | $params = {} unless ref $params eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 45 | my @headers = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 503 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 'Accept' => 'application/json, text/event-stream', | ||||||||||||||||||||||||||||||||||||||||||||||
| 505 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||
| 507 | # Absent for a request built without _meta, and for a notification sent | ||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # through send_notification with no params at all - this client sends none | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | # itself, but the method stays open to callers. Sending a made up version | ||||||||||||||||||||||||||||||||||||||||||||||
| 510 | # would be worse than sending none: the server only compares what it gets. | ||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 45 | ▶ | my $version = ($params->{_meta} // {})->{'io.modelcontextprotocol/protocolVersion'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params->{"_meta"} // {}
MC/DC100%1/1
$params->{"_meta"} // {}
$params->{"_meta"}
Truth table100%2/2
A
A= $params->{"_meta"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 45 | ▶ | push @headers, 'MCP-Protocol-Version' => $version if defined $version; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 45 | push @headers, 'Mcp-Method' => $method; | |||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 45 | ▶ | if (my $key = $NAME_PARAM{$method}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 20 | ▶ | push @headers, 'Mcp-Name' => $self->_encode_header($params->{$key} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{$key} // ""
MC/DC0%0/1
$params->{$key} // ""
$params->{$key}
Truth table50%1/2
A
A= $params->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||
| 520 | # Tool arguments annotated with x-mcp-header, already resolved and formatted | ||||||||||||||||||||||||||||||||||||||||||||||
| 521 | # by the client: which arguments these are and what they look like is MCP | ||||||||||||||||||||||||||||||||||||||||||||||
| 522 | # semantics, only the wire form is this transport's business. They travel | ||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # through the same sentinel encoding as Mcp-Name, which is what the server | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | # undoes before comparing. | ||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 45 | ▶ | for my $param (@{ $options{header_params} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"header_params"} // []
MC/DC100%1/1
$options{"header_params"} // []
$options{"header_params"}
Truth table100%2/2
A
A= $options{"header_params"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 526 | push @headers, | ||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 11 | ▶ | "Mcp-Param-$param->{name}" => $self->_encode_header($param->{value} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$param->{"value"} // ""
MC/DC0%0/1
$param->{"value"} // ""
$param->{"value"}
Truth table50%1/2
A
A= $param->{"value"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # The caller's own headers go first and the derived ones after, so an | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # Authorization can be added while an Mcp-Method cannot be taken over. | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 45 | return ( $self->_caller_headers(@headers), @headers ); | |||||||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||||||
| 535 | # The headers configured on this transport, minus every field the request | ||||||||||||||||||||||||||||||||||||||||||||||
| 536 | # derives from its body. Dropping them is not the same as ordering them: | ||||||||||||||||||||||||||||||||||||||||||||||
| 537 | # HTTP::Headers keeps a field given twice in one list as two values of one | ||||||||||||||||||||||||||||||||||||||||||||||
| 538 | # header rather than letting the later win, so a colliding caller header would | ||||||||||||||||||||||||||||||||||||||||||||||
| 539 | # travel alongside the derived one and diverge from the body just as visibly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 45 | ▶ | sub _caller_headers { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_caller_headers: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 45 | my ( $self, @derived ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 45 | my $headers = $self->{headers}; | |||||||||||||||||||||||||||||||||||||||||||||
| 544 | 45 | ▶ | return () unless ref $headers eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 3 | my %derived; | |||||||||||||||||||||||||||||||||||||||||||||
| 547 | 3 | ▶ | for (my $i = 0; $i < @derived; $i += 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 13 | $derived{ lc $derived[$i] } = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 4 | return map { $_ => $headers->{$_} } | |||||||||||||||||||||||||||||||||||||||||||||
| 552 | 3 | grep { !$derived{ lc $_ } } | |||||||||||||||||||||||||||||||||||||||||||||
| 553 | sort keys %$headers; | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||
| 556 | # A header value that is not printable ASCII travels base64 encoded in a | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # sentinel, as does one that already looks like the sentinel itself, which | ||||||||||||||||||||||||||||||||||||||||||||||
| 558 | # would otherwise be decoded by the server into something the body never said. | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 31 | ▶ | sub _encode_header { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 31 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 31 | ▶ | return $value | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value =~ /^[\x20-\x7e]*\z/ and not $value =~ /^=\?base64\?.*\?=$/
MC/DC100%2/2
$value =~ /^[\x20-\x7e]*\z/ and not $value =~ /^=\?base64\?.*\?=$/
$value =~ /^[\x20-\x7e]*\z/
not $value =~ /^=\?base64\?.*\?=$/
Truth table100%3/3
A and not(B)
A= $value =~ /^[\x20-\x7e]*\z/B= not $value =~ /^=\?base64\?.*\?=$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 563 | if $value =~ /^[\x20-\x7e]*\z/ && $value !~ /^=\?base64\?.*\?=$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 5 | return '=?base64?' . encode_base64(encode('UTF-8', $value), '') . '?='; | |||||||||||||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 30 | ▶ | sub _handle_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_response: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 30 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 30 | my $status = $response->code; | |||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 30 | ▶ | unless ($response->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # An MCP server renders JSON-RPC errors with a non-2xx status taken from | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | # the request context (400 for a rejected _meta, 403 for insufficient | ||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # scope, 404 for METHOD_NOT_FOUND), so the body carries the real error and | ||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # must win over the HTTP status. Only a non-2xx without a JSON-RPC error | ||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # body is an HTTP-level problem. | ||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 11 | ▶ | if (my @failure = $self->_jsonrpc_error_from_body($response)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 4 | return Future->fail(@failure); | |||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 7 | return Future->fail("MCP HTTP error: " . $response->status_line); | |||||||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 19 | ▶ | my $content_type = $response->content_type // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->content_type // ""
MC/DC0%0/1
$response->content_type // ""
$response->content_type
Truth table50%1/2
A
A= $response->content_type
| |||||||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||||||
| 588 | # charset => 'none' undoes Content-Encoding but leaves the body as UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 589 | # bytes, which is what the JSON decoder below expects; letting | ||||||||||||||||||||||||||||||||||||||||||||||
| 590 | # decoded_content apply the charset too would decode text/event-stream twice. | ||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 19 | ▶ | if ($content_type =~ m{^application/json}i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 16 | return $self->_handle_json_response($response->decoded_content(charset => 'none')); | |||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 594 | elsif ($content_type =~ m{^text/event-stream}i) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 3 | return $self->_handle_sse_response($response->decoded_content(charset => 'none')); | |||||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||
| 598 | # 202 Accepted with no body (for notifications/responses) | ||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | ▶ | if ($status == 202) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | return Future->done(undef); | |||||||||||||||||||||||||||||||||||||||||||||
| 601 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 0 | return Future->fail("MCP HTTP unexpected content-type: $content_type"); | |||||||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # A notification is answered with a status and nothing this client reads, so | ||||||||||||||||||||||||||||||||||||||||||||||
| 607 | # the status is all there is to judge - 202 Accepted with an empty body is the | ||||||||||||||||||||||||||||||||||||||||||||||
| 608 | # normal case. Deliberately not routed through _handle_response, which would | ||||||||||||||||||||||||||||||||||||||||||||||
| 609 | # fail exactly that empty body as invalid JSON. | ||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 4 | ▶ | sub _handle_notification_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_notification_response: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 4 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 4 | ▶ | return Future->done if $response->is_success; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 3 | ▶ | if (my @failure = $self->_jsonrpc_error_from_body($response)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 2 | return Future->fail(@failure); | |||||||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 1 | return Future->fail("MCP HTTP error: " . $response->status_line); | |||||||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 14 | ▶ | sub _jsonrpc_error_from_body { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_jsonrpc_error_from_body: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 14 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 14 | my $body = eval { $response->decoded_content(charset => 'none') }; | |||||||||||||||||||||||||||||||||||||||||||||
| 626 | 14 | ▶ | return () unless defined $body && length $body; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $body and length $body
MC/DC50%1/2
defined $body and length $body
defined $body
length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
| |||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 11 | my $data = eval { $self->{json}->decode($body) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 629 | 11 | return $self->_jsonrpc_error_failure($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||||
| 632 | # The Future->fail arguments for a decoded body that carries a JSON-RPC error | ||||||||||||||||||||||||||||||||||||||||||||||
| 633 | # object, and the empty list for anything else. Not every JSON error body is a | ||||||||||||||||||||||||||||||||||||||||||||||
| 634 | # JSON-RPC one: an MCP server answers a bad method with | ||||||||||||||||||||||||||||||||||||||||||||||
| 635 | # {error => 'Method not allowed'}, and a gateway in between may invent its own | ||||||||||||||||||||||||||||||||||||||||||||||
| 636 | # shape, so the shape is checked before it is read as an object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 637 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 638 | # The message alone cannot carry a code to switch on or an error->{data} to | ||||||||||||||||||||||||||||||||||||||||||||||
| 639 | # read, so the raw error object travels with it as the details of a failure in | ||||||||||||||||||||||||||||||||||||||||||||||
| 640 | # category "mcp". The message stays the first element, so a caller reading the | ||||||||||||||||||||||||||||||||||||||||||||||
| 641 | # failure in scalar context sees exactly what it saw before. | ||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 18 | ▶ | sub _jsonrpc_error_failure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_jsonrpc_error_failure: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 18 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 18 | ▶ | return () unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 14 | my $err = $data->{error}; | |||||||||||||||||||||||||||||||||||||||||||||
| 648 | 14 | ▶ | return () unless ref $err eq 'HASH' && defined $err->{code}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $err eq "HASH" and defined $err->{"code"}
MC/DC50%1/2
ref $err eq "HASH" and defined $err->{"code"}
ref $err eq "HASH"
defined $err->{"code"}
Truth table66%2/3
A and B
A= ref $err eq "HASH"B= defined $err->{"code"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||
| 650 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 10 | ▶ | "MCP error $err->{code}: " . ($err->{message} // '(no message)'), | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$err->{"message"} // "(no message)"
MC/DC100%1/1
$err->{"message"} // "(no message)"
$err->{"message"}
Truth table100%2/2
A
A= $err->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 652 | mcp => $err, | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||||||
| 656 | # A body that says "error" in a shape this client cannot read as JSON-RPC still | ||||||||||||||||||||||||||||||||||||||||||||||
| 657 | # says the request failed. Report it as a failure carrying whatever text it | ||||||||||||||||||||||||||||||||||||||||||||||
| 658 | # holds rather than reaching into it as if it were an object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 3 | ▶ | sub _foreign_error_message { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_foreign_error_message: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 3 | my ( $self, $err ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 3 | ▶ | my $text = ref $err ? eval { $self->{json}->encode($err) } // 'unknown error' : $err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
eval {
do {
$self->{"json"}->encode($err)
}
} // "unknown error"
MC/DC0%0/1
eval {
do {
$self->{"json"}->encode($err)
}
} // "unknown error"
eval {
do {
$self->{"json"}->encode($err)
}
}
Truth table0%0/2
A
A= eval {
do {
$self->{"json"}->encode($err)
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 3 | return "MCP HTTP error response: $text"; | |||||||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||||||
| 666 | 16 | ▶ | sub _handle_json_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_json_response: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 16 | my ( $self, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 16 | my $data = eval { $self->{json}->decode($body) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 670 | 16 | ▶ | return Future->fail("MCP HTTP invalid JSON: $@") if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 16 | ▶ | return Future->fail("MCP HTTP invalid response") unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 16 | ▶ | if (defined(my $err = $data->{error})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 4 | my @failure = $self->_jsonrpc_error_failure($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 675 | 4 | ▶ | return Future->fail(@failure) if @failure; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 2 | return Future->fail($self->_foreign_error_message($err)); | |||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 12 | return Future->done($data->{result}); | |||||||||||||||||||||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||
| 682 | # A whole SSE body in hand rather than a stream to read from, which is the | ||||||||||||||||||||||||||||||||||||||||||||||
| 683 | # same events through the same reader, all at once. Only a caller holding a | ||||||||||||||||||||||||||||||||||||||||||||||
| 684 | # complete response gets here: a streamed one is read by _sse_reader itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 3 | ▶ | sub _handle_sse_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_sse_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 3 | my ( $self, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 3 | my $read = $self->_sse_reader; | |||||||||||||||||||||||||||||||||||||||||||||
| 689 | 3 | $read->($body); | |||||||||||||||||||||||||||||||||||||||||||||
| 690 | 3 | return $read->(); | |||||||||||||||||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||
| 696 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||