| 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=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 0 | my $server = $self->server; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 13 | 0 | ▶ | while (my $input = <>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 0 | chomp $input; | |||||||||||||||||||||||||||
| 15 | 0 | my $request = eval { decode_json($input) }; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | next unless my $response = $server->handle($request, MCP::Server::Context->new(transport => $self)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 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")
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $response->then(sub { _print_response($_[0]) })->wait; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | 0 | else { _print_response($response) } | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub notify ($self, $session_id, $method, $params = {}) { | ||||||||||||||||||||||||||
|
Subroutine
notify: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | _print_response({jsonrpc => '2.0', method => $method, params => $params}); | |||||||||||||||||||||||||||
| 27 | 0 | return 1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub notify_all ($self, $method, $params = {}) { $self->notify(undef, $method, $params) } | ||||||||||||||||||||||||||
|
Subroutine
notify_all: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub _print_response ($response) { print encode_json($response) . "\n" } | ||||||||||||||||||||||||||
|
Subroutine
_print_response: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | 1; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =encoding utf8 | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 NAME | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | MCP::Server::Transport::Stdio - Stdio transport for MCP servers | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | use MCP::Server::Transport::Stdio; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $stdio = MCP::Server::Transport::Stdio->new; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | L<MCP::Server::Transport::Stdio> is a transport for MCP (Model Context Protocol) server that reads requests from | ||||||||||||||||||||||||||||
| 51 | standard input (STDIN) and writes responses to standard output (STDOUT). It is designed for command-line tools and | ||||||||||||||||||||||||||||
| 52 | debugging tasks. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | L<MCP::Server::Transport::Stdio> inherits all attributes from L<MCP::Server::Transport>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 METHODS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | L<MCP::Server::Transport::Stdio> inherits all methods from L<MCP::Server::Transport> and implements the following new | ||||||||||||||||||||||||||||
| 61 | ones. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 handle_requests | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | $stdio->handle_requests; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Reads requests from standard input and prints responses to standard output. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 notify | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $bool = $stdio->notify($session_id, $method); | ||||||||||||||||||||||||||||
| 72 | my $bool = $stdio->notify($session_id, $method, {foo => 'bar'}); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Send a JSON-RPC notification to standard output. The C<$session_id> is ignored. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 notify_all | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | my $bool = $stdio->notify_all($method); | ||||||||||||||||||||||||||||
| 79 | my $bool = $stdio->notify_all($method, {foo => 'bar'}); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Send a JSON-RPC notification to standard output. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||