ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Server/Connect.pm
Revision: 1.3
Committed: Wed Oct 21 17:28:16 2026 UTC (4 weeks ago) by root
Branch: MAIN
CVS Tags: rel-0_71, rel-0_61, rel-0_65, rel-0_6, rel-0_7, rel-0_62
Changes since 1.2: +21 -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     C<connect =E<gt> "http://host:port/token/alias">. It sends
9     C<["mcp-connect", token, alias]> and then ordinary MCP JSONL messages.
10     The TCP initiator remains the MCP server; the multibroker is the MCP client.
11    
12 root 1.3 This is useful when the MCP server must connect outward, rather than
13     exposing its own HTTP listener. The multibroker can accept connections
14     under an alias that becomes a tool-name prefix. For a standalone
15     server, C<AnyEvent::MCP::Server::Simple> handles the startup details;
16     use this object directly if your application already owns its event loop.
17    
18     use AnyEvent;
19     use AnyEvent::MCP::Server::Connect;
20    
21     my $server = AnyEvent::MCP::Server::Connect->new (
22     connect => "http://127.0.0.1:31740/TOKEN/files",
23     toolsets => [$tools],
24     );
25     $server->start;
26     AE::cv->recv;
27    
28     C<$tools> is an L<AnyEvent::MCP::Server::Toolset> registered by your
29     application. The URL identifies a raw-TCP extension listener, despite
30     its C<http://> spelling; it is not an HTTP POST endpoint. See
31     L<mcp-multibroker/Reverse connections> for setting up the other side.
32    
33 root 1.1 =head1 METHODS
34    
35     =over
36    
37     =item $server->start
38    
39     Attempt a connection immediately. Retry failures every second, up to sixty
40     attempts, and start a new retry period after an established connection closes.
41     Exhaustion closes the server. Outstanding requests are cancelled, not replayed.
42    
43     =item $server->close
44    
45     Cancel pending connections and retries, close streams and cancel requests.
46     Connecting remotely grants no managed child-shutdown authority.
47    
48     =back
49    
50     =cut
51    
52     package AnyEvent::MCP::Server::Connect;
53    
54     use common::sense;
55     use parent "AnyEvent::MCP::Server::JSONL";
56     use AnyEvent::MCP::Extension ();
57     use AnyEvent::Socket ();
58     use AnyEvent ();
59     use JSON::XS ();
60     use Scalar::Util qw(weaken);
61    
62     sub new {
63     my ($class, %kv) = @_;
64    
65     my $self = $class->SUPER::new (%kv);
66     $self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}];
67     my $on_close = $self->{on_connection_close};
68     weaken (my $weak = $self);
69     $self->{on_connection_close} = sub {
70     $on_close->(@_) if $on_close;
71     $weak->start if $weak && !$weak->{_closed};
72     };
73    
74     $self
75     }
76    
77     sub start {
78     my ($self) = @_;
79    
80     return if $self->{retry};
81     delete $self->{_closed};
82     $self->{attempts} = 0;
83     weaken $self;
84     $self->{retry} = AE::timer 0, 1, sub {
85     return unless $self;
86     return if $self->{connector};
87    
88     if (++$self->{attempts} > 60) {
89     AE::log warn => "MCP reverse connection: giving up after sixty attempts";
90     $self->close;
91     return
92     }
93    
94     my ($host, $port, $token, $alias) = @{$self->{endpoint}};
95     $self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub {
96     delete $self->{connector} if $self;
97     my ($fh) = @_;
98     return unless $self && !$self->{_closed};
99    
100     unless ($fh) {
101 root 1.2 AE::log debug => "MCP reverse connection '$alias': $!";
102 root 1.1 return
103     }
104    
105     delete $self->{retry};
106     my $handle = $self->accept ($fh);
107     $handle->push_write (JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n");
108     }, sub { 1 };
109     };
110     }
111    
112     sub request_idle_exit {
113     undef
114     }
115    
116     sub close {
117     my ($self) = @_;
118    
119     delete @$self{qw(retry connector)};
120     $self->SUPER::close;
121     }
122    
123     1;