ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Server/Connect.pm
Revision: 1.5
Committed: Wed Nov 4 23:58:49 2026 UTC (2 weeks ago) by root
Branch: MAIN
CVS Tags: rel-0_91, rel-0_81, rel-0_82, rel-0_8, rel-0_9, HEAD
Changes since 1.4: +31 -9 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 use common::sense;
58 use parent "AnyEvent::MCP::Server::JSONL";
59 use AnyEvent::MCP::Extension ();
60 use AnyEvent::Socket ();
61 use AnyEvent ();
62 use JSON::XS ();
63 use Errno qw(ECONNREFUSED);
64 use Scalar::Util qw(weaken);
65
66 sub new {
67 my ($class, %kv) = @_;
68
69 my $self = $class->SUPER::new (%kv);
70 $self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}];
71 my $on_close = $self->{on_connection_close};
72 weaken (my $weak = $self);
73 $self->{on_connection_close} = sub {
74 $on_close->(@_) if $on_close;
75 delete $weak->{tls_connecting} if $weak;
76 $weak->start if $weak && !$weak->{_closed};
77 };
78
79 $self
80 }
81
82 sub start {
83 my ($self) = @_;
84
85 return if $self->{retry};
86 delete $self->{_closed};
87 $self->{attempts} = 0;
88 weaken $self;
89 $self->{retry} = AE::timer 0, 1, sub {
90 return unless $self;
91 return if $self->{connector} || $self->{tls_connecting};
92
93 if (++$self->{attempts} > 60) {
94 AE::log error => "MCP reverse connection: giving up after sixty attempts";
95 $self->close;
96 return
97 }
98
99 my ($host, $port, $token, $alias, $scheme) = @{$self->{endpoint}};
100 $self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub {
101 delete $self->{connector} if $self;
102 my ($fh) = @_;
103 return unless $self && !$self->{_closed};
104
105 unless ($fh) {
106 if ($! == ECONNREFUSED) {
107 AE::log debug => "MCP reverse connection '$alias': $!";
108 return
109 }
110 AE::log error => "MCP reverse connection '$alias': TCP connection failed: $!";
111 $self->close;
112 return
113 }
114
115 my $handle = $self->accept ($fh);
116 my $hello = JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n";
117 if ($scheme eq "https") {
118 $self->{tls_connecting} = 1;
119 $handle->on_starttls (sub {
120 my ($handle, $ok, $error) = @_;
121 delete $self->{tls_connecting} if $self;
122 return unless $self && !$self->{_closed};
123 unless ($ok) {
124 AE::log error => "MCP reverse connection '$alias': HTTPS handshake failed: $error";
125 $self->close;
126 return
127 }
128 delete $self->{retry};
129 $handle->push_write ($hello);
130 });
131 $handle->{peername} = $host;
132 $handle->starttls ("connect", { verify => 1, verify_peername => "https" });
133 } else {
134 delete $self->{retry};
135 $handle->push_write ($hello);
136 }
137 }, sub { 1 };
138 };
139 }
140
141 sub request_idle_exit {
142 undef
143 }
144
145 sub close {
146 my ($self) = @_;
147
148 delete @$self{qw(retry connector)};
149 $self->SUPER::close;
150 }
151
152 1;