blib/lib/AnyEvent/MCP/Server/HTTP.pm

filter: Statementstmt 97.8% 230 / 235 Branchbran 82.1% 156 / 190 Conditioncond 72.0% 116 / 161 MC/DCmcdc 56.9% 49 / 86 Subroutinesub 100% 36 / 36 Podpod 100% 5 / 5 totaltotal 83.0% 592 / 713 CC 159 SCAR 29.8
CC
159
Cov
86%
_request
43.1
_respond
32.0
_tls_context
22.9
CRAP 19.7
« bin/mcp-fs ↑ Summary blib/lib/AnyEvent/MCP/Client/HTTP.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Server::HTTP - an optional MCP HTTP listener and socket handler
4  
5 =head1 DESCRIPTION
6  
7 A subclass of L<AnyEvent::MCP::Server> that handles HTTP POST requests.
8 It can open its own listener, or you can give it sockets accepted elsewhere.
9 If your application already parses HTTP, you can pass decoded requests to
10 L<AnyEvent::MCP::Server> instead of running another HTTP implementation.
11  
12 Requests sharing one connection are dispatched in sequence, preserving their
13 HTTP response order; requests on other connections are independent. This
14 implementation returns JSON and drops notifications. It does not support
15 SSE or subscriptions.
16  
17 Request bodies need C<Content-Length>: chunked transfer encoding is not
18 supported. The server does not assign protocol sessions. Legacy clients
19 use the 2025 request/response subset.
20  
21 C<Mcp-Param-*> headers from C<x-mcp-header> tool schema annotations are not
22 currently validated against the body. The built-in protocol headers
23 C<Mcp-Protocol-Version>, C<Mcp-Method> and, where applicable, C<Mcp-Name>
24 must agree with modern request metadata and parameters; a mismatch is
25 rejected. Custom schema-derived headers do not receive this check.
26  
27 =head1 FIRST USE
28  
29 For an application that already registers tools in
30 L<AnyEvent::MCP::Server::Toolset>, start an HTTP listener:
31  
32 use AnyEvent;
33 use AnyEvent::MCP::Server::HTTP;
34  
35 my $server = AnyEvent::MCP::Server::HTTP->new (
36 toolsets => [$tools], tokens => [$secret],
37 host => "127.0.0.1", port => 31740,
38 );
39 $server->listen;
40 print $server->url, "\n";
41 AE::cv->recv;
42  
43 C<$tools> is a Toolset and C<$secret> is a previously generated private
44 URL-safe token. Do not use a sample or hardcoded token in a real service.
45 This object handles sockets and HTTP; L<AnyEvent::MCP::Server> dispatches
46 requests, while the Toolset runs tool callbacks. For a standalone program
47 with automatic configuration and startup, prefer
48 L<AnyEvent::MCP::Server::Simple>.
49  
50 =head1 AUTHENTICATION
51  
52 Unlike L<AnyEvent::MCP::Server::Simple/mcp_start>, this low-level object
53 does not load global configuration or derive credentials. Supply C<tokens>
54 explicitly, for example:
55  
56 my $server = AnyEvent::MCP::Server::HTTP->new (
57 host => "127.0.0.1",
58 port => 31740,
59 path => "/mcp/",
60 tokens => ["a-long-random-secret", "another-long-random-secret"],
61 );
62 $server->listen;
63  
64 Each token authorizes the same requests. For either token, a client may
65 POST to C</TOKEN/mcp/> without an Authorization header, or POST to
66 C</mcp/> with C<Authorization: Bearer TOKEN>. URL credentials are path
67 components, B<not> HTTP cookies. C<url> advertises the first token;
68 C<url (0)> returns the clean endpoint. A trailing slash may be omitted
69 from either request path when the configured endpoint path ends in one;
70 this accommodates clients that strip it from a configured URL.
71  
72 The accepted tokens are static, URL-safe strings. HTTP Basic and OAuth
73 access-token validation are not implemented here. There is no automatic
74 fallback to the master-secret-derived token: that belongs to C<mcp_start>
75 or L<AnyEvent::MCP::Config/http_settings>.
76  
77 Authentication defaults to C<tokens>, and construction B<fails> unless a
78 nonempty array of valid tokens is provided. To deliberately delegate
79 authentication to a trusted embedding layer (for example, a reverse proxy
80 that rejects unauthorized requests before they reach MCP), specify
81 C<auth =E<gt> "none"> instead, B<without> C<tokens>. This is an explicit
82 opt-out, not a permissive default. The embedding layer then owns the
83 entire authentication boundary.
84 The C<origins> setting filters browser origins; it is not authentication.
85 URLs can leak through logs and histories, and HTTP alone does not encrypt
86 tokens. Use TLS when credentials cross an untrusted network. A local address
87 is a sensible default, but not a credential.
88  
89 =head1 METHODS
90  
91 =over
92  
93 =item $server = AnyEvent::MCP::Server::HTTP->new (%config)
94  
95 Construction does not start a listener. In addition to Server options,
96 the following configuration options are supported:
97  
98 =over
99  
100 =item host => $host
101  
102 The listening address, defaulting to C<127.0.0.1>.
103  
104 =item port => $port
105  
106 The listening port, defaulting to zero for an automatically assigned port.
107  
108 =item path => $path
109  
110 The MCP endpoint path, defaulting to C</mcp/>. Bearer authentication uses
111 C<$path> directly; URL tokens prefix it with C</TOKEN>. If this path ends
112 in C</>, requests without that final slash are also accepted. Other
113 path differences are not normalized.
114  
115 =item connection_close => $boolean
116  
117 When true, disables persistent connections, sends C<Connection: close> and
118 closes the socket after draining each response. Defaults to false.
119 This is useful when an existing listener routes sockets by their first
120 request and cannot route subsequent requests independently.
121  
122 =item max_size => $bytes
123  
124 Limits incoming headers and request bodies, defaulting to 8 MiB.
125  
126 =item origins => [$origin...]
127  
128 Allowed Origin header values. Defaults to C<["*"]>, permitting browser requests
129 from any origin. An empty array rejects requests with an Origin header, while
130 requests without one are accepted. Allowed origins receive CORS response headers,
131 and nonempty origin lists enable OPTIONS preflight for POST. Preflight does not
132 require authentication; the subsequent POST still does.
133  
134 =item handle => { %handle }
135  
136 AnyEvent::Handle constructor options, such as C<tls>, C<tls_ctx>, C<timeout>
137 or C<on_starttls>. The socket and the read/error/EOF handlers belong to this
138 class.
139  
140 =item tls_cert => $file
141  
142 Serve HTTP and HTTPS opportunistically on the same port. FILE contains
143 a PEM certificate chain and optionally its private key. The TLS context
144 is loaded once when listening starts. TLS needs L<Net::SSLeay>.
145  
146 =item tls_key => $file
147  
148 PEM private key for C<tls_cert>. If only one TLS option is supplied, its
149 file must contain both certificate and private key. On accepted connections,
150 C<< $handle->{tls} >> identifies encrypted traffic. Plain HTTP remains
151 available; TLS is not required of clients.
152  
153 =item tokens => [$token, ...]
154  
155 Ordered tokens accepted either as C</TOKEN> before the configured
156 C<path> or in an C<Authorization: Bearer TOKEN> header. The first token
157 is used in advertised URLs. Token strings must be URL-safe and nonempty.
158  
159 =item auth => "tokens" | "none"
160  
161 Select authentication mode. C<tokens> is the default and requires at least
162 one token; C<none> explicitly disables HTTP authentication and cannot be
163 combined with tokens. Other values are rejected. Browser origins are
164 not a replacement for authentication.
165  
166 =item name => $name
167  
168 Identifies the service. Use L<AnyEvent::MCP::Config/http_settings>
169 to resolve named listener configuration before constructing this transport.
170  
171 =item url_host => $hostname
172  
173 Overrides the advertised hostname, for example when listening on a wildcard
174 address. Otherwise a wildcard uses the nodename from C<uname>.
175  
176 =item url_scheme => $scheme
177  
178 Overrides the advertised scheme; otherwise TLS credentials or handle options
179 select C<https>, and plain handles select C<http>.
180  
181 =back
182  
183 Every configured token authenticates in either the URL or the Authorization
184 header. Without tokens, the constructor requires C<auth =E<gt> "none">.
185  
186 =item $url = $server->url ($with_token)
187  
188 Returns the advertised URL with the actual listening port. C<$with_token>
189 defaults to true, including the first configured token. Pass false for
190 the clean URL to use with an Authorization bearer token.
191  
192 The port must be known. Wildcard hostname resolution and reverse-proxy
193 routing remain the caller's responsibility.
194  
195 =item $server->listen ($host, $port, %handle)
196  
197 Starts a listener, replacing any previous listener. Omitted arguments use
198 the configured host and port. The actual bound port is stored in
199 C<< $server->{port} >>, including when requesting an ephemeral port.
200  
201 C<%handle> overrides the configured C<handle> options for connections
202 accepted by this listener.
203  
204 =item $handle = $server->accept ($socket, %handle)
205  
206 Starts handling an already accepted socket. The server takes ownership of
207 C<$socket>. No listener is needed.
208  
209 C<%handle> overrides the configured C<handle> options for this connection.
210  
211 =item $server->close
212  
213 Stops listening, closes accepted sockets and cancels outstanding requests.
214 The same object can subsequently listen or accept again.
215  
216 =back
217  
218 =head1 EVENTS
219  
220 =over
221  
222 =item on_extension => $callback->($server, $handle, $hello)
223  
224 Accept raw extension connections on the HTTP listener. A first byte of C<[>
225 selects a bounded, newline-terminated JSON hello whose first two fields are
226 the protocol name and an accepted listener token. Authenticate before invoking
227 this callback, which validates the protocol-specific fields and takes ownership
228 of the handle, preserving buffered input. Throw an exception to reject it.
229 HTTP connections continue through the normal parser. This dispatch examines
230 decrypted bytes when TLS is configured, but the built-in reverse connector
231 currently supports only plain C<http://> endpoints.
232  
233 =item on_http_request => $callback->($server, $request, $reply)
234  
235 Called for every complete, syntactically valid HTTP request before MCP routing
236 or authentication. C<$request> is a hash containing the parsed HTTP fields
237 (including C<method>, C<uri>, and C<http_version>) and a C<body> field holding
238 the raw request body. Return false to use the normal MCP handling, or true to
239 handle the request yourself.
240  
241 C<$reply-E<gt>($status, $reason, \%headers, $body)> sends a response, either
242 immediately or asynchronously after the callback returns. C<%headers> is
243 optional; C<Content-Type> defaults to C<application/json>, and the server
244 generates C<Content-Length> and manages persistent connections. The body is
245 an octet string. Reply at most once, and return true when taking ownership
246 of the request, even if the response will be sent later. The next request on
247 the same connection is not read until that response is sent.
248  
249 The hook runs B<before> MCP authentication and CORS checks. Application
250 endpoints must perform their own authentication and browser request checks.
251 It does not replace or bypass the normal authentication of unhandled MCP
252 requests. Malformed or oversized requests are rejected before the hook.
253  
254 =item on_connection_error => $callback->($server, $handle, $message)
255  
256 Called for socket errors before closing the connection.
257  
258 =item on_close => $callback->($server)
259  
260 Called once after C<close> stops the listener and closes all connections.
261 Calling C<listen> or C<accept> again starts a new lifetime.
262  
263 =back
264  
265 =cut
266  
267 package AnyEvent::MCP::Server::HTTP;
268  
269 61 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 61 ▶use parent "AnyEvent::MCP::Server";
Subroutine BEGIN: called CC=1 SCAR=0.0
271 61 ▶use AnyEvent::Log ();
Subroutine BEGIN: called CC=1 SCAR=0.0
272 61 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
273 61 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
274 61 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
275 61 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
276 61 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 121 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
279 121 my ($class, %kv) = @_;
280  
281 121 ▶ !grep (exists $kv{$_}, qw(cookie bearer users))
Branch: 1 branch
BranchTrueFalseTotal
grep exists $kv{$_}, ("cookie", "bearer", "users") and die "Legacy HTTP authentication options are unsupported; use tokens\n" 0 121 121
282 or die "Legacy HTTP authentication options are unsupported; use tokens\n";
283  
284 121 ▶ $kv{auth} //= "tokens";
Condition100%2/2
$kv{"auth"} //= "tokens"
l!l
5467
MC/DC100%1/1
$kv{"auth"} //= "tokens"
$kv{"auth"}
Truth table100%2/2
A
A= $kv{"auth"}
Aresult
11
00
285 121 ▶ if ($kv{auth} eq "tokens") {
Branch: 2 branches
BranchTrueFalseTotal
if ($kv{'auth'} eq 'tokens') { } 67 54 121
elsif ($kv{'auth'} eq 'none') { } 53 1 54
286 67 ▶ ref $kv{tokens} eq "ARRAY" && @{$kv{tokens}}
Branch: 1 branch
BranchTrueFalseTotal
ref $kv{"tokens"} eq "ARRAY" and @{$kv{"tokens"};} or die "HTTP server requires nonempty tokens\n" 65 2 67
Condition100%3/3
ref $kv{"tokens"} eq "ARRAY" and @{$kv{"tokens"};}
!ll&&!rl&&r
1165
MC/DC100%2/2
ref $kv{"tokens"} eq "ARRAY" and @{$kv{"tokens"};}
ref $kv{"tokens"} eq "ARRAY" @{$kv{"tokens"};}
Truth table100%3/3
A and B
A= ref $kv{"tokens"} eq "ARRAY"B= @{$kv{"tokens"};}
ABresult
0X0
100
111
287 or die "HTTP server requires nonempty tokens\n";
288 /\A[A-Za-z0-9_.~-]+\z/ or die "Invalid HTTP server token\n"
289 65 ▶ for @{$kv{tokens}};
Branch: 1 branch
BranchTrueFalseTotal
/\A[A-Za-z0-9_.~-]+\z/u or die "Invalid HTTP server token\n" 84 1 85
290 } elsif ($kv{auth} eq "none") {
291 !exists $kv{tokens}
292 53 ▶ or die "Unauthenticated HTTP server must not configure tokens\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{"tokens"} and die "Unauthenticated HTTP server must not configure tokens\n" 1 52 53
293 } else {
294 1 die "Unsupported HTTP authentication type '$kv{auth}'\n";
295 }
296  
297 116 $class->SUPER::new (host => "127.0.0.1", port => 0, path => "/mcp/", origins => ["*"], %kv)
298 }
299  
300 52 ▶sub _tls_context {
Subroutine _tls_context: called CC=6 SCAR=31.3
301 52 my ($self) = @_;
302  
303 52 ▶ return unless defined $self->{tls_cert} || defined $self->{tls_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tls_cert"} or defined $self->{"tls_key"} 52 0 52
Condition33%1/3
defined $self->{"tls_cert"} or defined $self->{"tls_key"}
l!l&&r!l&&!r
0052
MC/DC0%0/2
defined $self->{"tls_cert"} or defined $self->{"tls_key"}
defined $self->{"tls_cert"} defined $self->{"tls_key"}
Truth table33%1/3
A or B
A= defined $self->{"tls_cert"}B= defined $self->{"tls_key"}
ABresult
1X1
011
000
304  
305 0 ▶ $self->{_tls_context} ||= do {
Condition0%0/3
$self->{"_tls_context"} ||= do { require AnyEvent::TLS; "AnyEvent::TLS"->new("cert_file", $self->{"tls_cert"} // $self->{"tls_key"}, defined $self->{"tls_key"} ? ("key_file", $self->{"tls_key"}) : ()) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_tls_context"} ||= do { require AnyEvent::TLS; "AnyEvent::TLS"->new("cert_file", $self->{"tls_cert"} // $self->{"tls_key"}, defined $self->{"tls_key"} ? ("key_file", $self->{"tls_key"}) : ()) }
$self->{"_tls_context"} do { require AnyEvent::TLS; "AnyEvent::TLS"->new("cert_file", $self->{"tls_cert"} // $self->{"tls_key"}, defined $self->{"tls_key"} ? ("key_file", $self->{"tls_key"}) : ()) }
Truth table0%0/3
A ||= B
A= $self->{"_tls_context"}B= do { require AnyEvent::TLS; "AnyEvent::TLS"->new("cert_file", $self->{"tls_cert"} // $self->{"tls_key"}, defined $self->{"tls_key"} ? ("key_file", $self->{"tls_key"}) : ()) }
ABresult
1X1
011
000
306 0 require AnyEvent::TLS;
307 AnyEvent::TLS->new (
308 cert_file => $self->{tls_cert} // $self->{tls_key},
309 0 ▶ (defined $self->{tls_key} ? (key_file => $self->{tls_key}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'tls_key'} ? : 0 0 0
Condition0%0/3
$self->{"tls_cert"} // $self->{"tls_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tls_cert"} // $self->{"tls_key"}
$self->{"tls_cert"} $self->{"tls_key"}
Truth table0%0/3
A // B
A= $self->{"tls_cert"}B= $self->{"tls_key"}
ABresult
1X1
011
000
310 )
311 }
312 }
313  
314 52 ▶sub listen {
Subroutine listen: called CC=4 SCAR=14.1
Pod documented
315 52 my ($self, $host, $port, %handle) = @_;
316  
317 52 $self->_tls_context;
318 52 delete $self->{_closed};
319 52 ▶ $host = $self->{host} unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 52 0 52
320 52 ▶ $port = $self->{port} unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 52 0 52
321  
322 52 $self->{listen_host} = $host;
323 52 ▶ $self->{listen_handle} = { %{$self->{handle} || {}}, %handle };
Condition50%1/2
$self->{'handle'} || {}
l!l
052
MC/DC0%0/1
$self->{'handle'} || {}
$self->{'handle'}
Truth table50%1/2
A
A= $self->{'handle'}
Aresult
11
00
324  
325 52 delete $self->{listener};
326 52 weaken $self;
327  
328 186 ▶ $self->{listener} = AnyEvent::Socket::tcp_server $host, $port, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
329 186 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 186 186
330 186 my ($socket, $host, $port) = @_;
331 186 my $handle = $self->accept ($socket, %handle);
332 186 $handle->{mcp_remote} = AnyEvent::Socket::format_hostport ($host, $port);
333 52 ▶ }, sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
334 52 ▶ $self->{port} = $_[2] if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 52 0 52
335 ()
336 52 };
337 }
338  
339 179 ▶sub _token_path {
Subroutine _token_path: called CC=1 SCAR=0.0
340 179 my ($self, $token) = @_;
341  
342 179 "/$token$self->{path}"
343 }
344  
345 84 ▶sub url {
Subroutine url: called CC=17 SCAR=28.8
Pod documented
346 84 my ($self, $with_token) = @_;
347  
348 84 ▶ $with_token = 1 unless defined $with_token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $with_token 69 15 84
349  
350 my $host = $self->{url_host}
351 84 ▶ || (defined $self->{listen_host} ? $self->{listen_host} : $self->{host});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'listen_host'} ? : 42 40 82
Condition66%2/3
$self->{'url_host'} || (defined $self->{'listen_host'} ? $self->{'listen_host'} : $self->{'host'})
l!l&&r!l&&!r
2820
MC/DC0%0/2
$self->{'url_host'} || (defined $self->{'listen_host'} ? $self->{'listen_host'} : $self->{'host'})
$self->{'url_host'} (defined $self->{'listen_host'} ? $self->{'listen_host'} : $self->{'host'})
Truth table66%2/3
A || B
A= $self->{'url_host'}B= (defined $self->{'listen_host'} ? $self->{'listen_host'} : $self->{'host'})
ABresult
1X1
011
000
352 84 ▶ $host = (POSIX::uname)[1] if !length $host || $host eq "0.0.0.0" || $host eq "::";
Branch: 1 branch
BranchTrueFalseTotal
if not length $host or $host eq "0.0.0.0" or $host eq "::" 3 81 84
Condition83%5/6
not length $host or $host eq "0.0.0.0" or $host eq "::"
l!l&&r!l&&!r
2181
not length $host or $host eq "0.0.0.0"
l!l&&r!l&&!r
0282
MC/DC66%2/3
not length $host or $host eq "0.0.0.0" or $host eq "::"
not length $host $host eq "0.0.0.0" $host eq "::"
Truth table75%3/4
not(A) or B or C
A= not length $hostB= $host eq "0.0.0.0"C= $host eq "::"
ABCresult
1XX1
01X1
0011
0000
353 84 $host =~ s/\A\[(.*)\]\z/$1/; # allow already bracketed IPv6 URL hosts
354  
355 84 ▶ my $port = $self->{port} or die "HTTP port is not known\n";
Branch: 1 branch
BranchTrueFalseTotal
my $port = $self->{"port"} or die "HTTP port is not known\n" 80 4 84
356 my $scheme = $self->{url_scheme}
357 || (defined $self->{tls_cert} || defined $self->{tls_key}
358 80 ▶ || ($self->{listen_handle} || $self->{handle} || {})->{tls} ? "https" : "http");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'tls_cert'} || defined $self->{'tls_key'} || +($self->{'listen_handle'} || $self->{'handle'} || {})->{'tls'} ? : 2 53 55
Condition71%10/14
$self->{'url_scheme'} || (defined $self->{'tls_cert'} || defined $self->{'tls_key'} || +($self->{'listen_handle'} || $self->{'handle'} || {})->{'tls'} ? 'https' : 'http')
l!l&&r!l&&!r
25550
defined $self->{"tls_cert"} or defined $self->{"tls_key"} or +($self->{"listen_handle"} || $self->{"handle"} || {})->{"tls"}
l!l&&r!l&&!r
0253
defined $self->{"tls_cert"} or defined $self->{"tls_key"}
l!l&&r!l&&!r
0055
$self->{'listen_handle'} || $self->{'handle'} || {}
l!l
4411
$self->{"listen_handle"} or $self->{"handle"}
l!l&&r!l&&!r
42211
MC/DC60%3/5
$self->{'url_scheme'} || (defined $self->{'tls_cert'} || defined $self->{'tls_key'} || +($self->{'listen_handle'} || $self->{'handle'} || {})->{'tls'} ? 'https' : 'http')
$self->{'url_scheme'} defined $self->{"tls_cert"} defined $self->{"tls_key"} $self->{"listen_handle"} $self->{"handle"}
Truth table66%4/6
A || B or C or D or E
A= $self->{'url_scheme'}B= defined $self->{"tls_cert"}C= defined $self->{"tls_key"}D= $self->{"listen_handle"}E= $self->{"handle"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
359 36 my $path = $with_token && $self->{tokens} && @{$self->{tokens}}
360 80 ▶ ? $self->_token_path ($self->{tokens}[0]) : $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
$with_token && $self->{'tokens'} && @{$self->{'tokens'};} ? : 36 44 80
Condition83%5/6
$with_token and $self->{"tokens"} and @{$self->{"tokens"};}
!ll&&!rl&&r
44036
$with_token and $self->{"tokens"}
!ll&&!rl&&r
152936
MC/DC66%2/3
$with_token and $self->{"tokens"} and @{$self->{"tokens"};}
$with_token $self->{"tokens"} @{$self->{"tokens"};}
Truth table75%3/4
A and B and C
A= $with_tokenB= $self->{"tokens"}C= @{$self->{"tokens"};}
ABCresult
0XX0
10X0
1100
1111
361  
362 80 "$scheme://" . AnyEvent::Socket::format_hostport ($host, $port) . $path
363 }
364  
365 236 ▶sub accept {
Subroutine accept: called CC=6 SCAR=18.7
Pod documented
366 236 my ($self, $socket, %handle) = @_;
367  
368 236 delete $self->{_closed};
369 236 weaken $self;
370  
371 my $handle = AnyEvent::Handle->new (
372 rbuf_max => $self->{max_size} || 8 * 1024 * 1024,
373 236 %{$self->{handle}},
374 %handle,
375 fh => $socket,
376 on_read => undef,
377 2 ▶ on_error => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
378 2 ▶ $self->event (connection_error => $_[0], $_[2]) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 2 0 2
379 2 ▶ $self->_close ($_[0]) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 2 0 2
380 },
381 6 ▶ on_eof => sub { $self->_close ($_[0]) if $self },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $self 6 0 6
382 236 ▶ );
Condition100%2/2
$self->{'max_size'} || 8388608
l!l
4232
MC/DC100%1/1
$self->{'max_size'} || 8388608
$self->{'max_size'}
Truth table100%2/2
A
A= $self->{'max_size'}
Aresult
11
00
383  
384 236 $handle->{mcp_peer} = {};
385 236 $self->{connections}{$handle} = $handle;
386 $handle->push_read (tls_autostart => $self->_tls_context, "accept")
387 236 ▶ if (defined $self->{tls_cert} || defined $self->{tls_key}) && !$handle->{tls};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tls_cert"} || defined $self->{"tls_key"} and not $handle->{"tls"} 0 236 236
Condition33%2/6
defined $self->{"tls_cert"} || defined $self->{"tls_key"} and not $handle->{"tls"}
!ll&&!rl&&r
23600
defined $self->{"tls_cert"} or defined $self->{"tls_key"}
l!l&&r!l&&!r
00236
MC/DC0%0/3
defined $self->{"tls_cert"} || defined $self->{"tls_key"} and not $handle->{"tls"}
defined $self->{"tls_cert"} defined $self->{"tls_key"} not $handle->{"tls"}
Truth table20%1/5
A or B and not(C)
A= defined $self->{"tls_cert"}B= defined $self->{"tls_key"}C= not $handle->{"tls"}
ABCresult
00X0
1X00
0100
1X11
0111
388  
389 236 ▶ if ($self->{on_extension}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_extension'}) { } 23 213 236
390 23 $handle->rtimeout (15);
391 23 ▶ $handle->on_read (sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
392 23 my ($handle) = @_;
393  
394 23 $handle->on_read (undef);
395 23 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 23 23
396  
397 23 ▶ if (substr ($handle->{rbuf}, 0, 1) eq "[") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($handle->{'rbuf'}, 0, 1) eq '[') { } 9 14 23
398 9 require AnyEvent::MCP::Extension;
399 7 ▶ AnyEvent::MCP::Extension::accept ($handle, $self->{connect_tokens} || $self->{tokens} || [], sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
400 7 my ($handle, $hello) = @_;
401 7 ▶ return $handle->destroy unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 7 7
402 7 delete $self->{connections}{$handle};
403 7 $self->{on_extension}->($self, $handle, $hello);
404 9 ▶ }, sub { $self->_close ($_[0]) if $self });
Subroutine __ANON__: called CC=2 SCAR=8.5
Branch: 1 branch
BranchTrueFalseTotal
if $self 5 0 5
Condition40%2/5
$self->{'connect_tokens'} || $self->{'tokens'} || []
l!l
90
$self->{"connect_tokens"} or $self->{"tokens"}
l!l&&r!l&&!r
900
MC/DC0%0/2
$self->{'connect_tokens'} || $self->{'tokens'} || []
$self->{"connect_tokens"} $self->{"tokens"}
Truth table33%1/3
A or B
A= $self->{"connect_tokens"}B= $self->{"tokens"}
ABresult
1X1
011
000
405 } else {
406 14 $handle->rtimeout (0);
407 14 $self->_read ($handle, 1);
408 }
409 23 });
410 } else {
411 213 $self->_read ($handle);
412 }
413  
414 236 $handle
415 }
416  
417 # parse a request line and HTTP fields, using the same comma-combining
418 # approach as AnyEvent::HTTP::_parse_hdr. The caller already bounds headers.
419 233 ▶sub _parse_request_headers {
Subroutine _parse_request_headers: called CC=7 SCAR=19.5
420 233 local $_ = $_[0] . "\r\n";
421 233 ▶ /\A([!#$%&'*+.^_\x60|~0-9A-Za-z-]+) [ ]+
Branch: 1 branch
BranchTrueFalseTotal
m[\A([!#$%&'*+.^_\x60|~0-9A-Za-z-]+) [ ]+ ([^\x00-\x20]+) [ ]+ HTTP/0*([0-9.]+)\r\n]cgiux or return 232 1 233
422 ([^\x00-\x20]+) [ ]+ HTTP\/0*([0-9.]+)\r\n/gcix
423 or return;
424 232 my ($method, $uri, $version) = ($1, $2, $3);
425 232 ▶ return unless $version =~ /\A1\.[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /\A1\.[0-9]+\z/u 1 231 232
426 231 my %hdr;
427  
428 231 ▶ $hdr{lc $1} .= ",$2"
Branch: 1 branch
BranchTrueFalseTotal
/\G ([!\#$%&'*+.^_\x60|~0-9A-Za-z-]+): [\t ]* ([^\x00-\x08\x0a-\x1f\x7f]*) \r\n /cgux and do { $hdr{lc $1} .= ",$2" } 2034 231 2265
429 while /\G
430 ([!\#$%&'*+.^_\x60|~0-9A-Za-z-]+):
431 [\t ]*
432 ([^\x00-\x08\x0a-\x1f\x7f]*)
433 \r\n
434 /gcx;
435  
436 231 ▶ /\G\z/gc or return;
Branch: 1 branch
BranchTrueFalseTotal
/\G\z/cgu or return 227 4 231
437  
438 # discard the prefix used for comma-combining repeated field values.
439 227 substr $_, 0, 1, "" for values %hdr;
440 227 s/[\t ]+\z// for values %hdr;
441  
442 # request-line fields override any colliding HTTP header names.
443 227 +{ %hdr, method => $method, uri => $uri, http_version => $version }
444 }
445  
446 458 ▶sub _read {
Subroutine _read: called CC=2 SCAR=7.0
447 458 my ($self, $handle, $immediate) = @_;
448  
449 458 weaken $self;
450  
451 458 ▶ if ($immediate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate) { } 226 232 458
452 225 ▶ $handle->push_read (line => "\r\n\r\n", sub {
Subroutine __ANON__: called CC=16 SCAR=28.4
453 225 my ($handle, $header) = @_;
454  
455 225 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 225 225
456  
457 225 my $hdr = _parse_request_headers ($header);
458 225 ▶ $handle->{mcp_method} = $hdr ? substr ($hdr->{method}, 0, 32) : "-";
Branch: 1 branch
BranchTrueFalseTotal
$hdr ? : 223 2 225
459 225 ▶ $handle->{mcp_root_get} = $hdr && $hdr->{method} eq "GET" && $hdr->{uri} eq "/";
Condition100%6/6
$hdr && $hdr->{'method'} eq 'GET' && $hdr->{'uri'} eq '/'
!ll&&!rl&&r
22131
$hdr and $hdr->{"method"} eq "GET"
!ll&&!rl&&r
22194
MC/DC100%3/3
$hdr && $hdr->{'method'} eq 'GET' && $hdr->{'uri'} eq '/'
$hdr $hdr->{"method"} eq "GET" $hdr->{'uri'} eq '/'
Truth table100%4/4
A and B && C
A= $hdrB= $hdr->{"method"} eq "GET"C= $hdr->{'uri'} eq '/'
ABCresult
0XX0
10X0
1100
1111
460 $handle->{mcp_close} = $self->{connection_close}
461 || !$hdr || $hdr->{http_version} =~ m{\A1\.0+\z}
462 225 ▶ || $hdr->{connection} =~ /\bclose\b/i;
Condition88%8/9
$self->{'connection_close'} || !$hdr || $hdr->{'http_version'} =~ /\A1\.0+\z/u || $hdr->{'connection'} =~ /\bclose\b/iu
l!l&&r!l&&!r
717444
$self->{"connection_close"} or not $hdr or $hdr->{"http_version"} =~ /\A1\.0+\z/u
l!l&&r!l&&!r
70218
$self->{"connection_close"} or not $hdr
l!l&&r!l&&!r
52218
MC/DC75%3/4
$self->{'connection_close'} || !$hdr || $hdr->{'http_version'} =~ /\A1\.0+\z/u || $hdr->{'connection'} =~ /\bclose\b/iu
$self->{"connection_close"} not $hdr $hdr->{"http_version"} =~ /\A1\.0+\z/u $hdr->{'connection'} =~ /\bclose\b/iu
Truth table80%4/5
A or not(B) or C || D
A= $self->{"connection_close"}B= not $hdrC= $hdr->{"http_version"} =~ /\A1\.0+\z/uD= $hdr->{'connection'} =~ /\bclose\b/iu
ABCDresult
1XXX1
01XX1
001X1
00011
00000
463  
464 225 ▶ return $self->_respond ($handle, 400, "Bad Request", "") unless $hdr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr 2 223 225
465  
466 223 ▶ $hdr->{"content-length"} = 0 unless defined $hdr->{"content-length"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hdr->{"content-length"} 7 216 223
467 return $self->_respond ($handle, 400, "Bad Request", "")
468 if exists $hdr->{"transfer-encoding"}
469 223 ▶ || $hdr->{"content-length"} !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if exists $hdr->{"transfer-encoding"} or not $hdr->{"content-length"} =~ /\A[0-9]+\z/u 3 220 223
Condition100%3/3
exists $hdr->{"transfer-encoding"} or not $hdr->{"content-length"} =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
12220
MC/DC100%2/2
exists $hdr->{"transfer-encoding"} or not $hdr->{"content-length"} =~ /\A[0-9]+\z/u
exists $hdr->{"transfer-encoding"} not $hdr->{"content-length"} =~ /\A[0-9]+\z/u
Truth table100%3/3
A or not(B)
A= exists $hdr->{"transfer-encoding"}B= not $hdr->{"content-length"} =~ /\A[0-9]+\z/u
ABresult
1X1
011
000
470  
471 return $self->_respond ($handle, 413, "Content Too Large", "")
472 220 ▶ if $hdr->{"content-length"} > ($self->{max_size} || 8 * 1024 * 1024);
Branch: 1 branch
BranchTrueFalseTotal
if $hdr->{"content-length"} > ($self->{"max_size"} || 8388608) 0 220 220
Condition100%2/2
$self->{'max_size'} || 8388608
l!l
4216
MC/DC100%1/1
$self->{'max_size'} || 8388608
$self->{'max_size'}
Truth table100%2/2
A
A= $self->{'max_size'}
Aresult
11
00
473  
474 220 ▶ if ($hdr->{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"expect"}) 0 220 220
475 return $self->_respond ($handle, 417, "Expectation Failed", "")
476 0 ▶ unless lc $hdr->{expect} eq "100-continue";
Branch: 1 branch
BranchTrueFalseTotal
unless lc $hdr->{"expect"} eq "100-continue" 0 0 0
477  
478 0 $handle->push_write ("HTTP/1.1 100 Continue\r\n\r\n");
479 }
480  
481 219 ▶ $handle->push_read (chunk => $hdr->{"content-length"}, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
482 219 my ($handle, $body) = @_;
483  
484 219 ▶ $self->_request ($handle, $hdr, $body) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 219 0 219
485 220 });
486 226 });
487 } else {
488 212 ▶ $handle->on_read (sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
489 212 my ($handle) = @_;
490 212 $handle->on_read (undef);
491 212 ▶ $self->_read ($handle, 1) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 212 0 212
492 232 });
493 }
494 }
495  
496 219 ▶sub _request {
Subroutine _request: called CC=42 SCAR=37.6
497 219 my ($self, $handle, $hdr, $body) = @_;
498  
499 219 delete $handle->{mcp_origin};
500  
501 219 ▶ if (my $hook = $self->{on_http_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hook = $self->{"on_http_request"}) 13 206 219
502 13 weaken $self;
503 13 my $replied;
504 8 ▶ my $reply = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
505 8 ▶ return if $replied++ || !$self || $handle->{mcp_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $replied++ or not $self or $handle->{"mcp_closed"} 1 7 8
Condition66%4/6
$replied++ or not $self or $handle->{"mcp_closed"}
l!l&&r!l&&!r
107
$replied++ or not $self
l!l&&r!l&&!r
107
MC/DC33%1/3
$replied++ or not $self or $handle->{"mcp_closed"}
$replied++ not $self $handle->{"mcp_closed"}
Truth table50%2/4
A or not(B) or C
A= $replied++B= not $selfC= $handle->{"mcp_closed"}
ABCresult
1XX1
01X1
0011
0000
506 7 my ($status, $reason, $headers, $content) = @_;
507 7 $self->_respond ($handle, $status, $reason, $content, $headers);
508 13 };
509  
510 13 ▶ return if $hook->($self, { %$hdr, body => $body }, $reply);
Branch: 1 branch
BranchTrueFalseTotal
if &$hook($self, {%$hdr, "body", $body}, $reply) 7 6 13
511 }
512  
513 # some MCP clients strip a trailing slash from the configured URL.
514 212 my $uri = $hdr->{uri};
515 212 ▶ $uri .= "/" if $self->{path} =~ m{/$} && $uri !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path"} =~ m[/$]u and not $uri =~ m[/$]u 8 204 212
Condition100%3/3
$self->{"path"} =~ m[/$]u and not $uri =~ m[/$]u
!ll&&!rl&&r
12038
MC/DC100%2/2
$self->{"path"} =~ m[/$]u and not $uri =~ m[/$]u
$self->{"path"} =~ m[/$]u not $uri =~ m[/$]u
Truth table100%3/3
A and not(B)
A= $self->{"path"} =~ m[/$]uB= not $uri =~ m[/$]u
ABresult
0X0
100
111
516 my ($path_token) = $self->{tokens}
517 212 ▶ ? grep $uri eq $self->_token_path ($_), @{$self->{tokens}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tokens'} ? : 116 96 212
518 212 my $token_path = defined $path_token;
519  
520 return $self->_respond ($handle, 404, "Not Found", "")
521 212 ▶ unless $uri eq $self->{path} || $token_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri eq $self->{"path"} or $token_path 6 206 212
Condition100%3/3
$uri eq $self->{"path"} or $token_path
l!l&&r!l&&!r
177296
MC/DC100%2/2
$uri eq $self->{"path"} or $token_path
$uri eq $self->{"path"} $token_path
Truth table100%3/3
A or B
A= $uri eq $self->{"path"}B= $token_path
ABresult
1X1
011
000
522  
523 206 ▶ if (exists $hdr->{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hdr->{"origin"}) 11 195 206
524 11 my $wildcard = grep $_ eq "*", @{$self->{origins}};
525  
526 return $self->_respond ($handle, 403, "Forbidden", "")
527 11 ▶ unless $wildcard || grep $_ eq $hdr->{origin}, @{$self->{origins}};
Branch: 1 branch
BranchTrueFalseTotal
unless $wildcard or grep(($_ eq $hdr->{"origin"}), @{$$self{"origins"};}) 3 8 11
Condition100%3/3
$wildcard or grep(($_ eq $hdr->{"origin"}), @{$$self{"origins"};})
l!l&&r!l&&!r
713
MC/DC100%2/2
$wildcard or grep(($_ eq $hdr->{"origin"}), @{$$self{"origins"};})
$wildcard grep(($_ eq $hdr->{"origin"}), @{$$self{"origins"};})
Truth table100%3/3
A or B
A= $wildcardB= grep(($_ eq $hdr->{"origin"}), @{$$self{"origins"};})
ABresult
1X1
011
000
528  
529 8 ▶ $handle->{mcp_origin} = $wildcard ? "*" : $hdr->{origin};
Branch: 1 branch
BranchTrueFalseTotal
$wildcard ? : 7 1 8
530 }
531  
532 203 ▶ if ($hdr->{method} eq "OPTIONS" && @{$self->{origins}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"method"} eq "OPTIONS" and @{$self->{"origins"};}) 4 199 203
Condition100%3/3
$hdr->{"method"} eq "OPTIONS" and @{$self->{"origins"};}
!ll&&!rl&&r
19814
MC/DC100%2/2
$hdr->{"method"} eq "OPTIONS" and @{$self->{"origins"};}
$hdr->{"method"} eq "OPTIONS" @{$self->{"origins"};}
Truth table100%3/3
A and B
A= $hdr->{"method"} eq "OPTIONS"B= @{$self->{"origins"};}
ABresult
0X0
100
111
533 return $self->_respond ($handle, 405, "Method Not Allowed", "")
534 if exists $hdr->{"access-control-request-method"}
535 4 ▶ && $hdr->{"access-control-request-method"} ne "POST";
Branch: 1 branch
BranchTrueFalseTotal
if exists $hdr->{"access-control-request-method"} and $hdr->{"access-control-request-method"} ne "POST" 0 4 4
Condition33%1/3
exists $hdr->{"access-control-request-method"} and $hdr->{"access-control-request-method"} ne "POST"
!ll&&!rl&&r
040
MC/DC0%0/2
exists $hdr->{"access-control-request-method"} and $hdr->{"access-control-request-method"} ne "POST"
exists $hdr->{"access-control-request-method"} $hdr->{"access-control-request-method"} ne "POST"
Truth table33%1/3
A and B
A= exists $hdr->{"access-control-request-method"}B= $hdr->{"access-control-request-method"} ne "POST"
ABresult
0X0
100
111
536  
537 4 return $self->_respond ($handle, 204, "No Content", "");
538 }
539  
540 return $self->_respond ($handle, 405, "Method Not Allowed", "")
541 199 ▶ unless $hdr->{method} eq "POST";
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr->{"method"} eq "POST" 1 198 199
542  
543 198 my $authenticated = $path_token;
544 198 ▶ if ($self->{tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tokens"}) 103 95 198
545 103 my $authorization = $hdr->{authorization};
546  
547 103 ▶ if ($authorization =~ /\ABearer ([^\s]+)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authorization =~ /\ABearer ([^\s]+)\z/iu) 71 32 103
548 71 my $token = $1;
549 71 ▶ if (grep $_ eq $token, @{$self->{tokens}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq $token), @{$$self{"tokens"};})) 67 4 71
550 67 ▶ return $self->_respond ($handle, 401, "Unauthorized", "")
Branch: 1 branch
BranchTrueFalseTotal
if defined $authenticated and $authenticated ne $token 1 66 67
Condition66%2/3
defined $authenticated and $authenticated ne $token
!ll&&!rl&&r
6601
MC/DC50%1/2
defined $authenticated and $authenticated ne $token
defined $authenticated $authenticated ne $token
Truth table66%2/3
A and B
A= defined $authenticatedB= $authenticated ne $token
ABresult
0X0
100
111
551 if defined $authenticated && $authenticated ne $token;
552 66 $authenticated = $token;
553 }
554 }
555  
556 102 ▶ return $self->_respond ($handle, 401, "Unauthorized", "")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authenticated 11 91 102
557 unless defined $authenticated;
558 }
559  
560 return $self->_respond ($handle, 415, "Unsupported Media Type", "")
561 186 ▶ unless $hdr->{"content-type"} =~ m{\Aapplication/json(?:\s*;|\z)}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr->{"content-type"} =~ m[\Aapplication/json(?:\s*;|\z)]iu 0 186 186
562  
563 186 my $message = eval { JSON::XS::decode_json $body };
564  
565 186 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 185 186
566 1 ▶ return $self->_rpc_error (sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
567 1 $self->_respond ($handle, 400, "Bad Request", JSON::XS::encode_json $_[0]);
568 1 }, undef, -32700, "Parse error");
569 }
570  
571 185 ▶ my $params = ref $message eq "HASH" && ref $message->{params} eq "HASH" ? $message->{params} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $message eq 'HASH' && ref $message->{'params'} eq 'HASH' ? : 173 12 185
Condition66%2/3
ref $message eq "HASH" and ref $message->{"params"} eq "HASH"
!ll&&!rl&&r
012173
MC/DC50%1/2
ref $message eq "HASH" and ref $message->{"params"} eq "HASH"
ref $message eq "HASH" ref $message->{"params"} eq "HASH"
Truth table66%2/3
A and B
A= ref $message eq "HASH"B= ref $message->{"params"} eq "HASH"
ABresult
0X0
100
111
572 185 ▶ my $meta = ref $params->{_meta} eq "HASH" ? $params->{_meta} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'_meta'} eq 'HASH' ? : 158 27 185
573 my $modern = exists $meta->{"io.modelcontextprotocol/protocolVersion"}
574 185 ▶ || $hdr->{"mcp-protocol-version"} ge "2026";
Condition100%3/3
exists $meta->{'io.modelcontextprotocol/protocolVersion'} || $hdr->{'mcp-protocol-version'} ge '2026'
l!l&&r!l&&!r
158126
MC/DC100%2/2
exists $meta->{'io.modelcontextprotocol/protocolVersion'} || $hdr->{'mcp-protocol-version'} ge '2026'
exists $meta->{'io.modelcontextprotocol/protocolVersion'} $hdr->{'mcp-protocol-version'} ge '2026'
Truth table100%3/3
A || B
A= exists $meta->{'io.modelcontextprotocol/protocolVersion'}B= $hdr->{'mcp-protocol-version'} ge '2026'
ABresult
1X1
011
000
575  
576 185 ▶ if ($modern && ref $message eq "HASH" && exists $message->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modern and ref $message eq "HASH" and exists $message->{"id"}) 158 27 185
Condition83%5/6
$modern and ref $message eq "HASH" and exists $message->{"id"}
!ll&&!rl&&r
261158
$modern and ref $message eq "HASH"
!ll&&!rl&&r
260159
MC/DC66%2/3
$modern and ref $message eq "HASH" and exists $message->{"id"}
$modern ref $message eq "HASH" exists $message->{"id"}
Truth table75%3/4
A and B and C
A= $modernB= ref $message eq "HASH"C= exists $message->{"id"}
ABCresult
0XX0
10X0
1100
1111
577 my %expected = (
578 "mcp-protocol-version" => $meta->{"io.modelcontextprotocol/protocolVersion"},
579 "mcp-method" => $message->{method},
580 158 );
581  
582 $expected{"mcp-name"} = exists $params->{name} ? $params->{name} : $params->{uri}
583 158 ▶ if $message->{method} =~ /\A(?:tools\/call|prompts\/get|resources\/read)\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $message->{"method"} =~ m[\A(?:tools/call|prompts/get|resources/read)\z]u 77 81 158
exists $params->{'name'} ? : 77 0 77
584  
585 158 for my $name (keys %expected) {
586 393 my $value = $hdr->{$name};
587  
588 393 ▶ if ($name eq "mcp-name" && $value =~ /\A=\?base64\?([A-Za-z0-9+\/=]*)\?=\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "mcp-name" and $value =~ m[\A=\?base64\?([A-Za-z0-9+/=]*)\?=\z]u) 1 392 393
Condition100%3/3
$name eq "mcp-name" and $value =~ m[\A=\?base64\?([A-Za-z0-9+/=]*)\?=\z]u
!ll&&!rl&&r
316761
MC/DC100%2/2
$name eq "mcp-name" and $value =~ m[\A=\?base64\?([A-Za-z0-9+/=]*)\?=\z]u
$name eq "mcp-name" $value =~ m[\A=\?base64\?([A-Za-z0-9+/=]*)\?=\z]u
Truth table100%3/3
A and B
A= $name eq "mcp-name"B= $value =~ m[\A=\?base64\?([A-Za-z0-9+/=]*)\?=\z]u
ABresult
0X0
100
111
589 1 $value = eval { AnyEvent::MCP::utf8dec (MIME::Base64::decode_base64 ($1)) };
590 }
591  
592 393 ▶ unless (defined $value && defined $expected{$name} && $value eq $expected{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and defined $expected{$name} and $value eq $expected{$name}) 1 392 393
Condition50%3/6
defined $value and defined $expected{$name} and $value eq $expected{$name}
!ll&&!rl&&r
01392
defined $value and defined $expected{$name}
!ll&&!rl&&r
00393
MC/DC33%1/3
defined $value and defined $expected{$name} and $value eq $expected{$name}
defined $value defined $expected{$name} $value eq $expected{$name}
Truth table50%2/4
A and B and C
A= defined $valueB= defined $expected{$name}C= $value eq $expected{$name}
ABCresult
0XX0
10X0
1100
1111
593 1 ▶ return $self->_rpc_error (sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
594 1 $self->_respond ($handle, 400, "Bad Request", JSON::XS::encode_json $_[0]);
595 1 }, $message->{id}, -32020, "Header mismatch: $name");
596 }
597 }
598 }
599  
600 184 weaken $self;
601  
602 184 ▶ $self->rpc ($message, sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
603 184 my ($response) = @_;
604  
605 184 ▶ return unless $self && !$handle->{mcp_closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and not $handle->{"mcp_closed"} 0 184 184
Condition33%1/3
$self and not $handle->{"mcp_closed"}
!ll&&!rl&&r
00184
MC/DC0%0/2
$self and not $handle->{"mcp_closed"}
$self not $handle->{"mcp_closed"}
Truth table33%1/3
A and not(B)
A= $selfB= not $handle->{"mcp_closed"}
ABresult
0X0
100
111
606  
607 184 ▶ my ($status, $reason) = defined $response ? (200, "OK") : (202, "Accepted");
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 178 6 184
608  
609 184 ▶ if ($modern && defined $response && $response->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modern and defined $response and $response->{"error"}) 6 178 184
Condition100%6/6
$modern and defined $response and $response->{"error"}
!ll&&!rl&&r
271516
$modern and defined $response
!ll&&!rl&&r
261157
MC/DC100%3/3
$modern and defined $response and $response->{"error"}
$modern defined $response $response->{"error"}
Truth table100%4/4
A and B and C
A= $modernB= defined $responseC= $response->{"error"}
ABCresult
0XX0
10X0
1100
1111
610 6 ▶ ($status, $reason) = $response->{error}{code} == -32601
Branch: 1 branch
BranchTrueFalseTotal
$response->{'error'}{'code'} == -32601 ? : 1 5 6
611 ? (404, "Not Found") : (400, "Bad Request");
612 }
613  
614 184 ▶ $self->_respond ($handle, $status, $reason, defined $response ? JSON::XS::encode_json $response : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 178 6 184
615 },
616 peer => $handle->{mcp_peer},
617 user => defined $authenticated ? $self->{token_users}{$authenticated} : undef,
618 184 ▶ protocol_version => $hdr->{"mcp-protocol-version"} || "2025-03-26",
Branch: 1 branch
BranchTrueFalseTotal
defined $authenticated ? : 90 94 184
Condition100%2/2
$hdr->{'mcp-protocol-version'} || '2025-03-26'
l!l
17311
MC/DC100%1/1
$hdr->{'mcp-protocol-version'} || '2025-03-26'
$hdr->{'mcp-protocol-version'}
Truth table100%2/2
A
A= $hdr->{'mcp-protocol-version'}
Aresult
11
00
619 );
620 }
621  
622 224 ▶sub _respond {
Subroutine _respond: called CC=27 SCAR=34.7
623 224 my ($self, $handle, $status, $reason, $body, $headers) = @_;
624  
625 224 ▶ return if $handle->{mcp_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"mcp_closed"} 0 224 224
626  
627 224 ▶ $body = "" unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 224 224
628 224 my $content_type = "application/json";
629 224 my $extra = "";
630  
631 224 ▶ if ($headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers) 7 217 224
632 7 ▶ ref $headers eq "HASH" or die "HTTP response headers must be a hash\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq "HASH" or die "HTTP response headers must be a hash\n" 7 0 7
633 7 ▶ while (my ($name, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 4 7 11
634 4 ▶ $name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u and defined $value and not ref $value and not $value =~ /[\r\n\0]/u or die "Invalid HTTP response header\n" 4 0 4
Condition33%3/9
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u and defined $value and not ref $value and not $value =~ /[\r\n\0]/u
!ll&&!rl&&r
004
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u and defined $value and not ref $value
!ll&&!rl&&r
004
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u and defined $value
!ll&&!rl&&r
004
MC/DC0%0/4
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u and defined $value and not ref $value and not $value =~ /[\r\n\0]/u
$name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u defined $value not ref $value not $value =~ /[\r\n\0]/u
Truth table20%1/5
A and B and not(C) and not(D)
A= $name =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/uB= defined $valueC= not ref $valueD= not $value =~ /[\r\n\0]/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
635 && defined $value && !ref $value && $value !~ /[\r\n\0]/
636 or die "Invalid HTTP response header\n";
637 4 ▶ $name !~ /\A(?:content-length|connection|transfer-encoding)\z/i
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A(?:content-length|connection|transfer-encoding)\z/iu and die "HTTP response framing headers are managed by the server\n" 0 4 4
638 or die "HTTP response framing headers are managed by the server\n";
639 4 ▶ if (lc $name eq "content-type") {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq 'content-type') { } 2 2 4
640 2 $content_type = $value;
641 } else {
642 2 $extra .= "$name: $value\r\n";
643 }
644 }
645 }
646  
647 224 ▶ if ($status >= 400 && !$handle->{mcp_root_get}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status >= 400 and not $handle->{"mcp_root_get"}) 34 190 224
Condition100%3/3
$status >= 400 and not $handle->{"mcp_root_get"}
!ll&&!rl&&r
189134
MC/DC100%2/2
$status >= 400 and not $handle->{"mcp_root_get"}
$status >= 400 not $handle->{"mcp_root_get"}
Truth table100%3/3
A and not(B)
A= $status >= 400B= not $handle->{"mcp_root_get"}
ABresult
0X0
100
111
648 AE::log warn => "MCP HTTP id=" . sprintf ("%x", refaddr $handle)
649 . (defined $handle->{mcp_remote} ? " remote=$handle->{mcp_remote}" : "")
650 34 ▶ . ": " . ($handle->{mcp_method} // "-") . " $status";
Branch: 1 branch
BranchTrueFalseTotal
defined $handle->{'mcp_remote'} ? : 15 19 34
Condition50%1/2
$handle->{"mcp_method"} // "-"
l!l
340
MC/DC0%0/1
$handle->{"mcp_method"} // "-"
$handle->{"mcp_method"}
Truth table50%1/2
A
A= $handle->{"mcp_method"}
Aresult
11
00
651 }
652  
653 224 ▶ my $close = $handle->{mcp_close} || $status != 200 && $status != 202;
Condition83%5/6
$handle->{'mcp_close'} || $status != 200 && $status != 202
l!l&&r!l&&!r
1812419
$status != 200 and $status != 202
!ll&&!rl&&r
19024
MC/DC66%2/3
$handle->{'mcp_close'} || $status != 200 && $status != 202
$handle->{'mcp_close'} $status != 200 $status != 202
Truth table75%3/4
A || B and C
A= $handle->{'mcp_close'}B= $status != 200C= $status != 202
ABCresult
1XX1
0111
00X0
0100
654  
655 $handle->push_write (
656 "HTTP/1.1 $status $reason\r\n"
657 . "Content-Type: $content_type\r\n"
658 . "Content-Length: " . (length $body) . "\r\n"
659 . $extra
660 . (defined $handle->{mcp_origin} ? "Access-Control-Allow-Origin: $handle->{mcp_origin}\r\n"
661 . ($handle->{mcp_origin} ne "*" ? "Vary: Origin\r\n" : "") : "")
662 . ($status == 204 ? "Access-Control-Allow-Methods: POST, OPTIONS\r\n"
663 . "Access-Control-Allow-Headers: content-type,authorization,mcp-protocol-version,mcp-method,mcp-name\r\n"
664 . "Access-Control-Max-Age: 86400\r\n" : "")
665 1 ▶ . ($status == 405 ? "Allow: POST" . (@{$self->{origins}} ? ", OPTIONS" : "") . "\r\n" : "")
Branch: 2 branches
BranchTrueFalseTotal
$status == 401 && $self->{'tokens'} ? : 12 212 224
$close ? : 205 19 224
Condition66%2/3
$status == 401 and $self->{"tokens"}
!ll&&!rl&&r
212012
MC/DC50%1/2
$status == 401 and $self->{"tokens"}
$status == 401 $self->{"tokens"}
Truth table66%2/3
A and B
A= $status == 401B= $self->{"tokens"}
ABresult
0X0
100
111
666 224 ▶ . ($status == 401 && $self->{tokens} ? "WWW-Authenticate: Bearer\r\n" : "")
Branch: 5 branches
BranchTrueFalseTotal
defined $handle->{'mcp_origin'} ? : 8 216 224
$handle->{'mcp_origin'} ne '*' ? : 1 7 8
$status == 204 ? : 4 220 224
$status == 405 ? : 1 223 224
@{$self->{'origins'};} ? : 0 1 1
667 . ($close ? "Connection: close\r\n" : "")
668 . "\r\n$body"
669 );
670  
671 224 ▶ if ($close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close) { } 205 19 224
672 205 weaken $self;
673 205 ▶ $handle->on_drain (sub { $self->_close ($_[0]) if $self });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $self 205 0 205
674 } else {
675 19 $self->_read ($handle);
676 }
677 }
678  
679 232 ▶sub _close {
Subroutine _close: called CC=4 SCAR=13.9
680 232 my ($self, $handle) = @_;
681  
682 232 ▶ return if $handle->{mcp_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"mcp_closed"} 0 232 232
683 232 $handle->{mcp_closed} = 1;
684  
685 232 ▶ my $requests = delete $handle->{mcp_peer}{requests} || {};
Condition100%2/2
delete $handle->{'mcp_peer'}{'requests'} || {}
l!l
17260
MC/DC100%1/1
delete $handle->{'mcp_peer'}{'requests'} || {}
delete $handle->{'mcp_peer'}{'requests'}
Truth table100%2/2
A
A= delete $handle->{'mcp_peer'}{'requests'}
Aresult
11
00
686 232 $_->cancel ("Connection closed") for grep defined, values %$requests;
687  
688 232 delete $self->{connections}{$handle};
689 232 $handle->destroy;
690 }
691  
692 224 ▶sub close {
Subroutine close: called CC=4 SCAR=13.9
Pod documented
693 224 my ($self) = @_;
694  
695 224 ▶ return if $self->{_closed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed"}++ 96 128 224
696 128 delete $self->{listener};
697 128 ▶ $self->_close ($_) for values %{$self->{connections} || {}};
Condition100%2/2
$self->{'connections'} || {}
l!l
7652
MC/DC100%1/1
$self->{'connections'} || {}
$self->{'connections'}
Truth table100%2/2
A
A= $self->{'connections'}
Aresult
11
00
698 128 $self->event (close =>);
699 }
700  
701 110 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
702 110 $_[0]->close
703 }
704  
705 61 1
« bin/mcp-fs ↑ Summary blib/lib/AnyEvent/MCP/Client/HTTP.pm »