NAME

AnyEvent::MCP::Server::JSONL - MCP over a newline-delimited byte stream

DESCRIPTION

Use this transport for stdio servers or an existing stream connection. It implements AnyEvent::MCP::Server over newline-delimited UTF-8 JSON-RPC: one line is one complete message. Keep diagnostic output off the protocol stream; a banner or log message on stdout is not a JSON-RPC response.

Unlike HTTP, replies can complete in a different order from requests. The JSON-RPC ids associate them with the correct calls.

EXAMPLE

An application that already owns its tool registry and lifetime can serve one stdio MCP connection:

use AnyEvent;
use AnyEvent::MCP::Server::JSONL;

my $server = AnyEvent::MCP::Server::JSONL->new (
   toolsets => [$tools],
);
$server->accept (\*STDIN, \*STDOUT);
AE::cv->recv;

$tools is an AnyEvent::MCP::Server::Toolset. Keep stdout reserved for MCP messages and write diagnostics to stderr. The application is responsible for its event loop and termination. If you do not need to manage those details, use "mcp_stdio" in AnyEvent::MCP::Server::Simple. The matching client transport is AnyEvent::MCP::Client::JSONL.

METHODS

$handle = $server->accept ($fh, $fh_out)

Starts handling $fh. If $fh_out is supplied, it receives outgoing messages; otherwise $fh is used in both directions. Both handles belong to the server until the connection closes.

With separate input/output handles, input EOF waits for pending replies and drains the output before closing. EOF on a bidirectional socket closes it immediately and cancels outstanding requests.

max_size limits a buffered incoming message, defaulting to 1 MiB.

$server->close

Closes the server's connections and cancels outstanding requests.

EVENTS

on_connection_error => $callback->($server, $handle, $message)

Called for stream errors before closing the connection.

on_connection_close => $callback->($server, $handle)

Called after a connection has closed.

on_close => $callback->($server)

Called by close after closing all connections.

MANAGED SHUTDOWN

$server->request_idle_exit

Requests a managed shutdown, once, from a spawned stdio multibroker child whose peer advertised anyevent-mcp/multibroker version 1. The application decides when it is idle. The method returns true when it sends the notification, or undef when there is no eligible connection or it has already sent one. The broker can ignore the notification. When it sends shutdown, the server answers before draining the stream and closing its connections. Closing the server lets Simple's blocking starter return; an embedding application controls its own process lifetime.

on_idle_shutdown, if supplied, is $callback-($server)> returning true to accept or false to decline a late shutdown request (for example, if application work has resumed). Outstanding MCP requests also prevent acceptance.