| 1 | package MCP::Primitive; | ||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use MCP::Server::Context; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 12 | has cache_scope => 'public'; | |||||||||||||||||||||||||||
| 8 | 12 | has cache_ttl => 0; | |||||||||||||||||||||||||||
| 9 | 41 | ▶ | has scopes => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 25 | ▶ | sub context ($self) { $self->{context} || MCP::Server::Context->new } | ||||||||||||||||||||||||||
|
Subroutine
context: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | sub input_required ($self, $input_requests = undef, $state = undef) { | ||||||||||||||||||||||||||
|
Subroutine
input_required: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | croak 'Input required results need input requests or state' unless $input_requests || defined $state; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$input_requests or defined $state
MC/DC0%0/2
$input_requests or defined $state
$input_requests
defined $state
Truth table66%2/3
A or B
A= $input_requestsB= defined $state
| |||||||||||||||||||||||||||||
| 15 | 7 | my $result = {resultType => 'input_required'}; | |||||||||||||||||||||||||||
| 16 | 7 | ▶ | $result->{inputRequests} = $input_requests if $input_requests; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | $result->{requestState} = $self->context->seal_state($state) if defined $state; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 7 | return $result; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 12 | 1; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =encoding utf8 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | MCP::Primitive - Primitive base class | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package MyMCPPrimitive; | ||||||||||||||||||||||||||||
| 32 | use Mojo::Base 'MCP::Primitive'; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | L<MCP::Primitive> is a base class for MCP (Model Context Protocol) primitives such as L<MCP::Tool>, L<MCP::Prompt>, | ||||||||||||||||||||||||||||
| 39 | and L<MCP::Resource>. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | L<MCP::Primitive> implements the following attributes. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 cache_scope | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my $scope = $primitive->cache_scope; | ||||||||||||||||||||||||||||
| 48 | $primitive = $primitive->cache_scope('private'); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Cache scope advertised for results derived from this primitive, either C<public> for results shared by every caller, | ||||||||||||||||||||||||||||
| 51 | or C<private> for results a gateway may only cache per caller. Defaults to C<public>, and is forced to C<private> | ||||||||||||||||||||||||||||
| 52 | whenever L</"scopes"> is not empty. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 cache_ttl | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | my $ttl = $primitive->cache_ttl; | ||||||||||||||||||||||||||||
| 57 | $primitive = $primitive->cache_ttl(60_000); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | How long results derived from this primitive may be cached, in milliseconds. Defaults to C<0>, which means results | ||||||||||||||||||||||||||||
| 60 | must be revalidated on every request. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 scopes | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $scopes = $primitive->scopes; | ||||||||||||||||||||||||||||
| 65 | $primitive = $primitive->scopes(['mcp:read', 'mcp:write']); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | OAuth scopes required to list or call this primitive, as an array reference; all of them must be granted. This is a | ||||||||||||||||||||||||||||
| 68 | local authorization policy layered on the HTTP transport's L<MCP::Server::Transport::HTTP/"auth"> hook, not wire-level | ||||||||||||||||||||||||||||
| 69 | MCP metadata, and is only enforced for requests that supply scopes (so it has no effect over stdio). Defaults to no | ||||||||||||||||||||||||||||
| 70 | required scopes. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | L<MCP::Primitive> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 context | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | my $context = $primitive->context; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Returns the L<MCP::Server::Context> for the current request. Capture this before an async boundary to keep using | ||||||||||||||||||||||||||||
| 81 | its notification methods from later callbacks. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Get controller for requests using the HTTP transport | ||||||||||||||||||||||||||||
| 84 | my $c = $primitive->context->controller; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 input_required | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | my $result = $primitive->input_required($input_requests); | ||||||||||||||||||||||||||||
| 89 | my $result = $primitive->input_required($input_requests, $state); | ||||||||||||||||||||||||||||
| 90 | my $result = $primitive->input_required(undef, $state); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Returns an C<input_required> result, asking the client to gather information and call again. At least one of the two | ||||||||||||||||||||||||||||
| 93 | arguments is required. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | return $tool->input_required({ | ||||||||||||||||||||||||||||
| 96 | confirm => { | ||||||||||||||||||||||||||||
| 97 | method => 'elicitation/create', | ||||||||||||||||||||||||||||
| 98 | params => { | ||||||||||||||||||||||||||||
| 99 | message => 'Really deploy to production?', | ||||||||||||||||||||||||||||
| 100 | requestedSchema => {type => 'object', properties => {ok => {type => 'boolean'}}} | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | }, {target => $args->{target}}); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Input requests are keyed by names of your choosing, which the client mirrors back in | ||||||||||||||||||||||||||||
| 106 | L<MCP::Server::Context/"input_responses">. Only ask for capabilities the client declared in | ||||||||||||||||||||||||||||
| 107 | L<MCP::Server::Context/"client_capabilities">. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | The optional state is any Perl data structure, and is sealed with L<MCP::Server::Context/"seal_state"> before it | ||||||||||||||||||||||||||||
| 110 | travels through the client, so a retry can pick up where the first call left off without the server having to | ||||||||||||||||||||||||||||
| 111 | remember anything. Read it back with L<MCP::Server::Context/"request_state">, which returns C<undef> for state that | ||||||||||||||||||||||||||||
| 112 | cannot be trusted; treat that like a first call and ask again. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||