MCP/Server/Legacy.pm

filter: Statementstmt 100% 32 / 32 Branchbran 90.9% 20 / 22 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 3 / 3 totaltotal 89.1% 66 / 74 CC 16 SCAR 18.6
CC
16
Cov
92%
legacy_request
9.0
legacy_result
6.0
legacy_context
3.1
CRAP 6.4
« MCP/Server/Context.pm ↑ Summary MCP/Server/Transport/HTTP.pm »
1 package MCP::Server::Legacy;
2 10 ▶use Mojo::Base 'Exporter', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use MCP::Constants qw(META_PROTOCOL_VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our @EXPORT_OK = qw(legacy_context legacy_request legacy_result);
7  
8 10 my @VERSIONS = ('2024-10-07', '2024-11-05', '2025-03-26', '2025-06-18', '2025-11-25');
9  
10 9 ▶sub legacy_context ($params, $context) {
Subroutine legacy_context: called CC=3 SCAR=11.4
Pod documented
11 9 ▶ my $meta = $params->{_meta} // {};
Condition50%1/2
$params->{"_meta"} // {}
l!l
90
MC/DC0%0/1
$params->{"_meta"} // {}
$params->{"_meta"}
Truth table50%1/2
A
A= $params->{"_meta"}
Aresult
11
00
12 9 $context->client_capabilities({})->client_info({})->protocol_version($context->legacy);
13 9 ▶ $context->progress_token($meta->{progressToken}) if defined $meta->{progressToken};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->{"progressToken"} 0 9 9
14 9 return $context;
15 }
16  
17 148 ▶sub legacy_request ($version, $request) {
Subroutine legacy_request: called CC=9 SCAR=22.0
Pod documented
18 148 ▶ my $params = $request->{params} // {};
Condition100%2/2
$request->{"params"} // {}
l!l
1471
MC/DC100%1/1
$request->{"params"} // {}
$request->{"params"}
Truth table100%2/2
A
A= $request->{"params"}
Aresult
11
00
19  
20 # The handshake exists only in legacy revisions, so answer it with the closest version we can serve
21 148 ▶ if (($request->{method} // '') eq 'initialize') {
Branch: 1 branch
BranchTrueFalseTotal
if (($request->{"method"} // "") eq "initialize") 6 142 148
Condition50%1/2
$request->{"method"} // ""
l!l
1480
MC/DC0%0/1
$request->{"method"} // ""
$request->{"method"}
Truth table50%1/2
A
A= $request->{"method"}
Aresult
11
00
22 6 ▶ my $requested = $params->{protocolVersion} // '';
Condition50%1/2
$params->{"protocolVersion"} // ""
l!l
60
MC/DC0%0/1
$params->{"protocolVersion"} // ""
$params->{"protocolVersion"}
Truth table50%1/2
A
A= $params->{"protocolVersion"}
Aresult
11
00
23 6 ▶ return (grep { $_ eq $requested } @VERSIONS) ? $requested : $VERSIONS[-1];
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $requested;} @VERSIONS) ? : 5 1 6
24 }
25  
26 142 ▶ return undef if $params->{_meta}{+META_PROTOCOL_VERSION};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"_meta"}{"io.modelcontextprotocol/protocolVersion"} 129 13 142
27 13 ▶ return $VERSIONS[-1] unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 3 10 13
28 10 ▶ return (grep { $_ eq $version } @VERSIONS) ? $version : undef;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $version;} @VERSIONS) ? : 9 1 10
29 }
30  
31 16 ▶sub legacy_result ($server, $method, $version) {
Subroutine legacy_result: called CC=6 SCAR=17.9
Pod documented
32 16 ▶ return {} if $method eq 'ping';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "ping" 1 15 16
33 15 ▶ return undef unless $method eq 'initialize';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "initialize" 9 6 15
34  
35 6 my $capabilities = {};
36 6 ▶ $capabilities->{prompts} = {} if @{$server->prompts};
Branch: 1 branch
BranchTrueFalseTotal
if @{$server->prompts;} 5 1 6
37 6 ▶ $capabilities->{resources} = {} if @{$server->resources};
Branch: 1 branch
BranchTrueFalseTotal
if @{$server->resources;} 5 1 6
38 6 ▶ $capabilities->{tools} = {} if @{$server->tools};
Branch: 1 branch
BranchTrueFalseTotal
if @{$server->tools;} 6 0 6
39  
40 return {
41 6 protocolVersion => $version,
42 capabilities => $capabilities,
43 serverInfo => {name => $server->name, version => $server->version}
44 };
45 }
46  
47 10 1;
48  
49 =encoding utf8
50  
51 =head1 NAME
52  
53 MCP::Server::Legacy - Fallback for clients speaking a previous protocol version
54  
55 =head1 SYNOPSIS
56  
57 use MCP::Server::Legacy qw(legacy_request);
58  
59 my $version = legacy_request('2025-11-25', {jsonrpc => '2.0', id => 1, method => 'tools/call'});
60  
61 =head1 DESCRIPTION
62  
63 L<MCP::Server::Legacy> answers the C<initialize> handshake of clients speaking a protocol revision older than
64 L<MCP::Constants/"PROTOCOL_VERSION">, so they can still list and call tools, prompts and resources while the
65 ecosystem catches up. Nothing beyond that is supported, in particular no notifications and no multi round-trip
66 requests. It is a temporary convenience and will be removed again in a future release.
67  
68 =head1 FUNCTIONS
69  
70 L<MCP::Server::Legacy> implements the following functions, which can be imported individually.
71  
72 =head2 legacy_context
73  
74 my $context = legacy_context($params, $context);
75  
76 Populate an L<MCP::Server::Context> with the values a current request would have carried in C<_meta>.
77  
78 =head2 legacy_request
79  
80 my $version = legacy_request($version, $request);
81  
82 The protocol revision a request was made with, or C<undef> if it is not legacy, based on the
83 C<MCP-Protocol-Version> header and the JSON-RPC request. A request carrying
84 C<_meta.io.modelcontextprotocol/protocolVersion> is never legacy, whatever the header says, so a current client with
85 a misconfigured header still gets a proper error. Revisions before C<2025-06-18> predate the header, so a request
86 without one is assumed to be legacy.
87  
88 =head2 legacy_result
89  
90 my $result = legacy_result($server, $method, $version);
91  
92 The result for a C<initialize> or C<ping> request, or C<undef> for every other method. The handshake echoes the
93 requested protocol revision and advertises the same primitives as C<server/discover>, but never C<listChanged>,
94 since list change notifications are not supported.
95  
96 =head1 SEE ALSO
97  
98 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
99  
100 =cut
« MCP/Server/Context.pm ↑ Summary MCP/Server/Transport/HTTP.pm »