| 1 | package MCP::Server; | ||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Mojo::Base 'Mojo::EventEmitter', -signatures; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Mojo::JSON qw(false true); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | use MCP::Constants | ||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | qw(INSUFFICIENT_SCOPE INVALID_PARAMS INVALID_REQUEST METHOD_NOT_FOUND PARSE_ERROR PROTOCOL_VERSION RESOURCE_NOT_FOUND); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use MCP::Prompt; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use MCP::Resource; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use MCP::Server::Transport::HTTP; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MCP::Server::Transport::Stdio; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use MCP::Tool; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | 4 | has name => 'PerlServer'; | |||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | has prompts => sub { [] }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | has resources => sub { [] }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | has tools => sub { [] }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 4 | has 'transport'; | |||||||||||||||||||||||||||||||||||||
| 20 | 4 | has version => '1.0.0'; | |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | ▶ | sub handle ($self, $request, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
handle: called CC=18 SCAR=29.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 23 | 105 | ▶ | return _jsonrpc_error(PARSE_ERROR, 'Invalid JSON-RPC request') unless ref $request eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 24 | 105 | ▶ | return _jsonrpc_error(INVALID_REQUEST, 'Missing JSON-RPC method') unless my $method = $request->{method}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | # Requests | ||||||||||||||||||||||||||||||||||||||
| 27 | 105 | ▶ | if (defined(my $id = $request->{id})) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | 88 | ▶ | my $token = ($request->{params} // {})->{_meta}{progressToken}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||||||||
| 30 | 88 | ▶ | $context->progress_token($token) if defined $token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | 88 | ▶ | if ($method eq 'initialize') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 33 | 18 | ▶ | my $result = $self->_handle_initialize($request->{params} // {}); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||||||||
| 34 | 18 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||
| 36 | elsif ($method eq 'tools/list') { | ||||||||||||||||||||||||||||||||||||||
| 37 | 6 | my $result = $self->_handle_tools_list($context); | |||||||||||||||||||||||||||||||||||||
| 38 | 6 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||
| 40 | elsif ($method eq 'tools/call') { | ||||||||||||||||||||||||||||||||||||||
| 41 | 28 | ▶ | return $self->_handle_tools_call($request->{params} // {}, $id, $context); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||
| 43 | elsif ($method eq 'ping') { | ||||||||||||||||||||||||||||||||||||||
| 44 | 1 | return _jsonrpc_response({}, $id); | |||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||
| 46 | elsif ($method eq 'prompts/list') { | ||||||||||||||||||||||||||||||||||||||
| 47 | 5 | my $result = $self->_handle_prompts_list($context); | |||||||||||||||||||||||||||||||||||||
| 48 | 5 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||
| 50 | elsif ($method eq 'prompts/get') { | ||||||||||||||||||||||||||||||||||||||
| 51 | 13 | ▶ | return $self->_handle_prompts_get($request->{params} // {}, $id, $context); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | elsif ($method eq 'resources/list') { | ||||||||||||||||||||||||||||||||||||||
| 54 | 5 | my $result = $self->_handle_resources_list($context); | |||||||||||||||||||||||||||||||||||||
| 55 | 5 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||
| 57 | elsif ($method eq 'resources/read') { | ||||||||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | return $self->_handle_resources_read($request->{params} // {}, $id, $context); | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | # Method not found | ||||||||||||||||||||||||||||||||||||||
| 62 | 1 | return _jsonrpc_error(METHOD_NOT_FOUND, "Method '$method' not found", $id); | |||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | # Notifications (ignored for now) | ||||||||||||||||||||||||||||||||||||||
| 66 | 17 | return undef; | |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | ▶ | sub notify_list_changed ($self, $kind) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
notify_list_changed: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | return undef unless my $transport = $self->transport; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 71 | 3 | return $transport->notify_all("notifications/$kind/list_changed"); | |||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | ▶ | sub oauth_metadata ($self, %args) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
oauth_metadata: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my %scopes; | |||||||||||||||||||||||||||||||||||||
| 76 | 1 | for my $primitive (@{$self->tools}, @{$self->prompts}, @{$self->resources}) { | |||||||||||||||||||||||||||||||||||||
| 77 | 7 | $scopes{$_} = 1 for @{$primitive->scopes}; | |||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||
| 79 | 1 | my $metadata = {%args}; | |||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | $metadata->{scopes_supported} //= [sort keys %scopes] if keys %scopes; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$metadata->{"scopes_supported"} //= [sort(keys %scopes)]
MC/DC0%0/1
$metadata->{"scopes_supported"} //= [sort(keys %scopes)]
$metadata->{"scopes_supported"}
Truth table50%1/2
A
A= $metadata->{"scopes_supported"}
| |||||||||||||||||||||||||||||||||||||||
| 81 | 1 | return $metadata; | |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | ▶ | sub prompt ($self, %args) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
prompt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 85 | 7 | my $prompt = MCP::Prompt->new(%args); | |||||||||||||||||||||||||||||||||||||
| 86 | 7 | push @{$self->prompts}, $prompt; | |||||||||||||||||||||||||||||||||||||
| 87 | 7 | return $prompt; | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | ▶ | sub resource ($self, %args) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
resource: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 91 | 7 | my $resource = MCP::Resource->new(%args); | |||||||||||||||||||||||||||||||||||||
| 92 | 7 | push @{$self->resources}, $resource; | |||||||||||||||||||||||||||||||||||||
| 93 | 7 | return $resource; | |||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | sub to_action ($self, $options = {}) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
to_action: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 97 | 5 | $self->transport(my $http = MCP::Server::Transport::HTTP->new(server => $self, %$options)); | |||||||||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | return sub ($c) { $http->handle_request($c) }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | ▶ | sub to_stdio ($self) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
to_stdio: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $self->transport(my $stdio = MCP::Server::Transport::Stdio->new(server => $self)); | |||||||||||||||||||||||||||||||||||||
| 103 | 0 | $self->transport->handle_requests; | |||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | ▶ | sub tool ($self, %args) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
tool: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 107 | 17 | my $tool = MCP::Tool->new(%args); | |||||||||||||||||||||||||||||||||||||
| 108 | 17 | push @{$self->tools}, $tool; | |||||||||||||||||||||||||||||||||||||
| 109 | 17 | return $tool; | |||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | ▶ | sub _handle_initialize ($self, $params) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_initialize: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 113 | 18 | my $transport = $self->transport; | |||||||||||||||||||||||||||||||||||||
| 114 | 18 | ▶ | my $caps = $transport && $transport->notifications ? {listChanged => true} : {}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$transport and $transport->notifications
MC/DC50%1/2
$transport and $transport->notifications
$transport
$transport->notifications
Truth table66%2/3
A and B
A= $transportB= $transport->notifications
| |||||||||||||||||||||||||||||||||||||||
| 115 | return { | ||||||||||||||||||||||||||||||||||||||
| 116 | 18 | protocolVersion => PROTOCOL_VERSION, | |||||||||||||||||||||||||||||||||||||
| 117 | capabilities => {prompts => $caps, resources => $caps, tools => $caps}, | ||||||||||||||||||||||||||||||||||||||
| 118 | serverInfo => {name => $self->name, version => $self->version} | ||||||||||||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | ▶ | sub _handle_prompts_list ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_prompts_list: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 123 | 5 | my @prompts; | |||||||||||||||||||||||||||||||||||||
| 124 | 5 | for my $prompt (@{$self->_prompts($context)}) { | |||||||||||||||||||||||||||||||||||||
| 125 | 10 | ▶ | next unless $context->has_scope(@{$prompt->scopes}); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | 9 | my $info = {name => $prompt->name, description => $prompt->description, arguments => $prompt->arguments}; | |||||||||||||||||||||||||||||||||||||
| 127 | 9 | push @prompts, $info; | |||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 5 | return {prompts => \@prompts}; | |||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | ▶ | sub _handle_prompts_get ($self, $params, $id, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_prompts_get: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||
| 134 | 13 | ▶ | my $name = $params->{name} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"name"} // ""
MC/DC0%0/1
$params->{"name"} // ""
$params->{"name"}
Truth table50%1/2
A
A= $params->{"name"}
| |||||||||||||||||||||||||||||||||||||||
| 135 | 13 | ▶ | my $args = $params->{arguments} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"arguments"} // {}
MC/DC0%0/1
$params->{"arguments"} // {}
$params->{"arguments"}
Truth table50%1/2
A
A= $params->{"arguments"}
| |||||||||||||||||||||||||||||||||||||||
| 136 | return _jsonrpc_error(METHOD_NOT_FOUND, "Prompt '$name' not found", $id) | ||||||||||||||||||||||||||||||||||||||
| 137 | 13 | ▶ | unless my $prompt = first { $_->name eq $name } @{$self->_prompts($context)}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 138 | 11 | ▶ | if (my $err = $self->_check_scope($prompt, $context, $id)) { return $err } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 139 | 10 | ▶ | return _jsonrpc_error(INVALID_PARAMS, 'Invalid arguments', $id) if $prompt->validate_input($args); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | 9 | my $result = $prompt->call($args, $context); | |||||||||||||||||||||||||||||||||||||
| 142 | 9 | ▶ | return $result->then(sub { _jsonrpc_response($_[0], $id) }) if blessed($result) && $result->isa('Mojo::Promise'); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Mojo::Promise")
builtin::blessed($result)
$result->isa("Mojo::Promise")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Mojo::Promise")
| |||||||||||||||||||||||||||||||||||||||
| 143 | 7 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | ▶ | sub _handle_resources_list ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_resources_list: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 147 | 5 | my @resources; | |||||||||||||||||||||||||||||||||||||
| 148 | 5 | for my $resource (@{$self->_resources($context)}) { | |||||||||||||||||||||||||||||||||||||
| 149 | 10 | ▶ | next unless $context->has_scope(@{$resource->scopes}); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 150 | 9 | my $info = { | |||||||||||||||||||||||||||||||||||||
| 151 | uri => $resource->uri, | ||||||||||||||||||||||||||||||||||||||
| 152 | name => $resource->name, | ||||||||||||||||||||||||||||||||||||||
| 153 | description => $resource->description, | ||||||||||||||||||||||||||||||||||||||
| 154 | mimeType => $resource->mime_type | ||||||||||||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||||||||||||
| 156 | 9 | push @resources, $info; | |||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | 5 | return {resources => \@resources}; | |||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||
| 162 | ▶ | sub _handle_resources_read ($self, $params, $id, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_resources_read: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||
| 163 | 11 | ▶ | my $uri = $params->{uri} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"uri"} // ""
MC/DC0%0/1
$params->{"uri"} // ""
$params->{"uri"}
Truth table50%1/2
A
A= $params->{"uri"}
| |||||||||||||||||||||||||||||||||||||||
| 164 | return _jsonrpc_error(RESOURCE_NOT_FOUND, 'Resource not found', $id) | ||||||||||||||||||||||||||||||||||||||
| 165 | 11 | ▶ | unless my $resource = first { $_->uri eq $uri } @{$self->_resources($context)}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 166 | 9 | ▶ | if (my $err = $self->_check_scope($resource, $context, $id)) { return $err } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | 8 | my $result = $resource->call($context); | |||||||||||||||||||||||||||||||||||||
| 169 | 8 | ▶ | return $result->then(sub { _jsonrpc_response($_[0], $id) }) if blessed($result) && $result->isa('Mojo::Promise'); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Mojo::Promise")
builtin::blessed($result)
$result->isa("Mojo::Promise")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Mojo::Promise")
| |||||||||||||||||||||||||||||||||||||||
| 170 | 7 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | ▶ | sub _handle_tools_call ($self, $params, $id, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tools_call: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||
| 174 | 28 | ▶ | my $name = $params->{name} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"name"} // ""
MC/DC0%0/1
$params->{"name"} // ""
$params->{"name"}
Truth table50%1/2
A
A= $params->{"name"}
| |||||||||||||||||||||||||||||||||||||||
| 175 | 28 | ▶ | my $args = $params->{arguments} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"arguments"} // {}
MC/DC0%0/1
$params->{"arguments"} // {}
$params->{"arguments"}
Truth table50%1/2
A
A= $params->{"arguments"}
| |||||||||||||||||||||||||||||||||||||||
| 176 | return _jsonrpc_error(METHOD_NOT_FOUND, "Tool '$name' not found", $id) | ||||||||||||||||||||||||||||||||||||||
| 177 | 28 | ▶ | unless my $tool = first { $_->name eq $name } @{$self->_tools($context)}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 178 | 25 | ▶ | if (my $err = $self->_check_scope($tool, $context, $id)) { return $err } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 179 | 23 | ▶ | return _jsonrpc_error(INVALID_PARAMS, 'Invalid arguments', $id) if $tool->validate_input($args); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | 22 | my $result = $tool->call($args, $context); | |||||||||||||||||||||||||||||||||||||
| 182 | 22 | ▶ | return $result->then(sub { _jsonrpc_response($_[0], $id) }) if blessed($result) && $result->isa('Mojo::Promise'); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Mojo::Promise")
builtin::blessed($result)
$result->isa("Mojo::Promise")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Mojo::Promise")
| |||||||||||||||||||||||||||||||||||||||
| 183 | 19 | return _jsonrpc_response($result, $id); | |||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | ▶ | sub _handle_tools_list ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_tools_list: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||
| 187 | 6 | my @tools; | |||||||||||||||||||||||||||||||||||||
| 188 | 6 | for my $tool (@{$self->_tools($context)}) { | |||||||||||||||||||||||||||||||||||||
| 189 | 18 | ▶ | next unless $context->has_scope(@{$tool->scopes}); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 190 | 17 | my $info = {name => $tool->name, description => $tool->description, inputSchema => $tool->input_schema}; | |||||||||||||||||||||||||||||||||||||
| 191 | 17 | ▶ | if (my $output_schema = $tool->output_schema) { $info->{outputSchema} = $output_schema } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | 17 | my $annotations = $tool->annotations; | |||||||||||||||||||||||||||||||||||||
| 194 | 17 | ▶ | $info->{annotations} = $annotations if keys %$annotations; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 195 | 17 | push @tools, $info; | |||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | 6 | return {tools => \@tools}; | |||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||
| 201 | ▶ | sub _check_scope ($self, $primitive, $context, $id) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_scope: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 202 | 45 | my $scopes = $primitive->scopes; | |||||||||||||||||||||||||||||||||||||
| 203 | 45 | ▶ | return undef if $context->has_scope(@$scopes); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 204 | 4 | $context->insufficient_scope($scopes); | |||||||||||||||||||||||||||||||||||||
| 205 | 4 | return _jsonrpc_error(INSUFFICIENT_SCOPE, 'Insufficient scope', $id); | |||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||
| 208 | 14 | ▶ | sub _jsonrpc_error ($code, $message, $id = undef) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_jsonrpc_error: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 209 | 14 | return {jsonrpc => '2.0', id => $id, error => {code => $code, message => $message}}; | |||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||
| 212 | 74 | ▶ | sub _jsonrpc_response ($result, $id = undef) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_jsonrpc_response: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 213 | 74 | return {jsonrpc => '2.0', id => $id, result => $result}; | |||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||
| 216 | ▶ | sub _prompts ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_prompts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 217 | 18 | my $prompts = [@{$self->prompts}]; | |||||||||||||||||||||||||||||||||||||
| 218 | 18 | $self->emit('prompts', $prompts, $context); | |||||||||||||||||||||||||||||||||||||
| 219 | 18 | return $prompts; | |||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||
| 222 | ▶ | sub _resources ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_resources: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 223 | 16 | my $resources = [@{$self->resources}]; | |||||||||||||||||||||||||||||||||||||
| 224 | 16 | $self->emit('resources', $resources, $context); | |||||||||||||||||||||||||||||||||||||
| 225 | 16 | return $resources; | |||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||
| 228 | ▶ | sub _tools ($self, $context) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
_tools: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 229 | 34 | my $tools = [@{$self->tools}]; | |||||||||||||||||||||||||||||||||||||
| 230 | 34 | $self->emit('tools', $tools, $context); | |||||||||||||||||||||||||||||||||||||
| 231 | 34 | return $tools; | |||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||
| 234 | 4 | 1; | |||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||
| 236 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||
| 240 | MCP::Server - MCP server implementation | ||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||
| 244 | use MCP::Server; | ||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||
| 246 | my $server = MCP::Server->new(name => 'MyServer'); | ||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||
| 248 | $server->tool( | ||||||||||||||||||||||||||||||||||||||
| 249 | name => 'echo', | ||||||||||||||||||||||||||||||||||||||
| 250 | description => 'Echo the input text', | ||||||||||||||||||||||||||||||||||||||
| 251 | input_schema => {type => 'object', properties => {msg => {type => 'string'}}, required => ['msg']}, | ||||||||||||||||||||||||||||||||||||||
| 252 | code => sub ($tool, $args) { | ||||||||||||||||||||||||||||||||||||||
| 253 | return "Echo: $args->{msg}"; | ||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | $server->prompt( | ||||||||||||||||||||||||||||||||||||||
| 258 | name => 'echo', | ||||||||||||||||||||||||||||||||||||||
| 259 | description => 'A prompt to demonstrate the echo tool', | ||||||||||||||||||||||||||||||||||||||
| 260 | code => sub ($prompt, $args) { | ||||||||||||||||||||||||||||||||||||||
| 261 | return 'Use the echo tool with the message "Hello, World!"'; | ||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||
| 263 | ); | ||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||
| 265 | $server->resource( | ||||||||||||||||||||||||||||||||||||||
| 266 | uri => 'file:///example.txt', | ||||||||||||||||||||||||||||||||||||||
| 267 | name => 'example', | ||||||||||||||||||||||||||||||||||||||
| 268 | description => 'A simple text resource', | ||||||||||||||||||||||||||||||||||||||
| 269 | mime_type => 'text/plain', | ||||||||||||||||||||||||||||||||||||||
| 270 | code => sub ($resource) { | ||||||||||||||||||||||||||||||||||||||
| 271 | return 'This is an example resource content.'; | ||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||
| 275 | $server->to_stdio; | ||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||
| 277 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||
| 279 | L<MCP::Server> is an MCP (Model Context Protocol) server. | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | =head1 EVENTS | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | L<MCP::Server> inherits all events from L<Mojo::EventEmitter> and emits the following new ones. | ||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||
| 285 | =head2 prompts | ||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||
| 287 | $server->on(prompts => sub ($server, $prompts, $context) { ... }); | ||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||
| 289 | Emitted whenever the list of prompts is accessed. | ||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||
| 291 | =head2 resources | ||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||
| 293 | $server->on(resources => sub ($server, $resources, $context) { ... }); | ||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||
| 295 | Emitted whenever the list of resources is accessed. | ||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | =head2 tools | ||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||
| 299 | $server->on(tools => sub ($server, $tools, $context) { ... }); | ||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||
| 301 | Emitted whenever the list of tools is accessed. | ||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||
| 305 | L<MCP::Server> implements the following attributes. | ||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | =head2 name | ||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||
| 309 | my $name = $server->name; | ||||||||||||||||||||||||||||||||||||||
| 310 | $server = $server->name('MyServer'); | ||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||
| 312 | The name of the server, used for identification. | ||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||
| 314 | =head2 prompts | ||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||
| 316 | my $prompts = $server->prompts; | ||||||||||||||||||||||||||||||||||||||
| 317 | $server = $server->prompts([MCP::Prompt->new]); | ||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||
| 319 | An array reference containing registered prompts. | ||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||
| 321 | =head2 resources | ||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||
| 323 | my $resources = $server->resources; | ||||||||||||||||||||||||||||||||||||||
| 324 | $server = $server->resources([MCP::Resource->new]); | ||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | An array reference containing registered resources. | ||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||
| 328 | =head2 tools | ||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||
| 330 | my $tools = $server->tools; | ||||||||||||||||||||||||||||||||||||||
| 331 | $server = $server->tools([MCP::Tool->new]); | ||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||
| 333 | An array reference containing registered tools. | ||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||
| 335 | =head2 transport | ||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||
| 337 | my $transport = $server->transport; | ||||||||||||||||||||||||||||||||||||||
| 338 | $server = $server->transport(MCP::Server::Transport::HTTP->new); | ||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||
| 340 | The transport layer used by the server, such as L<MCP::Server::Transport::HTTP> or L<MCP::Server::Transport::Stdio>. | ||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||
| 342 | =head2 version | ||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||
| 344 | my $version = $server->version; | ||||||||||||||||||||||||||||||||||||||
| 345 | $server = $server->version('1.0.0'); | ||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | The version of the server. | ||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||
| 349 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||
| 351 | L<MCP::Tool> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||
| 353 | =head2 handle | ||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||
| 355 | my $response = $server->handle($request, $context); | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | Handle a JSON-RPC request and return a response. | ||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | =head2 notify_list_changed | ||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | my $bool = $server->notify_list_changed('tools'); | ||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | Broadcast a C<notifications/$kind/list_changed> JSON-RPC notification to all connected clients. Returns true on | ||||||||||||||||||||||||||||||||||||||
| 364 | success, or C<undef> if no notification could be delivered. | ||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||
| 366 | =head2 oauth_metadata | ||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||
| 368 | my $metadata = $server->oauth_metadata( | ||||||||||||||||||||||||||||||||||||||
| 369 | resource => 'https://example.com/mcp', | ||||||||||||||||||||||||||||||||||||||
| 370 | authorization_servers => ['https://auth.example.com'] | ||||||||||||||||||||||||||||||||||||||
| 371 | ); | ||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||
| 373 | Build an OAuth 2.0 Protected Resource Metadata document from the given fields, to be served from | ||||||||||||||||||||||||||||||||||||||
| 374 | C</.well-known/oauth-protected-resource>. Unless C<scopes_supported> is provided, it is filled in with the sorted | ||||||||||||||||||||||||||||||||||||||
| 375 | union of all scopes declared by registered tools, prompts, and resources. | ||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||
| 377 | =head2 prompt | ||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||
| 379 | my $prompt = $server->prompt( | ||||||||||||||||||||||||||||||||||||||
| 380 | name => 'my_prompt', | ||||||||||||||||||||||||||||||||||||||
| 381 | description => 'A sample prompt', | ||||||||||||||||||||||||||||||||||||||
| 382 | arguments => [{name => 'foo', description => 'Whatever', required => 1}], | ||||||||||||||||||||||||||||||||||||||
| 383 | code => sub ($prompt, $args) { ... } | ||||||||||||||||||||||||||||||||||||||
| 384 | ); | ||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||
| 386 | Register a new prompt with the server. | ||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||
| 388 | =head2 resource | ||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||
| 390 | my $resource = $server->resource( | ||||||||||||||||||||||||||||||||||||||
| 391 | uri => 'file://my_resource', | ||||||||||||||||||||||||||||||||||||||
| 392 | name => 'sample_resource', | ||||||||||||||||||||||||||||||||||||||
| 393 | description => 'A sample resource', | ||||||||||||||||||||||||||||||||||||||
| 394 | mime_type => 'text/plain', | ||||||||||||||||||||||||||||||||||||||
| 395 | code => sub ($resource) { ... } | ||||||||||||||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||
| 398 | Register a new resource with the server. | ||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||
| 400 | =head2 to_action | ||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||
| 402 | my $action = $server->to_action; | ||||||||||||||||||||||||||||||||||||||
| 403 | my $action = $server->to_action({streaming => 1}); | ||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||
| 405 | Convert the server to a L<Mojolicious> action. Any options are passed through to the constructor of | ||||||||||||||||||||||||||||||||||||||
| 406 | L<MCP::Server::Transport::HTTP>; in particular, C<< streaming => 1 >> opts in to the server-to-client SSE stream | ||||||||||||||||||||||||||||||||||||||
| 407 | and explicit session termination. | ||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||
| 409 | =head2 to_stdio | ||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||
| 411 | $server->to_stdio; | ||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||
| 413 | Handles JSON-RPC requests over standard input/output. | ||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||
| 415 | =head2 tool | ||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||
| 417 | my $tool = $server->tool( | ||||||||||||||||||||||||||||||||||||||
| 418 | name => 'my_tool', | ||||||||||||||||||||||||||||||||||||||
| 419 | description => 'A sample tool', | ||||||||||||||||||||||||||||||||||||||
| 420 | input_schema => {type => 'object', properties => {foo => {type => 'string'}}}, | ||||||||||||||||||||||||||||||||||||||
| 421 | code => sub ($tool, $args) { ... } | ||||||||||||||||||||||||||||||||||||||
| 422 | ); | ||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||
| 424 | Register a new tool with the server. | ||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||
| 426 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||
| 428 | L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||
| 430 | =cut | ||||||||||||||||||||||||||||||||||||||