| 1 | package MCP::Server::Subscription; | ||||||||||||
| 2 | 10 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 10 | ▶ | use MCP::Constants qw(META_SUBSCRIPTION_ID); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 10 | ▶ | use Mojo::JSON qw(true); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 10 | ▶ | use constant FILTERS => { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 'notifications/prompts/list_changed' => 'promptsListChanged', | ||||||||||||
| 9 | 'notifications/resources/list_changed' => 'resourcesListChanged', | ||||||||||||
| 10 | 'notifications/tools/list_changed' => 'toolsListChanged' | ||||||||||||
| 11 | 10 | }; | |||||||||||
| 12 | |||||||||||||
| 13 | 10 | has 'id'; | |||||||||||
| 14 | 0 | ▶ | has notifications => sub { {} }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 15 | 10 | has 'stream'; | |||||||||||
| 16 | |||||||||||||
| 17 | 2 | ▶ | sub acknowledgement ($self) { | ||||||||||
|
Subroutine
acknowledgement: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 18 | 2 | my $params = {_meta => {META_SUBSCRIPTION_ID() => $self->id}, notifications => $self->honoured}; | |||||||||||
| 19 | 2 | return {jsonrpc => '2.0', method => 'notifications/subscriptions/acknowledged', params => $params}; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 2 | ▶ | sub honoured ($self) { | ||||||||||
|
Subroutine
honoured: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 23 | 2 | my $requested = $self->notifications; | |||||||||||
| 24 | 2 | return {map { $_ => true } grep { $requested->{$_} } sort values %{(FILTERS)}}; | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | 1 | ▶ | sub notification ($self, $method, $params = {}) { | ||||||||||
|
Subroutine
notification: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 28 | 1 | ▶ | my $meta = {%{$params->{_meta} // {}}, META_SUBSCRIPTION_ID() => $self->id}; | ||||||||||
|
Condition50%1/2
$params->{"_meta"} // {}
MC/DC0%0/1
$params->{"_meta"} // {}
$params->{"_meta"}
Truth table50%1/2
A
A= $params->{"_meta"}
| |||||||||||||
| 29 | 1 | return {jsonrpc => '2.0', method => $method, params => {%$params, _meta => $meta}}; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | 2 | ▶ | sub wants ($self, $method) { | ||||||||||
|
Subroutine
wants: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 33 | 2 | ▶ | return undef unless my $key = FILTERS->{$method}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 2 | ▶ | return $self->notifications->{$key} ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 10 | 1; | |||||||||||
| 38 | |||||||||||||
| 39 | =encoding utf8 | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 NAME | ||||||||||||
| 42 | |||||||||||||
| 43 | MCP::Server::Subscription - Subscription container | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||
| 46 | |||||||||||||
| 47 | use MCP::Server::Subscription; | ||||||||||||
| 48 | |||||||||||||
| 49 | my $subscription = MCP::Server::Subscription->new(id => 1, notifications => {toolsListChanged => 1}); | ||||||||||||
| 50 | my $bool = $subscription->wants('notifications/tools/list_changed'); | ||||||||||||
| 51 | |||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||
| 53 | |||||||||||||
| 54 | L<MCP::Server::Subscription> is a container for a C<subscriptions/listen> notification stream. | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 ATTRIBUTES | ||||||||||||
| 57 | |||||||||||||
| 58 | L<MCP::Server::Subscription> implements the following attributes. | ||||||||||||
| 59 | |||||||||||||
| 60 | =head2 id | ||||||||||||
| 61 | |||||||||||||
| 62 | my $id = $subscription->id; | ||||||||||||
| 63 | $subscription = $subscription->id(1); | ||||||||||||
| 64 | |||||||||||||
| 65 | The JSON-RPC id of the C<subscriptions/listen> request that opened this subscription, sent to the client as | ||||||||||||
| 66 | C<_meta.io.modelcontextprotocol/subscriptionId> with every message on the stream. | ||||||||||||
| 67 | |||||||||||||
| 68 | =head2 notifications | ||||||||||||
| 69 | |||||||||||||
| 70 | my $filter = $subscription->notifications; | ||||||||||||
| 71 | $subscription = $subscription->notifications({toolsListChanged => 1}); | ||||||||||||
| 72 | |||||||||||||
| 73 | The notification filter the client requested, as a hash reference. Notification types that were not requested are | ||||||||||||
| 74 | never delivered. | ||||||||||||
| 75 | |||||||||||||
| 76 | =head2 stream | ||||||||||||
| 77 | |||||||||||||
| 78 | my $stream = $subscription->stream; | ||||||||||||
| 79 | $subscription = $subscription->stream(Mojolicious::Controller->new); | ||||||||||||
| 80 | |||||||||||||
| 81 | The L<Mojolicious::Controller> serving the SSE stream for this subscription, when the HTTP transport is in use. | ||||||||||||
| 82 | |||||||||||||
| 83 | =head1 METHODS | ||||||||||||
| 84 | |||||||||||||
| 85 | L<MCP::Server::Subscription> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||
| 86 | |||||||||||||
| 87 | =head2 acknowledgement | ||||||||||||
| 88 | |||||||||||||
| 89 | my $notification = $subscription->acknowledgement; | ||||||||||||
| 90 | |||||||||||||
| 91 | The C<notifications/subscriptions/acknowledged> notification a server has to send as the very first message on a | ||||||||||||
| 92 | subscription stream, listing the notification types it will actually deliver. | ||||||||||||
| 93 | |||||||||||||
| 94 | =head2 honoured | ||||||||||||
| 95 | |||||||||||||
| 96 | my $filter = $subscription->honoured; | ||||||||||||
| 97 | |||||||||||||
| 98 | The subset of L</"notifications"> the server actually supports. | ||||||||||||
| 99 | |||||||||||||
| 100 | =head2 notification | ||||||||||||
| 101 | |||||||||||||
| 102 | my $notification = $subscription->notification($method); | ||||||||||||
| 103 | my $notification = $subscription->notification($method, {foo => 'bar'}); | ||||||||||||
| 104 | |||||||||||||
| 105 | Build a JSON-RPC notification for this subscription, tagged with L</"id"> in | ||||||||||||
| 106 | C<_meta.io.modelcontextprotocol/subscriptionId>. | ||||||||||||
| 107 | |||||||||||||
| 108 | =head2 wants | ||||||||||||
| 109 | |||||||||||||
| 110 | my $bool = $subscription->wants('notifications/tools/list_changed'); | ||||||||||||
| 111 | |||||||||||||
| 112 | Returns true if the given notification method was requested in L</"notifications">. | ||||||||||||
| 113 | |||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||
| 115 | |||||||||||||
| 116 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||