| 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
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | next if $input eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | 0 | my $request = eval { decode_json($input) }; | |||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | next if $self->_cancel($request); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # Legacy | ||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $legacy ||= legacy_request(undef, ref $request eq 'HASH' ? $request : {}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$legacy ||= legacy_request(undef, ref $request eq "HASH" ? $request : {})
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 : {})
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (blessed($response) && $response->isa('MCP::Server::Subscription')) { $self->_subscribe($response) } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
builtin::blessed($response) and $response->isa("MCP::Server::Subscription")
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")
| |||||||||||||||||||||||||||||||||
| 33 | ▶ | elsif (blessed($response) && $response->isa('Mojo::Promise')) { | |||||||||||||||||||||||||||||||
|
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")
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return 0 unless ($request->{method} // '') eq 'notifications/cancelled'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$request->{"method"} // ""
MC/DC0%0/1
$request->{"method"} // ""
$request->{"method"}
Truth table0%0/2
A
A= $request->{"method"}
| |||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $id = ($request->{params} // {})->{requestId}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table0%0/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | delete $self->subscriptions->{$id} if defined $id; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | last unless sysread STDIN, my $chunk, 131072; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 0 | $$buffer .= $chunk; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return undef if $$buffer eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||