blib/lib/Langertha/Raider/WebTools.pm

filter: Statementstmt 94.2% 114 / 121 Branchbran 62.5% 20 / 32 Conditioncond 54.0% 20 / 37 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 75.3% 174 / 231 CC 35 SCAR 22.8
CC
35
Cov
81%
build_web_tools_server
16.1
__ANON__
15.3
__ANON__
10.0
CRAP 9.8
« blib/lib/Langertha/Raider/PerlTools.pm ↑ Summary blib/lib/Langertha/Raider/Hall.pm »
1 package Langertha::Raider::WebTools;
2 77 our $VERSION = '0.503';
3 # ABSTRACT: MCP::Server factory with web search and fetch tools (Net::Async::WebSearch + Net::Async::HTTP)
4  
5 77 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 77 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 77 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 77 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 77 ▶use MCP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 77 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 77 ▶use Net::Async::WebSearch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 77 ▶use Net::Async::WebSearch::Provider::DuckDuckGo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 77 ▶use Net::Async::WebSearch::Provider::Brave;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 77 ▶use Net::Async::WebSearch::Provider::Serper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 77 ▶use Net::Async::WebSearch::Provider::Google;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 77 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 77 ▶use Langertha::Raider::ConnectCheck qw( connect_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 77 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 77 our @EXPORT_OK = qw( build_web_tools_server );
21  
22  
23 1 ▶sub _flatten_html {
Subroutine _flatten_html: called CC=2 SCAR=6.9
24 1 my ($html) = @_;
25 1 my $t = HTML::TreeBuilder->new_from_content($html);
26 1 $_->delete for $t->look_down(_tag => qr/^(?:script|style|noscript)$/);
27 1 my $text = $t->as_text;
28 1 $t->delete;
29 1 $text =~ s/[ \t]+/ /g;
30 1 $text =~ s/\n{3,}/\n\n/g;
31 1 return $text;
32 }
33  
34 299 ▶sub build_web_tools_server {
Subroutine build_web_tools_server: called CC=13 SCAR=27.8
Pod documented
35 299 my %args = @_;
36 299 ▶ my $loop = $args{loop} or die "loop is required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $args{"loop"} or die "loop is required\n" 299 0 299
37 299 ▶ my $max_fetch_bytes = $args{max_fetch_bytes} // 2_000_000;
Condition50%1/2
$args{"max_fetch_bytes"} // 2000000
l!l
0299
MC/DC0%0/1
$args{"max_fetch_bytes"} // 2000000
$args{"max_fetch_bytes"}
Truth table50%1/2
A
A= $args{"max_fetch_bytes"}
Aresult
11
00
38  
39 299 ▶ my $http = $args{http} // Net::Async::HTTP->new(
Condition66%2/3
$args{"http"} // "Net::Async::HTTP"->new("user_agent", "raider/0.004", "max_in_flight", 4)
l!l&&r!l&&!r
12980
MC/DC0%0/2
$args{"http"} // "Net::Async::HTTP"->new("user_agent", "raider/0.004", "max_in_flight", 4)
$args{"http"} "Net::Async::HTTP"->new("user_agent", "raider/0.004", "max_in_flight", 4)
Truth table66%2/3
A // B
A= $args{"http"}B= "Net::Async::HTTP"->new("user_agent", "raider/0.004", "max_in_flight", 4)
ABresult
1X1
011
000
40 user_agent => 'raider/0.004',
41 max_in_flight => 4,
42 );
43 299 $loop->add($http);
44  
45 299 ▶ my $ws = $args{web_search} // Net::Async::WebSearch->new(http => $http);
Condition66%2/3
$args{"web_search"} // "Net::Async::WebSearch"->new("http", $http)
l!l&&r!l&&!r
12980
MC/DC0%0/2
$args{"web_search"} // "Net::Async::WebSearch"->new("http", $http)
$args{"web_search"} "Net::Async::WebSearch"->new("http", $http)
Truth table66%2/3
A // B
A= $args{"web_search"}B= "Net::Async::WebSearch"->new("http", $http)
ABresult
1X1
011
000
46 299 $loop->add($ws);
47  
48 $ws->add_provider(Net::Async::WebSearch::Provider::DuckDuckGo->new)
49 299 ▶ unless $args{web_search};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"web_search"} 298 1 299
50  
51 299 ▶ if (!$args{web_search} && $ENV{BRAVE_API_KEY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"web_search"} and $ENV{"BRAVE_API_KEY"}) 0 299 299
Condition66%2/3
not $args{"web_search"} and $ENV{"BRAVE_API_KEY"}
!ll&&!rl&&r
12980
MC/DC0%0/2
not $args{"web_search"} and $ENV{"BRAVE_API_KEY"}
not $args{"web_search"} $ENV{"BRAVE_API_KEY"}
Truth table66%2/3
not(A) and B
A= not $args{"web_search"}B= $ENV{"BRAVE_API_KEY"}
ABresult
0X0
100
111
52 $ws->add_provider(Net::Async::WebSearch::Provider::Brave->new(
53 api_key => $ENV{BRAVE_API_KEY},
54 0 ));
55 }
56 299 ▶ if (!$args{web_search} && $ENV{SERPER_API_KEY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"web_search"} and $ENV{"SERPER_API_KEY"}) 0 299 299
Condition66%2/3
not $args{"web_search"} and $ENV{"SERPER_API_KEY"}
!ll&&!rl&&r
12980
MC/DC0%0/2
not $args{"web_search"} and $ENV{"SERPER_API_KEY"}
not $args{"web_search"} $ENV{"SERPER_API_KEY"}
Truth table66%2/3
not(A) and B
A= not $args{"web_search"}B= $ENV{"SERPER_API_KEY"}
ABresult
0X0
100
111
57 $ws->add_provider(Net::Async::WebSearch::Provider::Serper->new(
58 api_key => $ENV{SERPER_API_KEY},
59 0 ));
60 }
61 299 ▶ if (!$args{web_search} && $ENV{GOOGLE_API_KEY} && $ENV{GOOGLE_CSE_ID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"web_search"} and $ENV{"GOOGLE_API_KEY"} and $ENV{"GOOGLE_CSE_ID"}) 0 299 299
Condition50%3/6
not $args{"web_search"} and $ENV{"GOOGLE_API_KEY"} and $ENV{"GOOGLE_CSE_ID"}
!ll&&!rl&&r
29900
not $args{"web_search"} and $ENV{"GOOGLE_API_KEY"}
!ll&&!rl&&r
12980
MC/DC0%0/3
not $args{"web_search"} and $ENV{"GOOGLE_API_KEY"} and $ENV{"GOOGLE_CSE_ID"}
not $args{"web_search"} $ENV{"GOOGLE_API_KEY"} $ENV{"GOOGLE_CSE_ID"}
Truth table50%2/4
not(A) and B and C
A= not $args{"web_search"}B= $ENV{"GOOGLE_API_KEY"}C= $ENV{"GOOGLE_CSE_ID"}
ABCresult
0XX0
10X0
1100
1111
62 $ws->add_provider(Net::Async::WebSearch::Provider::Google->new(
63 api_key => $ENV{GOOGLE_API_KEY},
64 cx => $ENV{GOOGLE_CSE_ID},
65 0 ));
66 }
67  
68 299 my $server = MCP::Server->new(name => 'raider-web', version => '1.0');
69  
70 $server->tool(
71 name => 'web_search',
72 description => 'Search the web across multiple providers (DuckDuckGo always; Brave/Serper/Google added when their API keys are in the environment). Returns ranked merged results.',
73 input_schema => {
74 type => 'object',
75 properties => {
76 query => { type => 'string', description => 'Search query' },
77 limit => { type => 'integer', description => 'Max results (default 8)' },
78 },
79 required => ['query'],
80 },
81 2 ▶ code => sub {
Subroutine __ANON__: called CC=13 SCAR=27.3
82 2 my ($tool, $in) = @_;
83 2 my $query = $in->{query};
84 2 ▶ my $limit = $in->{limit} // 8;
Condition50%1/2
$$in{"limit"} // 8
l!l
02
MC/DC0%0/1
$$in{"limit"} // 8
$$in{"limit"}
Truth table50%1/2
A
A= $$in{"limit"}
Aresult
11
00
85 # Every search provider is https (see connect_error for why a failed
86 # load must not reach Net::Async::HTTP).
87 2 ▶ if (my $error = connect_error('https:')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = connect_error("https:")) 1 1 2
88 1 return $tool->text_result("Error: $error", 1);
89 }
90 1 my $f = $ws->search(query => $query, limit => $limit);
91 1 my $out = eval {
92 1 $loop->await($f);
93 1 $f->get;
94 };
95 1 ▶ return $tool->text_result("Error: $@", 1) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
96 1 my @lines;
97 1 my $i = 0;
98 1 ▶ for my $r (@{$out->{results} // []}) {
Condition50%1/2
$$out{"results"} // []
l!l
10
MC/DC0%0/1
$$out{"results"} // []
$$out{"results"}
Truth table50%1/2
A
A= $$out{"results"}
Aresult
11
00
99 1 $i++;
100 1 ▶ my $title = $r->title // '(no title)';
Condition50%1/2
$r->title // "(no title)"
l!l
10
MC/DC0%0/1
$r->title // "(no title)"
$r->title
Truth table50%1/2
A
A= $r->title
Aresult
11
00
101 1 ▶ my $url = $r->url // '';
Condition50%1/2
$r->url // ""
l!l
10
MC/DC0%0/1
$r->url // ""
$r->url
Truth table50%1/2
A
A= $r->url
Aresult
11
00
102 1 ▶ my $sn = $r->snippet // '';
Condition50%1/2
$r->snippet // ""
l!l
10
MC/DC0%0/1
$r->snippet // ""
$r->snippet
Truth table50%1/2
A
A= $r->snippet
Aresult
11
00
103 1 $sn =~ s/\s+/ /g;
104 1 push @lines, "[$i] $title";
105 1 push @lines, " $url";
106 1 ▶ push @lines, " $sn" if length $sn;
Branch: 1 branch
BranchTrueFalseTotal
if length $sn 1 0 1
107 }
108 1 ▶ if (my $errs = $out->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errs = $$out{"errors"}) 0 1 1
109 0 for my $e (@$errs) {
110 0 push @lines, "(error from $e->{provider}: $e->{error})";
111 }
112 }
113 1 ▶ return $tool->text_result(@lines ? join("\n", @lines) : "No results.");
Branch: 1 branch
BranchTrueFalseTotal
@lines ? : 1 0 1
114 },
115 299 );
116  
117 $server->tool(
118 name => 'web_fetch',
119 description => 'Fetch a URL and return its body. HTML is flattened to readable text. Binary/large bodies are truncated.',
120 input_schema => {
121 type => 'object',
122 properties => {
123 url => { type => 'string', description => 'URL to fetch' },
124 as_html => { type => 'boolean', description => 'Keep raw HTML instead of flattening to text (default false)' },
125 },
126 required => ['url'],
127 },
128 5 ▶ code => sub {
Subroutine __ANON__: called CC=9 SCAR=23.0
129 5 my ($tool, $in) = @_;
130 5 my $url = $in->{url};
131 # A module Net::Async::HTTP fails to load while connecting leaves the
132 # host's connection slot taken: every later fetch from it would hang
133 # (karr k107).
134 5 ▶ if (my $error = connect_error($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = connect_error($url)) 2 3 5
135 2 return $tool->text_result("Error: $error", 1);
136 }
137 3 my $req = GET($url);
138 3 $req->header('User-Agent' => 'raider/0.001');
139 # The same holds for every hop of a redirect: a redirect to https
140 # fails here, before Net::Async::HTTP connects to it.
141 my $f = $http->do_request(
142 request => $req,
143 2 ▶ on_redirect => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
144 2 my ( undef, $location ) = @_;
145 2 my $error = connect_error($location);
146 2 ▶ die $error."\n" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 0 2
147 },
148 3 );
149 3 my $resp = eval {
150 3 $loop->await($f);
151 3 $f->get;
152 };
153 3 ▶ return $tool->text_result("Error: $@", 1) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 1 3
154 1 ▶ unless ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
155 0 return $tool->text_result(
156 sprintf("HTTP %s for %s", $resp->status_line, $url), 1);
157 }
158 1 ▶ my $body = $resp->decoded_content // '';
Condition50%1/2
$resp->decoded_content // ""
l!l
10
MC/DC0%0/1
$resp->decoded_content // ""
$resp->decoded_content
Truth table50%1/2
A
A= $resp->decoded_content
Aresult
11
00
159 1 ▶ if (length($body) > $max_fetch_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body > $max_fetch_bytes) 0 1 1
160 0 $body = substr($body, 0, $max_fetch_bytes) . "\n[truncated]\n";
161 }
162 1 ▶ my $ctype = $resp->header('Content-Type') // '';
Condition50%1/2
$resp->header("Content-Type") // ""
l!l
10
MC/DC0%0/1
$resp->header("Content-Type") // ""
$resp->header("Content-Type")
Truth table50%1/2
A
A= $resp->header("Content-Type")
Aresult
11
00
163 1 ▶ if (!$in->{as_html} && $ctype =~ m{text/html}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $$in{"as_html"} and $ctype =~ m[text/html]i) 1 0 1
Condition33%1/3
not $$in{"as_html"} and $ctype =~ m[text/html]i
!ll&&!rl&&r
001
MC/DC0%0/2
not $$in{"as_html"} and $ctype =~ m[text/html]i
not $$in{"as_html"} $ctype =~ m[text/html]i
Truth table33%1/3
not(A) and B
A= not $$in{"as_html"}B= $ctype =~ m[text/html]i
ABresult
0X0
100
111
164 1 $body = _flatten_html($body);
165 }
166 1 my $header = sprintf("URL: %s\nStatus: %s\nContent-Type: %s\n\n",
167 $url, $resp->status_line, $ctype);
168 1 return $tool->text_result($header . $body);
169 },
170 299 );
171  
172 299 return $server;
173 }
174  
175 77 1;
176  
177 __END__
178  
179 =pod
180  
181 =encoding UTF-8
182  
183 =head1 NAME
184  
185 Langertha::Raider::WebTools - MCP::Server factory with web search and fetch tools (Net::Async::WebSearch + Net::Async::HTTP)
186  
187 =head1 VERSION
188  
189 version 0.503
190  
191 =head1 DESCRIPTION
192  
193 The web tools of L<raider>: an L<MCP::Server> with C<web_search> and
194 C<web_fetch>, run on the caller's L<IO::Async::Loop>.
195  
196 =head2 build_web_tools_server
197  
198 my $server = Langertha::Raider::WebTools::build_web_tools_server(
199 loop => $loop, # required
200 max_fetch_bytes => 2_000_000,
201 );
202  
203 Returns an L<MCP::Server> instance with two tools:
204  
205 =over
206  
207 =item * C<web_search(query, [limit])> -- multi-provider search via
208 L<Net::Async::WebSearch>. DuckDuckGo is always enabled (keyless). Brave,
209 Serper and Google are auto-added when C<BRAVE_API_KEY>, C<SERPER_API_KEY>,
210 or both C<GOOGLE_API_KEY> + C<GOOGLE_CSE_ID> are set in the environment.
211  
212 =item * C<web_fetch(url, [as_html])> -- fetch a URL via L<Net::Async::HTTP>.
213 Returns the status, the content type and the body, cut at
214 C<max_fetch_bytes> (default 2_000_000). HTML is flattened to readable text
215 unless C<as_html> is true.
216  
217 C<limit> of C<web_search> defaults to 8.
218  
219 =back
220  
221 Both tools return a tool error, and send nothing, when a module
222 L<Net::Async::HTTP> needs to connect does not load
223 (L<IO::Async::Internals::Connector>; for https L<IO::Async::SSL>, which
224 needs L<IO::Socket::SSL>, L<Net::SSLeay> and the system libssl). The error
225 names the module. For C<web_fetch> that holds for every redirect it follows
226 as well: an http URL that redirects to https is the same tool error.
227  
228 =head1 ENVIRONMENT
229  
230 =head2 BRAVE_API_KEY
231  
232 Adds Brave to C<web_search>.
233  
234 =head2 SERPER_API_KEY
235  
236 Adds Serper to C<web_search>.
237  
238 =head2 GOOGLE_API_KEY
239  
240 With L</GOOGLE_CSE_ID>, adds Google Custom Search to C<web_search>.
241  
242 =head2 GOOGLE_CSE_ID
243  
244 The Custom Search engine id for L</GOOGLE_API_KEY>.
245  
246 =head1 SEE ALSO
247  
248 =over
249  
250 =item * L<MCP::Server>
251  
252 =item * L<Net::Async::WebSearch>
253  
254 =item * L<Net::Async::HTTP>
255  
256 =back
257  
258 =head1 SUPPORT
259  
260 =head2 Issues
261  
262 Please report bugs and feature requests on GitHub at
263 L<https://github.com/Getty/langertha-raider/issues>.
264  
265 =head2 IRC
266  
267 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
268  
269 =head1 CONTRIBUTING
270  
271 Contributions are welcome! Please fork the repository and submit a pull request.
272  
273 =head1 AUTHOR
274  
275 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is copyright (c) 2026 by Torsten Raudssus.
280  
281 This is free software; you can redistribute it and/or modify it under
282 the same terms as the Perl 5 programming language system itself.
283  
284 =cut
« blib/lib/Langertha/Raider/PerlTools.pm ↑ Summary blib/lib/Langertha/Raider/Hall.pm »