AnyEvent-MCP

 view release on metacpan or  search on metacpan

bin/mcp-vim  view on Meta::CPAN

use MIME::Base64 ();
use Socket ();
use POSIX ();
use JSON::XS ();

use AnyEvent ();
use AnyEvent::Socket ();
use AnyEvent::MCP ();
use AnyEvent::MCP::Config ();
use AnyEvent::MCP::Extension ();
use AnyEvent::MCP::Server::HTTP ();
use AnyEvent::MCP::Server::Toolset ();
use AnyEvent::MCP::Server::Permissions ();

use AnyEvent::MCP::Server::Simple qw(mcp_getoptions mcp_tool_help mcp_log mcp_log_request);

our $PORT;
our $NICK;

my $usage = "Usage: mcp-vim [OPTION]...\n";
my $server_options = mcp_getoptions
   {
      usage       => $usage,
      path        => "/",
      description => "Bridge live Vim buffers to MCP and browser WebSocket clients.\n"
         . "With --stdio, the broker also serves MCP on stdin/stdout; TCP stays active.",
      examples    => "  mcp-vim --name editor --bind 127.0.0.1:20792\n"
         . "  mcp-vim --name editor --print-url\n"
         . "  mcp-vim --auto-allow '*,write'",
   }
;
@ARGV and die $usage;

my $ask = delete $server_options->{ask};
my $name = $server_options->{name};
my $bind = $server_options->{bind};
my @tokens = @{$server_options->{tokens} || []};
my $connect = $server_options->{connect};
my $stdio = $server_options->{stdio} || !defined $connect && $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio";
!($stdio && defined $connect) or die "Cannot combine --stdio and --connect\n";

# private Vim/broker wire protocol (separate from the browser protocol)
my $vim_prot_major = 1;
my $vim_prot_minor = 0;

my $config = AnyEvent::MCP::Config::global_config ();
my %http = $config->http_settings ($name, $bind);
my $host = $http{host};
$PORT = $http{port};

$http{tokens} = \@tokens if @tokens;
$http{connect_tokens} = \@tokens if @tokens;
delete $http{token_users} if @tokens;
my @accepted_tokens = @{$http{tokens}};
my @connect_tokens = @{$http{connect_tokens}};
/\A[A-Za-z0-9_.~-]+\z/ or die "Invalid mcp-vim token\n" for @accepted_tokens;
my %connect_token = map { $_ => 1 } @connect_tokens;

$NICK //= (POSIX::uname)[1] || die "cannot determine mcp-vim hostname\n";

our $JSON_CODER = JSON::XS->new->utf8;

#############################################################################

my %xpcse;
my %vim;
my %target;

sub targets_changed {
   $_->send_json ([targets => keys %target]) for values %xpcse;
}

#############################################################################

sub base::emit {
   my ($self, $type, @args) = @_;

   eval {
      $type =~ y/-/_/;
      $type = "_on_$type";

      $self->$type (@args);
   };
   if ($@) {
      warn "error executing $type callback: $@";
   }
}

#############################################################################
# vim

@vim::ISA = base::;

sub vim::new {
   my ($class, $hdl, $hello) = @_;

   my ($requested, $instance_id) = @$hello[4, 5];

   (my $old) = grep
      defined $instance_id
      && defined $_->{instance_id}
      && $_->{requested} eq $requested
      && $_->{instance_id} eq $instance_id,
      values %vim;

   my $name = $old ? $old->{target} : $requested;
   $old->destroy if $old;

   unless ($old) {
      my $n = 2;
      $name = "$requested #" . $n++
         while exists $target{$name};
   }

   my $self = bless {
      ah          => $hdl,
      target      => $name,
      requested   => $requested,
      instance_id => $instance_id,
   }, $class;

   $vim{$self+0} = $self;

   AE::log debug => "new vim connection\n";

   # the line-based hello reader leaves subsequent bytes in the handle.
   $hdl->{json} = AnyEvent::MCP::json_class ()->new->utf8->max_depth (512);
   $hdl->rtimeout (0);

   {
      Scalar::Util::weaken (my $self = $self);

      $hdl->on_error (sub {
         $_[0]->destroy;
         $self->destroy;
      });
   }

   mcp_log "vim <$self->{target}> connected";
   $target{$self->{target}} = $self;
   targets_changed;

   $self->read_json;
   $self
}

sub vim::read_json {
   my ($self) = @_;

   Scalar::Util::weaken (my $weak_self = $self);

   $self->{ah}->push_read (json => sub {
      my (undef, $packet) = @_;

      my $self = $weak_self or return;

      $self->emit (@{ $packet->[1] });
      $self->read_json;
   });
}

sub vim::destroy {
   my ($self) = @_;

   delete $vim{$self+0}
      or return;

   $self->{ah}->destroy;

   if (my $save = delete $self->{save}) {
      $save->{xpcse}->cancel_save ($save);
   }

   delete $target{$self->{target}};
   targets_changed;

   $_->(0, "vim disconnected")
      for values %{ delete $self->{rpccb} || {} };

   mcp_log "vim <$self->{target}> disconnected";
}

sub vim::send_msg {
   my ($self, $msg) = @_;

   $self->{ah}->push_write (json => [0, $msg]);



( run in 1.124 second using v1.01-cache-2.11-cpan-234fa220139 )