blib/lib/AnyEvent/MCP/Server.pm

filter: Statementstmt 98.8% 171 / 173 Branchbran 91.2% 104 / 114 Conditioncond 71.8% 92 / 128 MC/DCmcdc 51.6% 32 / 62 Subroutinesub 94.1% 16 / 17 Podpod 100% 4 / 4 totaltotal 84.1% 419 / 498 CC 102 SCAR 40.2
CC
102
Cov
88%
rpc
94.2
_dispatch
11.1
dispatch
11.0
CRAP 55.9
« lib/AnyEvent/MCP/Worker/IMAP.pm ↑ Summary blib/lib/AnyEvent/MCP/Server/Permissions.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Server - MCP request dispatch and JSON-RPC framing
4  
5 =head1 DESCRIPTION
6  
7 An MCP server needs to understand requests, but it does not necessarily need
8 to own a listening socket. This class handles the protocol: it accepts decoded
9 JSON-RPC objects (or UTF-8 JSON), dispatches requests and delivers responses
10 through callbacks. Its subclasses L<AnyEvent::MCP::Server::JSONL> and
11 L<AnyEvent::MCP::Server::HTTP> handle the actual connections. If you are
12 writing a standalone server, L<AnyEvent::MCP::Server::Simple> is usually
13 the easier starting point.
14  
15 The server implements discovery, initialization, ping and tool listing/calls.
16 The 2025 compatibility path supports ordinary requests and responses, but
17 not server-initiated requests. Permission asking supports modern input
18 challenges as well as local helpers. SSE, subscriptions and schema validation
19 are not implemented yet.
20  
21 The usual object-level assembly is a
22 L<AnyEvent::MCP::Server::Toolset> holding tool callbacks, a Server
23 handling protocol messages, and a transport accepting connections.
24 Use L<AnyEvent::MCP::Server::HTTP/FIRST USE> for an executable HTTP
25 assembly or L<AnyEvent::MCP::Server::JSONL/EXAMPLE> for stdio.
26 The corresponding client entry point is L<AnyEvent::MCP::Client>.
27  
28 Only embedders that already own their transport need C<rpc> or
29 C<json_rpc> directly; these methods do not create connections or run
30 the event loop.
31  
32 =head1 METHODS
33  
34 =over
35  
36 =item $server = AnyEvent::MCP::Server->new (%config)
37  
38 Creates the protocol object without opening a listener or starting an
39 event loop. Options are:
40  
41 =over
42  
43 =item toolsets => [$toolset...]
44  
45 L<AnyEvent::MCP::Server::Toolset> objects searched in order, defaulting to
46 an empty array. The first Toolset containing a called tool handles it.
47  
48 =item server_info => { name => $name, version => $version }
49  
50 Advertised server identity, defaulting to C<AnyEvent::MCP> and this
51 distribution's version.
52  
53 =item instructions => $text
54  
55 Optional human-readable guidance. Surrounding whitespace and common
56 indentation are stripped. Empty instructions are omitted from discovery
57 and initialization results.
58  
59 =item short_instructions => $text
60  
61 Optional condensed guidance for deciding when to discover this server's tools.
62 Used instead of C<instructions> in discovery when C<auto_instructions> is true.
63 Without either short or custom instructions, the full instructions remain
64 advertised.
65  
66 =item custom_instructions => $text
67  
68 Instance-specific purpose, prepended to the advertised instructions.
69  
70 =item auto_instructions => $boolean
71  
72 When true, expose full C<instructions>, prefixed by any custom instance
73 purpose, in a C<meta-instructions> tool's description and call result.
74 Advertise C<short_instructions> instead, when provided, preceded by any
75 custom purpose. Append a pointer to the generated tool's description when
76 short or custom instructions exist. With neither, advertise the original
77 full instructions without a redundant pointer. Defaults to false;
78 a conflicting tool name is an error.
79  
80 =item capabilities => { %capabilities }
81  
82 Advertised server capabilities, defaulting to C<{ tools =E<gt> {} }>.
83 Declaring a capability does not implement it.
84  
85 =item versions => [$version...]
86  
87 Supported revisions in preference order. Defaults to C<2026-07-28>,
88 C<2025-11-25>, C<2025-06-18> and C<2025-03-26>. The 2026 protocol uses
89 discovery and per-request metadata; the 2025 protocol uses initialization.
90  
91 =item permissions => $permissions
92  
93 L<AnyEvent::MCP::Server::Permissions> object used by
94 C<< $req->require_permission >>. The server handles permission-needed
95 exceptions. The permission object's C<ask> setting chooses between modern
96 input challenges and local prompts; local asking retries the handler
97 internally. Checks must precede side effects.
98  
99 =back
100  
101 Event callbacks are also constructor options; see L</EVENTS>.
102  
103 =item $req = $server->rpc ($message, $reply->($message), %context)
104  
105 Accepts the decoded JSON-RPC hash reference C<$message>. C<$reply> receives
106 the decoded response, or undef when a notification is accepted or a request
107 is cancelled. Undef completes transport bookkeeping without a JSON-RPC
108 response. Returns the Request object for a request, or undef for
109 notifications and rejected messages.
110 C<$reply> can be called before this method returns, or later.
111  
112 C<peer> in C<%context> is a hash reference shared by messages on one connection.
113 It holds legacy initialization state and weak references to outstanding
114 requests for cancellation. Embedders supply a different peer for each
115 independent connection. Modern request metadata takes precedence over legacy
116 peer state.
117  
118 C<protocol_version> supplies the legacy HTTP protocol version.
119 C<on_notify> is an optional C<$callback->($req, $message)> receiving decoded
120 outgoing notifications. Without it, notifications are dropped.
121  
122 =item $req = $server->json_rpc ($json, $reply->($json), %context)
123  
124 Like C<rpc>, but C<$json> and the response are UTF-8 encoded JSON strings.
125 The notification callback still receives decoded objects. Parse errors are
126 returned as JSON-RPC errors.
127  
128 =item $server->dispatch ($req)
129  
130 Dispatches an already constructed Request. Normally called by C<rpc>.
131 Subclasses can override this method while retaining JSON-RPC handling.
132  
133 =back
134  
135 =head1 EVENTS
136  
137 =over
138  
139 =item on_request => $callback->($server, $req)
140  
141 Called before each dispatch attempt, including permission retries. This
142 synchronous hook can inspect the request or refresh application state.
143 It does not replace dispatch; avoid side effects that must happen only once.
144  
145 =item on_tools_list => $callback->($server, $req)
146  
147 Replaces the default Toolset listing. C<$callback> completes C<$req>.
148  
149 =item on_tools_call => $callback->($server, $req, $args)
150  
151 Replaces the default Toolset lookup and invocation. C<$args> is also available
152 as C<< $req->{args} >>. C<$callback> completes C<$req>.
153  
154 =item on_error => $callback->($server, $req, $error)
155  
156 Reports an exception caught during synchronous dispatch. A tool exception
157 becomes a text tool-error result; other exceptions become internal JSON-RPC
158 errors. Exceptions after a reply are propagated, as are exceptions from
159 asynchronous application callbacks.
160  
161 =back
162  
163 =cut
164  
165 package AnyEvent::MCP::Server;
166  
167 268 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 268 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
169 268 ▶use AnyEvent::MCP::Server::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
170 268 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
171 268 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 268 our @ISA = qw(AnyEvent::MCP::Base);
174  
175 353 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
176 353 my $class = shift;
177  
178 353 my $self = $class->SUPER::new (
179 toolsets => [],
180 server_info => { name => "AnyEvent::MCP", version => "$AnyEvent::MCP::VERSION" },
181 capabilities => { tools => {} },
182 versions => [qw(2026-07-28 2025-11-25 2025-06-18 2025-03-26)],
183 @_,
184 );
185  
186 $self->{instructions} = AnyEvent::MCP::_clean_text $self->{instructions}
187 353 ▶ if exists $self->{instructions};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"instructions"} 224 129 353
188  
189 353 my $instructions = $self->{instructions};
190 my $custom = exists $self->{custom_instructions}
191 353 ▶ ? AnyEvent::MCP::_clean_text $self->{custom_instructions} : "";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'custom_instructions'} ? : 7 346 353
192  
193 353 ▶ if ($self->{auto_instructions} && length $instructions) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'auto_instructions'} and length $instructions) { } 217 136 353
elsif (length $custom) { } 2 134 136
Condition100%3/3
$self->{"auto_instructions"} and length $instructions
!ll&&!rl&&r
8749217
MC/DC100%2/2
$self->{"auto_instructions"} and length $instructions
$self->{"auto_instructions"} length $instructions
Truth table100%3/3
A and B
A= $self->{"auto_instructions"}B= length $instructions
ABresult
0X0
100
111
194 217 require AnyEvent::MCP::Server::Toolset;
195  
196 217 my $name = "meta-instructions";
197 217 ▶ !grep { exists $_->{tool}{$name} } @{$self->{toolsets}}
Branch: 1 branch
BranchTrueFalseTotal
grep {exists $_->{"tool"}{$name};} @{$$self{"toolsets"};} and die "duplicate MCP tool '${name}'\n" 1 216 217
198 or die "duplicate MCP tool '$name'\n";
199  
200 216 my $full = join "\n\n", grep length, $custom, $instructions;
201 216 my $toolset = AnyEvent::MCP::Server::Toolset->new;
202 $toolset->tool (meta_instructions =>
203 "Server instructions. Call for full text if truncated.\n\n$full",
204 { type => "object", properties => {} },
205 4 ▶ sub { $_[0]->reply ({ content => [{ type => "text", text => $full }] }) },
Subroutine __ANON__: called CC=1 SCAR=0.0
206 216 );
207 216 $self->{toolsets} = [@{$self->{toolsets}}, $toolset];
208  
209 my $short = exists $self->{short_instructions}
210 216 ▶ ? AnyEvent::MCP::_clean_text $self->{short_instructions} : "";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'short_instructions'} ? : 200 16 216
211 216 ▶ $self->{instructions} = length $custom || length $short
Branch: 1 branch
BranchTrueFalseTotal
length $custom || length $short ? : 201 15 216
Condition100%3/3
length $custom or length $short
l!l&&r!l&&!r
519615
MC/DC100%2/2
length $custom or length $short
length $custom length $short
Truth table100%3/3
A or B
A= length $customB= length $short
ABresult
1X1
011
000
212 ? (join "\n\n", grep length, $custom, $short, "Full instructions: meta-instructions tool description.")
213 : $instructions;
214  
215 } elsif (length $custom) {
216 2 $self->{instructions} = join "\n\n", grep length, $custom, $instructions;
217 }
218  
219 $self
220 352 }
221  
222 22 ▶sub _rpc_error {
Subroutine _rpc_error: called CC=2 SCAR=6.9
223 22 my ($self, $reply, $id, $code, $message, $data) = @_;
224  
225 22 my $error = { code => $code, message => $message };
226 22 ▶ $error->{data} = $data if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 5 17 22
227  
228 22 $reply->({ jsonrpc => "2.0", id => $id, error => $error });
229 undef
230 22 }
231  
232 4242 ▶sub json_rpc {
Subroutine json_rpc: called CC=2 SCAR=6.9
Pod documented
233 4242 my ($self, $json, $reply, @context) = @_;
234  
235 4242 ▶ my $send = sub { $reply->(defined $_[0] ? JSON::XS::encode_json $_[0] : undef) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4118 26 4144
236 4242 my $message = eval { JSON::XS::decode_json $json };
237  
238 4242 ▶ return $self->_rpc_error ($send, undef, -32700, "Parse error")
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 4241 4242
239 if $@;
240  
241 4241 $self->rpc ($message, $send, @context)
242 }
243  
244 4510 ▶sub rpc {
Subroutine rpc: called CC=66 SCAR=45.5
Pod documented
245 4510 my ($self, $message, $reply, %context) = @_;
246  
247 return $self->_rpc_error ($reply, undef, -32600, "Invalid request")
248 unless ref $message eq "HASH"
249 && $message->{jsonrpc} eq "2.0"
250 && defined $message->{method} && !ref $message->{method}
251 4510 ▶ && (!exists $message->{id} || defined $message->{id} && !ref $message->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" and defined $message->{"method"} and not ref $message->{"method"} and !exists $message->{"id"} || defined $message->{"id"} && !ref($message->{"id"}) 1 4509 4510
Condition61%11/18
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" and defined $message->{"method"} and not ref $message->{"method"} and !exists $message->{"id"} || defined $message->{"id"} && !ref($message->{"id"})
!ll&&!rl&&r
104509
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" and defined $message->{"method"} and not ref $message->{"method"}
!ll&&!rl&&r
104509
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" and defined $message->{"method"}
!ll&&!rl&&r
104509
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0"
!ll&&!rl&&r
104509
not exists $message->{"id"} or defined $message->{"id"} and not ref $message->{"id"}
l!l&&r!l&&!r
2144880
defined $message->{"id"} and not ref $message->{"id"}
!ll&&!rl&&r
004488
MC/DC14%1/7
ref $message eq "HASH" and $message->{"jsonrpc"} eq "2.0" and defined $message->{"method"} and not ref $message->{"method"} and !exists $message->{"id"} || defined $message->{"id"} && !ref($message->{"id"})
ref $message eq "HASH" $message->{"jsonrpc"} eq "2.0" defined $message->{"method"} not ref $message->{"method"} not exists $message->{"id"} defined $message->{"id"} not ref $message->{"id"}
Truth table37%3/8
A and B and C and not(D) and not(E) or F and not(G)
A= ref $message eq "HASH"B= $message->{"jsonrpc"} eq "2.0"C= defined $message->{"method"}D= not ref $message->{"method"}E= not exists $message->{"id"}F= defined $message->{"id"}G= not ref $message->{"id"}
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
111100X0
11110100
11111XX1
11110111
252  
253 4509 my $id = $message->{id};
254 4509 my $key = $id; # Hash lookups must not stringify the wire ID.
255 4509 my $method = $message->{method};
256 4509 ▶ my $params = exists $message->{params} ? $message->{params} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $message->{'params'} ? : 4490 19 4509
257 4509 ▶ my $peer = $context{peer} || {};
Condition100%2/2
$context{'peer'} || {}
l!l
442485
MC/DC100%1/1
$context{'peer'} || {}
$context{'peer'}
Truth table100%2/2
A
A= $context{'peer'}
Aresult
11
00
258  
259 4509 ▶ unless (exists $message->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $message->{"id"}) 21 4488 4509
260 21 ▶ if ($method eq "notifications/cancelled" && ref $params eq "HASH" && defined $params->{requestId}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "notifications/cancelled" and ref $params eq "HASH" and defined $params->{"requestId"}) 9 12 21
Condition66%4/6
$method eq "notifications/cancelled" and ref $params eq "HASH" and defined $params->{"requestId"}
!ll&&!rl&&r
1209
$method eq "notifications/cancelled" and ref $params eq "HASH"
!ll&&!rl&&r
1209
MC/DC33%1/3
$method eq "notifications/cancelled" and ref $params eq "HASH" and defined $params->{"requestId"}
$method eq "notifications/cancelled" ref $params eq "HASH" defined $params->{"requestId"}
Truth table50%2/4
A and B and C
A= $method eq "notifications/cancelled"B= ref $params eq "HASH"C= defined $params->{"requestId"}
ABCresult
0XX0
10X0
1100
1111
261 9 my $req = delete $peer->{requests}{$params->{requestId}};
262 9 ▶ $req->cancel ($params->{reason}) if $req;
Branch: 1 branch
BranchTrueFalseTotal
if $req 9 0 9
263 }
264  
265 21 $reply->(undef);
266 return
267 21 }
268  
269 return $self->_rpc_error ($reply, $id, -32602, "Invalid params")
270 unless ref $params eq "HASH"
271 4488 ▶ && (!exists $params->{_meta} || ref $params->{_meta} eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" and !exists $params->{"_meta"} || ref $params->{"_meta"} eq "HASH" 0 4488 4488
Condition50%3/6
ref $params eq "HASH" and !exists $params->{"_meta"} || ref $params->{"_meta"} eq "HASH"
!ll&&!rl&&r
004488
not exists $params->{"_meta"} or ref $params->{"_meta"} eq "HASH"
l!l&&r!l&&!r
18643020
MC/DC0%0/3
ref $params eq "HASH" and !exists $params->{"_meta"} || ref $params->{"_meta"} eq "HASH"
ref $params eq "HASH" not exists $params->{"_meta"} ref $params->{"_meta"} eq "HASH"
Truth table50%2/4
A and not(B) or C
A= ref $params eq "HASH"B= not exists $params->{"_meta"}C= ref $params->{"_meta"} eq "HASH"
ABCresult
0XX0
1000
11X1
1011
272  
273 4488 ▶ my $meta = $params->{_meta} || {};
Condition100%2/2
$params->{'_meta'} || {}
l!l
4302186
MC/DC100%1/1
$params->{'_meta'} || {}
$params->{'_meta'}
Truth table100%2/2
A
A= $params->{'_meta'}
Aresult
11
00
274 my $version = $meta->{"io.modelcontextprotocol/protocolVersion"}
275 4488 ▶ || $context{protocol_version} || $peer->{protocol_version};
Condition100%6/6
$meta->{'io.modelcontextprotocol/protocolVersion'} || $context{'protocol_version'} || $peer->{'protocol_version'}
l!l&&r!l&&!r
434412915
$meta->{"io.modelcontextprotocol/protocolVersion"} or $context{"protocol_version"}
l!l&&r!l&&!r
429945144
MC/DC100%3/3
$meta->{'io.modelcontextprotocol/protocolVersion'} || $context{'protocol_version'} || $peer->{'protocol_version'}
$meta->{"io.modelcontextprotocol/protocolVersion"} $context{"protocol_version"} $peer->{'protocol_version'}
Truth table100%4/4
A or B || C
A= $meta->{"io.modelcontextprotocol/protocolVersion"}B= $context{"protocol_version"}C= $peer->{'protocol_version'}
ABCresult
1XX1
01X1
0011
0000
276  
277 $peer->{client_info} = $meta->{"io.modelcontextprotocol/clientInfo"}
278 4488 ▶ if $version && $version ge "2026" && ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if $version and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH" 4188 300 4488
Condition100%6/6
$version and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH"
!ll&&!rl&&r
1891114188
$version and $version ge 2026
!ll&&!rl&&r
151744299
MC/DC100%3/3
$version and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH"
$version $version ge 2026 ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH"
Truth table100%4/4
A and B and C
A= $versionB= $version ge 2026C= ref $meta->{"io.modelcontextprotocol/clientInfo"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
279  
280 4488 ▶ if ($method eq "initialize") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'initialize') { } 24 4464 4488
281 24 $version = $params->{protocolVersion};
282  
283 return $self->_rpc_error ($reply, $id, -32602, "Invalid initialize params")
284 unless defined $version && !ref $version
285 && ref $params->{capabilities} eq "HASH"
286 24 ▶ && ref $params->{clientInfo} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and not ref $version and ref $params->{"capabilities"} eq "HASH" and ref $params->{"clientInfo"} eq "HASH" 0 24 24
Condition33%3/9
defined $version and not ref $version and ref $params->{"capabilities"} eq "HASH" and ref $params->{"clientInfo"} eq "HASH"
!ll&&!rl&&r
0024
defined $version and not ref $version and ref $params->{"capabilities"} eq "HASH"
!ll&&!rl&&r
0024
defined $version and not ref $version
!ll&&!rl&&r
0024
MC/DC0%0/4
defined $version and not ref $version and ref $params->{"capabilities"} eq "HASH" and ref $params->{"clientInfo"} eq "HASH"
defined $version not ref $version ref $params->{"capabilities"} eq "HASH" ref $params->{"clientInfo"} eq "HASH"
Truth table20%1/5
A and not(B) and C and D
A= defined $versionB= not ref $versionC= ref $params->{"capabilities"} eq "HASH"D= ref $params->{"clientInfo"} eq "HASH"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
287  
288 24 ▶ unless (grep $_ eq $version && $_ lt "2026", @{$self->{versions}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_ eq $version && $_ lt 2026), @{$$self{"versions"};})) 4 20 24
Condition66%2/3
$_ eq $version && $_ lt '2026'
!ll&&!rl&&r
64020
MC/DC50%1/2
$_ eq $version && $_ lt '2026'
$_ eq $version $_ lt '2026'
Truth table66%2/3
A && B
A= $_ eq $versionB= $_ lt '2026'
ABresult
0X0
100
111
289 4 ($version) = grep $_ lt "2026", @{$self->{versions}};
290 4 ▶ return $self->_rpc_error ($reply, $id, -32601, "Legacy initialization is not supported")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 4 4
291 unless defined $version;
292 }
293 } else {
294 4464 ▶ return $self->_rpc_error ($reply, $id, -32602, "Missing protocol version")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and not ref $version 1 4463 4464
Condition66%2/3
defined $version and not ref $version
!ll&&!rl&&r
104463
MC/DC50%1/2
defined $version and not ref $version
defined $version not ref $version
Truth table66%2/3
A and not(B)
A= defined $versionB= not ref $version
ABresult
0X0
100
111
295 unless defined $version && !ref $version;
296  
297 return $self->_rpc_error ($reply, $id, -32022, "Unsupported protocol version", {
298 requested => $version,
299 supported => $self->{versions},
300 4463 ▶ }) unless grep $_ eq $version, @{$self->{versions}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $version), @{$$self{"versions"};}) 5 4458 4463
301  
302 4458 ▶ if ($version ge "2026") {
Branch: 1 branch
BranchTrueFalseTotal
if ($version ge 2026) 4290 168 4458
303 return $self->_rpc_error ($reply, $id, -32602, "Missing per-request metadata")
304 unless $meta->{"io.modelcontextprotocol/protocolVersion"} eq $version
305 4290 ▶ && ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"io.modelcontextprotocol/protocolVersion"} eq $version and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH" 0 4290 4290
Condition33%1/3
$meta->{"io.modelcontextprotocol/protocolVersion"} eq $version and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
!ll&&!rl&&r
004290
MC/DC0%0/2
$meta->{"io.modelcontextprotocol/protocolVersion"} eq $version and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
$meta->{"io.modelcontextprotocol/protocolVersion"} eq $version ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
Truth table33%1/3
A and B
A= $meta->{"io.modelcontextprotocol/protocolVersion"} eq $versionB= ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
ABresult
0X0
100
111
306 }
307 }
308  
309 return $self->_rpc_error ($reply, $id, -32600, "Request id already outstanding")
310 4482 ▶ if $peer->{requests}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $peer->{"requests"}{$key} 0 4482 4482
311  
312 # the private multibroker protocol uses the normal MCP capability handshake.
313 # keep the negotiated version on this stream's peer, not on the server.
314 my $capabilities = $method eq "initialize"
315 4482 ▶ ? $params->{capabilities} : $meta->{"io.modelcontextprotocol/clientCapabilities"};
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'initialize' ? : 24 4458 4482
316 4482 ▶ if (ref $capabilities eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $capabilities eq "HASH") 4314 168 4482
317 4314 my $extensions = $capabilities->{extensions};
318 4314 ▶ if (ref $extensions eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extensions eq "HASH") 162 4152 4314
319 162 my $extension = $extensions->{"anyevent-mcp/multibroker"};
320 $peer->{aemcp_multibroker} = 1
321 162 ▶ if ref $extension eq "HASH" && ($extension->{version} || 0) eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if ref $extension eq "HASH" and ($extension->{"version"} || 0) eq 1 162 0 162
Condition40%2/5
ref $extension eq "HASH" and ($extension->{"version"} || 0) eq 1
!ll&&!rl&&r
00162
$extension->{'version'} || 0
l!l
1620
MC/DC0%0/2
ref $extension eq "HASH" and ($extension->{"version"} || 0) eq 1
ref $extension eq "HASH" $extension->{'version'}
Truth table33%1/3
A and B
A= ref $extension eq "HASH"B= $extension->{'version'}
ABresult
0X0
100
111
322 }
323 }
324  
325 4482 my $user = $context{user};
326 4482 ▶ my $ask = $self->{permissions} && $self->{permissions}{ask};
Condition66%2/3
$self->{'permissions'} && $self->{'permissions'}{'ask'}
!ll&&!rl&&r
59703885
MC/DC50%1/2
$self->{'permissions'} && $self->{'permissions'}{'ask'}
$self->{'permissions'} $self->{'permissions'}{'ask'}
Truth table66%2/3
A && B
A= $self->{'permissions'}B= $self->{'permissions'}{'ask'}
ABresult
0X0
100
111
327 my $trust = defined $ask && !ref $ask && (
328 $ask eq "mcp"
329 || $ask eq "auto" && $version ge "2026"
330 && ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
331 && ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{elicitation} eq "HASH"
332 1 && (!keys %{$meta->{"io.modelcontextprotocol/clientCapabilities"}{elicitation}}
333 4482 ▶ || ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{elicitation}{form} eq "HASH")
Condition79%19/24
defined $ask && !ref($ask) && ($ask eq 'mcp' || $ask eq 'auto' && $version ge '2026' && ref $meta->{'io.modelcontextprotocol/clientCapabilities'} eq 'HASH' && ref $meta->{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'} eq 'HASH' && (!keys(%{$$meta{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'};}) || ref $meta->{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'}{'form'} eq 'HASH'))
!ll&&!rl&&r
6032663613
defined $ask and not ref $ask
!ll&&!rl&&r
59763879
$ask eq "mcp" or $ask eq "auto" and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH" and ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"} eq "HASH" and !keys(%{$$meta{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"};}) || ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"}{"form"} eq "HASH"
l!l&&r!l&&!r
36121266
$ask eq "auto" and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH" and ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"} eq "HASH" and !keys(%{$$meta{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"};}) || ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"}{"form"} eq "HASH"
!ll&&!rl&&r
26601
$ask eq "auto" and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH" and ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"} eq "HASH"
!ll&&!rl&&r
26601
$ask eq "auto" and $version ge 2026 and ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"
!ll&&!rl&&r
26601
$ask eq "auto" and $version ge 2026
!ll&&!rl&&r
26421
not keys %{$$meta{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"};} or ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"}{"form"} eq "HASH"
l!l&&r!l&&!r
010
MC/DC55%5/9
defined $ask && !ref($ask) && ($ask eq 'mcp' || $ask eq 'auto' && $version ge '2026' && ref $meta->{'io.modelcontextprotocol/clientCapabilities'} eq 'HASH' && ref $meta->{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'} eq 'HASH' && (!keys(%{$$meta{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'};}) || ref $meta->{'io.modelcontextprotocol/clientCapabilities'}{'elicitation'}{'form'} eq 'HASH'))
defined $ask not ref $ask $ask eq "mcp" $ask eq "auto" $version ge 2026 ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH" ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"} eq "HASH" not keys %{$$meta{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"};} ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"}{"form"} eq "HASH"
Truth table60%6/10
A and not(B) && C or D and E and F and G and not(H) or I
A= defined $askB= not ref $askC= $ask eq "mcp"D= $ask eq "auto"E= $version ge 2026F= ref $meta->{"io.modelcontextprotocol/clientCapabilities"} eq "HASH"G= ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"} eq "HASH"H= not keys %{$$meta{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"};}I= ref $meta->{"io.modelcontextprotocol/clientCapabilities"}{"elicitation"}{"form"} eq "HASH"
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
1100XXXXX0
11010XXXX0
110110XXX0
1101110XX0
1101111000
111XXXXXX1
11011111X1
1101111011
334 );
335 4482 ▶ if ($trust && exists $meta->{"anyevent-mcp/user"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trust and exists $meta->{"anyevent-mcp/user"}) 35 4447 4482
Condition100%3/3
$trust and exists $meta->{"anyevent-mcp/user"}
!ll&&!rl&&r
869357835
MC/DC100%2/2
$trust and exists $meta->{"anyevent-mcp/user"}
$trust exists $meta->{"anyevent-mcp/user"}
Truth table100%3/3
A and B
A= $trustB= exists $meta->{"anyevent-mcp/user"}
ABresult
0X0
100
111
336 35 $user = $meta->{"anyevent-mcp/user"};
337 35 ▶ return $self->_rpc_error ($reply, $id, -32602, "Invalid delegated user")
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and ref $user || !($user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u) 1 34 35
Condition83%5/6
defined $user and ref $user || !($user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u)
!ll&&!rl&&r
3041
ref $user or not $user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u
l!l&&r!l&&!r
014
MC/DC66%2/3
defined $user and ref $user || !($user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u)
defined $user ref $user not $user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u
Truth table75%3/4
A and B or not(C)
A= defined $userB= ref $userC= not $user =~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/u
ABCresult
0XX0
1000
11X1
1011
338 if defined $user && (ref $user
339 || $user !~ /\A(?:global|program|instance):[A-Za-z_][A-Za-z0-9_-]*\z/);
340 }
341  
342 my $req = AnyEvent::MCP::Server::Request->new (
343 server => $self,
344 peer => $peer,
345 id => $id,
346 method => $method,
347 params => $params,
348 user => $user,
349 protocol_version => $version,
350 4358 ▶ on_reply => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
351 4358 my ($req, $result) = @_;
352  
353 4358 delete $peer->{requests}{$key};
354  
355 4358 ▶ if ($req->{protocol_version} ge "2026") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"protocol_version"} ge 2026) 4176 182 4358
356 $result = {
357 resultType => "complete",
358 %$result,
359 _meta => {
360 "io.modelcontextprotocol/serverInfo" => $self->{server_info},
361 4176 ▶ %{$result->{_meta} || {}},
Condition100%2/2
$result->{'_meta'} || {}
l!l
664110
MC/DC100%1/1
$result->{'_meta'} || {}
$result->{'_meta'}
Truth table100%2/2
A
A= $result->{'_meta'}
Aresult
11
00
362 },
363 };
364 }
365  
366 4358 $reply->({ jsonrpc => "2.0", id => $id, result => $result });
367 },
368 4481 );
369  
370 4481 ▶ if (my $notify = $context{on_notify}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $notify = $context{"on_notify"}) 4213 268 4481
371 0 ▶ $req->{on_notify} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
372 0 my ($req, $method, $params) = @_;
373  
374 0 $notify->($req, { jsonrpc => "2.0", method => $method, params => $params });
375 4213 };
376 }
377  
378 4481 $peer->{requests}{$key} = $req;
379 4481 weaken $peer->{requests}{$key};
380  
381 4481 ▶ if ($method eq "tools/call") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "tools/call") 4049 432 4481
382 4049 ▶ unless (defined $params->{name} && !ref $params->{name}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"name"} and not ref $params->{"name"} and !exists $params->{"arguments"} || ref $params->{"arguments"} eq "HASH") 1 4048 4049
Condition66%6/9
defined $params->{"name"} and not ref $params->{"name"} and !exists $params->{"arguments"} || ref $params->{"arguments"} eq "HASH"
!ll&&!rl&&r
014048
defined $params->{"name"} and not ref $params->{"name"}
!ll&&!rl&&r
004049
not exists $params->{"arguments"} or ref $params->{"arguments"} eq "HASH"
l!l&&r!l&&!r
1040381
MC/DC50%2/4
defined $params->{"name"} and not ref $params->{"name"} and !exists $params->{"arguments"} || ref $params->{"arguments"} eq "HASH"
defined $params->{"name"} not ref $params->{"name"} not exists $params->{"arguments"} ref $params->{"arguments"} eq "HASH"
Truth table60%3/5
A and not(B) and not(C) or D
A= defined $params->{"name"}B= not ref $params->{"name"}C= not exists $params->{"arguments"}D= ref $params->{"arguments"} eq "HASH"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
383 && (!exists $params->{arguments} || ref $params->{arguments} eq "HASH")) {
384 1 $self->_reject ($req, $reply, -32602, "Invalid tool call");
385 1 return $req
386 }
387  
388 4048 $req->{tool} = $params->{name};
389 4048 ▶ $req->{args} = exists $params->{arguments} ? $params->{arguments} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'arguments'} ? : 4038 10 4048
390 }
391  
392 4480 $req->{_reply} = $reply;
393  
394 4480 $self->_dispatch ($req);
395  
396 4384 $req
397 }
398  
399 4489 ▶sub _dispatch {
Subroutine _dispatch: called CC=11 SCAR=24.0
400 4489 my ($self, $req) = @_;
401  
402 eval {
403 $self->{permissions}->_resume ($req)
404 4489 ▶ if $self->{permissions} && $req->{method} eq "tools/call";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"permissions"} and $req->{"method"} eq "tools/call" 3658 831 4489
Condition100%3/3
$self->{"permissions"} and $req->{"method"} eq "tools/call"
!ll&&!rl&&r
5962353658
MC/DC100%2/2
$self->{"permissions"} and $req->{"method"} eq "tools/call"
$self->{"permissions"} $req->{"method"} eq "tools/call"
Truth table100%3/3
A and B
A= $self->{"permissions"}B= $req->{"method"} eq "tools/call"
ABresult
0X0
100
111
405 4429 $self->event (request => $req);
406 4428 ▶ $self->dispatch ($req) unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 4415 13 4428
407 2559 1
408 4489 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"permissions"}->_resume($req) if $self->{"permissions"} and $req->{"method"} eq "tools/call"; $self->event("request", $req); $self->dispatch($req) unless $req->{"finished"}; 1 } } or do { my $error = $@; die $error if $req->{"finished"}; if (ref $error eq "AnyEvent::MCP::Server::Permissions::Required" and $self->{"permissions"}) { eval { do { $self->{"permissions"}->require($req, $error->{"request"}, sub { my($success, $grant) = @_; return if $req->{"finished"}; return $req->error($grant) unless $success; $self->{"permissions"}->_remember($req, $error->{"request"}, $grant); $self->_dispatch($req); } ); 1 } } or do { my $failure = $@; die $failure if $req->{"finished"}; $self->event("error", $req, $failure); $req->error($failure) }; } else { $self->event("error", $req, $error); if ($req->{"method"} eq "tools/call") { $req->error($error); } else { $self->_reject($req, $req->{"_reply"}, -32603, "Internal error"); } ; } } 2559 1833 4392
409 1833 my $error = $@;
410 1833 ▶ die $error if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 1833 1833
411  
412 1833 ▶ if (ref $error eq "AnyEvent::MCP::Server::Permissions::Required" && $self->{permissions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq 'AnyEvent::MCP::Server::Permissions::Required' and $self->{'permissions'}) { } 1413 420 1833
Condition66%2/3
ref $error eq "AnyEvent::MCP::Server::Permissions::Required" and $self->{"permissions"}
!ll&&!rl&&r
42001413
MC/DC50%1/2
ref $error eq "AnyEvent::MCP::Server::Permissions::Required" and $self->{"permissions"}
ref $error eq "AnyEvent::MCP::Server::Permissions::Required" $self->{"permissions"}
Truth table66%2/3
A and B
A= ref $error eq "AnyEvent::MCP::Server::Permissions::Required"B= $self->{"permissions"}
ABresult
0X0
100
111
413 eval {
414 12 ▶ $self->{permissions}->require ($req, $error->{request}, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
415 12 my ($success, $grant) = @_;
416  
417 12 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 12 12
418 12 ▶ return $req->error ($grant) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 3 9 12
419  
420 9 $self->{permissions}->_remember ($req, $error->{request}, $grant);
421 9 $self->_dispatch ($req);
422 1413 });
423 1412 1
424 1413 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"permissions"}->require($req, $error->{"request"}, sub { my($success, $grant) = @_; return if $req->{"finished"}; return $req->error($grant) unless $success; $self->{"permissions"}->_remember($req, $error->{"request"}, $grant); $self->_dispatch($req); } ); 1 } } or do { my $failure = $@; die $failure if $req->{"finished"}; $self->event("error", $req, $failure); $req->error($failure) } 1412 1 1413
425 1 my $failure = $@;
426 1 ▶ die $failure if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 1 1
427 1 $self->event (error => $req, $failure);
428 1 $req->error ($failure);
429 };
430 } else {
431 420 $self->event (error => $req, $error);
432  
433 420 ▶ if ($req->{method} eq "tools/call") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{'method'} eq 'tools/call') { } 419 1 420
434 419 $req->error ($error);
435 } else {
436 1 $self->_reject ($req, $req->{_reply}, -32603, "Internal error");
437 }
438 }
439 };
440 }
441  
442 11 ▶sub _reject {
Subroutine _reject: called CC=1 SCAR=0.0
443 11 my ($self, $req, $reply, $code, $message) = @_;
444  
445 11 $req->{finished} = 1;
446 11 my $key = $req->{id};
447 11 delete $req->{peer}{requests}{$key};
448 11 delete @$req{qw(on_reply on_notify on_cancel _reply)};
449  
450 11 $self->_rpc_error ($reply, $req->{id}, $code, $message)
451 }
452  
453 4415 ▶sub dispatch {
Subroutine dispatch: called CC=11 SCAR=24.0
Pod documented
454 4415 my ($self, $req) = @_;
455  
456 4415 my $method = $req->{method};
457  
458 4415 ▶ if ($method eq "initialize") {
Branch: 5 branches
BranchTrueFalseTotal
if ($method eq 'initialize') { } 24 4391 4415
elsif ($method eq 'server/discover') { } 259 4132 4391
elsif ($method eq 'ping') { } 32 4100 4132
elsif ($method eq 'tools/list') { } 106 3994 4100
elsif ($method eq 'tools/call') { } 3986 8 3994
459 24 $req->{peer}{protocol_version} = $req->{protocol_version};
460 24 $req->{peer}{client_info} = $req->{params}{clientInfo};
461 24 $req->{peer}{client_capabilities} = $req->{params}{capabilities};
462  
463 my $result = {
464 protocolVersion => $req->{protocol_version},
465 serverInfo => $self->{server_info},
466 capabilities => $self->{capabilities},
467 24 };
468 24 ▶ $result->{instructions} = $self->{instructions} if length $self->{instructions};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"instructions"} 12 12 24
469  
470 24 $req->reply ($result);
471  
472 } elsif ($method eq "server/discover") {
473 my $result = {
474 supportedVersions => $self->{versions},
475 capabilities => $self->{capabilities},
476 259 };
477 259 ▶ $result->{instructions} = $self->{instructions} if length $self->{instructions};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"instructions"} 213 46 259
478  
479 259 $req->reply ($result);
480  
481 } elsif ($method eq "ping") {
482 32 $req->reply ({});
483  
484 } elsif ($method eq "tools/list") {
485 106 ▶ if ($self->{on_tools_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_tools_list'}) { } 14 92 106
486 14 $self->event (tools_list => $req);
487 } else {
488 92 $req->reply ({ tools => [map @{$_->list}, @{$self->{toolsets}}] });
489 }
490  
491 } elsif ($method eq "tools/call") {
492 3986 ▶ if ($self->{on_tools_call}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_tools_call'}) { } 304 3682 3986
493 304 $self->event (tools_call => $req, $req->{args});
494 } else {
495 3682 my ($toolset) = grep exists $_->{tool}{$req->{tool}}, @{$self->{toolsets}};
496  
497 3682 ▶ if ($toolset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toolset) { } 3681 1 3682
498 3681 $toolset->call ($req);
499 } else {
500 1 $self->_reject ($req, $req->{_reply}, -32602, "Unknown tool '$req->{tool}'");
501 }
502 }
503  
504 } else {
505 8 $self->_reject ($req, $req->{_reply}, -32601, "Method not found");
506 }
507 }
508  
509 268 1
« lib/AnyEvent/MCP/Worker/IMAP.pm ↑ Summary blib/lib/AnyEvent/MCP/Server/Permissions.pm »