t/bin/legacy_mcp_server.pl

filter: Statementstmt 70.0% 56 / 80 Branchbran 54.5% 24 / 44 Conditioncond 36.3% 8 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 57.3% 98 / 171 CC 11 SCAR 21.5
CC
11
Cov
60%
__ANON__
30.0
__ANON__
6.0
__ANON__
6.0
CRAP 8.6
↑ Summary blib/lib/Net/Async/MCP.pm »
1 #!/usr/bin/env perl
2 # A strictly legacy MCP server for the tests: the initialize handshake of the
3 # 2024-11-05 .. 2025-11-25 revisions, hand-rolled JSON-RPC over stdin/stdout,
4 # and deliberately no part of the MCP distribution, whose server speaks the
5 # modern revision too. Configured through the environment:
6 #
7 # LEGACY_MCP_VERSIONS comma-separated revisions it accepts, newest first;
8 # initialize is answered with the requested one if it
9 # is among them, else with the first
10 # LEGACY_MCP_DISCOVER how server/discover is met: error (-32601, the
11 # default), silent (never answered), late (answered
12 # with a DiscoverResult only once initialize arrives,
13 # right before its answer), unsupported (-32022 naming
14 # LEGACY_MCP_VERSIONS)
15 #
16 # Everything but initialize, ping and test/transcript is refused until
17 # notifications/initialized has arrived, so a client that forgets it fails.
18 # test/transcript answers with every line received so far.
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Cwd qw( getcwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 my $json = JSON::MaybeXS->new(utf8 => 1, canonical => 1);
26 my @versions = split /,/, $ENV{LEGACY_MCP_VERSIONS}
27 1 ▶ // '2025-11-25,2025-06-18,2025-03-26,2024-11-05';
Condition50%1/2
$ENV{"LEGACY_MCP_VERSIONS"} // "2025-11-25,2025-06-18,2025-03-26,2024-11-05"
l!l
01
MC/DC0%0/1
$ENV{"LEGACY_MCP_VERSIONS"} // "2025-11-25,2025-06-18,2025-03-26,2024-11-05"
$ENV{"LEGACY_MCP_VERSIONS"}
Truth table50%1/2
A
A= $ENV{"LEGACY_MCP_VERSIONS"}
Aresult
11
00
28 1 ▶my $discover = $ENV{LEGACY_MCP_DISCOVER} // 'error';
Condition50%1/2
$ENV{"LEGACY_MCP_DISCOVER"} // "error"
l!l
01
MC/DC0%0/1
$ENV{"LEGACY_MCP_DISCOVER"} // "error"
$ENV{"LEGACY_MCP_DISCOVER"}
Truth table50%1/2
A
A= $ENV{"LEGACY_MCP_DISCOVER"}
Aresult
11
00
29  
30 1 binmode STDIN, ':raw';
31 1 binmode STDOUT, ':raw';
32 1 $| = 1;
33  
34 1 print STDERR "legacy test server starting\n";
35  
36 1 my ( @seen, $answered_initialize, $initialized, $late_discover );
37  
38 3 ▶sub send_message { print $json->encode({ jsonrpc => '2.0', %{ $_[0] } }), "\n" }
Subroutine send_message: called CC=1 SCAR=0.0
39  
40 2 ▶sub result { send_message({ id => $_[0], result => $_[1] }) }
Subroutine result: called CC=1 SCAR=0.0
41  
42 1 ▶sub error {
Subroutine error: called CC=2 SCAR=7.2
43 1 my ( $id, $code, $message, $data ) = @_;
44 1 ▶ send_message({ id => $id, error => {
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 1 1
45 code => $code,
46 message => $message,
47 defined $data ? ( data => $data ) : (),
48 } });
49 }
50  
51 1 ▶sub text { return { content => [ { type => 'text', text => $_[0] } ] } }
Subroutine text: called CC=1 SCAR=0.0
52  
53 5 ▶sub read_message {
Subroutine read_message: called CC=3 SCAR=11.0
54 5 my $line = <STDIN>;
55 5 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 4 5
56 4 chomp $line;
57 4 push @seen, $line;
58 4 ▶ return eval { $json->decode($line) } // {};
Condition50%1/2
eval { do { $json->decode($line) } } // {}
l!l
40
MC/DC0%0/1
eval { do { $json->decode($line) } } // {}
eval { do { $json->decode($line) } }
Truth table50%1/2
A
A= eval { do { $json->decode($line) } }
Aresult
11
00
59 }
60  
61 my %tools = (
62 0 ▶ echo => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
63 0 my ( $args ) = @_;
64 0 send_message({ method => 'notifications/message',
65 params => { level => 'info', data => 'echoing' } });
66 0 ▶ return text('Echo: ' . ( $args->{message} // '' ));
Condition0%0/2
$args->{"message"} // ""
l!l
00
MC/DC0%0/1
$args->{"message"} // ""
$args->{"message"}
Truth table0%0/2
A
A= $args->{"message"}
Aresult
11
00
67 },
68 1 ▶ whoami => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
69 1 my ( $args ) = @_;
70 return text($json->encode({
71 cwd => getcwd(),
72 1 ▶ env => { map { $_ => $ENV{$_} } @{ $args->{vars} // [] } },
Condition50%1/2
$args->{"vars"} // []
l!l
10
MC/DC0%0/1
$args->{"vars"} // []
$args->{"vars"}
Truth table50%1/2
A
A= $args->{"vars"}
Aresult
11
00
73 }));
74 },
75 # Asks the client something and reports what came back - result or error -
76 # as the tool's text.
77 0 ▶ ask_client => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
78 0 my ( $args ) = @_;
79 0 my $id = 'srv-' . scalar @seen;
80 0 send_message({ id => $id, method => $args->{method}, params => {} });
81 0 ▶ while (defined( my $message = read_message() )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $message = read_message())) 0 0 0
82 0 ▶ next unless defined $message->{id} && $message->{id} eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message->{"id"} and $message->{"id"} eq $id 0 0 0
Condition0%0/3
defined $message->{"id"} and $message->{"id"} eq $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $message->{"id"} and $message->{"id"} eq $id
defined $message->{"id"} $message->{"id"} eq $id
Truth table0%0/3
A and B
A= defined $message->{"id"}B= $message->{"id"} eq $id
ABresult
0X0
100
111
83 0 ▶ my %answer = map { exists $message->{$_} ? ( $_ => $message->{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $message->{$_} ? : 0 0 0
84 qw( result error );
85 0 return text($json->encode(\%answer));
86 }
87 0 exit 0;
88 },
89 0 ▶ crash => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 print STDERR "crash line $_\n" for 1 .. 25;
91 0 exit 3;
92 },
93 1 );
94  
95 1 ▶while (defined( my $message = read_message() )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $message = read_message())) 4 1 5
96 4 my ( $id, $method ) = @{$message}{qw( id method )};
97 4 ▶ next unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 4 4
98  
99 4 ▶ if ($method eq 'notifications/initialized') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "notifications/initialized") 1 3 4
100 1 ▶ $initialized = 1 if $answered_initialize;
Branch: 1 branch
BranchTrueFalseTotal
if $answered_initialize 1 0 1
101 1 next;
102 }
103 3 ▶ next unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 3 3
104  
105 3 ▶ if ($method eq 'test/transcript') {
Branch: 7 branches
BranchTrueFalseTotal
if ($method eq 'test/transcript') { } 0 3 3
elsif ($method eq 'ping') { } 0 3 3
elsif ($method eq 'server/discover') { } 1 2 3
elsif ($method eq 'initialize') { } 1 1 2
elsif (not $initialized) { } 0 1 1
elsif ($method eq 'tools/list') { } 0 1 1
elsif ($method eq 'tools/call') { } 1 0 1
106 0 result($id, { lines => [@seen] });
107 }
108 elsif ($method eq 'ping') {
109 0 result($id, {});
110 }
111 elsif ($method eq 'server/discover') {
112 1 ▶ if ($discover eq 'silent') { }
Branch: 3 branches
BranchTrueFalseTotal
if ($discover eq 'silent') { } 0 1 1
elsif ($discover eq 'late') { } 0 1 1
elsif ($discover eq 'unsupported') { } 0 1 1
113 0 elsif ($discover eq 'late') { $late_discover = $id }
114 elsif ($discover eq 'unsupported') {
115 0 error($id, -32022, 'Unsupported protocol version',
116 { supported => [@versions], requested => 'whatever' });
117 }
118 1 else { error($id, -32601, "Method not found: $method") }
119 }
120 elsif ($method eq 'initialize') {
121 1 ▶ if (defined $late_discover) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $late_discover) 0 1 1
122 0 result($late_discover, {
123 supportedVersions => ['2026-07-28'],
124 capabilities => { tools => {} },
125 });
126 0 undef $late_discover;
127 }
128 1 ▶ my $asked = $message->{params}{protocolVersion} // '';
Condition50%1/2
$message->{"params"}{"protocolVersion"} // ""
l!l
10
MC/DC0%0/1
$message->{"params"}{"protocolVersion"} // ""
$message->{"params"}{"protocolVersion"}
Truth table50%1/2
A
A= $message->{"params"}{"protocolVersion"}
Aresult
11
00
129 1 my ($agree) = grep { $_ eq $asked } @versions;
130 1 $answered_initialize = 1;
131 1 ▶ result($id, {
Condition33%1/3
$agree // $versions[0]
l!l&&r!l&&!r
100
MC/DC0%0/2
$agree // $versions[0]
$agree $versions[0]
Truth table33%1/3
A // B
A= $agreeB= $versions[0]
ABresult
1X1
011
000
132 protocolVersion => $agree // $versions[0],
133 capabilities => { tools => {} },
134 serverInfo => { name => 'LegacyServer', version => '1.0' },
135 instructions => 'Use echo to echo.',
136 });
137 }
138 elsif (!$initialized) {
139 0 error($id, -32600, "Server not initialized: $method before notifications/initialized");
140 }
141 elsif ($method eq 'tools/list') {
142 0 result($id, { tools => [ map { {
143 0 name => $_,
144 description => "The $_ tool",
145 inputSchema => { type => 'object' },
146 } } sort keys %tools ] });
147 }
148 elsif ($method eq 'tools/call') {
149 1 ▶ my $tool = $tools{ $message->{params}{name} // '' };
Condition50%1/2
$message->{"params"}{"name"} // ""
l!l
10
MC/DC0%0/1
$message->{"params"}{"name"} // ""
$message->{"params"}{"name"}
Truth table50%1/2
A
A= $message->{"params"}{"name"}
Aresult
11
00
150 1 ▶ $tool ? result($id, $tool->($message->{params}{arguments} // {}))
Branch: 1 branch
BranchTrueFalseTotal
$tool ? : 1 0 1
Condition50%1/2
$message->{"params"}{"arguments"} // {}
l!l
10
MC/DC0%0/1
$message->{"params"}{"arguments"} // {}
$message->{"params"}{"arguments"}
Truth table50%1/2
A
A= $message->{"params"}{"arguments"}
Aresult
11
00
151 : error($id, -32602, 'Unknown tool');
152 }
153 else {
154 0 error($id, -32601, "Method not found: $method");
155 }
156 }
↑ Summary blib/lib/Net/Async/MCP.pm »