AnyEvent-MCP
view release on metacpan or search on metacpan
lib/AnyEvent/MCP/Client.pm view on Meta::CPAN
=head1 NAME
AnyEvent::MCP::Client - asynchronous MCP client protocol
=head1 SYNOPSIS
use AnyEvent::MCP::Client::HTTP;
my $client = AnyEvent::MCP::Client::HTTP->new (
url => $url, bearer => $token,
);
$client->call (echo => { text => "hello" }, sub {
my ($success, $result) = @_;
...
});
=head1 DESCRIPTION
This is the common protocol implementation used by
L<AnyEvent::MCP::Client::HTTP> and L<AnyEvent::MCP::Client::JSONL>. Choose a
transport, keep the client object alive, and run an AnyEvent event loop.
You can call a tool without connecting explicitly: the client connects and
discovers or initializes the server as needed. Concurrent requests share that
work instead of starting independent connections.
An interrupted connection does not mean the server did nothing. A tool might
have completed its side effects just before the connection failed. For that
reason, the client never replays a tool call automatically. Applications that
want retries must decide whether the operation is safe to repeat.
Modern clients use discovery and per-request metadata. Legacy clients use
initialization and ordinary request/response. Legacy server-initiated requests
are accepted on JSONL; legacy HTTP GET streams are not implemented.
For a runnable call, see L<AnyEvent::MCP::Client::HTTP/FIRST CALL> when
connecting to an HTTP endpoint or
L<AnyEvent::MCP::Client::JSONL/FIRST CALL> when launching a subprocess.
Both use C<call> from this module. In its callback, C<$success> reports
JSON-RPC/transport success; a tool can still return C<isError> true.
Keep the client alive while its callbacks are pending.
=head1 METHODS
=over
=item $client = AnyEvent::MCP::Client->new (%config)
Normally construct a transport subclass. Its constructor accepts these
common options as well as the transport-specific options:
=over
=item client_info => { name => $name, version => $version }
Advertised client identity. Defaults to C<AnyEvent::MCP> and this
distribution's version.
=item client_capabilities => { %capabilities }
Supported client interactions, defaulting to an empty hash. Advertising a
capability does not install a handler; supply C<on_request> when needed.
=item versions => [$version...]
Supported protocol revisions in preference order. Defaults to
C<2026-07-28>, C<2025-11-25>, C<2025-06-18> and C<2025-03-26>.
Here, modern means the 2026 discovery/input-challenge protocol; legacy
means the 2025 initialization protocol.
=item request_timeout => $seconds
Optional overall deadline for C<rpc> and C<call>, including connection
and user input. Omitted or zero means no overall deadline. This is separate
from HTTP's inactivity timeout.
=item max_rounds => $count
Maximum input-challenge rounds handled by C<call>, defaulting to 32.
=item name => $name
Optional local configuration name, independent of C<client_info>.
HTTP loads C<[client NAME]> defaults only when this is supplied;
JSONL does not load transport defaults.
=back
Event callbacks are also constructor options; see L</EVENTS>.
=item $client->connect ($done->($success, $result))
Discovers or initializes the server. On success C<$result> is undef.
Any JSON-RPC error from discovery falls back to legacy initialization.
Transport failures do not trigger fallback.
After connecting, C<protocol_version>, C<server_info>, C<server_capabilities>
and C<instructions> are available directly in the client hash.
( run in 1.277 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )