MCP/Server/Transport/Stdio.pm

filter: Statementstmt 32.2% 20 / 62 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 42.8% 6 / 14 Podpod 100% 2 / 2 totaltotal 21.3% 28 / 131 CC 24 SCAR 39.7
CC
24
Cov
19%
handle_requests
132.0
_cancel
42.0
_read_line
30.0
CRAP 52.9
↑ Summary MCP/Server/Context.pm »
1 package MCP::Server::Transport::Stdio;
2 10 ▶use Mojo::Base 'MCP::Server::Transport', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use MCP::Server::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use MCP::Server::Legacy qw(legacy_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use MCP::Server::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶has subscriptions => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11  
12 0 ▶sub handle_requests ($self) {
Subroutine handle_requests: not called CC=11 SCAR=48.8
Pod documented
13 0 my $server = $self->server;
14  
15 0 binmode STDIN, ':raw';
16 0 binmode STDOUT, ':raw';
17 0 STDOUT->autoflush(1);
18  
19 0 my $buffer = '';
20 0 my $legacy = undef;
21 0 ▶ while (defined(my $input = _read_line(\$buffer))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $input = _read_line(\$buffer))) 0 0 0
22 0 ▶ next if $input eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $input eq "" 0 0 0
23 0 my $request = eval { decode_json($input) };
24 0 ▶ next if $self->_cancel($request);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cancel($request) 0 0 0
25  
26 # Legacy
27 0 ▶ $legacy ||= legacy_request(undef, ref $request eq 'HASH' ? $request : {});
Branch: 1 branch
BranchTrueFalseTotal
ref $request eq 'HASH' ? : 0 0 0
Condition0%0/3
$legacy ||= legacy_request(undef, ref $request eq "HASH" ? $request : {})
l!l&&r!l&&!r
000
MC/DC0%0/2
$legacy ||= legacy_request(undef, ref $request eq "HASH" ? $request : {})
$legacy legacy_request(undef, ref $request eq "HASH" ? $request : {})
Truth table0%0/3
A ||= B
A= $legacyB= legacy_request(undef, ref $request eq "HASH" ? $request : {})
ABresult
1X1
011
000
28  
29 0 my $context = MCP::Server::Context->new(legacy => $legacy, stream => \&_print_response, transport => $self);
30 0 ▶ next unless my $response = $server->handle($request, $context);
Branch: 1 branch
BranchTrueFalseTotal
unless my $response = $server->handle($request, $context) 0 0 0
31  
32 0 ▶ if (blessed($response) && $response->isa('MCP::Server::Subscription')) { $self->_subscribe($response) }
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->isa('MCP::Server::Subscription')) { } 0 0 0
elsif (builtin::blessed($response) and $response->isa('Mojo::Promise')) { } 0 0 0
Condition0%0/3
builtin::blessed($response) and $response->isa("MCP::Server::Subscription")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($response) and $response->isa("MCP::Server::Subscription")
builtin::blessed($response) $response->isa("MCP::Server::Subscription")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("MCP::Server::Subscription")
ABresult
0X0
100
111
33 ▶ elsif (blessed($response) && $response->isa('Mojo::Promise')) {
Condition0%0/3
builtin::blessed($response) and $response->isa("Mojo::Promise")
!ll&&!rl&&r
000
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")
ABresult
0X0
100
111
34 0 ▶ $response->then(sub { _print_response($_[0]) unless $context->is_cancelled })->wait;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $context->is_cancelled 0 0 0
35 }
36 0 else { _print_response($response) }
37 }
38 }
39  
40 0 ▶sub notify_all ($self, $method, $params = {}) {
Subroutine notify_all: not called CC=4 SCAR=30.0
Pod documented
41 0 for my $subscription (values %{$self->subscriptions}) {
42 0 ▶ next unless $subscription->wants($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $subscription->wants($method) 0 0 0
43 0 _print_response($subscription->notification($method, $params));
44 }
45 0 return 1;
46 }
47  
48 0 ▶sub _cancel ($self, $request) {
Subroutine _cancel: not called CC=6 SCAR=37.4
49 0 ▶ return 0 unless ref $request eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $request eq "HASH" 0 0 0
50 0 ▶ return 0 unless ($request->{method} // '') eq 'notifications/cancelled';
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->{"method"} // "") eq "notifications/cancelled" 0 0 0
Condition0%0/2
$request->{"method"} // ""
l!l
00
MC/DC0%0/1
$request->{"method"} // ""
$request->{"method"}
Truth table0%0/2
A
A= $request->{"method"}
Aresult
11
00
51 0 ▶ my $id = ($request->{params} // {})->{requestId};
Condition0%0/2
$request->{"params"} // {}
l!l
00
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table0%0/2
A
A= $request->{"params"}
Aresult
11
00
52 0 ▶ delete $self->subscriptions->{$id} if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
53 0 return 1;
54 }
55  
56 0 ▶sub _print_response ($response) { print encode_json($response) . "\n" }
Subroutine _print_response: not called CC=1 SCAR=6.9
57  
58 0 ▶sub _read_line ($buffer) {
Subroutine _read_line: not called CC=5 SCAR=34.0
59 0 ▶ while (index($$buffer, "\n") < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($$buffer, "\n") == -1) 0 0 0
60 0 ▶ last unless sysread STDIN, my $chunk, 131072;
Branch: 1 branch
BranchTrueFalseTotal
unless sysread STDIN, my $chunk, 131072 0 0 0
61 0 $$buffer .= $chunk;
62 }
63 0 ▶ return undef if $$buffer eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $$buffer eq "" 0 0 0
64  
65 0 my $pos = index($$buffer, "\n");
66 0 ▶ my $line = $pos < 0 ? substr($$buffer, 0, length($$buffer), '') : substr($$buffer, 0, $pos + 1, '');
Branch: 1 branch
BranchTrueFalseTotal
$pos < 0 ? : 0 0 0
67 0 $line =~ s/\r?\n?$//;
68 0 return $line;
69 }
70  
71 0 ▶sub _subscribe ($self, $subscription) {
Subroutine _subscribe: not called CC=1 SCAR=6.9
72 0 $self->subscriptions->{$subscription->id} = $subscription;
73 0 return _print_response($subscription->acknowledgement);
74 }
75  
76 10 1;
77  
78 =encoding utf8
79  
80 =head1 NAME
81  
82 MCP::Server::Transport::Stdio - Stdio transport for MCP servers
83  
84 =head1 SYNOPSIS
85  
86 use MCP::Server::Transport::Stdio;
87  
88 my $stdio = MCP::Server::Transport::Stdio->new;
89  
90 =head1 DESCRIPTION
91  
92 L<MCP::Server::Transport::Stdio> is a transport for MCP (Model Context Protocol) server that reads requests from
93 standard input (STDIN) and writes responses to standard output (STDOUT). It is designed for command-line tools and
94 debugging tasks.
95  
96 All messages share the one channel, so a C<subscriptions/listen> request is answered with an acknowledgement
97 notification and then left open, with its JSON-RPC response withheld until the client cancels it.
98  
99 Requests are processed strictly in order, so a C<notifications/cancelled> notification cannot be read while the
100 request it refers to is still in flight; it can only cancel a subscription. Use L<MCP::Server::Transport::HTTP> for
101 workloads where cancelling a running tool matters.
102  
103 =head1 ATTRIBUTES
104  
105 L<MCP::Server::Transport::Stdio> inherits all attributes from L<MCP::Server::Transport> and implements the following
106 new ones.
107  
108 =head2 subscriptions
109  
110 my $subscriptions = $stdio->subscriptions;
111 $stdio = $stdio->subscriptions({});
112  
113 Registry of active L<MCP::Server::Subscription> objects, keyed by the id of the request that opened them.
114  
115 =head1 METHODS
116  
117 L<MCP::Server::Transport::Stdio> inherits all methods from L<MCP::Server::Transport> and implements the following new
118 ones.
119  
120 =head2 handle_requests
121  
122 $stdio->handle_requests;
123  
124 Reads requests from standard input and prints responses to standard output.
125  
126 =head2 notify_all
127  
128 my $bool = $stdio->notify_all($method);
129 my $bool = $stdio->notify_all($method, {foo => 'bar'});
130  
131 Send a JSON-RPC notification to standard output, once for every subscription in L</"subscriptions"> that asked for
132 it.
133  
134 =head1 SEE ALSO
135  
136 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
137  
138 =cut
↑ Summary MCP/Server/Context.pm »