| 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"} // {}
MC/DC0%0/1
$params->{"_meta"} // {}
$params->{"_meta"}
Truth table50%1/2
A
A= $params->{"_meta"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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"} // {}
MC/DC100%1/1
$request->{"params"} // {}
$request->{"params"}
Truth table100%2/2
A
A= $request->{"params"}
| |||||||||||||||||||||
| 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
Condition50%1/2
$request->{"method"} // ""
MC/DC0%0/1
$request->{"method"} // ""
$request->{"method"}
Truth table50%1/2
A
A= $request->{"method"}
| |||||||||||||||||||||
| 22 | 6 | ▶ | my $requested = $params->{protocolVersion} // ''; | ||||||||||||||||||
|
Condition50%1/2
$params->{"protocolVersion"} // ""
MC/DC0%0/1
$params->{"protocolVersion"} // ""
$params->{"protocolVersion"}
Truth table50%1/2
A
A= $params->{"protocolVersion"}
| |||||||||||||||||||||
| 23 | 6 | ▶ | return (grep { $_ eq $requested } @VERSIONS) ? $requested : $VERSIONS[-1]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 142 | ▶ | return undef if $params->{_meta}{+META_PROTOCOL_VERSION}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 13 | ▶ | return $VERSIONS[-1] unless defined $version; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 10 | ▶ | return (grep { $_ eq $version } @VERSIONS) ? $version : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 33 | 15 | ▶ | return undef unless $method eq 'initialize'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 6 | my $capabilities = {}; | |||||||||||||||||||
| 36 | 6 | ▶ | $capabilities->{prompts} = {} if @{$server->prompts}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 6 | ▶ | $capabilities->{resources} = {} if @{$server->resources}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 6 | ▶ | $capabilities->{tools} = {} if @{$server->tools}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ||||||||||||||||||||