File Coverage

blib/lib/MCP/Server/Transport/Stdio.pm
Criterion Covered Total %
statement 12 30 40.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 1 1 100.0
total 17 45 37.7


line stmt bran cond sub pod time code
1             package MCP::Server::Transport::Stdio;
2 2     2   13 use Mojo::Base 'MCP::Server::Transport', -signatures;
  2         3  
  2         15  
3              
4 2     2   764 use Mojo::JSON qw(decode_json encode_json);
  2         7  
  2         152  
5 2     2   16 use Mojo::Log;
  2         5  
  2         26  
6 2     2   132 use Scalar::Util qw(blessed);
  2         32  
  2         1007  
7              
8 0     0 1   sub handle_requests ($self) {
  0            
  0            
9 0           my $server = $self->server;
10              
11 0           STDOUT->autoflush(1);
12 0           while (my $input = <>) {
13 0           chomp $input;
14 0           my $request = eval { decode_json($input) };
  0            
15 0 0         next unless my $response = $server->handle($request, {});
16              
17 0 0 0       if (blessed($response) && $response->isa('Mojo::Promise')) {
18 0     0     $response->then(sub { _print_response($_[0]) })->wait;
  0            
19             }
20 0           else { _print_response($response) }
21             }
22             }
23              
24 0     0     sub _print_response ($response) { print encode_json($response) . "\n" }
  0            
  0            
  0            
25              
26             1;
27              
28             =encoding utf8
29              
30             =head1 NAME
31              
32             MCP::Server::Transport::Stdio - Stdio transport for MCP servers
33              
34             =head1 SYNOPSIS
35              
36             use MCP::Server::Transport::Stdio;
37              
38             my $stdio = MCP::Server::Transport::Stdio->new;
39              
40             =head1 DESCRIPTION
41              
42             L is a transport for MCP (Model Context Protocol) server that reads requests from
43             standard input (STDIN) and writes responses to standard output (STDOUT). It is designed for command-line tools and
44             debugging tasks.
45              
46             =head1 ATTRIBUTES
47              
48             L inherits all attributes from L.
49              
50             =head1 METHODS
51              
52             L inherits all methods from L and implements the following new
53             ones.
54              
55             =head2 handle_requests
56              
57             $stdio->handle_requests;
58              
59             Reads requests from standard input and prints responses to standard output.
60              
61             =head1 SEE ALSO
62              
63             L, L, L.
64              
65             =cut