ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MCP/lib/AnyEvent/MCP/Server/Connect.pm
Revision: 1.6
Committed: Sun Nov 22 00:38:02 2026 UTC (35 hours, 11 minutes ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.5 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 root 1.6 Connected TCP sockets use SO_KEEPALIVE with the operating system's default
46     probe timings.
47 root 1.1
48     =item $server->close
49    
50     Cancel pending connections and retries, close streams and cancel requests.
51     Connecting remotely grants no managed child-shutdown authority.
52    
53     =back
54    
55     =cut
56    
57     package AnyEvent::MCP::Server::Connect;
58    
59     use common::sense;
60     use parent "AnyEvent::MCP::Server::JSONL";
61     use AnyEvent::MCP::Extension ();
62     use AnyEvent::Socket ();
63     use AnyEvent ();
64     use JSON::XS ();
65 root 1.5 use Errno qw(ECONNREFUSED);
66 root 1.1 use Scalar::Util qw(weaken);
67    
68     sub new {
69     my ($class, %kv) = @_;
70    
71     my $self = $class->SUPER::new (%kv);
72     $self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}];
73     my $on_close = $self->{on_connection_close};
74     weaken (my $weak = $self);
75     $self->{on_connection_close} = sub {
76     $on_close->(@_) if $on_close;
77 root 1.5 delete $weak->{tls_connecting} if $weak;
78 root 1.1 $weak->start if $weak && !$weak->{_closed};
79     };
80    
81     $self
82     }
83    
84     sub start {
85     my ($self) = @_;
86    
87     return if $self->{retry};
88     delete $self->{_closed};
89     $self->{attempts} = 0;
90     weaken $self;
91     $self->{retry} = AE::timer 0, 1, sub {
92     return unless $self;
93 root 1.5 return if $self->{connector} || $self->{tls_connecting};
94 root 1.1
95     if (++$self->{attempts} > 60) {
96 root 1.5 AE::log error => "MCP reverse connection: giving up after sixty attempts";
97 root 1.1 $self->close;
98     return
99     }
100    
101 root 1.4 my ($host, $port, $token, $alias, $scheme) = @{$self->{endpoint}};
102 root 1.1 $self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub {
103     delete $self->{connector} if $self;
104     my ($fh) = @_;
105     return unless $self && !$self->{_closed};
106    
107     unless ($fh) {
108 root 1.5 if ($! == ECONNREFUSED) {
109     AE::log debug => "MCP reverse connection '$alias': $!";
110     return
111     }
112     AE::log error => "MCP reverse connection '$alias': TCP connection failed: $!";
113     $self->close;
114 root 1.1 return
115     }
116    
117     my $handle = $self->accept ($fh);
118 root 1.6 $handle->keepalive (1);
119 root 1.5 my $hello = JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n";
120 root 1.4 if ($scheme eq "https") {
121 root 1.5 $self->{tls_connecting} = 1;
122     $handle->on_starttls (sub {
123     my ($handle, $ok, $error) = @_;
124     delete $self->{tls_connecting} if $self;
125     return unless $self && !$self->{_closed};
126     unless ($ok) {
127     AE::log error => "MCP reverse connection '$alias': HTTPS handshake failed: $error";
128     $self->close;
129     return
130     }
131     delete $self->{retry};
132     $handle->push_write ($hello);
133     });
134 root 1.4 $handle->{peername} = $host;
135     $handle->starttls ("connect", { verify => 1, verify_peername => "https" });
136 root 1.5 } else {
137     delete $self->{retry};
138     $handle->push_write ($hello);
139 root 1.4 }
140 root 1.1 }, sub { 1 };
141     };
142     }
143    
144     sub request_idle_exit {
145     undef
146     }
147    
148     sub close {
149     my ($self) = @_;
150    
151     delete @$self{qw(retry connector)};
152     $self->SUPER::close;
153     }
154    
155     1;