blib/lib/AnyEvent/MCP/Server/Connect.pm

filter: Statementstmt 30.1% 25 / 83 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 47.0% 8 / 17 Podpod 100% 4 / 4 totaltotal 24.0% 37 / 154 CC 20 SCAR 31.8
CC
20
Cov
20%
__ANON__
56.0
__ANON__
30.0
__ANON__
30.0
CRAP 24.2
« bin/mcp-vim ↑ Summary bin/mcp-multibroker »
1 =head1 NAME
2  
3 AnyEvent::MCP::Server::Connect - reverse-connecting MCP JSONL server
4  
5 =head1 DESCRIPTION
6  
7 This JSONL server connects to a multibroker's HTTP/extension listener using
8 C<connect =E<gt> "http://host:port/token/alias"> or
9 C<"https://host:port/token/alias">. It sends
10 C<["mcp-connect", token, alias]> and then ordinary MCP JSONL messages.
11 The TCP initiator remains the MCP server; the multibroker is the MCP client.
12  
13 This is useful when the MCP server must connect outward, rather than
14 exposing its own HTTP listener. The multibroker can accept connections
15 under an alias that becomes a tool-name prefix. For a standalone
16 server, C<AnyEvent::MCP::Server::Simple> handles the startup details;
17 use this object directly if your application already owns its event loop.
18  
19 use AnyEvent;
20 use AnyEvent::MCP::Server::Connect;
21  
22 my $server = AnyEvent::MCP::Server::Connect->new (
23 connect => "http://127.0.0.1:31740/TOKEN/files",
24 toolsets => [$tools],
25 );
26 $server->start;
27 AE::cv->recv;
28  
29 C<$tools> is an L<AnyEvent::MCP::Server::Toolset> registered by your
30 application. The URL identifies a raw extension listener, not an HTTP POST
31 endpoint. HTTPS verifies the server certificate and hostname before sending
32 the JSON hello. See
33 L<mcp-multibroker/Reverse connections> for setting up the other side.
34  
35 =head1 METHODS
36  
37 =over
38  
39 =item $server->start
40  
41 Attempt a connection immediately. Retry refused TCP connections every second,
42 up to sixty attempts. Other failures during connection setup, including failed
43 TLS verification, close the server immediately. Unexpected disconnects after
44 establishment start a new retry period. Requests are cancelled, not replayed.
45  
46 =item $server->close
47  
48 Cancel pending connections and retries, close streams and cancel requests.
49 Connecting remotely grants no managed child-shutdown authority.
50  
51 =back
52  
53 =cut
54  
55 package AnyEvent::MCP::Server::Connect;
56  
57 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use parent "AnyEvent::MCP::Server::JSONL";
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use AnyEvent::MCP::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Errno qw(ECONNREFUSED);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($class, %kv) = @_;
68  
69 0 my $self = $class->SUPER::new (%kv);
70 0 $self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}];
71 0 my $on_close = $self->{on_connection_close};
72 0 weaken (my $weak = $self);
73 0 ▶ $self->{on_connection_close} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
74 0 ▶ $on_close->(@_) if $on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_close 0 0 0
75 0 ▶ delete $weak->{tls_connecting} if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
76 0 ▶ $weak->start if $weak && !$weak->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $weak and not $weak->{"_closed"} 0 0 0
Condition0%0/3
$weak and not $weak->{"_closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and not $weak->{"_closed"}
$weak not $weak->{"_closed"}
Truth table0%0/3
A and not(B)
A= $weakB= not $weak->{"_closed"}
ABresult
0X0
100
111
77 0 };
78  
79 0 $self
80 }
81  
82 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self) = @_;
84  
85 0 ▶ return if $self->{retry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"retry"} 0 0 0
86 0 delete $self->{_closed};
87 0 $self->{attempts} = 0;
88 0 weaken $self;
89 0 ▶ $self->{retry} = AE::timer 0, 1, sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
90 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
91 0 ▶ return if $self->{connector} || $self->{tls_connecting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connector"} or $self->{"tls_connecting"} 0 0 0
Condition0%0/3
$self->{"connector"} or $self->{"tls_connecting"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"connector"} or $self->{"tls_connecting"}
$self->{"connector"} $self->{"tls_connecting"}
Truth table0%0/3
A or B
A= $self->{"connector"}B= $self->{"tls_connecting"}
ABresult
1X1
011
000
92  
93 0 ▶ if (++$self->{attempts} > 60) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"attempts"} > 60) 0 0 0
94 0 AE::log error => "MCP reverse connection: giving up after sixty attempts";
95 0 $self->close;
96 return
97 0 }
98  
99 0 my ($host, $port, $token, $alias, $scheme) = @{$self->{endpoint}};
100 0 ▶ $self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
101 0 ▶ delete $self->{connector} if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
102 0 my ($fh) = @_;
103 0 ▶ return unless $self && !$self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and not $self->{"_closed"} 0 0 0
Condition0%0/3
$self and not $self->{"_closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and not $self->{"_closed"}
$self not $self->{"_closed"}
Truth table0%0/3
A and not(B)
A= $selfB= not $self->{"_closed"}
ABresult
0X0
100
111
104  
105 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
106 0 ▶ if ($! == ECONNREFUSED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 111) 0 0 0
107 0 AE::log debug => "MCP reverse connection '$alias': $!";
108 return
109 0 }
110 0 AE::log error => "MCP reverse connection '$alias': TCP connection failed: $!";
111 0 $self->close;
112 return
113 0 }
114  
115 0 my $handle = $self->accept ($fh);
116 0 my $hello = JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n";
117 0 ▶ if ($scheme eq "https") {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'https') { } 0 0 0
118 0 $self->{tls_connecting} = 1;
119 0 ▶ $handle->on_starttls (sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
120 0 my ($handle, $ok, $error) = @_;
121 0 ▶ delete $self->{tls_connecting} if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
122 0 ▶ return unless $self && !$self->{_closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and not $self->{"_closed"} 0 0 0
Condition0%0/3
$self and not $self->{"_closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and not $self->{"_closed"}
$self not $self->{"_closed"}
Truth table0%0/3
A and not(B)
A= $selfB= not $self->{"_closed"}
ABresult
0X0
100
111
123 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
124 0 AE::log error => "MCP reverse connection '$alias': HTTPS handshake failed: $error";
125 0 $self->close;
126 return
127 0 }
128 0 delete $self->{retry};
129 0 $handle->push_write ($hello);
130 0 });
131 0 $handle->{peername} = $host;
132 0 $handle->starttls ("connect", { verify => 1, verify_peername => "https" });
133 } else {
134 0 delete $self->{retry};
135 0 $handle->push_write ($hello);
136 }
137 0 ▶ }, sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 };
139 }
140  
141 0 ▶sub request_idle_exit {
Subroutine request_idle_exit: not called CC=1 SCAR=6.9
Pod documented
142 undef
143 0 }
144  
145 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
146 0 my ($self) = @_;
147  
148 0 delete @$self{qw(retry connector)};
149 0 $self->SUPER::close;
150 }
151  
152 1 1;
« bin/mcp-vim ↑ Summary bin/mcp-multibroker »