| 1 |
root |
1.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 |
root |
1.4 |
C<connect =E<gt> "http://host:port/token/alias"> or |
| 9 |
|
|
C<"https://host:port/token/alias">. It sends |
| 10 |
root |
1.1 |
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 |
root |
1.3 |
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 |
root |
1.4 |
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 |
root |
1.3 |
L<mcp-multibroker/Reverse connections> for setting up the other side. |
| 34 |
|
|
|
| 35 |
root |
1.1 |
=head1 METHODS |
| 36 |
|
|
|
| 37 |
|
|
=over |
| 38 |
|
|
|
| 39 |
|
|
=item $server->start |
| 40 |
|
|
|
| 41 |
|
|
Attempt a connection immediately. Retry failures every second, up to sixty |
| 42 |
|
|
attempts, and start a new retry period after an established connection closes. |
| 43 |
|
|
Exhaustion closes the server. Outstanding requests are cancelled, not replayed. |
| 44 |
|
|
|
| 45 |
|
|
=item $server->close |
| 46 |
|
|
|
| 47 |
|
|
Cancel pending connections and retries, close streams and cancel requests. |
| 48 |
|
|
Connecting remotely grants no managed child-shutdown authority. |
| 49 |
|
|
|
| 50 |
|
|
=back |
| 51 |
|
|
|
| 52 |
|
|
=cut |
| 53 |
|
|
|
| 54 |
|
|
package AnyEvent::MCP::Server::Connect; |
| 55 |
|
|
|
| 56 |
|
|
use common::sense; |
| 57 |
|
|
use parent "AnyEvent::MCP::Server::JSONL"; |
| 58 |
|
|
use AnyEvent::MCP::Extension (); |
| 59 |
|
|
use AnyEvent::Socket (); |
| 60 |
|
|
use AnyEvent (); |
| 61 |
|
|
use JSON::XS (); |
| 62 |
|
|
use Scalar::Util qw(weaken); |
| 63 |
|
|
|
| 64 |
|
|
sub new { |
| 65 |
|
|
my ($class, %kv) = @_; |
| 66 |
|
|
|
| 67 |
|
|
my $self = $class->SUPER::new (%kv); |
| 68 |
|
|
$self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}]; |
| 69 |
|
|
my $on_close = $self->{on_connection_close}; |
| 70 |
|
|
weaken (my $weak = $self); |
| 71 |
|
|
$self->{on_connection_close} = sub { |
| 72 |
|
|
$on_close->(@_) if $on_close; |
| 73 |
|
|
$weak->start if $weak && !$weak->{_closed}; |
| 74 |
|
|
}; |
| 75 |
|
|
|
| 76 |
|
|
$self |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
sub start { |
| 80 |
|
|
my ($self) = @_; |
| 81 |
|
|
|
| 82 |
|
|
return if $self->{retry}; |
| 83 |
|
|
delete $self->{_closed}; |
| 84 |
|
|
$self->{attempts} = 0; |
| 85 |
|
|
weaken $self; |
| 86 |
|
|
$self->{retry} = AE::timer 0, 1, sub { |
| 87 |
|
|
return unless $self; |
| 88 |
|
|
return if $self->{connector}; |
| 89 |
|
|
|
| 90 |
|
|
if (++$self->{attempts} > 60) { |
| 91 |
|
|
AE::log warn => "MCP reverse connection: giving up after sixty attempts"; |
| 92 |
|
|
$self->close; |
| 93 |
|
|
return |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
root |
1.4 |
my ($host, $port, $token, $alias, $scheme) = @{$self->{endpoint}}; |
| 97 |
root |
1.1 |
$self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub { |
| 98 |
|
|
delete $self->{connector} if $self; |
| 99 |
|
|
my ($fh) = @_; |
| 100 |
|
|
return unless $self && !$self->{_closed}; |
| 101 |
|
|
|
| 102 |
|
|
unless ($fh) { |
| 103 |
root |
1.2 |
AE::log debug => "MCP reverse connection '$alias': $!"; |
| 104 |
root |
1.1 |
return |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
delete $self->{retry}; |
| 108 |
|
|
my $handle = $self->accept ($fh); |
| 109 |
root |
1.4 |
if ($scheme eq "https") { |
| 110 |
|
|
$handle->{peername} = $host; |
| 111 |
|
|
$handle->starttls ("connect", { verify => 1, verify_peername => "https" }); |
| 112 |
|
|
return if $handle->{mcp_closed}; |
| 113 |
|
|
} |
| 114 |
root |
1.1 |
$handle->push_write (JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n"); |
| 115 |
|
|
}, sub { 1 }; |
| 116 |
|
|
}; |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
sub request_idle_exit { |
| 120 |
|
|
undef |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
sub close { |
| 124 |
|
|
my ($self) = @_; |
| 125 |
|
|
|
| 126 |
|
|
delete @$self{qw(retry connector)}; |
| 127 |
|
|
$self->SUPER::close; |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
1; |