| 1 | package Net::Async::MCP::Server::Transport::Stdio; | ||
| 2 | # ABSTRACT: Stdio transport for Net::Async::MCP::Server | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use parent 'IO::Async::Notifier'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | use IO::Async::Loop; | ||
| 10 | use IO::Async::Stream; | ||
| 11 | use IO::Handle; | ||
| 12 | use JSON::MaybeXS qw( encode_json decode_json ); | ||
| 13 | use Scalar::Util qw( weaken ); | ||
| 14 | |||
| 15 | our $VERSION = '0.003'; | ||
| 16 | |||
| 17 | |||
| 18 | sub new { | ||
| 19 | my ( $class, %params ) = @_; | ||
| 20 | my $server = delete $params{server}; | ||
| 21 | my $self = $class->SUPER::new(%params); | ||
| 22 | $self->{server} = $server if defined $server; | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub configure { | ||
| 27 | my ( $self, %params ) = @_; | ||
| 28 | for my $key (qw( server read_handle write_handle )) { | ||
| 29 | $self->{$key} = delete $params{$key} if exists $params{$key}; | ||
| 30 | } | ||
| 31 | |||
| 32 | $self->{read_handle} //= \*STDIN; | ||
| 33 | $self->{write_handle} //= \*STDOUT; | ||
| 34 | |||
| 35 | $self->SUPER::configure(%params); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | sub server { | ||
| 40 | my ( $self ) = @_; | ||
| 41 | return $self->{server}; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub read_handle { | ||
| 46 | my ( $self ) = @_; | ||
| 47 | return $self->{read_handle}; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub write_handle { | ||
| 52 | my ( $self ) = @_; | ||
| 53 | return $self->{write_handle}; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub handle_requests { | ||
| 58 | my ( $self ) = @_; | ||
| 59 | |||
| 60 | my $loop = $self->loop; | ||
| 61 | unless ($loop) { | ||
| 62 | $loop = IO::Async::Loop->new; | ||
| 63 | $loop->add($self); | ||
| 64 | } | ||
| 65 | |||
| 66 | # The write handle is usually a pipe to the client, which Perl block | ||
| 67 | # buffers: a finished response would sit in the buffer while the client | ||
| 68 | # waits for it and this transport waits for the next request, until 4-8K | ||
| 69 | # had piled up or the process exited. Set here rather than in configure | ||
| 70 | # because this is the point where the transport takes over a handle the | ||
| 71 | # caller owns, and once per run rather than per response. | ||
| 72 | my $wh = $self->write_handle; | ||
| 73 | $wh->autoflush(1); | ||
| 74 | $wh->flush; # nothing may still sit in the PerlIO buffer ahead of the stream | ||
| 75 | |||
| 76 | # An in-memory handle has no descriptor to register with the loop and | ||
| 77 | # stays on the blocking print. | ||
| 78 | my $fd = fileno($wh); | ||
| 79 | my $wstream; | ||
| 80 | if ( defined $fd and $fd >= 0 ) { | ||
| 81 | $wstream = IO::Async::Stream->new( write_handle => $wh ); | ||
| 82 | $self->add_child($wstream); | ||
| 83 | $self->{_write_stream} = $wstream; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $eof_f = $loop->new_future; | ||
| 87 | |||
| 88 | # The stream holds this callback and this transport holds the stream as a | ||
| 89 | # child, so capturing $self strongly here would close a cycle no refcount | ||
| 90 | # ever breaks. Weakening costs nothing: for as long as the callback can | ||
| 91 | # fire at all, the loop is holding the transport for us. | ||
| 92 | weaken( my $weak_self = $self ); | ||
| 93 | |||
| 94 | my $stream = IO::Async::Stream->new( | ||
| 95 | read_handle => $self->read_handle, | ||
| 96 | close_on_read_eof => 0, | ||
| 97 | on_read => sub { | ||
| 98 | my ( undef, $buffref, $eof ) = @_; | ||
| 99 | my $self = $weak_self or return 0; | ||
| 100 | return $self->_on_read( $buffref, $eof ); | ||
| 101 | }, | ||
| 102 | on_read_eof => sub { | ||
| 103 | $eof_f->done unless $eof_f->is_ready; | ||
| 104 | }, | ||
| 105 | ); | ||
| 106 | |||
| 107 | $self->add_child($stream); | ||
| 108 | |||
| 109 | # A tool that dies no longer reaches here: handle() catches it and | ||
| 110 | # answers with an isError result (resources/prompts: -32603). Only an | ||
| 111 | # exception outside those evals, e.g. from list_tools, still propagates | ||
| 112 | # out of handle() and thus out of here — and the stream has to come back | ||
| 113 | # out of the loop either way. | ||
| 114 | my $ok = eval { $eof_f->get; 1 }; | ||
| 115 | my $err = $@; | ||
| 116 | |||
| 117 | if ($wstream) { | ||
| 118 | # A response queued when stdin closed must reach the client. The last | ||
| 119 | # write's future is ready once the stream has drained it; a failure | ||
| 120 | # means the client is gone and there is nobody left to tell. | ||
| 121 | my $last = delete $self->{_last_write_f}; | ||
| 122 | eval { $last->get } if $last and !$last->is_ready; | ||
| 123 | delete $self->{_write_stream}; | ||
| 124 | $self->remove_child($wstream); | ||
| 125 | } | ||
| 126 | |||
| 127 | $self->remove_child($stream); | ||
| 128 | |||
| 129 | die $err unless $ok; | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _on_read { | ||
| 134 | my ( $self, $buffref, $eof ) = @_; | ||
| 135 | |||
| 136 | if ( $$buffref =~ s/^([^\n]*)\n// ) { | ||
| 137 | $self->_handle_line($1); | ||
| 138 | return 1; # there may be another complete line in the buffer | ||
| 139 | } | ||
| 140 | |||
| 141 | # A final line that never got its newline is still a request; the readline | ||
| 142 | # loop this replaced delivered it too. | ||
| 143 | if ( $eof and length $$buffref ) { | ||
| 144 | my $line = $$buffref; | ||
| 145 | $$buffref = ''; | ||
| 146 | $self->_handle_line($line); | ||
| 147 | } | ||
| 148 | |||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _handle_line { | ||
| 153 | my ( $self, $line ) = @_; | ||
| 154 | |||
| 155 | return unless length $line; | ||
| 156 | |||
| 157 | my $request = eval { decode_json($line) }; | ||
| 158 | if ($@) { | ||
| 159 | $self->_send_response({ | ||
| 160 | jsonrpc => '2.0', | ||
| 161 | id => undef, | ||
| 162 | error => { code => -32700, message => 'Parse error' }, | ||
| 163 | }); | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | my $response = $self->server->handle( | ||
| 168 | $request, | ||
| 169 | { transport => 'stdio' }, | ||
| 170 | ); | ||
| 171 | $self->_send_response($response) if $response; | ||
| 172 | |||
| 173 | return; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub _send_response { | ||
| 177 | my ( $self, $response ) = @_; | ||
| 178 | my $line = encode_json($response) . "\n"; | ||
| 179 | if ( my $wstream = $self->{_write_stream} ) { | ||
| 180 | $self->{_last_write_f} = $wstream->write($line); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | print { $self->write_handle } $line; | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | |||
| 189 | __END__ | ||