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

filter: Statementstmt 61.6% 343 / 556 Branchbran 45.5% 184 / 404 Conditioncond 36.9% 99 / 268 MC/DCmcdc 20.1% 29 / 144 Subroutinesub 70.1% 40 / 57 Podpod 100% 18 / 18 totaltotal 49.2% 713 / 1447 CC 386 SCAR 53.1
CC
386
Cov
51%
mcp_getoptions
606.5
mcp_getoptions
606.5
mcp_log_request
291.1
CRAP 201.4
« blib/lib/AnyEvent/MCP/IMAP/Providers.pm ↑ Summary bin/anyevent-mcp-form-elicitation »
1 =head1 NAME
2  
3 AnyEvent::MCP::Server::Simple - simple function interface for MCP servers
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::MCP::Server::Simple;
8  
9 mcp_config
10 server_name => "example",
11 server_version => 0,
12 instructions => <<'EOF';
13 Use echo to return the supplied text.
14 EOF
15  
16 mcp_tool echo =>
17 "Return the supplied text.",
18 {
19 type => "object",
20 properties => { text => { type => "string" } },
21 required => ["text"],
22 },
23 sub {
24 my ($req, $args) = @_;
25  
26 $req->reply ({
27 content => [{ type => "text", text => $args->{text} }],
28 });
29 };
30  
31 mcp_start;
32  
33 =head1 DESCRIPTION
34  
35 For most MCP servers, registering a few tools and starting a transport is
36 enough. This module does just that: call C<mcp_tool> to register callbacks,
37 optionally configure the server with C<mcp_config>, then start it with
38 C<mcp_start>, C<mcp_stdio> or C<mcp_http>. Registration alone does not start
39 a server or an event loop.
40  
41 Human-readable descriptions and instructions are stripped of surrounding
42 whitespace and common indentation when registered, so indented here-documents
43 can be used naturally.
44  
45 C<mcp_start>, C<mcp_stdio> and C<mcp_http> are alternative blocking entry
46 points for a self-contained server. They run AnyEvent until shutdown; you
47 do not start another event loop. If your application already owns the server
48 lifetime or needs several transports, use the object interfaces instead.
49  
50 Simple uses L<AnyEvent::MCP::Server> and a default Toolset internally; it is
51 not a second server implementation. Applications needing custom dispatch,
52 such as a multibroker, can use the Server API directly.
53  
54 Start with L</TUTORIAL> for a complete server and a first client call.
55 For running the bundled servers without writing Perl, see
56 L<AnyEvent::MCP::Intro>.
57  
58 =head1 TUTORIAL
59  
60 This tutorial builds a small server, calls it, then adds options, permission
61 checks and asynchronous work. You do not need to implement JSON-RPC,
62 authentication or a transport. Simple supplies those parts; your program
63 declares tools and handles their arguments.
64  
65 For installation, connecting AI clients and configuring the bundled servers,
66 see L<AnyEvent::MCP::Intro>. The function reference follows this tutorial.
67  
68 =head2 A complete first server
69  
70 Save the following as C<echo-mcp>:
71  
72 #!/opt/bin/perl
73  
74 use common::sense;
75 use AnyEvent::MCP::Server::Simple;
76  
77 my $options = mcp_getoptions {
78 name => "echo-demo",
79 usage => "Usage: $0 [OPTION]...\n",
80 description => "Return text supplied to the echo tool.",
81 };
82  
83 @ARGV == 0
84 or die "Unexpected arguments\n";
85  
86 mcp_config
87 server_name => "echo-demo",
88 server_version => 1,
89 instructions => "Use echo to return the supplied text.";
90  
91 mcp_tool echo =>
92 "Return the supplied text.",
93 {
94 type => "object",
95 properties => {
96 text => {
97 type => "string",
98 maxLength => 4096,
99 },
100 },
101 required => ["text"],
102 },
103 sub {
104 my ($req, $args) = @_;
105  
106 my $text = $args->{text};
107 defined $text and !ref $text and length $text <= 4096
108 or die "text must be a string of at most 4096 characters\n";
109  
110 $req->reply ({
111 content => [{ type => "text", text => $text }],
112 });
113 };
114  
115 mcp_start %$options;
116  
117 Use your installed Perl's explicit path in the shebang, or invoke the file
118 with C<perl> as in the commands below.
119  
120 C<mcp_getoptions> parses the common server options and returns startup
121 settings. C<mcp_config> declares protocol metadata and instructions.
122 C<mcp_tool> registers a description, input schema and callback.
123 C<mcp_start> constructs the transport and runs AnyEvent until shutdown.
124 Register everything before starting: code after C<mcp_start> runs only
125 when the server stops.
126  
127 The input schema tells the client what to supply. The server does B<not>
128 validate arguments against JSON Schema, so the callback checks the value it
129 actually uses. A missing text, array or object fails as a tool error.
130 The example allows an empty string.
131  
132 C<$req> is an L<AnyEvent::MCP::Server::Request>; C<$args> is its argument
133 hash reference. Replying finishes the request. Merely returning from the
134 callback does not. You supply the MCP result, and the server supplies the
135 JSON-RPC envelope.
136  
137 =head2 Start and call the server
138  
139 Inspect the common options with:
140  
141 perl echo-mcp --help
142  
143 For a client launching a local subprocess, configure:
144  
145 perl /absolute/path/to/echo-mcp --stdio
146  
147 The client must launch the process itself. Stdin and stdout carry MCP,
148 so starting the program in a terminal and typing text at it is not a test
149 of the echo tool. Print diagnostics to stderr, not stdout.
150  
151 You can make a first call without an AI client. Save this as C<try-echo>
152 in the same directory as C<echo-mcp>:
153  
154 use common::sense;
155 use AnyEvent;
156 use AnyEvent::MCP::Client::JSONL;
157  
158 my $client = AnyEvent::MCP::Client::JSONL->new (
159 command => [$^X, "./echo-mcp", "--stdio"],
160 );
161  
162 my $cv = AE::cv;
163  
164 $client->call (echo => { text => "hello" }, sub {
165 $cv->send (@_);
166 });
167  
168 my ($success, $result) = $cv->recv;
169 $success
170 or die "$result->{message}\n";
171 !$result->{isError}
172 or die "$result->{content}[0]{text}\n";
173  
174 print $result->{content}[0]{text}, "\n";
175  
176 Run C<perl try-echo>. It prints:
177  
178 hello
179  
180 The JSONL client starts the server, connects and negotiates the protocol on
181 first use. C<$^X> chooses the same Perl interpreter as the test client.
182 The condition variable keeps the event loop running until the reply arrives.
183 Keep C<$client> alive while its operations are outstanding.
184  
185 There are two success checks. C<$success> means the JSON-RPC operation
186 completed successfully; a tool can still return C<isError> in that result.
187 This client expects our echo tool's text result. A general client must handle
188 other MCP content representations too.
189  
190 To use HTTP, start the server in one terminal:
191  
192 perl echo-mcp --bind :31742
193  
194 and retrieve its URL in another:
195  
196 perl echo-mcp --bind :31742 --print-url
197  
198 Configure that URL in your MCP client and ask it to call echo with text
199 C<hello>. Both commands use instance C<echo-demo>, unless you override it
200 with C<--name>. Repeat the same listener overrides when printing the URL.
201 C<--print-url> prints the address and exits; it does not start a server.
202  
203 For the small Perl client above, replace the JSONL import and constructor
204 with:
205  
206 use AnyEvent::MCP::Client::HTTP;
207  
208 my $client = AnyEvent::MCP::Client::HTTP->new (
209 url => $ARGV[0],
210 );
211  
212 Pass the printed URL as its first command-line argument.
213 The rest of the call and result handling stays the same.
214  
215 C<mcp_start> supplies named HTTP settings and a token derived from the
216 private global secret. Stdio needs no HTTP configuration. C<name> selects
217 local settings and credentials; C<server_name> identifies the advertised
218 MCP implementation. The two need not be the same.
219 See L<AnyEvent::MCP::Intro/CONFIGURING YOUR SETUP>.
220  
221 =head2 Add an application option
222  
223 Suppose you want the server user to choose the maximum text length.
224 Replace the original C<mcp_getoptions> call with:
225  
226 my $max_length = 4096;
227  
228 my $options = mcp_getoptions (
229 {
230 name => "echo-demo",
231 usage => "Usage: $0 [OPTION]...\n",
232 description => "Return text supplied to the echo tool.",
233 options => [
234 ["--max-length N", "Maximum text length in characters"],
235 ],
236 },
237 "max-length=i" => \$max_length,
238 );
239  
240 $max_length > 0
241 or die "--max-length must be positive\n";
242  
243 Use C<$max_length> in both the schema's C<maxLength> and the callback's
244 length check. The server user can then start:
245  
246 perl echo-mcp --stdio --max-length 1000
247  
248 The option specification is passed to L<Getopt::Long>. The C<options>
249 entries supply human-readable help; they do not register options.
250 The later tool examples use the original 4096-character limit; use your
251 chosen limit in them too if you keep this option.
252 Common options such as C<--stdio>, C<--bind> and C<--help> remain available.
253  
254 If your server also takes positional arguments, validate the remaining
255 C<@ARGV> after C<mcp_getoptions>. It handles C<--help> and C<--print-url>
256 before that validation, so the user can retrieve an HTTP URL without
257 supplying an application workdir or starting the application.
258  
259 Settings affecting URL discovery, such as C<name> and C<path>, should be
260 available to C<mcp_getoptions> through its leading settings hash or an
261 earlier C<mcp_config> call. A setting supplied only after the parser exits
262 for C<--print-url> cannot affect that printed URL.
263  
264 =head2 Choose a result representation
265  
266 Our echo callback returns one text content item. Use C<reply> when the
267 representation matters: text, other content types, a larger result or a
268 particular MCP result shape.
269  
270 For an ordinary result object, you can replace the reply with:
271  
272 $req->result ({ text => $text, length => length $text });
273  
274 C<result> returns that hash as C<structuredContent> and also serializes it
275 as JSON in one text content item. Clients consuming only text can still
276 see the data, but it is deliberately duplicated. Wrap arrays or scalars in
277 an object, or use C<reply> to choose a different representation.
278  
279 To report a tool failure explicitly:
280  
281 $req->error ("The requested operation cannot be completed");
282  
283 A synchronous exception from the handler also becomes a tool-error result.
284 Malformed protocol requests and an unknown tool name are handled by the
285 server. Your callback does not construct a JSON-RPC response itself.
286  
287 Reply once. Code that calls C<reply>, C<result> or C<error> and then calls
288 another reply method is a programming error. Keep a final reply separate
289 from the work leading to it so the completion point is easy to see.
290  
291 =head2 Add a permission check before changing state
292  
293 Authentication alone does not decide which tool operations a user allows.
294 For an example with a side effect but no external worker, add a tool that
295 stores one note in the server's memory.
296  
297 Add this before registering the tools:
298  
299 use AnyEvent::MCP::Server::Permissions;
300  
301 my $note;
302  
303 mcp_permissions +AnyEvent::MCP::Server::Permissions->new (
304 name => "echo-notes",
305 actions => {
306 remember => "Replace the server's in-memory note",
307 },
308 );
309  
310 Then register this tool before C<mcp_start>:
311  
312 mcp_tool remember =>
313 "Replace the server's in-memory note with the supplied text.",
314 {
315 type => "object",
316 properties => {
317 text => { type => "string", maxLength => 4096 },
318 },
319 required => ["text"],
320 },
321 sub {
322 my ($req, $args) = @_;
323  
324 my $text = $args->{text};
325 defined $text and !ref $text and length $text <= 4096
326 or die "text must be a string of at most 4096 characters\n";
327  
328 $req->require_permission ({
329 tool => $req->{tool},
330 action => "remember",
331 sandbox_grants => [],
332 reason => "replace the server's in-memory note",
333 });
334  
335 $note = $text;
336  
337 $req->result ({ remembered => length $note });
338 };
339  
340 Restart the server and call remember through your client. The default
341 permission helper asks on the server machine. Allow once permits this
342 invocation. Allow always saves a rule for action C<remember>; subsequent
343 matching calls do not ask again. The note itself lasts only for this
344 server process and is shared by its clients.
345  
346 The permission engine's name C<echo-notes> selects its default policy file:
347  
348 ~/.config/anyevent-mcp/rules/echo-notes.ini
349  
350 The actual directory follows C<XDG_CONFIG_HOME> when set.
351 This is the shared engine's default convention, different from mcp-fs's
352 C<rules/fs-NAME.ini>. Manage it using C<anyevent-mcp manage-rules-file>
353 with the actual pathname, or supply C<file> to the engine explicitly.
354  
355 For example, a manually written allow rule is:
356  
357 [rule]
358 decision=allow
359 action=remember
360 sandbox-grants=
361  
362 The empty grant list is intentional. This tool updates an in-memory value;
363 it does not launch a sandboxed worker. C<action> is the operation class
364 to authorize, while C<sandbox_grants> describes worker authority.
365  
366 The permission engine checks policy; it does not restrict filesystem or
367 network access by itself. If you add file or command tools, define the
368 necessary grants and enforce the returned set through your worker or
369 sandbox. Do not copy C<sandbox_grants =E<gt> []> into a file-writing
370 handler and assume it confines access.
371  
372 Here the permission name is deliberately fixed and independent of the
373 HTTP instance name. If you want each C<--name> to have separate policy,
374 pass C<$options{name}> as the permission engine's name instead.
375  
376 =head2 Why checks must precede side effects
377  
378 When permission is missing, C<require_permission> throws a special
379 exception. Server catches it, asks, and may invoke the handler again.
380 The assignment to C<$note> therefore comes B<after> the check.
381  
382 With a local prompt, the server retries internally. Modern MCP form asking
383 returns C<input_required>; the client repeats the call with its answer.
384 Earlier one-time grants survive permission retries within this invocation,
385 but do not authorize the next call. Saved rules are rechecked.
386  
387 Do not catch that exception as an ordinary failure around the permission
388 check. It must reach Server's synchronous dispatch boundary.
389 For several checks, complete all of them before modifying state or starting
390 asynchronous work. An C<on_request> hook also runs on retry, so it must not
391 perform a once-only side effect before the handler's checks.
392  
393 To choose client-side form asking for this example:
394  
395 PERL_ANYEVENT_MCP_PERMISSION_ASK=auto perl echo-mcp --stdio
396  
397 C<auto> uses modern form elicitation when the client advertises support,
398 otherwise the local helper. C<mcp> requires that support; C<helper> keeps
399 the prompt local. The tiny try-echo client advertises no form support,
400 so auto uses the helper there. To handle forms in a Perl client, see
401 L<AnyEvent::MCP::Client/on_request> and
402 L<AnyEvent::MCP::Permissions::Ask/elicitation>.
403  
404 For grant definitions, subject scopes and multi-path operations, see
405 L<AnyEvent::MCP::Server::Permissions> and
406 L<AnyEvent::MCP::Server::Toolset/Permission checks and retries>.
407  
408 =head2 Let an asynchronous operation finish later
409  
410 A tool callback runs in the AnyEvent event loop. Blocking it with C<sleep>
411 or a synchronous long-running command also blocks other requests.
412  
413 Add C<use AnyEvent;> and register the following before C<mcp_start>:
414  
415 mcp_tool delayed_echo =>
416 "Return the supplied text after a delay of up to 60 seconds.",
417 {
418 type => "object",
419 properties => {
420 text => { type => "string", maxLength => 4096 },
421 seconds => {
422 type => "integer",
423 minimum => 0,
424 maximum => 60,
425 },
426 },
427 required => ["text", "seconds"],
428 },
429 sub {
430 my ($req, $args) = @_;
431  
432 my $text = $args->{text};
433 defined $text and !ref $text and length $text <= 4096
434 or die "text must be a string of at most 4096 characters\n";
435  
436 my $seconds = $args->{seconds};
437 defined $seconds and !ref $seconds
438 and $seconds =~ /\A[0-9]+\z/ and $seconds <= 60
439 or die "seconds must be an integer from 0 to 60\n";
440  
441 $req->progress (0, 1, "Waiting");
442  
443 my $timer = AnyEvent->timer (after => $seconds, cb => sub {
444 $req->{finished}
445 and return;
446  
447 $req->progress (1, 1, "Done");
448 $req->reply ({
449 content => [{ type => "text", text => $text }],
450 });
451 });
452  
453 $req->{on_cancel} = sub {
454 undef $timer;
455 };
456 };
457  
458 The tool is advertised as C<delayed-echo>, because registration converts
459 underscores to hyphens. Ask the client to call it with text C<hello> and
460 seconds C<2>. The callback returns immediately; the timer supplies the reply
461 later. Other requests can run while it waits.
462  
463 Keep watcher objects alive while they are needed. Here the cancellation
464 callback retains C<$timer>, and the timer's callback retains C<$req> and
465 C<$text>. Cancellation releases the timer. Replying finishes the request
466 and removes its cancellation callback, releasing the watcher as well.
467 No separate job registry is needed for this example.
468  
469 Check C<finished> in late completion callbacks before replying.
470 Cancellation sets both C<finished> and C<cancelled> and sends no reply.
471 For subprocesses or network work, the cancellation callback should stop
472 that operation and release its resources instead of merely abandoning
473 the eventual reply.
474  
475 Server catches exceptions during synchronous dispatch, not exceptions
476 from a later timer or I/O callback. If an asynchronous operation can fail,
477 handle that failure in its completion callback and report it with
478 C<error> or C<reply>, provided the request is still active.
479  
480 C<progress> only sends when the caller supplied a progress token.
481 An outstanding tool request with a progress token emits an automatic
482 elapsed-time heartbeat every 30 seconds. Delivery is best effort:
483 stdio sends notifications directly, while Streamable HTTP sends them
484 over the request's SSE response stream. Clients may ignore progress.
485  
486 =head2 When to use the object interfaces
487  
488 Simple owns one default Toolset and the blocking server lifetime.
489 That is useful for a small standalone program. If you already run an
490 event loop, want multiple transports or tool registries, or need custom
491 protocol dispatch, construct the objects directly.
492  
493 L<AnyEvent::MCP::Server::Toolset> registers reusable tools.
494 L<AnyEvent::MCP::Server> handles the protocol.
495 L<AnyEvent::MCP::Server::HTTP> and L<AnyEvent::MCP::Server::JSONL> manage
496 connections. L<AnyEvent::MCP::Server::Request> describes request fields,
497 replies, progress and cancellation. The same tool callback model applies
498 at both levels.
499  
500 =head1 FUNCTIONS
501  
502 =over
503  
504 =item mcp_config %config
505  
506 Adds server configuration. Repeated calls extend or replace previously declared
507 values. C<instructions> is treated as human-readable text and normalized.
508  
509 Server and transport options are passed to the underlying objects.
510 Common options include:
511  
512 =over
513  
514 =item name => $instance
515  
516 Local instance name used to select configuration and credentials. Independent
517 of the name reported to MCP clients.
518  
519 =item server_name => $name
520  
521 Advertised MCP server implementation name. Defaults to C<AnyEvent::MCP>.
522  
523 =item server_version => $version
524  
525 Advertised application version, not the MCP protocol revision. Accepts
526 an ordinary Perl version number; Simple stringifies it for MCP.
527 Defaults to the AnyEvent::MCP library version.
528  
529 =item server_info => { name => $name, version => $version }
530  
531 Raw MCP server identity, passed through unchanged to the underlying
532 server. Cannot be combined with C<server_name> or C<server_version>.
533  
534 =item instructions => $text
535  
536 Human-readable guidance for the client, normalized when configured.
537  
538 =item short_instructions => $text
539  
540 Optional condensed guidance for finding this server's tools. Used as the
541 advertised MCP instructions when C<auto_instructions> is enabled.
542  
543 =item auto_instructions => $boolean
544  
545 Enabled by default. Exposes full instructions through the generated
546 C<meta-instructions> tool while advertising the short instructions if set.
547 When short or custom instructions are provided, append a brief hint pointing
548 to the full tool description; otherwise advertise full instructions as before.
549 Disable to advertise full instructions and omit the generated tool.
550  
551 =item custom_instructions => $text
552  
553 Instance-specific purpose prepended to advertised instructions. Can also be
554 set through C<--custom-instructions> or C<custom_instructions> in the program
555 or instance section of the global INI configuration.
556  
557 =item on_request => $callback->($server, $req)
558  
559 Synchronous hook before each dispatch attempt. For example, refresh
560 database state here. This does not replace dispatch or receive a
561 completion callback, and permission retries can invoke it again.
562  
563 =item on_start => $callback->($server)
564  
565 Called after the transport is ready, before entering the event loop.
566 Use it for startup information or installing watchers. Stdout carries
567 protocol messages in stdio mode; print diagnostics to stderr.
568 The callback must not block.
569  
570 =item idle_exit => $seconds
571  
572 Optional positive idle time for spawned stdio servers. With a multibroker
573 advertising C<anyevent-mcp/multibroker> version 1, Simple requests shutdown
574 once no MCP requests have been active for that long. The broker can ignore
575 the request. An application may veto shutdown with C<on_idle_shutdown>;
576 Simple retries after the configured interval if it is declined. HTTP servers,
577 ordinary clients and servers without this option are unaffected.
578  
579 =back
580  
581 See L<AnyEvent::MCP::Server> for further protocol options and events.
582  
583 =item mcp_permissions $object
584  
585 Select the L<AnyEvent::MCP::Server::Permissions> object used by the Simple
586 starters. If never called (and no explicit C<permissions> option is supplied),
587 Simple creates one automatically, named from the program and selected instance.
588 C<mcp_permissions undef> explicitly disables the permission system:
589 tool callbacks calling C<require_permission> then fail instead of prompting.
590 Applications can still pass C<permissions> through C<mcp_config> or startup
591 arguments when needed.
592  
593 The common C<--ask> option overrides the object's prompting mode, and CLI
594 automatic permission rules precede configured rules and rules originally
595 supplied with the object. Those rules are applied when starting the server,
596 not when C<mcp_permissions> is called. No permission object is created when
597 it is explicitly disabled.
598  
599 =item mcp_tool $name, $description, $input_schema, $callback->($req, $args)
600  
601 Registers a tool named C<$name> with C<$description> and the JSON Schema
602 hash reference C<$input_schema>. C<$description> is normalized like
603 C<instructions>.
604  
605 C<$callback> receives an L<AnyEvent::MCP::Server::Request> object C<$req>
606 and its C<< $req->{args} >> hash reference as C<$args>, for convenience.
607  
608 Tools complete the request with C<< $req->reply ($result) >>, or use
609 C<< $req->result ($data) >> and C<< $req->error ($message) >> for convenience.
610 Request context is available directly in
611 the hash, for example C<< $req->{tool} >> and C<< $req->{server} >>.
612  
613 =item $server_options = mcp_getoptions ({ %settings }, @getopt_options)
614  
615 Parse the switches documented in L</COMMON COMMAND-LINE OPTIONS> alongside
616 application-specific L<Getopt::Long> option specifications:
617  
618 my $verbose;
619 my $options = mcp_getoptions (
620 {
621 usage => "Usage: $0 [OPTION]... WORKDIR\n",
622 description => "Provide example tools through MCP.",
623 option_heading => "Example options:",
624 options => [
625 ["--verbose", "Show additional diagnostics"],
626 ],
627 examples => " $0 --stdio ./project",
628 path => "/",
629 },
630 "verbose!" => \$verbose,
631 );
632 @ARGV == 1 or die "workdir required\n";
633 mcp_start %$options;
634  
635 The optional leading hash accepts these help settings:
636  
637 =over
638  
639 =item usage => $text
640  
641 Usage synopsis, including its trailing newline. A default synopsis is
642 generated when omitted.
643  
644 =item description => $text
645  
646 Introductory help paragraph.
647  
648 =item options => [[$label, $description]...]
649  
650 Application option labels and descriptions. A plain string among the
651 pairs starts a new section; a string at the beginning replaces the
652 default section heading.
653  
654 =item option_heading => $text
655  
656 Initial section heading, defaulting to C<Application options:>.
657  
658 =item examples => $text
659  
660 Preformatted examples.
661  
662 =item without => [OPTION...]
663  
664 Exclude selected common options by their long names, for example
665 C<< without => ["ask"] >>. The parser rejects excluded switches, and
666 C<--help> omits them. All common options are enabled by default.
667  
668 =item print_url_extra => $callback
669  
670 Append application-specific URLs to C<--print-url> output. Called once with
671 the resolved HTTP settings after the normal MCP URLs have been printed.
672  
673 =item default_port => $port
674  
675 Choose an application-specific fallback port for C<--print-url>, without
676 replacing the bind address or overriding ports explicitly specified by
677 C<--bind> or configuration. The application must also pass it to
678 C<http_settings> when starting its listener.
679  
680 =back
681  
682 See L</COMMON COMMAND-LINE OPTIONS> for the full reference to shared
683 server switches. Application-specific Getopt::Long options are parsed alongside them.
684  
685 These switches also appear in C<--help>; application options are passed
686 through to L<Getopt::Long>. Other leading hash settings (e.g. C<path>,
687 C<auth>, C<url_scheme>) go to C<mcp_start>. Parse positional C<@ARGV>
688 in the application; C<mcp_getoptions> deliberately does not require them
689 for help or URL discovery. Default activity logging includes asynchronous
690 tool failures and formats time, program, instance and PID. Environment
691 C<PERL_ANYEVENT_LOG> can select destinations and verbosity.
692  
693 =item mcp_tool_help ($enabled, @toolsets)
694  
695 If C<$enabled> is true, print the definitions registered in the supplied
696 L<AnyEvent::MCP::Server::Toolset> objects and exit. Otherwise do nothing.
697 Call after registering tools but before starting listeners for executables
698 which manage their own toolsets. Only registered tools appear.
699  
700 =item mcp_log $message
701  
702 Emit operator-facing activity to the configured AnyEvent::Log destination,
703 bypassing its diagnostic verbosity filter. C<--quiet> suppresses this log.
704  
705 =item mcp_log_request ($server, $req, %extra)
706  
707 C<on_request> callback to report tool invocations once across permission
708 retries, and log tool-error replies, including asynchronous ones. It logs
709 selected arguments rather than entire request or reply payloads. This includes
710 JSON-quoted previews of command arguments and VCS filenames (up to four,
711 80 characters each), and Vim C<ex> commands (up to 200 characters).
712 These can contain sensitive text. The Simple starters install it by default;
713 custom servers can use it too.
714 C<%extra> appends application-specific C<key=value> fields in key order.
715 Sanitize sensitive arguments before passing them.
716  
717 =item mcp_start %config
718  
719 Starts and runs the usual server. When C<stdio> is true or
720 C<PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL> is C<stdio>, uses C<mcp_stdio>.
721 Otherwise starts HTTP without printing its URL. HTTP-capable executables
722 provide C<--print-url> to retrieve the URL explicitly.
723  
724 The HTTP defaults are IPv4 loopback, a stable name-derived port in
725 16384..32767, and C</> as the endpoint path.
726 Without explicit C<tokens>, one server-specific token is derived from
727 the private global secret. The selected token authenticates either its URL
728 path or an
729 C<Authorization: Bearer TOKEN> header at the clean URL returned by
730 C<< $server->url (0) >>. The corresponding executable's C<--print-url>
731 option prints the first accepted token in its URL.
732  
733 C<name> selects the C<[instance NAME]> configuration section and credential.
734 It defaults to C<default>.
735 Named settings C<bind>, C<path>, C<url_host>, C<url_scheme>, C<tokens>,
736 C<tls_cert> and C<tls_key> are loaded from the global configuration.
737 Explicit C<tokens> (an ordered,
738 nonempty array reference of URL-safe strings) replaces the configured list
739 and the derived default. Every token works in the URL and bearer header.
740 An explicit C<bind> overrides the configured bind address. Other arguments
741 override these settings.
742  
743 The global secret is generated automatically when needed; see
744 L<AnyEvent::MCP/GLOBAL CONFIGURATION>. No configuration is loaded for stdio.
745 Unless explicitly disabled using C<mcp_permissions undef>, Simple constructs
746 a default permission engine. C<--ask> and automatic permission rules configure
747 it along with any application-supplied permission object.
748 HTTP startup neither prints to stdout nor changes its buffering.
749 C<mcp_start> does not parse C<@ARGV>; applications parse their own options.
750 The bundled executables use C<mcp_getoptions>, which handles shared
751 options and URL discovery before starting the event loop.
752  
753 =item mcp_connect %config
754  
755 Run a reverse-connecting JSONL server using C<connect =E<gt>
756 "http[s]://host:port/token/alias">. HTTPS verifies the server certificate
757 and hostname. It sends the authenticated extension hello only after the
758 transport is established, then serves MCP over the socket. Refused TCP
759 connections retry every second for at most sixty attempts, and unexpected
760 established disconnects start a new retry period. Other TCP setup and TLS
761 handshake errors are reported and stop the server.
762 Calls are cancelled rather than replayed.
763 This connection has no spawned-child lifecycle authority.
764  
765 =item mcp_stdio %config
766  
767 Creates and runs a server over stdin/stdout. Arguments override the collected
768 configuration. Stdout buffering is disabled. On stdin EOF, pending replies
769 finish and stdout drains before
770 this function returns. Stream errors close the connection and cancel pending
771 requests.
772  
773 =item mcp_http %config
774  
775 Creates and runs an HTTP server. For example:
776  
777 mcp_http
778 host => "127.0.0.1",
779 port => 31740,
780 path => "/mcp/",
781 tokens => ["your-long-random-secret"];
782  
783 Arguments override the collected configuration. Unlike C<mcp_start>, this
784 function does not derive or supply authentication. B<Always> pass C<tokens>,
785 or explicitly pass C<auth =E<gt> "none"> when a trusted embedding layer
786 enforces access. Omitting both is an error, not an unauthenticated default.
787 It does not print a URL; use
788 C<on_start> for application-specific startup output. Neither function
789 implements OAuth or Basic authentication. See
790 L<AnyEvent::MCP::Server::HTTP/AUTHENTICATION> for details.
791  
792 All three starters return when the server is closed or on C<SIGINT> or
793 C<SIGTERM>. Signal shutdown closes connections and cancels outstanding
794 requests. The signal watchers belong to the blocking call.
795  
796 =back
797  
798 =head1 COMMON COMMAND-LINE OPTIONS
799  
800 C<mcp_getoptions> parses these options for executables using
801 L<AnyEvent::MCP::Server::Simple>. The executable's own OPTIONS section
802 documents any additional switches or differences in shared-option behavior.
803 An application can exclude specific common switches via C<without>.
804  
805 =over
806  
807 =item --name NAME
808  
809 Select the instance (default C<default>). NAME starts with an ASCII letter,
810 digit or underscore; subsequent characters may also contain C<.>, C<@>,
811 C<+> and C<->.
812 Selects C<[instance NAME]> configuration, derived HTTP port and credentials;
813 does not change the advertised MCP implementation name.
814 Example: C<--name project>.
815  
816 =item --bind ADDRESS[:PORT]
817  
818 Override HTTP listening address and port. Defaults to C<127.0.0.1> and a
819 stable instance-derived port in 16384..32767. C<:31740> uses the loopback
820 address and port 31740; C<127.0.0.1:31740> specifies both.
821 Port zero requests an ephemeral port and is incompatible with
822 C<--print-url>. Ignored for stdio-only startup.
823  
824 =item --tls-cert FILE
825  
826 Enable opportunistic HTTPS using a PEM certificate chain. Without
827 C<--tls-key>, FILE must also contain the private key. The same listener
828 continues to accept plain HTTP.
829  
830 =item --tls-key FILE
831  
832 Private-key PEM file for C<--tls-cert>. If supplied alone, FILE must
833 contain both certificate chain and private key. Requires C<Net::SSLeay>.
834  
835 =item --token TOKEN
836  
837 Accept TOKEN for HTTP authentication. Repeat to accept multiple tokens.
838 Explicit tokens replace configured or master-secret-derived defaults.
839 Tokens work both as credential-bearing URL components and with the
840 C<Authorization: Bearer TOKEN> header.
841 Example: C<--token first --token second>.
842  
843 =item -s, --set KEY=VALUE
844  
845 Override one trusted configuration entry for the selected instance in this
846 process. Applied after C<[program NAME]> and C<[instance NAME]> defaults,
847 without changing the configuration file. Repeat to override multiple entries;
848 the last assignment to a key wins. Empty values suppress inherited settings,
849 and additional equals signs belong to the value. The supported keys depend
850 on the server (for example, C<executable.perl> and C<prepend-path> in
851 L<mcp-fs>). Example: C<-s executable.perl=/opt/perl/bin/perl>.
852  
853 =item --stdio
854  
855 Serve MCP JSONL on stdin/stdout rather than the default HTTP listener.
856 Applications with auxiliary listeners can keep those active. Cannot
857 combine explicitly with C<--connect>.
858  
859 =item --connect URL
860  
861 Reverse-connect to an upstream multibroker. For example:
862 C<http://host:port/TOKEN/alias> or C<https://host:port/TOKEN/alias>.
863 HTTPS requires successful certificate and hostname verification; it
864 never falls back to plaintext. Replaces the default MCP listener and
865 overrides implicit stdio selection. Cannot combine with explicit
866 C<--stdio>.
867  
868 =item --custom-instructions TEXT
869  
870 Override configured instance-purpose guidance describing when MCP
871 clients should use the server.
872 Example: C<--custom-instructions 'Use for build logs'>.
873  
874 =item --auto-allow TOOL[,ACTION[,CAPABILITY]]
875  
876 Add a process-local permission allow rule, repeatable. TOOL identifies
877 the permission-checked tool; ACTION and CAPABILITY narrow the match.
878 Missing fields and C<*> match anything.
879 Example: C<--auto-allow '*,read,workdir-read'>.
880 Rules run in command-line order before saved rules; they cannot grant
881 permissions to tools that do not check permissions.
882 See L<AnyEvent::MCP::Server::Permissions/auto_rules>.
883  
884 =item --auto-deny TOOL[,ACTION[,CAPABILITY]]
885  
886 Add a repeatable process-local deny rule with the same matching syntax
887 and evaluation order as C<--auto-allow>.
888 Example: C<--auto-deny '*,exec'>.
889  
890 =item --ask MODE
891  
892 Choose how permissions or other human input are requested: C<helper>,
893 C<tty>, C<yad>, C<mcp>, or C<auto>. C<none> immediately declines requests
894 that would otherwise require prompting; existing allow and deny rules
895 still apply. Multibroker replies to downstream forms with a standard decline.
896 The default depends on the application and its multibroker environment.
897  
898 =item --quiet
899  
900 Suppress routine tool activity and tool-failure logging. Warnings and
901 ordinary diagnostics remain; C<PERL_ANYEVENT_LOG> determines diagnostic
902 levels and destinations.
903  
904 =item --print-url[=USER]
905  
906 Print credential-bearing URLs and exit without starting a listener.
907 With managed users, prints each effective enabled user's URL, or only
908 the exact username USER. Comments identify scope-qualified principals
909 and the selected auxiliary connection user. With configured TLS, HTTPS
910 is printed before HTTP. Explicit tokens retain a single-token output
911 and cannot be combined with a user selector. Requires a predictable,
912 nonzero port and a persistent master secret for derived credentials.
913 Example: C<--name project --print-url=claude>.
914  
915 =item --print-urls
916  
917 Alias for C<--print-url>.
918  
919 =item --tool-help
920  
921 Print currently registered tool names, descriptions, argument types,
922 required fields and defaults; exit without starting the MCP listener.
923 Registered tools can depend on installed utilities and application setup.
924  
925 =item --help
926  
927 Print shared and application-specific command-line help; exit without
928 starting a server. Invalid options instead report usage.
929  
930 =back
931  
932 =cut
933  
934 package AnyEvent::MCP::Server::Simple;
935  
936 75 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
937  
938 75 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
939 75 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
940 75 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
941  
942 75 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
943 75 ▶use AnyEvent::Log ();
Subroutine BEGIN: called CC=1 SCAR=0.0
944 75 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
945 75 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
946 75 ▶use AnyEvent::MCP::Server::Toolset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
947  
948 75 our @ISA = qw(Exporter);
949 75 our @EXPORT = qw(mcp_config mcp_permissions mcp_tool mcp_getoptions mcp_tool_help mcp_log mcp_log_request mcp_start mcp_stdio mcp_http mcp_connect);
950  
951 my %config = (
952 auto_instructions => 1,
953 on_request => \&mcp_log_request,
954 0 ▶ on_connection_error => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
955 0 my ($server, $handle, $error) = @_;
956 0 my $info = $handle->{mcp_peer}{client_info};
957 0 ▶ my $name = ref $info eq "HASH" ? $info->{name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq 'HASH' ? : 0 0 0
958 my @details = (
959 "id=" . sprintf ("%x", refaddr $handle),
960 0 ▶ defined $handle->{mcp_remote} ? "remote=$handle->{mcp_remote}" : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $handle->{'mcp_remote'} ? : 0 0 0
defined $name && !ref($name) ? : 0 0 0
Condition0%0/3
defined $name and not ref $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and not ref $name
defined $name not ref $name
Truth table0%0/3
A and not(B)
A= defined $nameB= not ref $name
ABresult
0X0
100
111
961 defined $name && !ref $name ? "client=" . JSON::XS::encode_json (substr $name, 0, 80) : (),
962 );
963 0 AE::log trace => "MCP connection " . join (" ", @details) . ": $error";
964 },
965 75 );
966 75 my $toolset = AnyEvent::MCP::Server::Toolset->new;
967 75 my $quiet;
968  
969 sub _run;
970  
971 # operational messages go to the configured AnyEvent::Log destination,
972 # independently of the diagnostic verbosity filter.
973 341 ▶sub mcp_log {
Subroutine mcp_log: called CC=2 SCAR=10.9
Pod documented
974 0 ▶ return if $quiet;
Subroutine mcp_log: not called CC=2 SCAR=10.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $quiet 0 341 341
if $quiet 0 0 0
975 0 $AnyEvent::Log::LOG->log (info => @_);
976 }
977  
978 334 ▶sub mcp_log_request {
Subroutine mcp_log_request: called CC=35 SCAR=56.7
Pod documented
979 0 ▶ my ($server, $req, %extra) = @_;
Subroutine mcp_log_request: not called CC=35 SCAR=56.7
Pod documented
980  
981 0 ▶ return unless $req->{method} eq "tools/call";
Branch: 2 branches
BranchTrueFalseTotal
unless $req->{"method"} eq "tools/call" 90 244 334
unless $req->{"method"} eq "tools/call" 0 0 0
982 0 ▶ return if $req->{mcp_logged}++;
Branch: 2 branches
BranchTrueFalseTotal
if $req->{"mcp_logged"}++ 0 244 244
if $req->{"mcp_logged"}++ 0 0 0
983  
984 0 my $args = $req->{args};
985 0 my $vcs = $req->{tool} =~ /\Avcs-(?:diff|status|log)\z/;
986 0 my @details;
987 0 ▶ for (qw(target workdir path cwd), $vcs ? "backend" : ()) {
Branch: 2 branches
BranchTrueFalseTotal
$vcs ? : 0 244 244
$vcs ? : 0 0 0
988 push @details, "$_=" . (substr $args->{$_}, 0, 160)
989 0 ▶ if defined $args->{$_} && !ref $args->{$_};
Branch: 2 branches
BranchTrueFalseTotal
if defined $args->{$_} and not ref $args->{$_} 74 902 976
if defined $args->{$_} and not ref $args->{$_} 0 0 0
Condition50%3/6
defined $args->{$_} and not ref $args->{$_}
!ll&&!rl&&r
901174
defined $args->{$_} and not ref $args->{$_}
!ll&&!rl&&r
000
MC/DC100%2/2
defined $args->{$_} and not ref $args->{$_}
defined $args->{$_} not ref $args->{$_}
MC/DC0%0/2
defined $args->{$_} and not ref $args->{$_}
defined $args->{$_} not ref $args->{$_}
Truth table100%3/3
A and not(B)
A= defined $args->{$_}B= not ref $args->{$_}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $args->{$_}B= not ref $args->{$_}
ABresult
0X0
100
111
990 }
991 0 push @details, substr +(join " ", @{$args->{ranges}}), 0, 180
992 0 ▶ if ref $args->{ranges} eq "ARRAY";
Branch: 2 branches
BranchTrueFalseTotal
if ref $args->{"ranges"} eq "ARRAY" 3 241 244
if ref $args->{"ranges"} eq "ARRAY" 0 0 0
993 0 ▶ for my $key ("argv", $vcs ? "files" : ()) {
Branch: 2 branches
BranchTrueFalseTotal
$vcs ? : 0 244 244
$vcs ? : 0 0 0
994 0 ▶ next unless ref $args->{$key} eq "ARRAY" && @{$args->{$key}};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $args->{$key} eq "ARRAY" and @{$args->{$key};} 242 2 244
unless ref $args->{$key} eq "ARRAY" and @{$args->{$key};} 0 0 0
Condition33%2/6
ref $args->{$key} eq "ARRAY" and @{$args->{$key};}
!ll&&!rl&&r
24202
ref $args->{$key} eq "ARRAY" and @{$args->{$key};}
!ll&&!rl&&r
000
MC/DC50%1/2
ref $args->{$key} eq "ARRAY" and @{$args->{$key};}
ref $args->{$key} eq "ARRAY" @{$args->{$key};}
MC/DC0%0/2
ref $args->{$key} eq "ARRAY" and @{$args->{$key};}
ref $args->{$key} eq "ARRAY" @{$args->{$key};}
Truth table66%2/3
A and B
A= ref $args->{$key} eq "ARRAY"B= @{$args->{$key};}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $args->{$key} eq "ARRAY"B= @{$args->{$key};}
ABresult
0X0
100
111
995 0 require JSON::XS;
996 0 my @values = @{$args->{$key}};
997 push @details, "$key=" . join (" ", map {
998 0 ▶ JSON::XS::encode_json (substr $_, 0, 80) . (length $_ > 80 ? "..." : "")
Branch: 6 branches
BranchTrueFalseTotal
length $_ > 80 ? : 0 6 6
$#values < 3 ? : 2 0 2
@values > 4 ? : 0 2 2
length $_ > 80 ? : 0 0 0
$#values < 3 ? : 0 0 0
@values > 4 ? : 0 0 0
999 } @values[0 .. ($#values < 3 ? $#values : 3)])
1000 . (@values > 4 ? " ..." : "");
1001 }
1002 0 ▶ if ($req->{tool} eq "grep" && defined $args->{pattern} && !ref $args->{pattern}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}) 6 238 244
if ($req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}) 0 0 0
Condition33%4/12
$req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}
!ll&&!rl&&r
23806
$req->{"tool"} eq "grep" and defined $args->{"pattern"}
!ll&&!rl&&r
23806
$req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}
!ll&&!rl&&r
000
$req->{"tool"} eq "grep" and defined $args->{"pattern"}
!ll&&!rl&&r
000
MC/DC33%1/3
$req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}
$req->{"tool"} eq "grep" defined $args->{"pattern"} not ref $args->{"pattern"}
MC/DC0%0/3
$req->{"tool"} eq "grep" and defined $args->{"pattern"} and not ref $args->{"pattern"}
$req->{"tool"} eq "grep" defined $args->{"pattern"} not ref $args->{"pattern"}
Truth table50%2/4
A and B and not(C)
A= $req->{"tool"} eq "grep"B= defined $args->{"pattern"}C= not ref $args->{"pattern"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and not(C)
A= $req->{"tool"} eq "grep"B= defined $args->{"pattern"}C= not ref $args->{"pattern"}
ABCresult
0XX0
10X0
1100
1111
1003 0 require JSON::XS;
1004 push @details, "pattern=" . JSON::XS::encode_json (substr $args->{pattern}, 0, 160)
1005 0 ▶ . (length $args->{pattern} > 160 ? "..." : "");
Branch: 2 branches
BranchTrueFalseTotal
length $args->{'pattern'} > 160 ? : 0 6 6
length $args->{'pattern'} > 160 ? : 0 0 0
1006 }
1007 push @details, "name=" . (substr $args->{name}, 0, 160)
1008 0 ▶ if $req->{tool} eq "call-tool" && defined $args->{name} && !ref $args->{name};
Branch: 2 branches
BranchTrueFalseTotal
if $req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"} 8 236 244
if $req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"} 0 0 0
Condition33%4/12
$req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"}
!ll&&!rl&&r
23608
$req->{"tool"} eq "call-tool" and defined $args->{"name"}
!ll&&!rl&&r
23608
$req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"}
!ll&&!rl&&r
000
$req->{"tool"} eq "call-tool" and defined $args->{"name"}
!ll&&!rl&&r
000
MC/DC33%1/3
$req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"}
$req->{"tool"} eq "call-tool" defined $args->{"name"} not ref $args->{"name"}
MC/DC0%0/3
$req->{"tool"} eq "call-tool" and defined $args->{"name"} and not ref $args->{"name"}
$req->{"tool"} eq "call-tool" defined $args->{"name"} not ref $args->{"name"}
Truth table50%2/4
A and B and not(C)
A= $req->{"tool"} eq "call-tool"B= defined $args->{"name"}C= not ref $args->{"name"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and not(C)
A= $req->{"tool"} eq "call-tool"B= defined $args->{"name"}C= not ref $args->{"name"}
ABCresult
0XX0
10X0
1100
1111
1009 0 ▶ if ($req->{tool} eq "ex" && defined $args->{command} && !ref $args->{command}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}) 18 226 244
if ($req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}) 0 0 0
Condition41%5/12
$req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}
!ll&&!rl&&r
225118
$req->{"tool"} eq "ex" and defined $args->{"command"}
!ll&&!rl&&r
225019
$req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}
!ll&&!rl&&r
000
$req->{"tool"} eq "ex" and defined $args->{"command"}
!ll&&!rl&&r
000
MC/DC66%2/3
$req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}
$req->{"tool"} eq "ex" defined $args->{"command"} not ref $args->{"command"}
MC/DC0%0/3
$req->{"tool"} eq "ex" and defined $args->{"command"} and not ref $args->{"command"}
$req->{"tool"} eq "ex" defined $args->{"command"} not ref $args->{"command"}
Truth table75%3/4
A and B and not(C)
A= $req->{"tool"} eq "ex"B= defined $args->{"command"}C= not ref $args->{"command"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and not(C)
A= $req->{"tool"} eq "ex"B= defined $args->{"command"}C= not ref $args->{"command"}
ABCresult
0XX0
10X0
1100
1111
1010 0 require JSON::XS;
1011 push @details, "command=" . JSON::XS::encode_json (substr $args->{command}, 0, 200)
1012 0 ▶ . (length $args->{command} > 200 ? "..." : "");
Branch: 2 branches
BranchTrueFalseTotal
length $args->{'command'} > 200 ? : 0 18 18
length $args->{'command'} > 200 ? : 0 0 0
1013 }
1014 0 ▶ if ($req->{tool} eq "path-op") {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{"tool"} eq "path-op") 2 242 244
if ($req->{"tool"} eq "path-op") 0 0 0
1015 push @details, map {
1016 0 ($_ eq "unlink" || $_ eq "rmdir")
1017 ? ($args->{$_} ? $_ : ())
1018 : (defined $args->{$_} && !ref $args->{$_}
1019 0 ▶ ? "$_=" . (substr $args->{$_}, 0, 80) : ())
Branch: 6 branches
BranchTrueFalseTotal
$_ eq 'unlink' || $_ eq 'rmdir' ? : 4 12 16
$args->{$_} ? : 0 4 4
defined $args->{$_} && !ref($args->{$_}) ? : 2 10 12
$_ eq 'unlink' || $_ eq 'rmdir' ? : 0 0 0
$args->{$_} ? : 0 0 0
defined $args->{$_} && !ref($args->{$_}) ? : 0 0 0
Condition41%5/12
$_ eq "unlink" or $_ eq "rmdir"
l!l&&r!l&&!r
2212
defined $args->{$_} and not ref $args->{$_}
!ll&&!rl&&r
1002
$_ eq "unlink" or $_ eq "rmdir"
l!l&&r!l&&!r
000
defined $args->{$_} and not ref $args->{$_}
!ll&&!rl&&r
000
MC/DC100%2/2
$_ eq "unlink" or $_ eq "rmdir"
$_ eq "unlink" $_ eq "rmdir"
MC/DC50%1/2
defined $args->{$_} and not ref $args->{$_}
defined $args->{$_} not ref $args->{$_}
MC/DC0%0/2
$_ eq "unlink" or $_ eq "rmdir"
$_ eq "unlink" $_ eq "rmdir"
MC/DC0%0/2
defined $args->{$_} and not ref $args->{$_}
defined $args->{$_} not ref $args->{$_}
Truth table100%3/3
A or B
A= $_ eq "unlink"B= $_ eq "rmdir"
ABresult
1X1
011
000
Truth table66%2/3
A and not(B)
A= defined $args->{$_}B= not ref $args->{$_}
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $_ eq "unlink"B= $_ eq "rmdir"
ABresult
1X1
011
000
Truth table0%0/3
A and not(B)
A= defined $args->{$_}B= not ref $args->{$_}
ABresult
0X0
100
111
1020 } qw(unlink rmdir mkpath mkdir symlink truncate chmod rename);
1021 }
1022 0 for my $key (sort keys %extra) {
1023 0 ▶ next unless defined $extra{$key} && !ref $extra{$key};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $extra{$key} and not ref $extra{$key} 0 34 34
unless defined $extra{$key} and not ref $extra{$key} 0 0 0
Condition16%1/6
defined $extra{$key} and not ref $extra{$key}
!ll&&!rl&&r
0034
defined $extra{$key} and not ref $extra{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $extra{$key} and not ref $extra{$key}
defined $extra{$key} not ref $extra{$key}
MC/DC0%0/2
defined $extra{$key} and not ref $extra{$key}
defined $extra{$key} not ref $extra{$key}
Truth table33%1/3
A and not(B)
A= defined $extra{$key}B= not ref $extra{$key}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $extra{$key}B= not ref $extra{$key}
ABresult
0X0
100
111
1024 0 push @details, "$key=" . (substr $extra{$key}, 0, 160);
1025 }
1026  
1027 0 (my $message = join " ", $req->{tool}, @details) =~ s/[\r\n]/ /g;
1028 0 mcp_log $message;
1029  
1030 0 my $reply = $req->{on_reply};
1031 239 ▶ $req->{on_reply} = sub {
Subroutine __ANON__: called CC=5 SCAR=24.3
1032 0 ▶ my ($req, $result) = @_;
Subroutine __ANON__: not called CC=5 SCAR=24.3
1033  
1034 0 ▶ if ($result->{isError}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{"isError"}) 62 177 239
if ($result->{"isError"}) 0 0 0
1035 my $message = ref $result->{content} eq "ARRAY"
1036 && ref $result->{content}[0] eq "HASH"
1037 0 ▶ ? $result->{content}[0]{text} // "unknown error" : "unknown error";
Branch: 2 branches
BranchTrueFalseTotal
ref $result->{'content'} eq 'ARRAY' && ref $result->{'content'}[0] eq 'HASH' ? : 62 0 62
ref $result->{'content'} eq 'ARRAY' && ref $result->{'content'}[0] eq 'HASH' ? : 0 0 0
Condition20%2/10
ref $result->{"content"} eq "ARRAY" and ref $result->{"content"}[0] eq "HASH"
!ll&&!rl&&r
0062
$result->{"content"}[0]{"text"} // "unknown error"
l!l
620
ref $result->{"content"} eq "ARRAY" and ref $result->{"content"}[0] eq "HASH"
!ll&&!rl&&r
000
$result->{"content"}[0]{"text"} // "unknown error"
l!l
00
MC/DC0%0/2
ref $result->{"content"} eq "ARRAY" and ref $result->{"content"}[0] eq "HASH"
ref $result->{"content"} eq "ARRAY" ref $result->{"content"}[0] eq "HASH"
MC/DC0%0/1
$result->{"content"}[0]{"text"} // "unknown error"
$result->{"content"}[0]{"text"}
MC/DC0%0/2
ref $result->{"content"} eq "ARRAY" and ref $result->{"content"}[0] eq "HASH"
ref $result->{"content"} eq "ARRAY" ref $result->{"content"}[0] eq "HASH"
MC/DC0%0/1
$result->{"content"}[0]{"text"} // "unknown error"
$result->{"content"}[0]{"text"}
Truth table33%1/3
A and B
A= ref $result->{"content"} eq "ARRAY"B= ref $result->{"content"}[0] eq "HASH"
ABresult
0X0
100
111
Truth table50%1/2
A
A= $result->{"content"}[0]{"text"}
Aresult
11
00
Truth table0%0/3
A and B
A= ref $result->{"content"} eq "ARRAY"B= ref $result->{"content"}[0] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/2
A
A= $result->{"content"}[0]{"text"}
Aresult
11
00
1038 0 $message =~ s/\n.*//s;
1039 0 mcp_log "$req->{tool} failed: " . (substr $message, 0, 240);
1040 }
1041 0 $reply->(@_);
1042 0 };
1043 }
1044  
1045 53 ▶sub _mcp_log_init {
Subroutine _mcp_log_init: called CC=1 SCAR=0.8
1046 0 ▶ my ($name, $silent) = @_;
Subroutine _mcp_log_init: not called CC=1 SCAR=0.8
1047  
1048 0 $quiet = $silent;
1049 0 my $identity = AnyEvent::MCP::Config::program_name () . "/$name" . "[$$]";
1050  
1051 75 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
1052 132 ▶ *AnyEvent::Log::default_format = sub ($$$$) {
Subroutine __ANON__: called CC=2 SCAR=9.2
1053 0 ▶ my ($when, $ctx, $level, $message) = @_;
Subroutine __ANON__: not called CC=2 SCAR=9.2
1054 0 my $stamp = substr +(AnyEvent::Log::format_time $when), 8, 14;
1055 0 ▶ my $source = $ctx == $AnyEvent::Log::LOG ? "" : "$AnyEvent::Log::LEVEL2STR[$level] $ctx->[0]: ";
Branch: 2 branches
BranchTrueFalseTotal
$ctx == $AnyEvent::Log::LOG ? : 125 7 132
$ctx == $AnyEvent::Log::LOG ? : 0 0 0
1056 0 join "", map "$stamp $identity $source$_\n", split /\n/, $message;
1057 0 };
1058 }
1059  
1060 65 ▶sub mcp_config {
Subroutine mcp_config: called CC=2 SCAR=9.2
Pod documented
1061 0 ▶ my %kv = @_;
Subroutine mcp_config: not called CC=2 SCAR=9.2
Pod documented
1062  
1063 $kv{instructions} = AnyEvent::MCP::_clean_text $kv{instructions}
1064 0 ▶ if exists $kv{instructions};
Branch: 2 branches
BranchTrueFalseTotal
if exists $kv{"instructions"} 49 16 65
if exists $kv{"instructions"} 0 0 0
1065  
1066 0 %config = (%config, %kv);
1067 }
1068  
1069 39 ▶sub mcp_permissions {
Subroutine mcp_permissions: called CC=2 SCAR=9.2
Pod documented
1070 0 ▶ @_ == 1 or die "mcp_permissions expects one argument (object or undef)\n";
Subroutine mcp_permissions: not called CC=2 SCAR=9.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 or die "mcp_permissions expects one argument (object or undef)\n" 38 1 39
@_ == 1 or die "mcp_permissions expects one argument (object or undef)\n" 0 0 0
1071 0 $config{permissions} = $_[0];
1072 }
1073  
1074 115 ▶sub mcp_tool {
Subroutine mcp_tool: called CC=1 SCAR=1.2
Pod documented
1075 0 ▶ $toolset->tool (@_)
Subroutine mcp_tool: not called CC=1 SCAR=1.2
Pod documented
1076 }
1077  
1078 # format a JSON Schema type for command-line tool help.
1079 194 ▶sub _tool_type {
Subroutine _tool_type: called CC=7 SCAR=28.0
1080 0 ▶ my ($schema) = @_;
Subroutine _tool_type: not called CC=7 SCAR=28.0
1081 0 ▶ return "any" unless ref $schema eq "HASH";
Branch: 2 branches
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 194 194
unless ref $schema eq "HASH" 0 0 0
1082 0 for my $key (qw(anyOf oneOf)) {
1083 0 return join "|", map _tool_type ($_), @{$schema->{$key}}
1084 0 ▶ if ref $schema->{$key} eq "ARRAY";
Branch: 2 branches
BranchTrueFalseTotal
if ref $schema->{$key} eq "ARRAY" 15 358 373
if ref $schema->{$key} eq "ARRAY" 0 0 0
1085 }
1086 0 ▶ my $type = $schema->{type} // "any";
Condition25%1/4
$schema->{"type"} // "any"
l!l
1790
$schema->{"type"} // "any"
l!l
00
MC/DC0%0/1
$schema->{"type"} // "any"
$schema->{"type"}
MC/DC0%0/1
$schema->{"type"} // "any"
$schema->{"type"}
Truth table50%1/2
A
A= $schema->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $schema->{"type"}
Aresult
11
00
1087 0 ▶ $type = join "|", @$type if ref $type eq "ARRAY";
Branch: 2 branches
BranchTrueFalseTotal
if ref $type eq "ARRAY" 0 179 179
if ref $type eq "ARRAY" 0 0 0
1088 0 ▶ $type = "array[" . _tool_type ($schema->{items}) . "]" if $type eq "array";
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "array" 10 169 179
if $type eq "array" 0 0 0
1089 0 $type
1090 }
1091  
1092 141 ▶sub mcp_tool_help {
Subroutine mcp_tool_help: called CC=16 SCAR=40.5
Pod documented
1093 0 ▶ my ($enabled, @toolsets) = @_;
Subroutine mcp_tool_help: not called CC=16 SCAR=40.5
Pod documented
1094 0 ▶ return unless $enabled;
Branch: 2 branches
BranchTrueFalseTotal
unless $enabled 136 5 141
unless $enabled 0 0 0
1095  
1096 0 my $json = AnyEvent::MCP::json_class ()->new->canonical;
1097 0 my @tools = map @{$_->list}, @toolsets;
1098 0 for my $tool (@tools) {
1099 0 print "$tool->{name}\n";
1100 0 ▶ (my $description = $tool->{description} // "") =~ s/\n/\n /g;
Condition25%1/4
$tool->{"description"} // ""
l!l
360
$tool->{"description"} // ""
l!l
00
MC/DC0%0/1
$tool->{"description"} // ""
$tool->{"description"}
MC/DC0%0/1
$tool->{"description"} // ""
$tool->{"description"}
Truth table50%1/2
A
A= $tool->{"description"}
Aresult
11
00
Truth table0%0/2
A
A= $tool->{"description"}
Aresult
11
00
1101 0 ▶ print " $description\n" if length $description;
Branch: 2 branches
BranchTrueFalseTotal
if length $description 36 0 36
if length $description 0 0 0
1102  
1103 0 ▶ my $schema = $tool->{inputSchema} || {};
Condition25%1/4
$tool->{'inputSchema'} || {}
l!l
360
$tool->{'inputSchema'} || {}
l!l
00
MC/DC0%0/1
$tool->{'inputSchema'} || {}
$tool->{'inputSchema'}
MC/DC0%0/1
$tool->{'inputSchema'} || {}
$tool->{'inputSchema'}
Truth table50%1/2
A
A= $tool->{'inputSchema'}
Aresult
11
00
Truth table0%0/2
A
A= $tool->{'inputSchema'}
Aresult
11
00
1104 0 ▶ my $properties = $schema->{properties} || {};
Condition25%1/4
$schema->{'properties'} || {}
l!l
360
$schema->{'properties'} || {}
l!l
00
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table50%1/2
A
A= $schema->{'properties'}
Aresult
11
00
Truth table0%0/2
A
A= $schema->{'properties'}
Aresult
11
00
1105 0 ▶ my %required = map { $_ => 1 } @{$schema->{required} || []};
Condition50%2/4
$schema->{'required'} || []
l!l
306
$schema->{'required'} || []
l!l
00
MC/DC100%1/1
$schema->{'required'} || []
$schema->{'required'}
MC/DC0%0/1
$schema->{'required'} || []
$schema->{'required'}
Truth table100%2/2
A
A= $schema->{'required'}
Aresult
11
00
Truth table0%0/2
A
A= $schema->{'required'}
Aresult
11
00
1106 0 for my $name (sort keys %$properties) {
1107 0 my $property = $properties->{$name};
1108 0 my $type = _tool_type ($property);
1109 0 ▶ my $status = $required{$name} ? "required" : "optional";
Branch: 2 branches
BranchTrueFalseTotal
$required{$name} ? : 62 92 154
$required{$name} ? : 0 0 0
1110 0 my @extras;
1111 push @extras, "default: " . $json->encode ($property->{default})
1112 0 ▶ if exists $property->{default};
Branch: 2 branches
BranchTrueFalseTotal
if exists $property->{"default"} 34 120 154
if exists $property->{"default"} 0 0 0
1113 0 push @extras, "values: " . join (", ", @{$property->{enum}})
1114 0 ▶ if ref $property->{enum} eq "ARRAY";
Branch: 2 branches
BranchTrueFalseTotal
if ref $property->{"enum"} eq "ARRAY" 7 147 154
if ref $property->{"enum"} eq "ARRAY" 0 0 0
1115 0 ▶ print " $name $type $status", (@extras ? " (" . join ("; ", @extras) . ")" : ""), "\n";
Branch: 2 branches
BranchTrueFalseTotal
@extras ? : 38 116 154
@extras ? : 0 0 0
1116 0 ▶ if (length ($property->{description} // "")) {
Branch: 2 branches
BranchTrueFalseTotal
if (length($property->{"description"} // "")) 102 52 154
if (length($property->{"description"} // "")) 0 0 0
Condition50%2/4
$property->{"description"} // ""
l!l
10252
$property->{"description"} // ""
l!l
00
MC/DC100%1/1
$property->{"description"} // ""
$property->{"description"}
MC/DC0%0/1
$property->{"description"} // ""
$property->{"description"}
Truth table100%2/2
A
A= $property->{"description"}
Aresult
11
00
Truth table0%0/2
A
A= $property->{"description"}
Aresult
11
00
1117 0 (my $detail = $property->{description}) =~ s/\n/\n /g;
1118 0 print " $detail\n";
1119 }
1120 }
1121 0 print "\n";
1122 }
1123 0 ▶ print "No tools registered.\n" unless @tools;
Branch: 2 branches
BranchTrueFalseTotal
unless @tools 0 5 5
unless @tools 0 0 0
1124 0 exit;
1125 }
1126  
1127 116 ▶sub _mcp_help_row {
Subroutine _mcp_help_row: called CC=7 SCAR=25.7
1128 0 ▶ my ($option, $description) = @_;
Subroutine _mcp_help_row: not called CC=7 SCAR=25.7
1129  
1130 0 my $start = " $option";
1131 0 my $margin = " " x 34;
1132 0 ▶ my $out = length $start < 34 ? "" : "$start\n";
Branch: 2 branches
BranchTrueFalseTotal
length $start < 34 ? : 98 18 116
length $start < 34 ? : 0 0 0
1133 0 ▶ my $line = length $start < 34 ? $start . (substr $margin, length $start) : $margin;
Branch: 2 branches
BranchTrueFalseTotal
length $start < 34 ? : 98 18 116
length $start < 34 ? : 0 0 0
1134  
1135 0 for my $word (split /\s+/, $description) {
1136 0 ▶ if (length $line > 34 && (length $line) + 1 + length $word > 79) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $line > 34 and length($line) + 1 + length($word) > 79) 92 773 865
if (length $line > 34 and length($line) + 1 + length($word) > 79) 0 0 0
Condition50%3/6
length $line > 34 and length($line) + 1 + length($word) > 79
!ll&&!rl&&r
11665792
length $line > 34 and length($line) + 1 + length($word) > 79
!ll&&!rl&&r
000
MC/DC100%2/2
length $line > 34 and length($line) + 1 + length($word) > 79
length $line > 34 length($line) + 1 + length($word) > 79
MC/DC0%0/2
length $line > 34 and length($line) + 1 + length($word) > 79
length $line > 34 length($line) + 1 + length($word) > 79
Truth table100%3/3
A and B
A= length $line > 34B= length($line) + 1 + length($word) > 79
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= length $line > 34B= length($line) + 1 + length($word) > 79
ABresult
0X0
100
111
1137 0 $out .= "$line\n";
1138 0 $line = $margin;
1139 }
1140  
1141 0 ▶ $line .= " " if length $line > 34;
Branch: 2 branches
BranchTrueFalseTotal
if length $line > 34 657 208 865
if length $line > 34 0 0 0
1142 0 $line .= $word;
1143 }
1144  
1145 "$out$line\n"
1146 0 }
1147  
1148 6 ▶sub _mcp_help {
Subroutine _mcp_help: called CC=13 SCAR=39.3
1149 0 ▶ my ($usage, $description, $options, $option_heading, $examples, $without) = @_;
Subroutine _mcp_help: not called CC=13 SCAR=39.3
1150  
1151 0 $usage =~ s/\n*\z/\n/;
1152 0 my $help = $usage;
1153 0 ▶ $help .= "\n$description\n" if defined $description && length $description;
Branch: 2 branches
BranchTrueFalseTotal
if defined $description and length $description 6 0 6
if defined $description and length $description 0 0 0
Condition16%1/6
defined $description and length $description
!ll&&!rl&&r
006
defined $description and length $description
!ll&&!rl&&r
000
MC/DC0%0/2
defined $description and length $description
defined $description length $description
MC/DC0%0/2
defined $description and length $description
defined $description length $description
Truth table33%1/3
A and B
A= defined $descriptionB= length $description
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $descriptionB= length $description
ABresult
0X0
100
111
1154  
1155 0 $help .= "\nCommon options:\n";
1156 0 my @common = (
1157 ["--name NAME", "Select named configuration (instance defaults to 'default')"],
1158 ["--bind ADDRESS[:PORT]", "Override the HTTP listen address or port"],
1159 ["--tls-cert FILE", "PEM certificate (and optionally key) for HTTPS"],
1160 ["--tls-key FILE", "PEM private key (or combined key/certificate PEM)"],
1161 ["--token TOKEN", "Set an accepted token (repeatable; replaces defaults)"],
1162 ["-s, --set KEY=VALUE", "Override a trusted instance setting (repeatable)"],
1163 ["--stdio", "Serve MCP on stdin/stdout; other listeners may remain"],
1164 ["--connect URL", "Connect to a multibroker at http[s]://host:port/token/alias"],
1165 ["--custom-instructions TEXT", "Describe when this server instance should be used"],
1166 ["--auto-allow TOOL[,ACTION[,CAPABILITY]]", "Add an ordered automatic allow rule (repeatable)"],
1167 ["--auto-deny TOOL[,ACTION[,CAPABILITY]]", "Add an ordered automatic deny rule (repeatable)"],
1168 ["--ask MODE", "Prompt via helper, tty, yad, mcp, auto; none declines without prompting"],
1169 ["--quiet", "Suppress routine tool activity and tool-failure logging"],
1170 ["--print-url[=USER] (--print-urls)", "Print enabled user URLs, or one exact user"],
1171 ["--tool-help", "Print registered tool descriptions and arguments, then exit"],
1172 ["--help", "Display this help and exit"],
1173 );
1174 0 $help .= _mcp_help_row (@$_) for grep {
1175 0 my ($name) = $_->[0] =~ /--([a-z-]+)/;
1176 0 !$without->{$name}
1177 } @common;
1178  
1179 0 ▶ if ($options && @$options) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options and @$options) 3 3 6
if ($options and @$options) 0 0 0
Condition33%2/6
$options and @$options
!ll&&!rl&&r
303
$options and @$options
!ll&&!rl&&r
000
MC/DC50%1/2
$options and @$options
$options @$options
MC/DC0%0/2
$options and @$options
$options @$options
Truth table66%2/3
A and B
A= $optionsB= @$options
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $optionsB= @$options
ABresult
0X0
100
111
1180 0 ▶ $help .= "\n" . ($option_heading // "Application options:") . "\n"
Branch: 2 branches
BranchTrueFalseTotal
if ref $options->[0] 1 2 3
if ref $options->[0] 0 0 0
Condition25%1/4
$option_heading // "Application options:"
l!l
10
$option_heading // "Application options:"
l!l
00
MC/DC0%0/1
$option_heading // "Application options:"
$option_heading
MC/DC0%0/1
$option_heading // "Application options:"
$option_heading
Truth table50%1/2
A
A= $option_heading
Aresult
11
00
Truth table0%0/2
A
A= $option_heading
Aresult
11
00
1181 if ref $options->[0];
1182 0 for my $option (@$options) {
1183 0 ▶ if (ref $option) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $option) { } 20 4 24
if (ref $option) { } 0 0 0
1184 0 $help .= _mcp_help_row (@$option);
1185 } else {
1186 0 ▶ $help .= "\n" unless $help =~ /\n\n\z/;
Branch: 2 branches
BranchTrueFalseTotal
unless $help =~ /\n\n\z/u 4 0 4
unless $help =~ /\n\n\z/u 0 0 0
1187 0 $help .= "$option\n";
1188 }
1189 }
1190 }
1191 0 ▶ $help .= "\nExamples:\n$examples\n"
Branch: 2 branches
BranchTrueFalseTotal
if defined $examples and length $examples 6 0 6
if defined $examples and length $examples 0 0 0
Condition16%1/6
defined $examples and length $examples
!ll&&!rl&&r
006
defined $examples and length $examples
!ll&&!rl&&r
000
MC/DC0%0/2
defined $examples and length $examples
defined $examples length $examples
MC/DC0%0/2
defined $examples and length $examples
defined $examples length $examples
Truth table33%1/3
A and B
A= defined $examplesB= length $examples
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $examplesB= length $examples
ABresult
0X0
100
111
1192 if defined $examples && length $examples;
1193 0 $help .= "\nNamed users and URLs:\n"
1194 . " anyevent-mcp create-user chatgpt\n"
1195 . " $0 --print-urls # list URLs for all enabled users\n"
1196 . " $0 --print-url=chatgpt # select one user\n"
1197 . "A URL marked (connect) is the one used for browser, Vim, or reverse connections.\n"
1198 . "Users inherit from global, program, then instance; server restart is needed\n"
1199 . "after changing user definitions.\n";
1200  
1201 0 $help
1202 }
1203  
1204 62 ▶sub mcp_getoptions {
Subroutine mcp_getoptions: called CC=54 SCAR=64.1
Pod documented
1205 0 ▶ require Getopt::Long;
Subroutine mcp_getoptions: not called CC=54 SCAR=64.1
Pod documented
1206  
1207 0 ▶ my %settings = %{ref $_[0] eq "HASH" ? shift : {}};
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 58 4 62
ref $_[0] eq 'HASH' ? : 0 0 0
1208 0 my $print_url_extra = delete $settings{print_url_extra};
1209 my $usage = delete $settings{usage}
1210 0 ▶ // "Usage: $0 [OPTION]... [ARG]...\n";
Condition50%2/4
delete $settings{"usage"} // "Usage: $0 [OPTION]... [ARG]...\n"
l!l
548
delete $settings{"usage"} // "Usage: $0 [OPTION]... [ARG]...\n"
l!l
00
MC/DC100%1/1
delete $settings{"usage"} // "Usage: $0 [OPTION]... [ARG]...\n"
delete $settings{"usage"}
MC/DC0%0/1
delete $settings{"usage"} // "Usage: $0 [OPTION]... [ARG]...\n"
delete $settings{"usage"}
Truth table100%2/2
A
A= delete $settings{"usage"}
Aresult
11
00
Truth table0%0/2
A
A= delete $settings{"usage"}
Aresult
11
00
1211 0 my @help = delete @settings{qw(description options option_heading examples)};
1212 0 ▶ my %without = map { $_ => 1 } @{delete $settings{without} || []};
Condition50%2/4
delete $settings{'without'} || []
l!l
260
delete $settings{'without'} || []
l!l
00
MC/DC100%1/1
delete $settings{'without'} || []
delete $settings{'without'}
MC/DC0%0/1
delete $settings{'without'} || []
delete $settings{'without'}
Truth table100%2/2
A
A= delete $settings{'without'}
Aresult
11
00
Truth table0%0/2
A
A= delete $settings{'without'}
Aresult
11
00
1213 0 my %url = (%config, %settings);
1214 0 delete @url{qw(name bind stdio)};
1215 0 my @auto_rules;
1216 my %set;
1217  
1218 my @common = (
1219 AnyEvent::MCP::Config::http_getopt (\my $name, \my $bind, \my $print_url, \my @tokens),
1220 "tls-cert=s" => \my $tls_cert,
1221 "tls-key=s" => \my $tls_key,
1222 "stdio" => \my $stdio,
1223 "connect=s" => \my $connect,
1224 "custom-instructions=s" => \my $custom_instructions,
1225 6 ▶ "auto-allow=s" => sub { push @auto_rules, ["allow", $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
1226 2 ▶ "auto-deny=s" => sub { push @auto_rules, ["deny", $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
1227 4 ▶ "set|s=s" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
1228 4 my ($key, $value) = $_[1] =~ /\A([^\s=]+)=(.*)\z/;
1229 4 ▶ defined $value or die "Invalid --set KEY=VALUE\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or die "Invalid --set KEY=VALUE\n" 4 0 4
1230 4 $set{$key} = $value;
1231 },
1232 0 "tool-help" => \my $tool_help,
1233 "quiet" => \my $silent,
1234 "ask=s" => \my $ask,
1235 "help" => \my $help,
1236 );
1237 0 my @enabled;
1238 my %known;
1239 0 ▶ while (@common) {
Branch: 2 branches
BranchTrueFalseTotal
if (@common) 992 62 1054
if (@common) 0 0 0
1240 0 my ($option, $target) = splice @common, 0, 2;
1241 0 my ($name) = $option =~ /\A([a-z-]+)/;
1242 0 $known{$name} = 1;
1243 0 ▶ push @enabled, $option => $target unless $without{$name};
Branch: 2 branches
BranchTrueFalseTotal
unless $without{$name} 991 1 992
unless $without{$name} 0 0 0
1244 }
1245 die "Unknown common option: " . join (", ", sort grep !$known{$_}, keys %without) . "\n"
1246 0 ▶ if grep !$known{$_}, keys %without;
Branch: 2 branches
BranchTrueFalseTotal
if grep((!$known{$_}), keys %without) 1 61 62
if grep((!$known{$_}), keys %without) 0 0 0
1247 0 ▶ Getopt::Long::GetOptions (
Branch: 2 branches
BranchTrueFalseTotal
Getopt::Long::GetOptions(@enabled, @_) or die $usage 59 2 61
&Getopt::Long::GetOptions(@enabled, @_) or die $usage 0 0 0
1248 @enabled,
1249 @_,
1250 ) or die $usage;
1251 0 ▶ die "--ask must be helper, tty, yad, mcp, auto, or none\n"
Branch: 2 branches
BranchTrueFalseTotal
if defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u 0 59 59
if defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u 0 0 0
Condition33%2/6
defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
!ll&&!rl&&r
40190
defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
defined $ask not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
MC/DC0%0/2
defined $ask and not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
defined $ask not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
Truth table66%2/3
A and not(B)
A= defined $askB= not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $askB= not $ask =~ /\A(?:helper|tty|yad|mcp|auto|none)\z/u
ABresult
0X0
100
111
1252 if defined $ask && $ask !~ /\A(?:helper|tty|yad|mcp|auto|none)\z/;
1253  
1254 0 ▶ if ($help) {
Branch: 2 branches
BranchTrueFalseTotal
if ($help) 6 53 59
if ($help) 0 0 0
1255 0 print _mcp_help $usage, @help, \%without;
1256 0 exit;
1257 }
1258  
1259 0 ▶ $name = AnyEvent::MCP::instance_name ($name // $settings{name} // $config{name});
Condition41%5/12
($name // $settings{"name"}) // $config{"name"}
l!l&&r!l&&!r
6047
$name // $settings{"name"}
l!l&&r!l&&!r
5147
($name // $settings{"name"}) // $config{"name"}
l!l&&r!l&&!r
000
$name // $settings{"name"}
l!l&&r!l&&!r
000
MC/DC66%2/3
($name // $settings{"name"}) // $config{"name"}
$name $settings{"name"} $config{"name"}
MC/DC0%0/3
($name // $settings{"name"}) // $config{"name"}
$name $settings{"name"} $config{"name"}
Truth table75%3/4
A // B // C
A= $nameB= $settings{"name"}C= $config{"name"}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A // B // C
A= $nameB= $settings{"name"}C= $config{"name"}
ABCresult
1XX1
01X1
0011
0000
1260 0 ▶ if (%set) {
Branch: 2 branches
BranchTrueFalseTotal
if (%set) 4 49 53
if (%set) 0 0 0
1261 0 ▶ my $instance = (AnyEvent::MCP::Config::global_config)->{section}{"instance $name"} ||= {};
Condition25%1/4
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"} ||= {}
l!l
04
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"} ||= {}
l!l
00
MC/DC0%0/1
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"} ||= {}
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"}
MC/DC0%0/1
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"} ||= {}
AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"}
Truth table50%1/2
A
A= AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"}
Aresult
11
00
Truth table0%0/2
A
A= AnyEvent::MCP::Config::global_config()->{"section"}{"instance $name"}
Aresult
11
00
1262 0 %$instance = (%$instance, %set);
1263 }
1264  
1265 0 _mcp_log_init ($name, $silent);
1266 0 ▶ my %server = (
Branch: 20 branches
BranchTrueFalseTotal
defined $bind ? : 8 45 53
defined $tls_cert ? : 1 52 53
defined $tls_key ? : 1 52 53
@tokens ? : 4 49 53
$stdio ? : 12 41 53
defined $connect ? : 0 53 53
defined $ask ? : 19 34 53
defined $custom_instructions ? : 1 52 53
@auto_rules ? : 6 47 53
$tool_help ? : 6 47 53
defined $bind ? : 0 0 0
defined $tls_cert ? : 0 0 0
defined $tls_key ? : 0 0 0
@tokens ? : 0 0 0
$stdio ? : 0 0 0
defined $connect ? : 0 0 0
defined $ask ? : 0 0 0
defined $custom_instructions ? : 0 0 0
@auto_rules ? : 0 0 0
$tool_help ? : 0 0 0
1267 %settings,
1268 name => $name,
1269 (defined $bind ? (bind => $bind) : ()),
1270 (defined $tls_cert ? (tls_cert => $tls_cert) : ()),
1271 (defined $tls_key ? (tls_key => $tls_key) : ()),
1272 (@tokens ? (tokens => \@tokens) : ()),
1273 ($stdio ? (stdio => 1) : ()),
1274 (defined $connect ? (connect => $connect) : ()),
1275 (defined $ask ? (ask => $ask) : ()),
1276 (defined $custom_instructions ? (custom_instructions => $custom_instructions) : ()),
1277 (@auto_rules ? (auto_rules => \@auto_rules) : ()),
1278 ($tool_help ? (tool_help => 1) : ()),
1279 );
1280  
1281 0 ▶ if (defined $print_url) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $print_url) 6 47 53
if (defined $print_url) 0 0 0
1282 0 ▶ my %options = (
Branch: 6 branches
BranchTrueFalseTotal
defined $tls_cert ? : 0 6 6
defined $tls_key ? : 0 6 6
@tokens ? : 1 5 6
defined $tls_cert ? : 0 0 0
defined $tls_key ? : 0 0 0
@tokens ? : 0 0 0
1283 %url,
1284 (defined $tls_cert ? (tls_cert => $tls_cert) : ()),
1285 (defined $tls_key ? (tls_key => $tls_key) : ()),
1286 (@tokens ? (tokens => \@tokens) : ()),
1287 );
1288 0 my $global = AnyEvent::MCP::Config::global_config;
1289 $options{path} //= $global->{section}{"instance $name"}{path}
1290 0 ▶ // $global->{section}{"program " . AnyEvent::MCP::Config::program_name ()}{path} // "/";
Condition28%4/14
$options{"path"} //= ($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
l!l
42
($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
l!l
02
$global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}
l!l&&r!l&&!r
002
$options{"path"} //= ($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
l!l
00
($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
l!l
00
$global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}
l!l&&r!l&&!r
000
MC/DC100%1/1
$options{"path"} //= ($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
$options{"path"}
MC/DC0%0/1
$options{"path"} //= ($global->{"section"}{"instance $name"}{"path"} // $global->{"section"}{'program ' . AnyEvent::MCP::Config::program_name()}{"path"}) // "/"
$options{"path"}
Truth table100%2/2
A
A= $options{"path"}
Aresult
11
00
Truth table0%0/2
A
A= $options{"path"}
Aresult
11
00
1291 0 ▶ if (($options{auth} || "") eq "none") {
Branch: 2 branches
BranchTrueFalseTotal
if (($options{"auth"} || "") eq "none") 0 6 6
if (($options{"auth"} || "") eq "none") 0 0 0
Condition25%1/4
$options{'auth'} || ''
l!l
06
$options{'auth'} || ''
l!l
00
MC/DC0%0/1
$options{'auth'} || ''
$options{'auth'}
MC/DC0%0/1
$options{'auth'} || ''
$options{'auth'}
Truth table50%1/2
A
A= $options{'auth'}
Aresult
11
00
Truth table0%0/2
A
A= $options{'auth'}
Aresult
11
00
1292 0 ▶ !exists $options{tokens} or die "Cannot combine auth => none with tokens\n";
Branch: 2 branches
BranchTrueFalseTotal
exists $options{"tokens"} and die "Cannot combine auth => none with tokens\n" 0 0 0
exists $options{"tokens"} and die "Cannot combine auth => none with tokens\n" 0 0 0
1293 }
1294 0 ▶ my $selected_bind = defined $bind ? $bind : ($settings{bind} // $config{bind});
Branch: 2 branches
BranchTrueFalseTotal
defined $bind ? : 3 3 6
defined $bind ? : 0 0 0
Condition16%1/6
$settings{"bind"} // $config{"bind"}
l!l&&r!l&&!r
003
$settings{"bind"} // $config{"bind"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$settings{"bind"} // $config{"bind"}
$settings{"bind"} $config{"bind"}
MC/DC0%0/2
$settings{"bind"} // $config{"bind"}
$settings{"bind"} $config{"bind"}
Truth table33%1/3
A // B
A= $settings{"bind"}B= $config{"bind"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $settings{"bind"}B= $config{"bind"}
ABresult
1X1
011
000
1295 0 my %http = ($global->http_settings ($name, $selected_bind, $options{default_port}), %options);
1296 my @schemes = defined $http{tls_cert} || defined $http{tls_key}
1297 0 ▶ ? ("https", "http") : ($http{url_scheme} || "http");
Branch: 2 branches
BranchTrueFalseTotal
defined $http{'tls_cert'} || defined $http{'tls_key'} ? : 0 6 6
defined $http{'tls_cert'} || defined $http{'tls_key'} ? : 0 0 0
Condition20%2/10
defined $http{"tls_cert"} or defined $http{"tls_key"}
l!l&&r!l&&!r
006
$http{"url_scheme"} or "http"
l!l
06
defined $http{"tls_cert"} or defined $http{"tls_key"}
l!l&&r!l&&!r
000
$http{"url_scheme"} or "http"
l!l
00
MC/DC0%0/2
defined $http{"tls_cert"} or defined $http{"tls_key"}
defined $http{"tls_cert"} defined $http{"tls_key"}
MC/DC0%0/1
$http{"url_scheme"} or "http"
$http{"url_scheme"}
MC/DC0%0/2
defined $http{"tls_cert"} or defined $http{"tls_key"}
defined $http{"tls_cert"} defined $http{"tls_key"}
MC/DC0%0/1
$http{"url_scheme"} or "http"
$http{"url_scheme"}
Truth table33%1/3
A or B
A= defined $http{"tls_cert"}B= defined $http{"tls_key"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $http{"url_scheme"}
Aresult
11
00
Truth table0%0/3
A or B
A= defined $http{"tls_cert"}B= defined $http{"tls_key"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $http{"url_scheme"}
Aresult
11
00
1298 my $managed = ($http{auth} || "") ne "none"
1299 && !exists $options{tokens}
1300 && !exists $global->{section}{"instance $name"}{tokens}
1301 0 ▶ && !exists $global->{section}{"program " . AnyEvent::MCP::Config::program_name ()}{tokens};
Condition31%7/22
($http{'auth'} || '') ne 'none' && !exists $options{'tokens'} && !exists $global->{'section'}{"instance $name"}{'tokens'} && !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
!ll&&!rl&&r
105
($http{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $global->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
105
($http{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
015
$http{'auth'} || ''
l!l
06
($http{'auth'} || '') ne 'none' && !exists $options{'tokens'} && !exists $global->{'section'}{"instance $name"}{'tokens'} && !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
!ll&&!rl&&r
000
($http{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $global->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
000
($http{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
000
$http{'auth'} || ''
l!l
00
MC/DC0%0/4
($http{'auth'} || '') ne 'none' && !exists $options{'tokens'} && !exists $global->{'section'}{"instance $name"}{'tokens'} && !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
$http{'auth'} not exists $options{"tokens"} not exists $global->{"section"}{"instance $name"}{"tokens"} !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
MC/DC0%0/4
($http{'auth'} || '') ne 'none' && !exists $options{'tokens'} && !exists $global->{'section'}{"instance $name"}{'tokens'} && !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
$http{'auth'} not exists $options{"tokens"} not exists $global->{"section"}{"instance $name"}{"tokens"} !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
Truth table0%0/5
A and not(B) and not(C) && not(D)
A= $http{'auth'}B= not exists $options{"tokens"}C= not exists $global->{"section"}{"instance $name"}{"tokens"}D= !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and not(B) and not(C) && not(D)
A= $http{'auth'}B= not exists $options{"tokens"}C= not exists $global->{"section"}{"instance $name"}{"tokens"}D= !exists $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}{'tokens'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1302  
1303 0 ▶ if ($managed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($managed) { } 5 1 6
if ($managed) { } 0 0 0
1304 0 my $saved = (ref $global)->new (file => $global->{file})->load;
1305 $saved->{section}{global}{secret} eq $global->{section}{global}{secret}
1306 0 ▶ or die "MCP master secret is not persistently stored in $global->{file}\n";
Branch: 2 branches
BranchTrueFalseTotal
$saved->{"section"}{"global"}{"secret"} eq $global->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $global->{"file"}\n] 5 0 5
$saved->{"section"}{"global"}{"secret"} eq $global->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $global->{"file"}\n] 0 0 0
1307 0 my $credentials = $global->user_credentials ($name);
1308 0 ▶ if (length $print_url) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $print_url) 3 2 5
if (length $print_url) 0 0 0
1309 0 @$credentials = grep $_->[0] eq $print_url, @$credentials;
1310 0 ▶ @$credentials or die "User '$print_url' is not enabled\n";
Branch: 2 branches
BranchTrueFalseTotal
@$credentials or die "User '${print_url}' is not enabled\n" 2 1 3
@$credentials or die "User '${print_url}' is not enabled\n" 0 0 0
1311 }
1312 0 my $connect = $global->connect_user ($name);
1313 0 for my $entry (@$credentials) {
1314 0 my ($user, $principal, $token) = @$entry;
1315 0 for my $scheme (@schemes) {
1316 0 ▶ print $global->http_url ($name, $selected_bind, %options,
Branch: 2 branches
BranchTrueFalseTotal
$user eq $connect ? : 2 3 5
$user eq $connect ? : 0 0 0
1317 tokens => [$token], url_scheme => $scheme),
1318 " # user $principal", ($user eq $connect ? " (connect)" : ""), "\n";
1319 }
1320 }
1321 } else {
1322 0 ▶ length $print_url
Branch: 2 branches
BranchTrueFalseTotal
length $print_url and die "Cannot select a managed user with explicit tokens or auth=none\n" 0 1 1
length $print_url and die "Cannot select a managed user with explicit tokens or auth=none\n" 0 0 0
1323 and die "Cannot select a managed user with explicit tokens or auth=none\n";
1324 0 for my $scheme (@schemes) {
1325 0 print $global->http_url ($name, $selected_bind, %options, url_scheme => $scheme), "\n";
1326 }
1327 }
1328 0 ▶ $print_url_extra->(\%http) if $print_url_extra;
Branch: 2 branches
BranchTrueFalseTotal
if $print_url_extra 0 5 5
if $print_url_extra 0 0 0
1329 0 exit;
1330 }
1331  
1332 0 \%server
1333 }
1334  
1335 112 ▶sub _permission_options {
Subroutine _permission_options: called CC=6 SCAR=17.9
1336 112 my %kv = @_;
1337  
1338 112 my $rules = delete $kv{auto_rules};
1339 112 my $ask = delete $kv{ask};
1340 112 ▶ unless (exists $kv{permissions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $kv{"permissions"}) 12 100 112
1341 12 require AnyEvent::MCP::Server::Permissions;
1342 $kv{permissions} = AnyEvent::MCP::Server::Permissions->new (
1343 12 name => AnyEvent::MCP::Config::program_name () . "-" . AnyEvent::MCP::instance_name ($kv{name}),
1344 );
1345 }
1346 112 ▶ if (my $permissions = $kv{permissions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $permissions = $kv{"permissions"}) 61 51 112
1347 61 ▶ $permissions->{ask} = $ask if defined $ask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ask 2 59 61
1348 61 ▶ $permissions->auto_rules ($rules, 1) if $rules && @$rules;
Branch: 1 branch
BranchTrueFalseTotal
if $rules and @$rules 7 54 61
Condition66%2/3
$rules and @$rules
!ll&&!rl&&r
5407
MC/DC50%1/2
$rules and @$rules
$rules @$rules
Truth table66%2/3
A and B
A= $rulesB= @$rules
ABresult
0X0
100
111
1349 }
1350  
1351 %kv
1352 112 }
1353  
1354 62 ▶sub _server_options {
Subroutine _server_options: called CC=6 SCAR=17.9
1355 62 my %kv = @_;
1356  
1357 62 ▶ if (exists $kv{server_name} || exists $kv{server_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $kv{"server_name"} or exists $kv{"server_version"}) 33 29 62
Condition100%3/3
exists $kv{"server_name"} or exists $kv{"server_version"}
l!l&&r!l&&!r
32129
MC/DC100%2/2
exists $kv{"server_name"} or exists $kv{"server_version"}
exists $kv{"server_name"} exists $kv{"server_version"}
Truth table100%3/3
A or B
A= exists $kv{"server_name"}B= exists $kv{"server_version"}
ABresult
1X1
011
000
1358 !exists $kv{server_info}
1359 33 ▶ or die "Cannot combine server_info with server_name/server_version\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{"server_info"} and die "Cannot combine server_info with server_name/server_version\n" 1 32 33
1360  
1361 32 my $name = delete $kv{server_name};
1362 32 my $version = delete $kv{server_version};
1363 $kv{server_info} = {
1364 32 ▶ name => defined $name ? $name : "AnyEvent::MCP",
Branch: 2 branches
BranchTrueFalseTotal
defined $name ? : 31 1 32
defined $version ? : 32 0 32
1365 version => defined $version ? "$version" : "$AnyEvent::MCP::VERSION",
1366 };
1367 }
1368  
1369 %kv
1370 61 }
1371  
1372 53 ▶sub mcp_start {
Subroutine mcp_start: called CC=25 SCAR=36.3
Pod documented
1373 53 my %kv = (%config, @_);
1374 53 ▶ mcp_tool_help (delete $kv{tool_help}, @{$kv{toolsets} || [$toolset]});
Condition100%2/2
$kv{'toolsets'} || [$toolset]
l!l
2132
MC/DC100%1/1
$kv{'toolsets'} || [$toolset]
$kv{'toolsets'}
Truth table100%2/2
A
A= $kv{'toolsets'}
Aresult
11
00
1375 51 my $name = AnyEvent::MCP::instance_name ($kv{name});
1376 51 my $config_file = AnyEvent::MCP::Config::_file ();
1377 51 ▶ my $global = -e $config_file ? AnyEvent::MCP::Config::global_config : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $config_file ? : 38 13 51
1378 my %ini = $global ? (
1379 38 ▶ %{$global->{section}{"program " . AnyEvent::MCP::Config::program_name ()} || {}},
Condition50%1/2
$global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()} || {}
l!l
038
MC/DC0%0/1
$global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()} || {}
$global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}
Truth table50%1/2
A
A= $global->{'section'}{'program ' . AnyEvent::MCP::Config::program_name()}
Aresult
11
00
1380 51 ▶ %{$global->{section}{"instance $name"} || {}},
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 38 13 51
Condition100%2/2
$global->{'section'}{"instance $name"} || {}
l!l
731
MC/DC100%1/1
$global->{'section'}{"instance $name"} || {}
$global->{'section'}{"instance $name"}
Truth table100%2/2
A
A= $global->{'section'}{"instance $name"}
Aresult
11
00
1381 ) : ();
1382 51 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) 38 13 51
1383 38 my @rules = $global->auto_rules ($name);
1384 0 ▶ $kv{auto_rules} = [@{$kv{auto_rules} || []}, @rules] if @rules;
Branch: 1 branch
BranchTrueFalseTotal
if @rules 0 38 38
Condition0%0/2
$kv{'auto_rules'} || []
l!l
00
MC/DC0%0/1
$kv{'auto_rules'} || []
$kv{'auto_rules'}
Truth table0%0/2
A
A= $kv{'auto_rules'}
Aresult
11
00
1385 }
1386 $kv{custom_instructions} = $ini{custom_instructions}
1387 51 ▶ if !exists $kv{custom_instructions} && exists $ini{custom_instructions};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $kv{"custom_instructions"} and exists $ini{"custom_instructions"} 0 51 51
Condition33%1/3
not exists $kv{"custom_instructions"} and exists $ini{"custom_instructions"}
!ll&&!rl&&r
0510
MC/DC0%0/2
not exists $kv{"custom_instructions"} and exists $ini{"custom_instructions"}
not exists $kv{"custom_instructions"} exists $ini{"custom_instructions"}
Truth table33%1/3
not(A) and B
A= not exists $kv{"custom_instructions"}B= exists $ini{"custom_instructions"}
ABresult
0X0
100
111
1388 51 %kv = _permission_options (%kv);
1389 51 my $stdio = delete $kv{stdio};
1390 51 my $listen = delete $kv{listen};
1391 51 my $connect = delete $kv{connect};
1392 51 ▶ !($stdio && defined $connect) or die "Cannot combine --stdio and --connect\n";
Branch: 1 branch
BranchTrueFalseTotal
($stdio and defined $connect) and die "Cannot combine --stdio and --connect\n" 0 51 51
Condition66%2/3
$stdio and defined $connect
!ll&&!rl&&r
4380
MC/DC0%0/2
$stdio and defined $connect
$stdio defined $connect
Truth table66%2/3
A and B
A= $stdioB= defined $connect
ABresult
0X0
100
111
1393 51 ▶ $stdio ||= !defined $connect && $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio";
Condition83%5/6
$stdio ||= !defined($connect) && $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
l!l&&r!l&&!r
8394
!defined($connect) && $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
!ll&&!rl&&r
0439
MC/DC0%0/3
$stdio ||= !defined($connect) && $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio"
$stdio !defined($connect) $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
Truth table0%0/4
A ||= not(B) && C
A= $stdioB= !defined($connect)C= $ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL'} eq 'stdio'
ABCresult
1XX1
0111
00X0
0100
1394  
1395 51 $kv{name} = $name;
1396  
1397 51 ▶ if (!$listen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listen) 51 0 51
1398 51 ▶ return mcp_connect (%kv, connect => $connect) if defined $connect;
Branch: 1 branch
BranchTrueFalseTotal
if defined $connect 0 51 51
1399 51 ▶ return mcp_stdio (%kv) if $stdio;
Branch: 1 branch
BranchTrueFalseTotal
if $stdio 47 4 51
1400 }
1401  
1402 4 ▶ my %http = ($global ||= AnyEvent::MCP::Config::global_config)->http_settings ($name, delete $kv{bind});
Condition66%2/3
$global ||= AnyEvent::MCP::Config::global_config()
l!l&&r!l&&!r
310
MC/DC0%0/2
$global ||= AnyEvent::MCP::Config::global_config()
$global AnyEvent::MCP::Config::global_config()
Truth table66%2/3
A ||= B
A= $globalB= AnyEvent::MCP::Config::global_config()
ABresult
1X1
011
000
1403 4 ▶ delete @http{qw(tokens connect_tokens token_users)} if ($kv{auth} || "") eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if ($kv{"auth"} || "") eq "none" 1 3 4
Condition100%2/2
$kv{'auth'} || ''
l!l
13
MC/DC100%1/1
$kv{'auth'} || ''
$kv{'auth'}
Truth table100%2/2
A
A= $kv{'auth'}
Aresult
11
00
1404 4 my %options = (path => "/", %http, %kv);
1405 4 ▶ if (exists $kv{tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $kv{"tokens"}) 1 3 4
1406 1 $options{connect_tokens} = $kv{tokens};
1407 1 delete $options{token_users};
1408 }
1409  
1410 4 ▶ if ($stdio || defined $connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdio or defined $connect) 0 4 4
Condition33%1/3
$stdio or defined $connect
l!l&&r!l&&!r
004
MC/DC0%0/2
$stdio or defined $connect
$stdio defined $connect
Truth table33%1/3
A or B
A= $stdioB= defined $connect
ABresult
1X1
011
000
1411 0 require AnyEvent::MCP::Server::HTTP;
1412 0 my $listener = AnyEvent::MCP::Server::HTTP->new (toolsets => [$toolset], _server_options (%options));
1413 0 $listener->listen;
1414 0 ▶ my $result = defined $connect
Branch: 1 branch
BranchTrueFalseTotal
defined $connect ? : 0 0 0
1415 ? mcp_connect (%kv, connect => $connect)
1416 : mcp_stdio (%kv);
1417 0 $listener->close;
1418 0 return $result
1419 }
1420  
1421 4 mcp_http (%options)
1422 }
1423  
1424 0 ▶sub mcp_connect {
Subroutine mcp_connect: not called CC=2 SCAR=17.9
Pod documented
1425 0 require AnyEvent::MCP::Server::Connect;
1426  
1427 0 my %kv = _server_options (%config, @_);
1428 0 ▶ mcp_tool_help (delete $kv{tool_help}, @{$kv{toolsets} || [$toolset]});
Condition0%0/2
$kv{'toolsets'} || [$toolset]
l!l
00
MC/DC0%0/1
$kv{'toolsets'} || [$toolset]
$kv{'toolsets'}
Truth table0%0/2
A
A= $kv{'toolsets'}
Aresult
11
00
1429 0 my $server = AnyEvent::MCP::Server::Connect->new (toolsets => [$toolset], _permission_options (%kv));
1430 0 $server->start;
1431 0 _run ($server, 1)
1432 }
1433  
1434 48 ▶sub mcp_stdio {
Subroutine mcp_stdio: called CC=6 SCAR=20.1
Pod documented
1435 48 require AnyEvent::MCP::Server::JSONL;
1436  
1437 48 my %kv = _permission_options (_server_options (%config, @_));
1438 48 ▶ mcp_tool_help (delete $kv{tool_help}, @{$kv{toolsets} || [$toolset]});
Condition100%2/2
$kv{'toolsets'} || [$toolset]
l!l
1929
MC/DC100%1/1
$kv{'toolsets'} || [$toolset]
$kv{'toolsets'}
Truth table100%2/2
A
A= $kv{'toolsets'}
Aresult
11
00
1439 48 my $idle_exit = delete $kv{idle_exit};
1440 48 binmode STDIN;
1441 48 binmode STDOUT;
1442 48 my $selected = select STDOUT;
1443 48 $| = 1;
1444 48 select $selected;
1445  
1446 48 my $server = AnyEvent::MCP::Server::JSONL->new (toolsets => [$toolset], %kv);
1447 48 $server->accept (\*STDIN, \*STDOUT);
1448  
1449 48 ▶ if ($idle_exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idle_exit) 9 39 48
1450 9 ▶ $idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/ && $idle_exit > 0
Branch: 1 branch
BranchTrueFalseTotal
$idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/u and $idle_exit > 0 or die "invalid idle_exit seconds\n" 9 0 9
Condition33%1/3
$idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/u and $idle_exit > 0
!ll&&!rl&&r
009
MC/DC0%0/2
$idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/u and $idle_exit > 0
$idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/u $idle_exit > 0
Truth table33%1/3
A and B
A= $idle_exit =~ /\A[0-9]+(?:\.[0-9]+)?\z/uB= $idle_exit > 0
ABresult
0X0
100
111
1451 or die "invalid idle_exit seconds\n";
1452 9 ▶ if ($ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio") 1 8 9
1453 1 my ($timer, $check);
1454 0 ▶ $check = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1455 0 ▶ return if $server->{idle_exit_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $server->{"idle_exit_requested"} 0 0 0
1456 0 ▶ if (grep $_->{mcp_active}, values %{$server->{connections} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep $_->{'mcp_active'}, values %{$server->{'connections'} or {};}) { } 0 0 0
Condition0%0/2
$server->{'connections'} || {}
l!l
00
MC/DC0%0/1
$server->{'connections'} || {}
$server->{'connections'}
Truth table0%0/2
A
A= $server->{'connections'}
Aresult
11
00
1457 0 $timer = AnyEvent->timer (after => $idle_exit, cb => $check);
1458 } else {
1459 0 $server->request_idle_exit;
1460 }
1461 1 };
1462 21 ▶ my $schedule = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1463 21 $timer = AnyEvent->timer (after => $idle_exit, cb => $check);
1464 1 };
1465 1 my $on_idle_shutdown = $server->{on_idle_shutdown};
1466 0 ▶ $server->{on_idle_shutdown} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1467 0 ▶ my $allowed = !$on_idle_shutdown || $on_idle_shutdown->(@_);
Condition0%0/3
!$on_idle_shutdown || &$on_idle_shutdown(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
!$on_idle_shutdown || &$on_idle_shutdown(@_)
!$on_idle_shutdown &$on_idle_shutdown(@_)
Truth table0%0/3
not(A) || B
A= !$on_idle_shutdownB= &$on_idle_shutdown(@_)
ABresult
1X1
011
000
1468 0 ▶ $schedule->() unless $allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed 0 0 0
1469 0 $allowed
1470 1 };
1471 1 my $on_request = $server->{on_request};
1472 10 ▶ $server->{on_request} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
1473 10 my ($server, $req) = @_;
1474  
1475 10 ▶ if ($req->{method} ne "anyevent-mcp/shutdown") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"method"} ne "anyevent-mcp/shutdown") 10 0 10
1476 10 $server->{idle_exit_requested} = 0;
1477 10 $schedule->();
1478 10 my $reply = $req->{on_reply};
1479 10 ▶ $req->{on_reply} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1480 10 $reply->(@_);
1481 10 ▶ $schedule->() unless $server->{_idle_shutting_down};
Branch: 1 branch
BranchTrueFalseTotal
unless $server->{"_idle_shutting_down"} 10 0 10
1482 10 };
1483 }
1484 10 ▶ $on_request->(@_) if $on_request;
Branch: 1 branch
BranchTrueFalseTotal
if $on_request 10 0 10
1485 1 };
1486 1 my $on_close = $server->{on_close};
1487 1 ▶ $server->{on_close} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1488 1 undef $timer;
1489 1 ▶ $on_close->(@_) if $on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_close 0 1 1
1490 1 };
1491 1 $schedule->();
1492 }
1493 }
1494 48 _run $server
1495 }
1496  
1497 14 ▶sub mcp_http {
Subroutine mcp_http: called CC=2 SCAR=6.9
Pod documented
1498 14 require AnyEvent::MCP::Server::HTTP;
1499  
1500 14 my %kv = _permission_options (_server_options (%config, @_));
1501 13 ▶ mcp_tool_help (delete $kv{tool_help}, @{$kv{toolsets} || [$toolset]});
Condition100%2/2
$kv{'toolsets'} || [$toolset]
l!l
211
MC/DC100%1/1
$kv{'toolsets'} || [$toolset]
$kv{'toolsets'}
Truth table100%2/2
A
A= $kv{'toolsets'}
Aresult
11
00
1502 13 my $server = AnyEvent::MCP::Server::HTTP->new (toolsets => [$toolset], %kv);
1503 13 $server->listen;
1504 13 _run $server
1505 }
1506  
1507 61 ▶sub _run {
Subroutine _run: called CC=4 SCAR=13.9
1508 61 my ($server, $keep_alive) = @_;
1509  
1510 61 my $cv = AnyEvent->condvar;
1511 61 my $stopped;
1512 61 ▶ my $stop = sub { $cv->send unless $stopped++ };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $stopped++ 59 46 105
1513 61 my $on_close = $server->{on_close};
1514 59 ▶ $server->{on_close} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1515 59 ▶ $on_close->(@_) if $on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_close 2 57 59
1516 59 $stop->();
1517 61 };
1518  
1519 61 ▶ if (!$keep_alive && $server->isa ("AnyEvent::MCP::Server::JSONL")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $keep_alive and $server->isa("AnyEvent::MCP::Server::JSONL")) 48 13 61
Condition66%2/3
not $keep_alive and $server->isa("AnyEvent::MCP::Server::JSONL")
!ll&&!rl&&r
01348
MC/DC50%1/2
not $keep_alive and $server->isa("AnyEvent::MCP::Server::JSONL")
not $keep_alive $server->isa("AnyEvent::MCP::Server::JSONL")
Truth table66%2/3
not(A) and B
A= not $keep_aliveB= $server->isa("AnyEvent::MCP::Server::JSONL")
ABresult
0X0
100
111
1520 48 my $on_connection_close = $server->{on_connection_close};
1521 46 ▶ $server->{on_connection_close} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1522 46 ▶ $on_connection_close->(@_) if $on_connection_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_connection_close 0 46 46
1523 46 $stop->();
1524 48 };
1525 }
1526  
1527 my @signals = map {
1528 61 ▶ AnyEvent->signal (signal => $_, cb => sub { $server->close })
Subroutine __ANON__: called CC=1 SCAR=0.0
1529 122 } qw(INT TERM);
1530  
1531 61 my $ok = eval {
1532 61 $server->event (start =>);
1533 60 $cv->recv;
1534 58 1
1535 };
1536 59 my $error = $@;
1537 59 $server->close;
1538 59 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 58 59
1539 return
1540 58 }
1541  
1542 75 1
« blib/lib/AnyEvent/MCP/IMAP/Providers.pm ↑ Summary bin/anyevent-mcp-form-elicitation »