bin/mcp-http

filter: Statementstmt 96.3% 130 / 135 Branchbran 77.2% 68 / 88 Conditioncond 59.6% 59 / 99 MC/DCmcdc 30.1% 16 / 53 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 73.7% 289 / 392 CC 79 SCAR 32.3
CC
79
Cov
80%
__ANON__
50.3
url_scopes
21.5
__ANON__
17.5
CRAP 25.4
« bin/mcp-ask-user ↑ Summary bin/mcp-vim »
1 #!/opt/bin/perl
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 mcp-http - HTTP requests with URL and method permissions
8  
9 =head1 SYNOPSIS
10  
11 mcp-http --stdio
12 mcp-http --connect http://host:port/TOKEN/http
13  
14 =head1 DESCRIPTION
15  
16 One MCP C<request> tool backed by L<AnyEvent::HTTP>. HTTP methods are
17 permission actions. URL scopes: exact URL, path with any query, origin,
18 hostname, hostname and subdomains, or any HTTP(S) URL.
19  
20 Supply a C<cookie_jar> from an earlier response to continue a session;
21 the server keeps no cookies. This uses AnyEvent::HTTP's simplified jar,
22 not a full browser cookie implementation. Treat the jar as confidential.
23  
24 Responses include status, headers, UTF-8 body or Base64 bytes. Optional
25 C<grep> keeps lines containing a literal string; C<regex> keeps lines
26 matching a Perl regex; C<substitute> replaces regex matches with literal
27 replacement text. C<max_bytes> bounds downloads and C<truncated> reports
28 partial results.
29  
30 Redirects and retries are disabled. HTTPS verifies certificates.
31 Network access is NOT sandboxed; permitted names can reach local or
32 private hosts. HTTP failures do not prove that a write did not happen.
33  
34 =head1 FIRST USE
35  
36 Configure your MCP client to launch:
37  
38 mcp-http --stdio --name web
39  
40 Or combine it with other servers:
41  
42 mcp-multibroker --stdio web --- mcp-http --name web --stdio ---
43  
44 Ask the assistant:
45  
46 Fetch https://example.org/ and report the status and page title.
47 Do not submit forms or send any modifying requests.
48  
49 The C<request> tool receives arguments such as:
50  
51 {"url": "https://example.org/", "method": "GET", "max_bytes": 8192}
52  
53 The first request asks for permission to GET that URL. An approval for
54 GET does not authorize POST, and an approval for an exact URL need not
55 authorize its whole hostname. Review broader scope choices carefully,
56 especially on hosts serving private or local resources.
57  
58 To carry a session between requests, supply the returned C<cookie_jar>
59 to the next call. To inspect a large text response without returning it
60 all, use C<grep> or C<regex> and a suitable C<max_bytes>. Redirect
61 responses are returned to the caller; they are not followed. Repeating
62 a failed modifying request can repeat its side effects.
63  
64 =head1 TOOL
65  
66 =over
67  
68 =item request
69  
70 Required C<url>. Optional C<method> (GET), C<headers>, UTF-8 C<body>
71 or C<body_base64>, C<cookie_jar>, C<timeout> (30s inactivity),
72 C<max_bytes> (65536 default, 2097152 maximum), C<grep>, C<regex>,
73 and C<substitute> with C<pattern> and C<replacement>. Responses contain
74 C<captured_bytes> and C<truncated>. Replacement text is literal.
75  
76 =back
77  
78 =head1 PERMISSIONS
79  
80 Profile C<http-NAME>. Rules match HTTP method as C<action> and URL
81 as C<match.url>. Hostname and subdomain scopes are textual DNS suffixes,
82 not organizational or public-suffix boundaries. No sandbox grants.
83  
84 =head1 OPTIONS
85  
86 Shared switches are documented in
87 L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>.
88 The following switch is specific to C<mcp-http>.
89  
90 =over
91  
92 =item --ask MODE
93  
94 Select permission prompt handling: C<helper> (default) uses the local
95 permission helper; C<tty> or C<yad> selects only that interface;
96 C<mcp> forwards prompts to the MCP client; C<auto> uses client forms
97 when advertised, otherwise the local helper.
98  
99 =back
100  
101 =cut
102  
103 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 2 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
106 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 2 ▶use AnyEvent::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
111 2 ▶use AnyEvent::MCP::Server::Permissions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 2 ▶use AnyEvent::MCP::Server::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 2 my $options = mcp_getoptions
116 {
117 usage => "Usage: mcp-http [OPTION]...\n",
118 description => "HTTP requests with URL/method permissions and caller-managed cookies.",
119 options => [["--ask MODE", "Permission prompts: helper (default), tty, yad, mcp, auto"]],
120 examples => " mcp-http --stdio\n",
121 },
122 "ask=s" => \my $ask,
123 ;
124  
125 2 ▶@ARGV and die "Unexpected arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV and die "Unexpected arguments\n" 0 2 2
126  
127 2 ▶my $permissions = AnyEvent::MCP::Server::Permissions->new (
Branch: 1 branch
BranchTrueFalseTotal
defined $ask ? : 1 1 2
128 name => "http-$options->{name}",
129 (defined $ask ? (ask => $ask) : ()),
130 );
131  
132 mcp_config
133 permissions => $permissions,
134 server_name => "mcp-http",
135 server_version => $AnyEvent::MCP::VERSION,
136 short_instructions => "HTTP requests with URL/method permissions; pass cookie_jar between calls. No redirects.",
137 instructions => "request makes authorized HTTP(S) requests. Method is the permission action. "
138 . "URL scopes range from exact URL to any HTTP(S) URL. No automatic redirects or retries. "
139 . "Pass a returned cookie_jar to subsequent calls. "
140 . "grep/regex select lines; substitute applies literal replacements. "
141 . "Responses may be truncated and failed writes can still have taken effect.",
142 17 ▶ on_request => sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
143 17 my ($server, $req) = @_;
144  
145 return mcp_log_request $server, $req
146 17 ▶ unless $req->{method} eq "tools/call" && $req->{tool} eq "request";
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"method"} eq "tools/call" and $req->{"tool"} eq "request" 3 14 17
Condition66%2/3
$req->{"method"} eq "tools/call" and $req->{"tool"} eq "request"
!ll&&!rl&&r
3014
MC/DC50%1/2
$req->{"method"} eq "tools/call" and $req->{"tool"} eq "request"
$req->{"method"} eq "tools/call" $req->{"tool"} eq "request"
Truth table66%2/3
A and B
A= $req->{"method"} eq "tools/call"B= $req->{"tool"} eq "request"
ABresult
0X0
100
111
147  
148 14 my $args = $req->{args};
149 14 ▶ my $method = $args->{method} // "GET";
Condition100%2/2
$args->{"method"} // "GET"
l!l
311
MC/DC100%1/1
$args->{"method"} // "GET"
$args->{"method"}
Truth table100%2/2
A
A= $args->{"method"}
Aresult
11
00
150 14 ▶ my $url = $args->{url} // "";
Condition50%1/2
$args->{"url"} // ""
l!l
140
MC/DC0%0/1
$args->{"url"} // ""
$args->{"url"}
Truth table50%1/2
A
A= $args->{"url"}
Aresult
11
00
151 14 ▶ $method = "<invalid>" if ref $method;
Branch: 1 branch
BranchTrueFalseTotal
if ref $method 0 14 14
152 14 ▶ $url = "<invalid>" if ref $url;
Branch: 1 branch
BranchTrueFalseTotal
if ref $url 0 14 14
153 14 $url =~ s/[?#].*//s;
154 14 $url =~ s{(://)[^/@]*@}{$1};
155 14 $url =~ s/[\x00-\x1f\x7f]/ /g;
156  
157 14 mcp_log_request $server, $req,
158 method => (substr $method, 0, 20),
159 url => (substr $url, 0, 160);
160 2 };
161  
162 13 ▶sub url_scopes {
Subroutine url_scopes: called CC=17 SCAR=30.7
163 13 my ($url) = @_;
164  
165 13 ▶ defined $url && !ref $url && $url !~ /[\x00-\x20\x7f]/
Branch: 1 branch
BranchTrueFalseTotal
defined $url and not ref $url and not $url =~ /[\x00-\x20\x7f]/u or die "Invalid URL\n" 13 0 13
Condition33%2/6
defined $url and not ref $url and not $url =~ /[\x00-\x20\x7f]/u
!ll&&!rl&&r
0013
defined $url and not ref $url
!ll&&!rl&&r
0013
MC/DC0%0/3
defined $url and not ref $url and not $url =~ /[\x00-\x20\x7f]/u
defined $url not ref $url not $url =~ /[\x00-\x20\x7f]/u
Truth table25%1/4
A and not(B) and not(C)
A= defined $urlB= not ref $urlC= not $url =~ /[\x00-\x20\x7f]/u
ABCresult
0XX0
10X0
1100
1111
166 or die "Invalid URL\n";
167 13 ▶ $url =~ m{\A(https?)://(\[[0-9a-f:.]+\]|[a-z0-9.-]+)(?::([0-9]+))?([^#]*)(?:#.*)?\z}i
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m(\A(https?)://(\[[0-9a-f:.]+\]|[a-z0-9.-]+)(?::([0-9]+))?([^#]*)(?:#.*)?\z)iu or die "Expected HTTP(S) URL without userinfo\n" 13 0 13
168 or die "Expected HTTP(S) URL without userinfo\n";
169  
170 13 my ($scheme, $host, $port, $rest) = (lc $1, lc $2, $3, $4);
171 13 ▶ !defined $port || $port >= 1 && $port <= 65535
Branch: 1 branch
BranchTrueFalseTotal
not defined $port or $port >= 1 and $port <= 65535 or die "Invalid URL port\n" 13 0 13
Condition50%3/6
not defined $port or $port >= 1 and $port <= 65535
l!l&&r!l&&!r
1120
$port >= 1 and $port <= 65535
!ll&&!rl&&r
0012
MC/DC0%0/3
not defined $port or $port >= 1 and $port <= 65535
not defined $port $port >= 1 $port <= 65535
Truth table50%2/4
not(A) or B and C
A= not defined $portB= $port >= 1C= $port <= 65535
ABCresult
1XX1
0111
00X0
0100
172 or die "Invalid URL port\n";
173 13 ▶ $port = undef if defined $port && $port == ($scheme eq "https" ? 443 : 80);
Branch: 2 branches
BranchTrueFalseTotal
if defined $port and $port == ($scheme eq "https" ? 443 : 80) 1 12 13
$scheme eq 'https' ? : 1 11 12
Condition100%3/3
defined $port and $port == ($scheme eq "https" ? 443 : 80)
!ll&&!rl&&r
1111
MC/DC100%2/2
defined $port and $port == ($scheme eq "https" ? 443 : 80)
defined $port $port == ($scheme eq "https" ? 443 : 80)
Truth table100%3/3
A and B
A= defined $portB= $port == ($scheme eq "https" ? 443 : 80)
ABresult
0X0
100
111
174 13 ▶ $rest =~ m{\A(?:/|\?|\z)} or die "Invalid URL path\n";
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ m[\A(?:/|\?|\z)]u or die "Invalid URL path\n" 12 1 13
175 12 ▶ $rest = "/$rest" unless $rest =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ m[\A/]u 0 12 12
176  
177 12 ▶ my $origin = "$scheme://$host" . (defined $port ? ":$port" : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $port ? : 11 1 12
178 12 my $path = $rest;
179 12 $path =~ s/\?.*//s;
180 12 my $full = "$origin$rest";
181 12 my @choices = (
182 { label => "Exact URL including query", match => $full },
183 { label => "This path, any query",
184 match => "^" . (quotemeta "$origin$path") . "(?:\\?.*)?\\z" },
185 { label => "This origin (scheme, host, port)",
186 match => "^" . (quotemeta $origin) . "/" },
187 { label => "This hostname, any scheme or port",
188 match => "^https?://" . (quotemeta $host) . "(?::[0-9]+)?/" },
189 );
190 12 ▶ if ($host !~ /^\[/ && $host =~ /\./ && $host !~ /^[0-9.]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $host =~ /^\[/u and $host =~ /\./u and not $host =~ /^[0-9.]+$/u) 1 11 12
Condition50%3/6
not $host =~ /^\[/u and $host =~ /\./u and not $host =~ /^[0-9.]+$/u
!ll&&!rl&&r
0111
not $host =~ /^\[/u and $host =~ /\./u
!ll&&!rl&&r
0012
MC/DC33%1/3
not $host =~ /^\[/u and $host =~ /\./u and not $host =~ /^[0-9.]+$/u
not $host =~ /^\[/u $host =~ /\./u not $host =~ /^[0-9.]+$/u
Truth table50%2/4
not(A) and B and not(C)
A= not $host =~ /^\[/uB= $host =~ /\./uC= not $host =~ /^[0-9.]+$/u
ABCresult
0XX0
10X0
1100
1111
191 1 push @choices, {
192 label => "Hostname and subdomains, any scheme or port",
193 match => "^https?://(?:[a-z0-9-]+\\.)*" . (quotemeta $host) . "(?::[0-9]+)?/",
194 };
195 }
196 12 push @choices, { label => "Any HTTP(S) URL", match => "^https?://" };
197  
198 12 ($full, \@choices)
199 }
200  
201 5 ▶sub text_regex {
Subroutine text_regex: called CC=5 SCAR=17.5
202 5 my ($pattern) = @_;
203  
204 5 ▶ defined $pattern && !ref $pattern && length $pattern <= 512
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern and not ref $pattern and length $pattern <= 512 or die "Invalid regex pattern\n" 5 0 5
Condition33%2/6
defined $pattern and not ref $pattern and length $pattern <= 512
!ll&&!rl&&r
005
defined $pattern and not ref $pattern
!ll&&!rl&&r
005
MC/DC0%0/3
defined $pattern and not ref $pattern and length $pattern <= 512
defined $pattern not ref $pattern length $pattern <= 512
Truth table25%1/4
A and not(B) and C
A= defined $patternB= not ref $patternC= length $pattern <= 512
ABCresult
0XX0
10X0
1100
1111
205 or die "Invalid regex pattern\n";
206 5 my $regex = eval { qr/$pattern/ };
207 5 ▶ $@ and die "Invalid regex: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Invalid regex: $@" 1 4 5
208  
209 4 $regex
210 }
211  
212 7 ▶sub filter_text {
Subroutine filter_text: called CC=8 SCAR=20.8
213 7 my ($text, $args) = @_;
214  
215 7 ▶ if (defined $args->{grep} || defined $args->{regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"grep"} or defined $args->{"regex"}) 2 5 7
Condition100%3/3
defined $args->{"grep"} or defined $args->{"regex"}
l!l&&r!l&&!r
115
MC/DC100%2/2
defined $args->{"grep"} or defined $args->{"regex"}
defined $args->{"grep"} defined $args->{"regex"}
Truth table100%3/3
A or B
A= defined $args->{"grep"}B= defined $args->{"regex"}
ABresult
1X1
011
000
216 2 my $grep = $args->{grep};
217 2 ▶ my $regex = defined $args->{regex} ? text_regex ($args->{regex}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'regex'} ? : 1 1 2
218 $text = join "", grep {
219 2 ▶ (!defined $grep || index ($_, $grep) >= 0)
Condition100%9/9
(!defined($grep) || index($_, $grep) != -1) && (!defined($regex) || /$regex/u)
!ll&&!rl&&r
213
not defined $grep or index($_, $grep) != -1
l!l&&r!l&&!r
312
not defined $regex or /$regex/u
l!l&&r!l&&!r
121
MC/DC75%3/4
(!defined($grep) || index($_, $grep) != -1) && (!defined($regex) || /$regex/u)
not defined $grep index($_, $grep) != -1 not defined $regex /$regex/u
Truth table57%4/7
not(A) or B && not(C) or D
A= not defined $grepB= index($_, $grep) != -1C= not defined $regexD= /$regex/u
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
220 && (!defined $regex || /$regex/)
221 } split /(?<=\n)/, $text;
222 }
223  
224 7 ▶ if (my $subst = $args->{substitute}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subst = $args->{"substitute"}) 1 6 7
225 1 my $regex = text_regex ($subst->{pattern});
226 1 my $replacement = $subst->{replacement};
227 1 $text =~ s/$regex/$replacement/g;
228 }
229  
230 $text
231 7 }
232  
233 mcp_tool request => "HTTP request. Method and URL permissions. No redirects; caller-managed cookie_jar returned. "
234 . "UTF-8 body or Base64, bounded response, optional grep/regex/substitute.",
235 {
236 type => "object",
237 required => ["url"],
238 additionalProperties => JSON::XS::false,
239 properties => {
240 url => { type => "string", minLength => 1 },
241 method => { type => "string", default => "GET" },
242 headers => { type => "object", additionalProperties => { type => "string" } },
243 body => { type => "string", description => "UTF-8 request body" },
244 body_base64 => { type => "string", description => "Raw request body, Base64" },
245 cookie_jar => { type => "object", description => "AnyEvent::HTTP cookie jar returned by an earlier call" },
246 timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
247 max_bytes => { type => "integer", minimum => 1, maximum => 2097152, default => 65536 },
248 grep => { type => "string", description => "Keep lines containing this literal substring" },
249 regex => { type => "string", description => "Keep lines matching this Perl regex" },
250 substitute => {
251 type => "object",
252 required => ["pattern", "replacement"],
253 additionalProperties => JSON::XS::false,
254 properties => {
255 pattern => { type => "string", maxLength => 512 },
256 replacement => { type => "string", maxLength => 512 },
257 },
258 },
259 },
260 },
261 14 ▶ sub {
Subroutine __ANON__: called CC=33 SCAR=39.2
262 14 my ($req, $args) = @_;
263  
264 14 ▶ my $method = $args->{method} // "GET";
Condition100%2/2
$args->{"method"} // "GET"
l!l
311
MC/DC100%1/1
$args->{"method"} // "GET"
$args->{"method"}
Truth table100%2/2
A
A= $args->{"method"}
Aresult
11
00
265 14 ▶ $method =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u or die "Invalid HTTP method\n" 14 0 14
266 or die "Invalid HTTP method\n";
267 exists $args->{body} && exists $args->{body_base64}
268 14 ▶ and die "Specify body or body_base64, not both\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{"body"} and exists $args->{"body_base64"} and die "Specify body or body_base64, not both\n" 1 13 14
Condition66%2/3
exists $args->{"body"} and exists $args->{"body_base64"}
!ll&&!rl&&r
1301
MC/DC50%1/2
exists $args->{"body"} and exists $args->{"body_base64"}
exists $args->{"body"} exists $args->{"body_base64"}
Truth table66%2/3
A and B
A= exists $args->{"body"}B= exists $args->{"body_base64"}
ABresult
0X0
100
111
269 13 my ($url, $scopes) = url_scopes ($args->{url});
270  
271 12 ▶ my $headers = $args->{headers} || {};
Condition100%2/2
$args->{'headers'} || {}
l!l
39
MC/DC100%1/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table100%2/2
A
A= $args->{'headers'}
Aresult
11
00
272 12 for my $key (keys %$headers) {
273 $key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/
274 && defined $headers->{$key} && !ref $headers->{$key}
275 3 ▶ && $headers->{$key} !~ /[\r\n\x00]/
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key} and not $headers->{$key} =~ /[\r\n\x00]/u or die "Invalid HTTP header\n" 2 1 3
Condition44%4/9
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key} and not $headers->{$key} =~ /[\r\n\x00]/u
!ll&&!rl&&r
012
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key}
!ll&&!rl&&r
003
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key}
!ll&&!rl&&r
003
MC/DC25%1/4
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key} and not $headers->{$key} =~ /[\r\n\x00]/u
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u defined $headers->{$key} not ref $headers->{$key} not $headers->{$key} =~ /[\r\n\x00]/u
Truth table40%2/5
A and B and not(C) and not(D)
A= $key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/uB= defined $headers->{$key}C= not ref $headers->{$key}D= not $headers->{$key} =~ /[\r\n\x00]/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
276 or die "Invalid HTTP header\n";
277 }
278 11 $headers = { %$headers };
279 11 ▶ $headers->{"User-Agent"} = "mcp-http/$AnyEvent::MCP::VERSION"
Branch: 1 branch
BranchTrueFalseTotal
unless grep((lc $_ eq "user-agent"), keys %$headers) 10 1 11
280 unless grep lc $_ eq "user-agent", keys %$headers;
281 11 ▶ my $data = exists $args->{body} ? (AnyEvent::MCP::utf8enc $args->{body}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'body'} ? : 0 11 11
282 11 ▶ if (defined $args->{body_base64}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"body_base64"}) 1 10 11
283 1 ▶ $args->{body_base64} =~ /\A(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?\z/
Branch: 1 branch
BranchTrueFalseTotal
$args->{"body_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u or die "Invalid Base64 body\n" 1 0 1
284 or die "Invalid Base64 body\n";
285 1 $data = MIME::Base64::decode_base64 ($args->{body_base64});
286 }
287 11 ▶ !defined $data || length $data <= 1048576 or die "Request body exceeds 1 MiB\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $data or length $data <= 1048576 or die "Request body exceeds 1 MiB\n" 11 0 11
Condition66%2/3
not defined $data or length $data <= 1048576
l!l&&r!l&&!r
1010
MC/DC0%0/2
not defined $data or length $data <= 1048576
not defined $data length $data <= 1048576
Truth table66%2/3
not(A) or B
A= not defined $dataB= length $data <= 1048576
ABresult
1X1
011
000
288 11 ▶ my $limit = $args->{max_bytes} // 65536;
Condition100%2/2
$args->{"max_bytes"} // 65536
l!l
110
MC/DC100%1/1
$args->{"max_bytes"} // 65536
$args->{"max_bytes"}
Truth table100%2/2
A
A= $args->{"max_bytes"}
Aresult
11
00
289 11 ▶ $limit >= 1 && $limit <= 2097152 or die "Invalid max_bytes\n";
Branch: 1 branch
BranchTrueFalseTotal
$limit >= 1 and $limit <= 2097152 or die "Invalid max_bytes\n" 11 0 11
Condition33%1/3
$limit >= 1 and $limit <= 2097152
!ll&&!rl&&r
0011
MC/DC0%0/2
$limit >= 1 and $limit <= 2097152
$limit >= 1 $limit <= 2097152
Truth table33%1/3
A and B
A= $limit >= 1B= $limit <= 2097152
ABresult
0X0
100
111
290  
291 11 ▶ !defined $args->{grep} || !ref $args->{grep} or die "Invalid grep\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"grep"} and ref $args->{"grep"} and die "Invalid grep\n" 0 11 11
Condition66%2/3
defined $args->{"grep"} and ref $args->{"grep"}
!ll&&!rl&&r
1010
MC/DC0%0/2
defined $args->{"grep"} and ref $args->{"grep"}
defined $args->{"grep"} ref $args->{"grep"}
Truth table66%2/3
A and B
A= defined $args->{"grep"}B= ref $args->{"grep"}
ABresult
0X0
100
111
292 11 ▶ text_regex ($args->{regex}) if defined $args->{regex};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"regex"} 2 9 11
293 10 ▶ if (defined $args->{substitute}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"substitute"}) 1 9 10
294 ref $args->{substitute} eq "HASH" && defined $args->{substitute}{replacement}
295 1 ▶ && !ref $args->{substitute}{replacement} && length $args->{substitute}{replacement} <= 512
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"} and length $args->{"substitute"}{"replacement"} <= 512 or die "Invalid substitute\n" 1 0 1
Condition33%3/9
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"} and length $args->{"substitute"}{"replacement"} <= 512
!ll&&!rl&&r
001
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"}
!ll&&!rl&&r
001
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"}
!ll&&!rl&&r
001
MC/DC0%0/4
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"} and length $args->{"substitute"}{"replacement"} <= 512
ref $args->{"substitute"} eq "HASH" defined $args->{"substitute"}{"replacement"} not ref $args->{"substitute"}{"replacement"} length $args->{"substitute"}{"replacement"} <= 512
Truth table20%1/5
A and B and not(C) and D
A= ref $args->{"substitute"} eq "HASH"B= defined $args->{"substitute"}{"replacement"}C= not ref $args->{"substitute"}{"replacement"}D= length $args->{"substitute"}{"replacement"} <= 512
ABCDresult
0XXX0
10XX0
110X0
11100
11111
296 or die "Invalid substitute\n";
297 1 text_regex ($args->{substitute}{pattern});
298 }
299 10 my $jar = $args->{cookie_jar};
300 10 ▶ !defined $jar || ref $jar eq "HASH" or die "Invalid cookie jar\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $jar or ref $jar eq "HASH" or die "Invalid cookie jar\n" 10 0 10
Condition66%2/3
not defined $jar or ref $jar eq "HASH"
l!l&&r!l&&!r
820
MC/DC0%0/2
not defined $jar or ref $jar eq "HASH"
not defined $jar ref $jar eq "HASH"
Truth table66%2/3
not(A) or B
A= not defined $jarB= ref $jar eq "HASH"
ABresult
1X1
011
000
301  
302 $req->require_permission ({
303 tool => $req->{tool},
304 10 action => $method,
305 dimensions => { url => { label => "URL", value => $url, choices => $scopes } },
306 sandbox_grants => [],
307 reason => "$method $url",
308 });
309  
310 8 my $body = "";
311 8 my $truncated = 0;
312 8 my $response_headers;
313 my $guard;
314 $guard = AnyEvent::HTTP::http_request
315 $method => $url,
316 recurse => 0,
317 persistent => 0,
318 proxy => undef,
319 tls_ctx => "high",
320 timeout => $args->{timeout} // 30,
321 headers => $headers,
322 (defined $data ? (body => $data) : ()),
323 (defined $jar ? (cookie_jar => $jar) : ()),
324 8 ▶ on_header => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
325 8 $response_headers = { %{$_[0]} };
326 8 1
327 },
328 7 ▶ on_body => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
329 7 my ($part) = @_;
330 7 ▶ if (length ($body) + length ($part) > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($body) + length($part) > $limit) 1 6 7
331 1 $body .= substr $part, 0, $limit - length $body;
332 1 $truncated = 1;
333 1 return 0;
334 }
335 6 $body .= $part;
336 6 1
337 },
338 8 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=28.6
339 8 my ($unused, $hdr) = @_;
340  
341 8 undef $guard;
342 8 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 0 8 8
343 8 ▶ $hdr = $response_headers if $truncated && $response_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated and $response_headers 1 7 8
Condition66%2/3
$truncated and $response_headers
!ll&&!rl&&r
701
MC/DC50%1/2
$truncated and $response_headers
$truncated $response_headers
Truth table66%2/3
A and B
A= $truncatedB= $response_headers
ABresult
0X0
100
111
344 my %result = (
345 status => 0 + $hdr->{Status},
346 reason => $hdr->{Reason},
347 8 ▶ headers => { map +($_ => $hdr->{$_}), grep /^[a-z]/, keys %$hdr },
Branch: 2 branches
BranchTrueFalseTotal
$truncated ? : 1 7 8
defined $jar ? : 2 6 8
348 captured_bytes => length $body,
349 truncated => $truncated ? JSON::XS::true : JSON::XS::false,
350 (defined $jar ? (cookie_jar => $jar) : ()),
351 );
352  
353 8 ▶ if (defined $unused || $truncated) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $unused or $truncated) 8 0 8
Condition66%2/3
defined $unused or $truncated
l!l&&r!l&&!r
710
MC/DC0%0/2
defined $unused or $truncated
defined $unused $truncated
Truth table66%2/3
A or B
A= defined $unusedB= $truncated
ABresult
1X1
011
000
354 8 my $bytes = $body;
355 8 my $text = eval { Encode::decode ("UTF-8", $bytes, Encode::FB_CROAK) };
356 8 ▶ if (defined $text) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $text) { } 7 1 8
elsif (defined $args->{'grep'} or defined $args->{'regex'} or defined $args->{'substitute'}) { } 0 1 1
357 7 $text = filter_text ($text, $args);
358 7 ▶ if (length $text > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text > $limit) 0 7 7
359 0 $text = substr $text, 0, $limit;
360 0 $result{truncated} = JSON::XS::true;
361 }
362 7 $result{body} = $text;
363 ▶ } elsif (defined $args->{grep} || defined $args->{regex} || defined $args->{substitute}) {
Condition33%2/6
defined $args->{"grep"} or defined $args->{"regex"} or defined $args->{"substitute"}
l!l&&r!l&&!r
001
defined $args->{"grep"} or defined $args->{"regex"}
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $args->{"grep"} or defined $args->{"regex"} or defined $args->{"substitute"}
defined $args->{"grep"} defined $args->{"regex"} defined $args->{"substitute"}
Truth table25%1/4
A or B or C
A= defined $args->{"grep"}B= defined $args->{"regex"}C= defined $args->{"substitute"}
ABCresult
1XX1
01X1
0011
0000
364 0 $req->error ("Cannot filter a non-UTF-8 response");
365 0 return;
366 } else {
367 1 $result{body_base64} = MIME::Base64::encode_base64 ($body, "");
368 }
369 }
370  
371 8 $req->result (\%result);
372 }
373 8 ▶ ;
Branch: 2 branches
BranchTrueFalseTotal
defined $data ? : 1 7 8
defined $jar ? : 2 6 8
Condition50%1/2
$args->{"timeout"} // 30
l!l
08
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table50%1/2
A
A= $args->{"timeout"}
Aresult
11
00
374 0 ▶ $req->{on_cancel} = sub { undef $guard };
Subroutine __ANON__: not called CC=1 SCAR=1.2
375 2 };
376  
377 2 mcp_start %$options;
« bin/mcp-ask-user ↑ Summary bin/mcp-vim »