MCP/Server.pm

filter: Statementstmt 98.8% 174 / 176 Branchbran 93.9% 62 / 66 Conditioncond 59.4% 22 / 37 MC/DCmcdc 28.5% 6 / 21 Subroutinesub 97.5% 40 / 41 Podpod 100% 8 / 8 totaltotal 89.4% 312 / 349 CC 58 SCAR 18.8
CC
58
Cov
92%
handle
18.7
_handle_prompts_get
8.1
_handle_tools_call
8.1
CRAP 6.6
« MCP/Server/Transport/Stdio.pm ↑ Summary MCP/Tool.pm »
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
BranchTrueFalseTotal
unless ref $request eq "HASH" 0 105 105
24 105 ▶ return _jsonrpc_error(INVALID_REQUEST, 'Missing JSON-RPC method') unless my $method = $request->{method};
Branch: 1 branch
BranchTrueFalseTotal
unless my $method = $request->{"method"} 0 105 105
25  
26 # Requests
27 105 ▶ if (defined(my $id = $request->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $id = $request->{"id"})) 88 17 105
28  
29 88 ▶ my $token = ($request->{params} // {})->{_meta}{progressToken};
Condition50%1/2
$request->{"params"} // {}
l!l
880
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
30 88 ▶ $context->progress_token($token) if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 2 86 88
31  
32 88 ▶ if ($method eq 'initialize') {
Branch: 8 branches
BranchTrueFalseTotal
if ($method eq 'initialize') { } 18 70 88
elsif ($method eq 'tools/list') { } 6 64 70
elsif ($method eq 'tools/call') { } 28 36 64
elsif ($method eq 'ping') { } 1 35 36
elsif ($method eq 'prompts/list') { } 5 30 35
elsif ($method eq 'prompts/get') { } 13 17 30
elsif ($method eq 'resources/list') { } 5 12 17
elsif ($method eq 'resources/read') { } 11 1 12
33 18 ▶ my $result = $self->_handle_initialize($request->{params} // {});
Condition50%1/2
$request->{"params"} // {}
l!l
180
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
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"} // {}
l!l
280
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
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"} // {}
l!l
130
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
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"} // {}
l!l
110
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
Aresult
11
00
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
BranchTrueFalseTotal
unless my $transport = $self->transport 0 3 3
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
BranchTrueFalseTotal
if keys %scopes 1 0 1
Condition50%1/2
$metadata->{"scopes_supported"} //= [sort(keys %scopes)]
l!l
01
MC/DC0%0/1
$metadata->{"scopes_supported"} //= [sort(keys %scopes)]
$metadata->{"scopes_supported"}
Truth table50%1/2
A
A= $metadata->{"scopes_supported"}
Aresult
11
00
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
BranchTrueFalseTotal
$transport && $transport->notifications ? : 11 7 18
Condition66%2/3
$transport and $transport->notifications
!ll&&!rl&&r
0711
MC/DC50%1/2
$transport and $transport->notifications
$transport $transport->notifications
Truth table66%2/3
A and B
A= $transportB= $transport->notifications
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $context->has_scope(@{$prompt->scopes;}) 1 9 10
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"} // ""
l!l
130
MC/DC0%0/1
$params->{"name"} // ""
$params->{"name"}
Truth table50%1/2
A
A= $params->{"name"}
Aresult
11
00
135 13 ▶ my $args = $params->{arguments} // {};
Condition50%1/2
$params->{"arguments"} // {}
l!l
130
MC/DC0%0/1
$params->{"arguments"} // {}
$params->{"arguments"}
Truth table50%1/2
A
A= $params->{"arguments"}
Aresult
11
00
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
BranchTrueFalseTotal
unless my $prompt = &first(sub { $_->name eq $name; } , @{$self->_prompts($context);}) 2 11 13
138 11 ▶ if (my $err = $self->_check_scope($prompt, $context, $id)) { return $err }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->_check_scope($prompt, $context, $id)) 1 10 11
139 10 ▶ return _jsonrpc_error(INVALID_PARAMS, 'Invalid arguments', $id) if $prompt->validate_input($args);
Branch: 1 branch
BranchTrueFalseTotal
if $prompt->validate_input($args) 1 9 10
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
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Mojo::Promise") 2 7 9
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
702
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $context->has_scope(@{$resource->scopes;}) 1 9 10
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"} // ""
l!l
110
MC/DC0%0/1
$params->{"uri"} // ""
$params->{"uri"}
Truth table50%1/2
A
A= $params->{"uri"}
Aresult
11
00
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
BranchTrueFalseTotal
unless my $resource = &first(sub { $_->uri eq $uri; } , @{$self->_resources($context);}) 2 9 11
166 9 ▶ if (my $err = $self->_check_scope($resource, $context, $id)) { return $err }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->_check_scope($resource, $context, $id)) 1 8 9
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
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Mojo::Promise") 1 7 8
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
701
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")
ABresult
0X0
100
111
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"} // ""
l!l
280
MC/DC0%0/1
$params->{"name"} // ""
$params->{"name"}
Truth table50%1/2
A
A= $params->{"name"}
Aresult
11
00
175 28 ▶ my $args = $params->{arguments} // {};
Condition50%1/2
$params->{"arguments"} // {}
l!l
280
MC/DC0%0/1
$params->{"arguments"} // {}
$params->{"arguments"}
Truth table50%1/2
A
A= $params->{"arguments"}
Aresult
11
00
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
BranchTrueFalseTotal
unless my $tool = &first(sub { $_->name eq $name; } , @{$self->_tools($context);}) 3 25 28
178 25 ▶ if (my $err = $self->_check_scope($tool, $context, $id)) { return $err }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->_check_scope($tool, $context, $id)) 2 23 25
179 23 ▶ return _jsonrpc_error(INVALID_PARAMS, 'Invalid arguments', $id) if $tool->validate_input($args);
Branch: 1 branch
BranchTrueFalseTotal
if $tool->validate_input($args) 1 22 23
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
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Mojo::Promise") 3 19 22
Condition66%2/3
builtin::blessed($result) and $result->isa("Mojo::Promise")
!ll&&!rl&&r
1903
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")
ABresult
0X0
100
111
183 19 return _jsonrpc_response($result, $id);
184 }
185  
186 ▶sub _handle_tools_list ($self, $context) {
Subroutine _handle_tools_list: called CC=6 SCAR=17.9
187 6 my @tools;
188 6 for my $tool (@{$self->_tools($context)}) {
189 18 my $scopes = $tool->scopes;
190 18 ▶ next if @$scopes && !$context->has_scope(@$scopes);
Branch: 1 branch
BranchTrueFalseTotal
if @$scopes and not $context->has_scope(@$scopes) 1 17 18
Condition100%3/3
@$scopes and not $context->has_scope(@$scopes)
!ll&&!rl&&r
1161
MC/DC100%2/2
@$scopes and not $context->has_scope(@$scopes)
@$scopes not $context->has_scope(@$scopes)
Truth table100%3/3
A and not(B)
A= @$scopesB= not $context->has_scope(@$scopes)
ABresult
0X0
100
111
191 17 my $info = {name => $tool->name, description => $tool->description, inputSchema => $tool->input_schema};
192 17 ▶ if (my $output_schema = $tool->output_schema) { $info->{outputSchema} = $output_schema }
Branch: 1 branch
BranchTrueFalseTotal
if (my $output_schema = $tool->output_schema) 1 16 17
193  
194 17 my $annotations = $tool->annotations;
195 17 ▶ $info->{annotations} = $annotations if keys %$annotations;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$annotations 2 15 17
196 17 push @tools, $info;
197 }
198  
199 6 return {tools => \@tools};
200 }
201  
202 ▶sub _check_scope ($self, $primitive, $context, $id) {
Subroutine _check_scope: called CC=3 SCAR=11.0
203 45 my $scopes = $primitive->scopes;
204 45 ▶ return undef unless @$scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$scopes 34 11 45
205 11 ▶ return undef if $context->has_scope(@$scopes);
Branch: 1 branch
BranchTrueFalseTotal
if $context->has_scope(@$scopes) 7 4 11
206 4 $context->insufficient_scope($scopes);
207 4 return _jsonrpc_error(INSUFFICIENT_SCOPE, 'Insufficient scope', $id);
208 }
209  
210 14 ▶sub _jsonrpc_error ($code, $message, $id = undef) {
Subroutine _jsonrpc_error: called CC=2 SCAR=6.9
211 14 return {jsonrpc => '2.0', id => $id, error => {code => $code, message => $message}};
212 }
213  
214 74 ▶sub _jsonrpc_response ($result, $id = undef) {
Subroutine _jsonrpc_response: called CC=2 SCAR=6.9
215 74 return {jsonrpc => '2.0', id => $id, result => $result};
216 }
217  
218 ▶sub _prompts ($self, $context) {
Subroutine _prompts: called CC=1 SCAR=0.0
219 18 my $prompts = [@{$self->prompts}];
220 18 $self->emit('prompts', $prompts, $context);
221 18 return $prompts;
222 }
223  
224 ▶sub _resources ($self, $context) {
Subroutine _resources: called CC=1 SCAR=0.0
225 16 my $resources = [@{$self->resources}];
226 16 $self->emit('resources', $resources, $context);
227 16 return $resources;
228 }
229  
230 ▶sub _tools ($self, $context) {
Subroutine _tools: called CC=1 SCAR=0.0
231 34 my $tools = [@{$self->tools}];
232 34 $self->emit('tools', $tools, $context);
233 34 return $tools;
234 }
235  
236 4 1;
237  
238 =encoding utf8
239  
240 =head1 NAME
241  
242 MCP::Server - MCP server implementation
243  
244 =head1 SYNOPSIS
245  
246 use MCP::Server;
247  
248 my $server = MCP::Server->new(name => 'MyServer');
249  
250 $server->tool(
251 name => 'echo',
252 description => 'Echo the input text',
253 input_schema => {type => 'object', properties => {msg => {type => 'string'}}, required => ['msg']},
254 code => sub ($tool, $args) {
255 return "Echo: $args->{msg}";
256 }
257 );
258  
259 $server->prompt(
260 name => 'echo',
261 description => 'A prompt to demonstrate the echo tool',
262 code => sub ($prompt, $args) {
263 return 'Use the echo tool with the message "Hello, World!"';
264 }
265 );
266  
267 $server->resource(
268 uri => 'file:///example.txt',
269 name => 'example',
270 description => 'A simple text resource',
271 mime_type => 'text/plain',
272 code => sub ($resource) {
273 return 'This is an example resource content.';
274 }
275 );
276  
277 $server->to_stdio;
278  
279 =head1 DESCRIPTION
280  
281 L<MCP::Server> is an MCP (Model Context Protocol) server.
282  
283 =head1 EVENTS
284  
285 L<MCP::Server> inherits all events from L<Mojo::EventEmitter> and emits the following new ones.
286  
287 =head2 prompts
288  
289 $server->on(prompts => sub ($server, $prompts, $context) { ... });
290  
291 Emitted whenever the list of prompts is accessed.
292  
293 =head2 resources
294  
295 $server->on(resources => sub ($server, $resources, $context) { ... });
296  
297 Emitted whenever the list of resources is accessed.
298  
299 =head2 tools
300  
301 $server->on(tools => sub ($server, $tools, $context) { ... });
302  
303 Emitted whenever the list of tools is accessed.
304  
305 =head1 ATTRIBUTES
306  
307 L<MCP::Server> implements the following attributes.
308  
309 =head2 name
310  
311 my $name = $server->name;
312 $server = $server->name('MyServer');
313  
314 The name of the server, used for identification.
315  
316 =head2 prompts
317  
318 my $prompts = $server->prompts;
319 $server = $server->prompts([MCP::Prompt->new]);
320  
321 An array reference containing registered prompts.
322  
323 =head2 resources
324  
325 my $resources = $server->resources;
326 $server = $server->resources([MCP::Resource->new]);
327  
328 An array reference containing registered resources.
329  
330 =head2 tools
331  
332 my $tools = $server->tools;
333 $server = $server->tools([MCP::Tool->new]);
334  
335 An array reference containing registered tools.
336  
337 =head2 transport
338  
339 my $transport = $server->transport;
340 $server = $server->transport(MCP::Server::Transport::HTTP->new);
341  
342 The transport layer used by the server, such as L<MCP::Server::Transport::HTTP> or L<MCP::Server::Transport::Stdio>.
343  
344 =head2 version
345  
346 my $version = $server->version;
347 $server = $server->version('1.0.0');
348  
349 The version of the server.
350  
351 =head1 METHODS
352  
353 L<MCP::Tool> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
354  
355 =head2 handle
356  
357 my $response = $server->handle($request, $context);
358  
359 Handle a JSON-RPC request and return a response.
360  
361 =head2 notify_list_changed
362  
363 my $bool = $server->notify_list_changed('tools');
364  
365 Broadcast a C<notifications/$kind/list_changed> JSON-RPC notification to all connected clients. Returns true on
366 success, or C<undef> if no notification could be delivered.
367  
368 =head2 oauth_metadata
369  
370 my $metadata = $server->oauth_metadata(
371 resource => 'https://example.com/mcp',
372 authorization_servers => ['https://auth.example.com']
373 );
374  
375 Build an OAuth 2.0 Protected Resource Metadata document from the given fields, to be served from
376 C</.well-known/oauth-protected-resource>. Unless C<scopes_supported> is provided, it is filled in with the sorted
377 union of all scopes declared by registered tools, prompts, and resources.
378  
379 =head2 prompt
380  
381 my $prompt = $server->prompt(
382 name => 'my_prompt',
383 description => 'A sample prompt',
384 arguments => [{name => 'foo', description => 'Whatever', required => 1}],
385 code => sub ($prompt, $args) { ... }
386 );
387  
388 Register a new prompt with the server.
389  
390 =head2 resource
391  
392 my $resource = $server->resource(
393 uri => 'file://my_resource',
394 name => 'sample_resource',
395 description => 'A sample resource',
396 mime_type => 'text/plain',
397 code => sub ($resource) { ... }
398 );
399  
400 Register a new resource with the server.
401  
402 =head2 to_action
403  
404 my $action = $server->to_action;
405 my $action = $server->to_action({streaming => 1});
406  
407 Convert the server to a L<Mojolicious> action. Any options are passed through to the constructor of
408 L<MCP::Server::Transport::HTTP>; in particular, C<< streaming => 1 >> opts in to the server-to-client SSE stream
409 and explicit session termination.
410  
411 =head2 to_stdio
412  
413 $server->to_stdio;
414  
415 Handles JSON-RPC requests over standard input/output.
416  
417 =head2 tool
418  
419 my $tool = $server->tool(
420 name => 'my_tool',
421 description => 'A sample tool',
422 input_schema => {type => 'object', properties => {foo => {type => 'string'}}},
423 code => sub ($tool, $args) { ... }
424 );
425  
426 Register a new tool with the server.
427  
428 =head1 SEE ALSO
429  
430 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
431  
432 =cut
« MCP/Server/Transport/Stdio.pm ↑ Summary MCP/Tool.pm »