AnyEvent-MCP

 view release on metacpan or  search on metacpan

lib/AnyEvent/MCP/Server.pm  view on Meta::CPAN

C<on_notify> is an optional C<$callback->($req, $message)> receiving decoded
outgoing notifications. Without it, notifications are dropped.

=item $req = $server->json_rpc ($json, $reply->($json), %context)

Like C<rpc>, but C<$json> and the response are UTF-8 encoded JSON strings.
The notification callback still receives decoded objects. Parse errors are
returned as JSON-RPC errors.

=item $server->dispatch ($req)

Dispatches an already constructed Request. Normally called by C<rpc>.
Subclasses can override this method while retaining JSON-RPC handling.

=back

=head1 EVENTS

=over

=item on_request => $callback->($server, $req)

Called before each dispatch attempt, including permission retries. This
synchronous hook can inspect the request or refresh application state.
It does not replace dispatch; avoid side effects that must happen only once.

=item on_tools_list => $callback->($server, $req)

Replaces the default Toolset listing. C<$callback> completes C<$req>.

=item on_tools_call => $callback->($server, $req, $args)

Replaces the default Toolset lookup and invocation. C<$args> is also available
as C<< $req->{args} >>. C<$callback> completes C<$req>.

=item on_error => $callback->($server, $req, $error)

Reports an exception caught during synchronous dispatch. A tool exception
becomes a text tool-error result; other exceptions become internal JSON-RPC
errors. Exceptions after a reply are propagated, as are exceptions from
asynchronous application callbacks.

=back

=cut

package AnyEvent::MCP::Server;

use common::sense;
use AnyEvent::MCP ();
use AnyEvent::MCP::Server::Request ();
use AnyEvent ();
use JSON::XS ();
use Scalar::Util qw(weaken);

our @ISA = qw(AnyEvent::MCP::Base);

sub new {
   my $class = shift;

   my $self = $class->SUPER::new (
      toolsets     => [],
      server_info  => { name => "AnyEvent::MCP", version => "$AnyEvent::MCP::VERSION" },
      capabilities => { tools => {} },
      versions     => [qw(2026-07-28 2025-11-25 2025-06-18 2025-03-26)],
      @_,
   );

   $self->{instructions} = AnyEvent::MCP::_clean_text $self->{instructions}
      if exists $self->{instructions};

   my $instructions = $self->{instructions};
   my $custom = exists $self->{custom_instructions}
      ? AnyEvent::MCP::_clean_text $self->{custom_instructions} : "";

   if ($self->{auto_instructions} && length $instructions) {
      require AnyEvent::MCP::Server::Toolset;

      my $name = "meta-instructions";
      !grep { exists $_->{tool}{$name} } @{$self->{toolsets}}
         or die "duplicate MCP tool '$name'\n";

      my $full = join "\n\n", grep length, $custom, $instructions;
      my $toolset = AnyEvent::MCP::Server::Toolset->new;
      $toolset->tool (meta_instructions =>
         "Server instructions. Call for full text if truncated.\n\n$full",
         { type => "object", properties => {} },
         sub { $_[0]->reply ({ content => [{ type => "text", text => $full }] }) },
      );
      $self->{toolsets} = [@{$self->{toolsets}}, $toolset];

      my $short = exists $self->{short_instructions}
         ? AnyEvent::MCP::_clean_text $self->{short_instructions} : "";
      $self->{instructions} = length $custom || length $short
         ? (join "\n\n", grep length, $custom, $short, "Full instructions: meta-instructions tool description.")
         : $instructions;

   } elsif (length $custom) {
      $self->{instructions} = join "\n\n", grep length, $custom, $instructions;
   }

   $self
}

sub _rpc_error {
   my ($self, $reply, $id, $code, $message, $data) = @_;

   my $error = { code => $code, message => $message };
   $error->{data} = $data if defined $data;

   $reply->({ jsonrpc => "2.0", id => $id, error => $error });
   undef
}

sub json_rpc {
   my ($self, $json, $reply, @context) = @_;

   my $send = sub { $reply->(defined $_[0] ? JSON::XS::encode_json $_[0] : undef) };
   my $message = eval { JSON::XS::decode_json $json };

   return $self->_rpc_error ($send, undef, -32700, "Parse error")



( run in 1.081 second using v1.01-cache-2.11-cpan-60884a8df76 )