| 1 | package MCP::Server::Transport::Stdio; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Mojo::Base 'MCP::Server::Transport', -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use MCP::Server::Context; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Mojo::JSON qw(decode_json encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Mojo::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | ▶ | sub handle_requests ($self) { | |||||||||||||||||||||||||||
|
Subroutine
handle_requests: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 0 | my $server = $self->server; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | binmode STDIN, ':raw'; | |||||||||||||||||||||||||||
| 13 | 0 | binmode STDOUT, ':raw'; | |||||||||||||||||||||||||||
| 14 | 0 | STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $buffer = ''; | |||||||||||||||||||||||||||
| 17 | 0 | ▶ | while (defined(my $input = _read_line(\$buffer))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | next if $input eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | my $request = eval { decode_json($input) }; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | next unless my $response = $server->handle($request, MCP::Server::Context->new(transport => $self)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | if (blessed($response) && $response->isa('Mojo::Promise')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($response) and $response->isa("Mojo::Promise")
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Mojo::Promise")
builtin::blessed($response)
$response->isa("Mojo::Promise")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("Mojo::Promise")
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $response->then(sub { _print_response($_[0]) })->wait; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 0 | else { _print_response($response) } | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ▶ | sub _read_line ($buffer) { | |||||||||||||||||||||||||||
|
Subroutine
_read_line: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | while (index($$buffer, "\n") < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | last unless sysread STDIN, my $chunk, 131072; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $$buffer .= $chunk; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return undef if $$buffer eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $pos = index($$buffer, "\n"); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $line = $pos < 0 ? substr($$buffer, 0, length($$buffer), '') : substr($$buffer, 0, $pos + 1, ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $line =~ s/\r?\n?$//; | |||||||||||||||||||||||||||
| 39 | 0 | return $line; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub notify ($self, $session_id, $method, $params = {}) { | ||||||||||||||||||||||||||
|
Subroutine
notify: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | _print_response({jsonrpc => '2.0', method => $method, params => $params}); | |||||||||||||||||||||||||||
| 44 | 0 | return 1; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub notify_all ($self, $method, $params = {}) { $self->notify(undef, $method, $params) } | ||||||||||||||||||||||||||
|
Subroutine
notify_all: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _print_response ($response) { print encode_json($response) . "\n" } | ||||||||||||||||||||||||||
|
Subroutine
_print_response: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | 1; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =encoding utf8 | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 NAME | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | MCP::Server::Transport::Stdio - Stdio transport for MCP servers | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | use MCP::Server::Transport::Stdio; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $stdio = MCP::Server::Transport::Stdio->new; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | L<MCP::Server::Transport::Stdio> is a transport for MCP (Model Context Protocol) server that reads requests from | ||||||||||||||||||||||||||||
| 68 | standard input (STDIN) and writes responses to standard output (STDOUT). It is designed for command-line tools and | ||||||||||||||||||||||||||||
| 69 | debugging tasks. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | L<MCP::Server::Transport::Stdio> inherits all attributes from L<MCP::Server::Transport>. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 METHODS | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | L<MCP::Server::Transport::Stdio> inherits all methods from L<MCP::Server::Transport> and implements the following new | ||||||||||||||||||||||||||||
| 78 | ones. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 handle_requests | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | $stdio->handle_requests; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Reads requests from standard input and prints responses to standard output. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 notify | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | my $bool = $stdio->notify($session_id, $method); | ||||||||||||||||||||||||||||
| 89 | my $bool = $stdio->notify($session_id, $method, {foo => 'bar'}); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Send a JSON-RPC notification to standard output. The C<$session_id> is ignored. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 notify_all | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my $bool = $stdio->notify_all($method); | ||||||||||||||||||||||||||||
| 96 | my $bool = $stdio->notify_all($method, {foo => 'bar'}); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Send a JSON-RPC notification to standard output. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||