bin/mcp-multibroker

filter: Statementstmt 77.2% 254 / 329 Branchbran 49.5% 112 / 226 Conditioncond 41.7% 58 / 139 MC/DCmcdc 26.6% 20 / 75 Subroutinesub 71.7% 38 / 53 Podpod n/a totaltotal 58.6% 482 / 822 CC 148 SCAR 32.1
CC
148
Cov
61%
__ANON__
90.0
on_child_close
90.0
on_child_notification
57.9
CRAP 24.9
« bin/mcp-http ↑ Summary bin/mcp-vim »
1 #!/opt/bin/perl
2  
3 =head1 NAME
4  
5 mcp-multibroker - combine MCP servers under tool name prefixes
6  
7 =head1 SYNOPSIS
8  
9 mcp-multibroker files 'stdio:mcp-fs /home/alice/src/project' \
10 search https://example.org/mcp/ --bearer TOKEN
11 mcp-multibroker vim --- mcp-vim --bind :: --- --required
12  
13 =head1 DESCRIPTION
14  
15 C<mcp-multibroker> combines several MCP servers behind one connection to
16 your MCP client. You can give an assistant access to filesystem tools,
17 Vim editors and other services by configuring them here, then connecting
18 the client to this broker.
19  
20 For each server, give a name prefix and an HTTP URL, a C<stdio:COMMAND>,
21 or C<--- PROGRAM ARG... ---> to start it as a subprocess. These are the downstream
22 servers. The multibroker starts or connects to them, obtains their tool
23 definitions and forwards tool calls and replies between them and your
24 client.
25  
26 The prefix identifies which server a tool belongs to. For example, a
27 filesystem server configured as C<files> has a tool named
28 C<files.read-lines>. Your client can ask the multibroker which servers and
29 tools are available, then call a tool by its prefixed name.
30  
31 The connection from your MCP client to the multibroker is the upstream
32 connection. It can use HTTP or stdin/stdout, independently of how the
33 downstream servers connect. See L<AnyEvent::MCP::Intro> for a complete
34 setup example.
35  
36 =head1 FIRST USE
37  
38 Configure a local MCP client to start a multibroker with two servers:
39  
40 mcp-multibroker --stdio \
41 files --- mcp-fs --name project /home/alice/src/project --- \
42 questions --- mcp-ask-user ---
43  
44 The client launches only the broker; it starts children as needed. Ask:
45  
46 Call discover-brokers and list-tools, then use files.workdirs
47 to show which project directories are configured.
48  
49 C<files> and C<questions> are routing prefixes, not child instance names
50 or permission profiles. Each child still applies its own permissions.
51 The broker selects stdio automatically for bundled AnyEvent::MCP children.
52 Third-party servers must support MCP over stdio in their own startup mode.
53 You can replace a child with any stdio-speaking MCP server by putting
54 its executable and arguments between the C<---> delimiters, or connect
55 an existing HTTP server by URL; see L</DOWNSTREAM SERVERS>.
56  
57 For HTTP clients, omit C<--stdio>, leave the broker running, and obtain
58 its upstream URL with C<--print-url> using the same listener settings.
59 For missing tools, try C<discover-brokers> for connection errors and
60 C<list-tools> to refresh discovery; your MCP client may also need to
61 refresh its cached list. C<--tool-help> only shows the broker's own
62 tools, not those of its children. Ordinary children start lazily. Use C<--eager>
63 after the child specification for services that accept independent
64 connections, such as Vim or browser brokers.
65  
66 =head1 TOOLS
67  
68 The broker exposes three tools of its own:
69  
70 =over
71  
72 =item discover-brokers
73  
74 Lists the configured downstream servers and their instructions by prefix.
75 An entry is a string of instructions, C<null> for a reachable server with
76 no instructions, or C<{error: MESSAGE}> for an unreachable server.
77 Hidden servers are listed, with instructions on how to discover their tools.
78  
79 =item list-tools
80  
81 Queries downstream servers for their tool definitions and refreshes the
82 combined list. An optional C<broker> argument selects one exact downstream
83 prefix, including hidden servers and excluding the multibroker's own tools.
84 Without C<broker>, hidden servers are omitted. Unknown broker names or
85 arguments are errors. Unreachable servers are omitted from tool listings
86 but reported by C<discover-brokers>.
87  
88 =item call-tool
89  
90 Calls a downstream tool by its C<PREFIX.TOOL> name with the supplied
91 arguments, including hidden servers. Calls to an unavailable server fail
92 as tool errors; calls are
93 not replayed after transport failures.
94  
95 =back
96  
97 A refreshed list is a snapshot of what is reachable at that point. It
98 includes configured servers and currently registered reverse connections.
99 A listed server might become unavailable before a later call.
100  
101 =head1 DOWNSTREAM SERVERS
102  
103 Each prefix identifies one downstream server. HTTP URLs may include a URL-path token,
104 or be followed by C<--bearer TOKEN>. C<bearer> and C<timeout> defaults
105 are read from C<[client PREFIX]> in the global configuration; the URL in
106 the command line overrides any configured C<url>. Stdio children receive
107 C<PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL=stdio> and
108 C<PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS=PREFIX> in their environment.
109 The alias may be displayed in downstream permission prompts, without changing
110 the server's instance name or permission rules.
111  
112 For direct execution, use C<PREFIX --- PROGRAM ARG... --->:
113  
114 mcp-multibroker vim --- mcp-vim --bind :: --- --eager \
115 files --- mcp-fs '/home/alice/project with spaces' ---
116  
117 The next exact C<---> argument closes the list. Everything inside, including
118 C<-->, C<--eager> and C<--bearer>, is passed unchanged to the child. There is
119 no shell interpretation. Empty arguments are preserved; the program must be
120 nonempty. An empty list or missing closing delimiter is an error.
121  
122 A subprocess can also connect to an MCP server on another machine via SSH:
123  
124 mcp-multibroker remote-fs --- ssh remotehost mcp-fs --stdio --ask mcp /workdir ---
125  
126 C<--stdio> tells the remote C<mcp-fs> to use stdin/stdout for MCP. It must be
127 specified explicitly because the multibroker's environment variables are
128 not passed through SSH. C<--ask mcp> forwards permission prompts over MCP to
129 the local multibroker, instead of trying to display dialogs on the remote
130 host. The local broker then presents them according to its own C<--ask> mode
131 (normally using a local helper); see L</PERMISSION PROMPTS>.
132  
133 C<stdio:COMMAND> retains shell interpretation. Use it for shell syntax or
134 when a child needs a literal C<---> argument. Quoting C<'---'> in the
135 delimited form cannot escape the delimiter: shell quoting is already gone.
136  
137 Broker-wide options precede the first server entry. Downstream options
138 follow that server's URL or closing C<--->; Getopt::Long stops at the next
139 prefix. Options inside an argv list belong exclusively to the child.
140  
141 C<--hidden> after a server keeps its tools out of normal MCP C<tools/list>
142 and unfiltered C<list-tools> results. This reduces the initial tool list
143 for large or rarely used servers. C<discover-brokers> still lists it;
144 C<list-tools broker=PREFIX>
145 returns its definitions, and C<call-tool> still works. Hiding reduces the
146 advertised tool set, not the permissions or availability of the server.
147  
148 C<--eager> after either stdio form starts that child at
149 multibroker startup rather than waiting for its first MCP use. This is useful
150 for children that also provide their own TCP service, such as C<mcp-vim>.
151 Other stdio children remain lazy. Eager startup attempts one connection and
152 logs failures; it does not continually restart failed children.
153 C<--required> implies C<--eager> and makes the multibroker exit with failure
154 if that child cannot initialize or later disconnects unexpectedly. A supervisor
155 can then restart the broker. An explicitly accepted managed idle shutdown is
156 not a failure; the child can be started again on the next use. Both options
157 apply only to spawned stdio children, never remote HTTP peers.
158  
159 =head2 Reverse connections
160  
161 The HTTP listener also accepts authenticated reverse MCP connections. It
162 can run with no configured downstream servers. With upstream stdio, add
163 C<--listen> to keep this listener active:
164  
165 mcp-multibroker --stdio --listen --bind 127.0.0.1:31740
166 mcp-multibroker --bind 127.0.0.1:31740 --print-url
167 mcp-fs --connect http://127.0.0.1:31740/TOKEN/files /home/alice/project
168  
169 Use the token returned by C<--print-url>. The connecting server extracts
170 the host, port, token and alias from this URL, sends
171 C<["mcp-connect", token, alias]> followed by a newline, then ordinary MCP
172 JSONL. The connection uses the HTTP listener but is not an HTTP request.
173 With C<--tls-cert> or C<--tls-key>, the same port also accepts TLS, and
174 C<--print-url> prints HTTPS first and HTTP second. Reverse connections using
175 C<https://> verify the listener certificate and hostname; C<http://> uses
176 plaintext.
177 It uses the same accepted tokens as HTTP. Authentication grants registration
178 under any free alias; configured or connected aliases cannot be replaced.
179  
180 The connecting program remains the MCP server. The multibroker attaches an
181 MCP client and discovers its tools immediately. Forms and cancellation follow
182 the same routes as other downstream connections. An unexpected disconnect
183 fails pending calls, removes the registration and starts a new connection
184 period. Refused TCP connections are retried every second, up to sixty times.
185 Other TCP setup failures and TLS verification failures log an error and stop;
186 there is no automatic downgrade to plaintext. No interrupted operation is
187 replayed. Plain HTTP endpoint URLs do not provide transport encryption;
188 use a trusted connection or tunnel as needed.
189  
190 =head2 Process lifetime
191  
192 Each downstream connection advertises C<anyevent-mcp/multibroker> version 1
193 in its client capabilities. A spawned stdio child can request managed idle
194 shutdown with C<anyevent-mcp/idle-exit>. When no calls are outstanding, the
195 broker may send C<anyevent-mcp/shutdown>; the child acknowledges before
196 exiting. This is optional and never makes HTTP children exit. Unexpected
197 disconnects are logged, not proactively restarted. A later use reconnects.
198  
199 =head1 PERMISSION PROMPTS
200  
201 C<--ask helper> (the default) answers downstream form challenges locally.
202 Ordinary forms use C<anyevent-mcp-form-elicitation>; permission requests keep their
203 specialised permission dialogs. C<--ask mcp> forwards modern challenges to
204 the upstream client. C<--ask auto> uses upstream form support when advertised,
205 otherwise the local helper. Both modes deliberately trust the upstream
206 client's user assertion when permission authority has been delegated to it.
207 The multibroker forwards the original authenticated user with every tool call,
208 even when a downstream saved permission rule resolves that request without
209 another prompt. A decline or cancel is never retried through another
210 interface. If a client advertises forms but silently declines them,
211 select C<helper> explicitly.
212  
213 C<--ask tty> and C<--ask yad> select only the built-in terminal or YAD
214 interface for both ordinary forms and permission dialogs. These modes
215 ignore custom helper variables and do not fall back to another interface.
216  
217 The broker advertises its local form support to downstream calls when it
218 will handle their forms itself. In C<mcp> mode it forwards the upstream
219 caller's capabilities instead. Local forms work for HTTP and stdio children,
220 including when the upstream caller cannot handle native input challenges.
221 Continuation tokens remain opaque; the broker sends the human response back
222 to the same downstream operation and returns its eventual result.
223  
224 Spawned stdio children default to C<--ask mcp>, delegating permissions to
225 the multibroker, which handles them according to its own C<--ask> mode.
226 An explicit child C<--ask> overrides this default. Independently running
227 servers keep their own configuration.
228 Spawned C<mcp-ask-user> instances normally delegate presentation to this broker.
229 Use C<--ask mcp> on a separately started C<mcp-ask-user> to delegate to its
230 connecting client, including a multibroker.
231  
232 Local permission and question dialogs share one queue. Set
233 C<PERL_ANYEVENT_MCP_ELICITATION_HELPER> to replace the form helper and
234 C<PERL_ANYEVENT_MCP_PERMISSION_HELPER> to replace the permission helper.
235 Cancelling a tool call cancels its downstream operation and pending local
236 prompt.
237 Downstream C<notifications/progress> are forwarded to the corresponding
238 upstream request by its progress token, including through HTTP children.
239  
240 =head1 CLIENT CONNECTION
241  
242 The upstream listener uses L<AnyEvent::MCP::Server::Simple/mcp_start>: HTTP
243 without stdout output by default, or stdio when run by another broker.
244 The service instance name defaults to C<default>. Repeated
245 C<--token TOKEN> options before the broker pairs configure credentials
246 accepted by the upstream HTTP listener. They are separate from C<--bearer>
247 after a downstream broker URL, which authenticates the client connection.
248 C<--name NAME>
249 selects C<[instance NAME]> in the global configuration, including its
250 credentials and optional C<bind> address. C<--bind ADDRESS[:PORT]> overrides
251 that address. The default is IPv4 loopback with a stable name-derived port
252 in 16384..32767. Port zero requests an ephemeral port.
253 C<--print-url> prints the credential-bearing URL without opening a listener
254 or starting children; it requires a nonzero port. Upstream stdio startup
255 skips HTTP listener configuration unless C<--listen> is specified; named HTTP
256 children still load their client defaults. Browser origins are unrestricted by default; HTTP requests
257 still require an accepted token. See L<AnyEvent::MCP::Intro> for examples.
258  
259 =head1 OPTIONS
260  
261 Shared upstream switches are documented in
262 L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>.
263 Put broker-wide options before the first PREFIX/URL pair. Downstream
264 options must immediately follow the server they configure.
265  
266 =head2 Broker options
267  
268 =over
269  
270 =item --listen
271  
272 Keep the upstream HTTP/extension listener active alongside C<--stdio>
273 or C<--connect>. Without those, HTTP is already the default.
274 Applies to the broker, not its downstream servers.
275  
276 =item --ask MODE
277  
278 Select how downstream permission prompts are presented:
279 C<helper> (default), C<tty>, C<yad>, C<mcp>, C<auto>, or C<none>.
280 C<none> replies with a standard decline without invoking any UI.
281 Spawned stdio children delegate to the broker with their own default C<mcp>.
282 See L</PERMISSION PROMPTS>.
283  
284 =back
285  
286 =head2 Downstream options
287  
288 =over
289  
290 =item --bearer TOKEN
291  
292 Authenticate the preceding HTTP(S) child with a bearer token, not
293 the broker's upstream listener. Supply after the child URL;
294 invalid for stdio children.
295  
296 =item --hidden
297  
298 Omit the preceding child's tools from ordinary tool discovery,
299 including unfiltered C<list-tools>. Discover its definitions with
300 C<list-tools> specifying its C<broker> prefix. Its tools remain
301 callable by name. Applies to HTTP and stdio children.
302  
303 =item --eager
304  
305 Start the preceding stdio child at broker startup instead of on
306 first use. Not available for HTTP children.
307  
308 =item --required
309  
310 Require the preceding stdio child to initialize and remain connected.
311 Implies C<--eager>; an unexpected disconnect causes the broker to exit.
312 Managed idle shutdown is permitted. Not available for HTTP children.
313  
314 =back
315  
316 =head2 Shared-option behavior
317  
318 =over
319  
320 =item --name NAME
321  
322 Select the upstream broker's instance (default C<default>).
323 Does not affect downstream prefixes or their instance names.
324  
325 =item --token TOKEN
326  
327 Authenticate clients of the upstream broker. To authenticate an
328 HTTP(S) child, use C<--bearer> after its URL instead.
329  
330 =item --tool-help
331  
332 Lists only the broker's own tools. Use its C<list-tools> tool to
333 discover individual downstream registries.
334  
335 =item --auto-allow TOOL[,ACTION[,CAPABILITY]]
336  
337 Matches the broker-visible tool names, not a downstream server's
338 independent permission checks.
339  
340 =back
341  
342 =cut
343  
344 27 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
345 27 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
346  
347 27 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
348 27 ▶use AnyEvent::MCP::Client::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
349 27 ▶use AnyEvent::MCP::Client::JSONL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
350 27 ▶use AnyEvent::MCP::Permissions::Ask ();
Subroutine BEGIN: called CC=1 SCAR=0.0
351 27 ▶use AnyEvent::MCP::Helper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
352 27 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
353 27 ▶use AnyEvent::MCP::Server::Toolset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
354  
355 27 ▶use AnyEvent::MCP::Server::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
356  
357 27 AnyEvent::detect; # initialise child watchers before starting subprocesses.
358 27 $| = 1;
359  
360 27 my $usage = "Usage: $0 [OPTION]... PREFIX (URL | --- PROGRAM ARG... ---) [CHILD-OPTION] ...\n";
361 27 Getopt::Long::Configure ("require_order");
362 27 my $server_options = mcp_getoptions
363 {
364 usage => $usage,
365 path => "/",
366 description => "Combine MCP servers behind one endpoint, with tools named PREFIX.TOOL.\n"
367 . "Each server is HTTP(S), stdio:COMMAND, or --- PROGRAM ARG... ---; downstream options follow its entry.",
368 options => [
369 "Broker-wide options:",
370 ["--listen", "Keep the upstream HTTP listener active alongside stdio or --connect"],
371 "Downstream options (after the URL or closing ---):",
372 ["--eager", "Start the preceding stdio child at broker startup"],
373 ["--required", "Require stdio child; implies --eager, exit on failure"],
374 ["--bearer TOKEN", "Authenticate to the preceding HTTP child"],
375 ["--hidden", "Hide child's tools from default discovery; explicit list-tools and calls still work"],
376 ],
377 examples => " $0 --ask auto files 'stdio:mcp-fs /home/alice/src/project'\n"
378 . " $0 files --- mcp-fs /home/alice/src/project --- --eager\n"
379 . " $0 vim --- mcp-vim --bind :: --- --required\n"
380 . " $0 remote https://example.org/mcp/ --bearer TOKEN\n"
381 . " $0 --name tools --print-url",
382 },
383 "listen" => \my $listen,
384 ;
385  
386 21 my $ask_mode = delete $server_options->{ask};
387  
388 $AnyEvent::Log::FILTER->level ("debug")
389 unless defined $ENV{PERL_ANYEVENT_VERBOSE}
390 21 ▶ || ($ENV{PERL_ANYEVENT_LOG} // "") =~ /(?:\A|\s)filter=/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"PERL_ANYEVENT_VERBOSE"} or ($ENV{"PERL_ANYEVENT_LOG"} // "") =~ /(?:\A|\s)filter=/u 1 20 21
Condition60%3/5
defined $ENV{"PERL_ANYEVENT_VERBOSE"} or ($ENV{"PERL_ANYEVENT_LOG"} // "") =~ /(?:\A|\s)filter=/u
l!l&&r!l&&!r
0201
$ENV{"PERL_ANYEVENT_LOG"} // ""
l!l
210
MC/DC0%0/2
defined $ENV{"PERL_ANYEVENT_VERBOSE"} or ($ENV{"PERL_ANYEVENT_LOG"} // "") =~ /(?:\A|\s)filter=/u
defined $ENV{"PERL_ANYEVENT_VERBOSE"} $ENV{"PERL_ANYEVENT_LOG"}
Truth table33%1/3
A or B
A= defined $ENV{"PERL_ANYEVENT_VERBOSE"}B= $ENV{"PERL_ANYEVENT_LOG"}
ABresult
1X1
011
000
391 21 ▶$ask_mode //= $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK} || "helper";
Condition100%4/4
$ask_mode //= $ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} || "helper"
l!l
138
$ENV{'PERL_ANYEVENT_MCP_PERMISSION_ASK'} || 'helper'
l!l
17
MC/DC100%1/1
$ask_mode //= $ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} || "helper"
$ask_mode
Truth table100%2/2
A
A= $ask_mode
Aresult
11
00
392 21 ▶$ask_mode =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/
Branch: 1 branch
BranchTrueFalseTotal
$ask_mode =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/u or die "--ask must be helper, tty, yad, auto, mcp, or none\n" 21 0 21
393 or die "--ask must be helper, tty, yad, auto, mcp, or none\n";
394  
395 21 my (%broker, %stdio, %eager, %required, %hidden, %progress);
396 21 my $shutting_down;
397 21 my @prompts;
398 21 my $prompt;
399  
400 # keep local dialogs serial, including dialogs from different brokers.
401 sub next_prompt;
402 5 ▶sub ask_prompt {
Subroutine ask_prompt: called CC=1 SCAR=2.4
403 5 my ($req, $prepared, $done, $form) = @_;
404  
405 5 my $entry = { request => $req, prepared => $prepared, done => $done, form => $form };
406 5 push @prompts, $entry;
407  
408 5 my $old_cancel = $req->{on_cancel};
409 0 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
410 AE::log trace => "local prompt cancelled "
411 0 ▶ . (defined $entry->{started} ? sprintf ("after %.2fs", AE::now - $entry->{started}) : "while queued")
Branch: 2 branches
BranchTrueFalseTotal
defined $entry->{'started'} ? : 0 0 0
defined $_[1] ? : 0 0 0
412 . (defined $_[1] ? ": $_[1]" : "");
413 0 ▶ $entry->{job}->cancel if $entry->{job};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"job"} 0 0 0
414 0 ▶ $old_cancel->(@_) if $old_cancel;
Branch: 1 branch
BranchTrueFalseTotal
if $old_cancel 0 0 0
415 5 };
416 5 next_prompt;
417 }
418  
419 10 ▶sub next_prompt {
Subroutine next_prompt: called CC=11 SCAR=27.6
420 10 ▶ return if $prompt;
Branch: 1 branch
BranchTrueFalseTotal
if $prompt 0 10 10
421 10 ▶ while (@prompts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prompts) 5 5 10
422 5 my $entry = shift @prompts;
423 5 ▶ next if $entry->{request}{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"request"}{"finished"} 0 5 5
424 5 $prompt = $entry;
425 $entry->{job} = $entry->{form}
426 ? AnyEvent::MCP::Helper->new (
427 helper => $ask_mode eq "tty" || $ask_mode eq "yad"
428 ? ["anyevent-mcp-form-elicitation", "--$ask_mode"]
429 5 ▶ : $ENV{PERL_ANYEVENT_MCP_ELICITATION_HELPER} || "anyevent-mcp-form-elicitation",
Branch: 3 branches
BranchTrueFalseTotal
$entry->{'form'} ? : 0 5 5
$ask_mode eq 'tty' || $ask_mode eq 'yad' ? : 0 0 0
$ask_mode eq 'tty' || $ask_mode eq 'yad' ? : 2 3 5
Condition37%3/8
$ask_mode eq "tty" or $ask_mode eq "yad"
l!l&&r!l&&!r
000
$ENV{"PERL_ANYEVENT_MCP_ELICITATION_HELPER"} or "anyevent-mcp-form-elicitation"
l!l
00
$ask_mode eq "tty" or $ask_mode eq "yad"
l!l&&r!l&&!r
113
MC/DC0%0/2
$ask_mode eq "tty" or $ask_mode eq "yad"
$ask_mode eq "tty" $ask_mode eq "yad"
MC/DC0%0/1
$ENV{"PERL_ANYEVENT_MCP_ELICITATION_HELPER"} or "anyevent-mcp-form-elicitation"
$ENV{"PERL_ANYEVENT_MCP_ELICITATION_HELPER"}
MC/DC100%2/2
$ask_mode eq "tty" or $ask_mode eq "yad"
$ask_mode eq "tty" $ask_mode eq "yad"
Truth table0%0/3
A or B
A= $ask_mode eq "tty"B= $ask_mode eq "yad"
ABresult
1X1
011
000
Truth table0%0/2
A
A= $ENV{"PERL_ANYEVENT_MCP_ELICITATION_HELPER"}
Aresult
11
00
Truth table100%3/3
A or B
A= $ask_mode eq "tty"B= $ask_mode eq "yad"
ABresult
1X1
011
000
430 timeout => 1800,
431 )
432 : AnyEvent::MCP::Permissions::Ask->new (
433 ($ask_mode eq "tty" || $ask_mode eq "yad"
434 ? (helper => ["anyevent-mcp-askpermission", "--$ask_mode"]) : ()),
435 );
436 5 $entry->{started} = AE::now;
437 5 ▶ AE::log trace => "local prompt started (" . ($entry->{form} ? "form" : "permission") . ")";
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'form'} ? : 0 5 5
438 5 ▶ $entry->{job}->ask ($entry->{prepared}, sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
439 5 my ($success, $result) = @_;
440  
441 5 ▶ unless ($entry->{request}{finished}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->{"request"}{"finished"}) 5 0 5
442 my $outcome = $success
443 5 ▶ ? (ref $result eq "HASH" ? $result->{decision} || $result->{action} || "answered" : "answered")
Branch: 2 branches
BranchTrueFalseTotal
$success ? : 5 0 5
ref $result eq 'HASH' ? : 5 0 5
Condition40%2/5
$result->{"decision"} or $result->{"action"} or "answered"
l!l
50
$result->{"decision"} or $result->{"action"}
l!l&&r!l&&!r
500
MC/DC0%0/2
$result->{"decision"} or $result->{"action"} or "answered"
$result->{"decision"} $result->{"action"}
Truth table33%1/3
A or B
A= $result->{"decision"}B= $result->{"action"}
ABresult
1X1
011
000
444 : "failed: $result";
445 5 AE::log trace => sprintf "local prompt %s after %.2fs", $outcome, AE::now - $entry->{started};
446 }
447 5 undef $prompt;
448 5 delete $entry->{job};
449 5 ▶ $entry->{done}->($success, $result) unless $entry->{request}{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"request"}{"finished"} 5 0 5
450 5 next_prompt;
451 5 });
452 return
453 5 }
454 }
455  
456 7 ▶sub on_input {
Subroutine on_input: called CC=12 SCAR=39.4
457 7 my ($client, $req) = @_;
458  
459 return $req->error (-32601, "Only form elicitation is supported locally")
460 7 ▶ unless $req->{method} eq "elicitation/create";
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"method"} eq "elicitation/create" 0 7 7
461 7 ▶ return $req->reply ({ action => "decline" }) if $ask_mode eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $ask_mode eq "none" 2 5 7
462  
463 5 my $meta = $req->{params}{_meta}{"anyevent-mcp/permission"};
464 5 ▶ unless (defined $meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $meta) 0 5 5
465 return $req->error (-32602, "Only form elicitation is supported locally")
466 0 ▶ if defined $req->{params}{mode} && $req->{params}{mode} ne "form";
Branch: 1 branch
BranchTrueFalseTotal
if defined $req->{"params"}{"mode"} and $req->{"params"}{"mode"} ne "form" 0 0 0
Condition0%0/3
defined $req->{"params"}{"mode"} and $req->{"params"}{"mode"} ne "form"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $req->{"params"}{"mode"} and $req->{"params"}{"mode"} ne "form"
defined $req->{"params"}{"mode"} $req->{"params"}{"mode"} ne "form"
Truth table0%0/3
A and B
A= defined $req->{"params"}{"mode"}B= $req->{"params"}{"mode"} ne "form"
ABresult
0X0
100
111
467 defined $req->{params}{message} && !ref $req->{params}{message}
468 0 ▶ or return $req->error (-32602, "Form needs a message");
Branch: 1 branch
BranchTrueFalseTotal
defined $req->{"params"}{"message"} and not ref $req->{"params"}{"message"} or return $req->error(-32602, "Form needs a message") 0 0 0
Condition0%0/3
defined $req->{"params"}{"message"} and not ref $req->{"params"}{"message"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $req->{"params"}{"message"} and not ref $req->{"params"}{"message"}
defined $req->{"params"}{"message"} not ref $req->{"params"}{"message"}
Truth table0%0/3
A and not(B)
A= defined $req->{"params"}{"message"}B= not ref $req->{"params"}{"message"}
ABresult
0X0
100
111
469 ask_prompt $req, {
470 0 %{$req->{params}},
471 message => "Server: $client->{name}\n\n$req->{params}{message}",
472 0 ▶ }, sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
473 0 my ($success, $response) = @_;
474  
475 0 ▶ return $req->error (-32603, $response) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
476 0 ▶ my $action = ref $response eq "HASH" ? $response->{action} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $response eq 'HASH' ? : 0 0 0
477 defined $action && !ref $action && $action =~ /\A(?:accept|decline|cancel)\z/
478 0 ▶ && ($action ne "accept" || ref $response->{content} eq "HASH")
Branch: 1 branch
BranchTrueFalseTotal
defined $action and not ref $action and $action =~ /\A(?:accept|decline|cancel)\z/u and $action ne "accept" || ref $response->{"content"} eq "HASH" or return $req->error(-32603, "Invalid form helper response") 0 0 0
Condition0%0/12
defined $action and not ref $action and $action =~ /\A(?:accept|decline|cancel)\z/u and $action ne "accept" || ref $response->{"content"} eq "HASH"
!ll&&!rl&&r
000
defined $action and not ref $action and $action =~ /\A(?:accept|decline|cancel)\z/u
!ll&&!rl&&r
000
defined $action and not ref $action
!ll&&!rl&&r
000
$action ne "accept" or ref $response->{"content"} eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $action and not ref $action and $action =~ /\A(?:accept|decline|cancel)\z/u and $action ne "accept" || ref $response->{"content"} eq "HASH"
defined $action not ref $action $action =~ /\A(?:accept|decline|cancel)\z/u $action ne "accept" ref $response->{"content"} eq "HASH"
Truth table0%0/6
A and not(B) and C and D or E
A= defined $actionB= not ref $actionC= $action =~ /\A(?:accept|decline|cancel)\z/uD= $action ne "accept"E= ref $response->{"content"} eq "HASH"
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
479 or return $req->error (-32603, "Invalid form helper response");
480 $req->reply ({
481 action => $action,
482 0 ▶ ($action eq "accept" ? (content => $response->{content}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'accept' ? : 0 0 0
483 });
484 0 }, 1;
485 0 return;
486 }
487 return $req->error (-32601, "Invalid permission elicitation")
488 5 ▶ unless ref $meta eq "HASH" && $meta->{version} == 1 && ref $meta->{request} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meta eq "HASH" and $meta->{"version"} == 1 and ref $meta->{"request"} eq "HASH" 0 5 5
Condition33%2/6
ref $meta eq "HASH" and $meta->{"version"} == 1 and ref $meta->{"request"} eq "HASH"
!ll&&!rl&&r
005
ref $meta eq "HASH" and $meta->{"version"} == 1
!ll&&!rl&&r
005
MC/DC0%0/3
ref $meta eq "HASH" and $meta->{"version"} == 1 and ref $meta->{"request"} eq "HASH"
ref $meta eq "HASH" $meta->{"version"} == 1 ref $meta->{"request"} eq "HASH"
Truth table25%1/4
A and B and C
A= ref $meta eq "HASH"B= $meta->{"version"} == 1C= ref $meta->{"request"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
489  
490 5 my $stage = $meta->{stage};
491  
492 ask_prompt $req, {
493 5 ▶ %{$meta->{request}},
Condition100%2/2
$meta->{'selection'} || {}
l!l
23
MC/DC100%1/1
$meta->{'selection'} || {}
$meta->{'selection'}
Truth table100%2/2
A
A= $meta->{'selection'}
Aresult
11
00
494 _permission_stage => $stage,
495 _permission_selection => $meta->{selection} || {},
496 5 ▶ }, sub {
Subroutine __ANON__: called CC=11 SCAR=33.1
497 5 my ($success, $decision) = @_;
498  
499 5 ▶ return $req->error (-32603, $decision) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 5 5
500 5 my $valid = eval {
501 AnyEvent::MCP::Permissions::Ask::build ($meta->{request}, {
502 %$decision,
503 (ref $decision->{rule} eq "HASH"
504 0 ▶ ? (map { $_ => $decision->{rule}{$_} } qw(path)) : ()),
Branch: 1 branch
BranchTrueFalseTotal
ref $decision->{'rule'} eq 'HASH' ? : 0 5 5
505 (ref $decision->{rule} eq "HASH"
506 0 ? (map { (my $key = $_) =~ s/^match\./dim_/; $key => $decision->{rule}{$_} }
507 0 grep /^match\./, keys %{$decision->{rule}}) : ()),
508 }, $stage, $meta->{selection})
509 5 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $decision->{'rule'} eq 'HASH' ? : 0 5 5
510 5 ▶ return $req->error (-32603, "Invalid permission helper decision: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
511 $req->reply ({
512 action => "accept",
513 content => {
514 decision => $valid->{decision},
515 (exists $valid->{sandbox_grants}
516 5 ▶ ? (sandbox_grants => join ",", @{$valid->{sandbox_grants}}) : ()),
Branch: 4 branches
BranchTrueFalseTotal
defined $valid->{'message'} ? : 0 5 5
exists $valid->{'lifetime'} ? : 0 5 5
ref $valid->{'rule'} eq 'HASH' && defined $valid->{'rule'}{'path'} ? : 0 5 5
ref $valid->{'rule'} eq 'HASH' ? : 0 5 5
Condition33%1/3
ref $valid->{"rule"} eq "HASH" and defined $valid->{"rule"}{"path"}
!ll&&!rl&&r
500
MC/DC0%0/2
ref $valid->{"rule"} eq "HASH" and defined $valid->{"rule"}{"path"}
ref $valid->{"rule"} eq "HASH" defined $valid->{"rule"}{"path"}
Truth table33%1/3
A and B
A= ref $valid->{"rule"} eq "HASH"B= defined $valid->{"rule"}{"path"}
ABresult
0X0
100
111
517 (defined $valid->{message} ? (message => $valid->{message}) : ()),
518 (exists $valid->{lifetime} ? (lifetime => $valid->{lifetime}) : ()),
519 (ref $valid->{rule} eq "HASH" && defined $valid->{rule}{path}
520 ? (path => $valid->{rule}{path}) : ()),
521 (ref $valid->{rule} eq "HASH"
522 0 ? (map { (my $key = $_) =~ s/^match\./dim_/; $key => $valid->{rule}{$_} }
523 0 ▶ grep /^match\./, keys %{$valid->{rule}}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $valid->{'sandbox_grants'} ? : 5 0 5
524 },
525 });
526 5 };
527 }
528  
529 2 ▶sub on_child_notification {
Subroutine on_child_notification: called CC=11 SCAR=40.6
530 2 my ($prefix, $client, $method, $params) = @_;
531  
532 2 ▶ if ($method eq "notifications/progress" && ref $params eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "notifications/progress" and ref $params eq "HASH") 2 0 2
Condition33%1/3
$method eq "notifications/progress" and ref $params eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
$method eq "notifications/progress" and ref $params eq "HASH"
$method eq "notifications/progress" ref $params eq "HASH"
Truth table33%1/3
A and B
A= $method eq "notifications/progress"B= ref $params eq "HASH"
ABresult
0X0
100
111
533 2 my $token = $params->{progressToken};
534 2 ▶ return unless defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and not ref $token 0 2 2
Condition33%1/3
defined $token and not ref $token
!ll&&!rl&&r
002
MC/DC0%0/2
defined $token and not ref $token
defined $token not ref $token
Truth table33%1/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
535 2 ▶ my $req = $progress{$prefix}{$token} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $req = $progress{$prefix}{$token} or return 2 0 2
536 $req->progress ($params->{progress}, $params->{total}, $params->{message})
537 2 ▶ unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 2 0 2
538 2 return;
539 }
540  
541 0 ▶ return unless $stdio{$prefix} && $method eq "anyevent-mcp/idle-exit";
Branch: 1 branch
BranchTrueFalseTotal
unless $stdio{$prefix} and $method eq "anyevent-mcp/idle-exit" 0 0 0
Condition0%0/3
$stdio{$prefix} and $method eq "anyevent-mcp/idle-exit"
!ll&&!rl&&r
000
MC/DC0%0/2
$stdio{$prefix} and $method eq "anyevent-mcp/idle-exit"
$stdio{$prefix} $method eq "anyevent-mcp/idle-exit"
Truth table0%0/3
A and B
A= $stdio{$prefix}B= $method eq "anyevent-mcp/idle-exit"
ABresult
0X0
100
111
542 0 ▶ return if $client->{idle_shutdown_pending};
Branch: 1 branch
BranchTrueFalseTotal
if $client->{"idle_shutdown_pending"} 0 0 0
543 # the child offered to exit; only act while no broker calls need it.
544 0 ▶ return if keys %{$client->{operations}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$client{"operations"};} 0 0 0
545  
546 0 $client->{idle_shutdown_pending} = 1;
547 0 ▶ $client->rpc ("anyevent-mcp/shutdown", {}, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
548 0 my ($success, $result) = @_;
549  
550 0 ▶ if ($success && $result->{accepted}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success and $result->{'accepted'}) { } 0 0 0
Condition0%0/3
$success and $result->{"accepted"}
!ll&&!rl&&r
000
MC/DC0%0/2
$success and $result->{"accepted"}
$success $result->{"accepted"}
Truth table0%0/3
A and B
A= $successB= $result->{"accepted"}
ABresult
0X0
100
111
551 0 $client->{idle_shutdown_expected} = 1;
552 } else {
553 AE::log debug => "$prefix: idle shutdown declined: "
554 0 ▶ . ($result->{message} || "child is busy");
Condition0%0/2
$result->{'message'} || 'child is busy'
l!l
00
MC/DC0%0/1
$result->{'message'} || 'child is busy'
$result->{'message'}
Truth table0%0/2
A
A= $result->{'message'}
Aresult
11
00
555 }
556 0 delete $client->{idle_shutdown_pending};
557 0 });
558 }
559  
560 # required children must not leave spawned peers running after broker failure.
561 0 ▶sub required_failure {
Subroutine required_failure: not called CC=2 SCAR=17.9
562 0 my ($prefix, $message) = @_;
563 0 AE::log error => "$prefix: required child $message";
564 0 $shutting_down = 1;
565 0 $_->close for values %broker;
566 0 exit 1;
567 }
568  
569 0 ▶sub on_child_close {
Subroutine on_child_close: not called CC=9 SCAR=45.0
570 0 my ($prefix, $client, $error) = @_;
571  
572 0 ▶ return if $shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down 0 0 0
573 0 ▶ if (delete $client->{idle_shutdown_expected}) {
Branch: 3 branches
BranchTrueFalseTotal
if (delete $client->{'idle_shutdown_expected'}) { } 0 0 0
elsif ($required{$prefix}) { } 0 0 0
elsif ($stdio{$prefix} or $error->{'pending'}) { } 0 0 0
574 0 mcp_log "$prefix: managed idle shutdown";
575 } elsif ($required{$prefix}) {
576 required_failure $prefix, "disconnected: $error->{message}"
577 0 ▶ . ($error->{pending} ? " ($error->{pending} outstanding requests)" : "");
Branch: 1 branch
BranchTrueFalseTotal
$error->{'pending'} ? : 0 0 0
578 ▶ } elsif ($stdio{$prefix} || $error->{pending}) {
Condition0%0/3
$stdio{$prefix} or $error->{"pending"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$stdio{$prefix} or $error->{"pending"}
$stdio{$prefix} $error->{"pending"}
Truth table0%0/3
A or B
A= $stdio{$prefix}B= $error->{"pending"}
ABresult
1X1
011
000
579 my $message = "$prefix: unexpected disconnect: $error->{message}"
580 0 ▶ . ($error->{pending} ? " ($error->{pending} outstanding requests)" : "");
Branch: 1 branch
BranchTrueFalseTotal
$error->{'pending'} ? : 0 0 0
581 0 ▶ AE::log warn => $message or mcp_log $message;
Branch: 1 branch
BranchTrueFalseTotal
&AE::log("warn", $message) or mcp_log($message) 0 0 0
582 } else {
583 0 mcp_log "$prefix: disconnected: $error->{message}";
584 }
585 0 delete $client->{idle_shutdown_pending};
586 }
587  
588 37 ▶sub broker_config {
Subroutine broker_config: called CC=1 SCAR=0.1
589 37 my ($prefix) = @_;
590  
591 (
592 name => $prefix,
593 client_info => { name => "mcp-multibroker", version => "$AnyEvent::MCP::VERSION" },
594 client_capabilities => {
595 elicitation => { form => {} },
596 extensions => { "anyevent-mcp/multibroker" => { version => 1 } },
597 },
598 on_request => \&on_input,
599 30 ▶ on_ready => sub { mcp_log "$prefix: connected" },
Subroutine __ANON__: called CC=1 SCAR=0.0
600 2 ▶ on_notification => sub { on_child_notification ($prefix, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
601 0 ▶ on_close => sub { on_child_close ($prefix, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
602 )
603 37 }
604  
605 0 ▶sub accept_extension {
Subroutine accept_extension: not called CC=7 SCAR=40.3
606 0 my ($server, $handle, $hello) = @_;
607  
608 0 ▶ @$hello == 3 && $hello->[0] eq "mcp-connect"
Branch: 1 branch
BranchTrueFalseTotal
@$hello == 3 and $hello->[0] eq "mcp-connect" and defined $hello->[2] and not ref $hello->[2] and $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/u or die "Invalid reverse MCP hello\n" 0 0 0
Condition0%0/12
@$hello == 3 and $hello->[0] eq "mcp-connect" and defined $hello->[2] and not ref $hello->[2] and $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/u
!ll&&!rl&&r
000
@$hello == 3 and $hello->[0] eq "mcp-connect" and defined $hello->[2] and not ref $hello->[2]
!ll&&!rl&&r
000
@$hello == 3 and $hello->[0] eq "mcp-connect" and defined $hello->[2]
!ll&&!rl&&r
000
@$hello == 3 and $hello->[0] eq "mcp-connect"
!ll&&!rl&&r
000
MC/DC0%0/5
@$hello == 3 and $hello->[0] eq "mcp-connect" and defined $hello->[2] and not ref $hello->[2] and $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/u
@$hello == 3 $hello->[0] eq "mcp-connect" defined $hello->[2] not ref $hello->[2] $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/u
Truth table0%0/6
A and B and C and not(D) and E
A= @$hello == 3B= $hello->[0] eq "mcp-connect"C= defined $hello->[2]D= not ref $hello->[2]E= $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/u
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
609 && defined $hello->[2] && !ref $hello->[2] && $hello->[2] =~ /\A[A-Za-z0-9_-]+\z/
610 or die "Invalid reverse MCP hello\n";
611 0 my $prefix = $hello->[2];
612 0 ▶ !exists $broker{$prefix} or die "Broker alias '$prefix' is occupied\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $broker{$prefix} and die "Broker alias '${prefix}' is occupied\n" 0 0 0
613  
614 my $client = AnyEvent::MCP::Client::JSONL->new (
615 broker_config ($prefix),
616 0 ▶ on_close => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
617 0 on_child_close ($prefix, @_);
618 0 ▶ delete $broker{$prefix} if $broker{$prefix} == $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $broker{$prefix} == $_[0] 0 0 0
619 },
620 0 );
621 0 $broker{$prefix} = $client;
622 0 $client->attach ($handle);
623 0 ▶ $client->connect (sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
624 0 my ($success, $error) = @_;
625  
626 0 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
627 0 AE::log warn => "$prefix: reverse MCP initialization failed: $error->{message}";
628 0 ▶ delete $broker{$prefix} if $broker{$prefix} == $client;
Branch: 1 branch
BranchTrueFalseTotal
if $broker{$prefix} == $client 0 0 0
629 0 $client->close;
630 }
631 0 });
632 }
633  
634 21 ▶while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 37 21 58
635 37 ▶ @ARGV >= 2 or die $usage;
Branch: 1 branch
BranchTrueFalseTotal
@ARGV >= 2 or die $usage 37 0 37
636 37 my ($prefix, $url) = splice @ARGV, 0, 2;
637 37 ▶ $prefix =~ /\A[A-Za-z0-9_-]+\z/ or die "Invalid broker prefix '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
$prefix =~ /\A[A-Za-z0-9_-]+\z/u or die "Invalid broker prefix '${prefix}'\n" 37 0 37
638 37 ▶ exists $broker{$prefix} and die "Duplicate broker prefix '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $broker{$prefix} and die "Duplicate broker prefix '${prefix}'\n" 0 37 37
639  
640 37 my $command;
641  
642 37 ▶ if ($url eq "---") {
Branch: 2 branches
BranchTrueFalseTotal
if ($url eq '---') { } 1 36 37
elsif ($url =~ s/\Astdio://u) { } 21 15 36
643 1 my ($end) = grep $ARGV[$_] eq "---", 0 .. $#ARGV;
644 1 ▶ defined $end or die "Missing closing --- for '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $end or die "Missing closing --- for '${prefix}'\n" 1 0 1
645 1 $command = [splice @ARGV, 0, $end];
646 1 shift @ARGV;
647 1 ▶ @$command && length $command->[0] or die "Empty stdio argv for '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
@$command and length $command->[0] or die "Empty stdio argv for '${prefix}'\n" 1 0 1
Condition33%1/3
@$command and length $command->[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@$command and length $command->[0]
@$command length $command->[0]
Truth table33%1/3
A and B
A= @$commandB= length $command->[0]
ABresult
0X0
100
111
648 } elsif ($url =~ s/\Astdio://) {
649 21 ▶ length $url or die "Empty stdio command for '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
length $url or die "Empty stdio command for '${prefix}'\n" 21 0 21
650 # preserve shell commands, but let ordinary programs receive TERM directly.
651 21 ▶ $command = $url =~ / ^\s*(?: exec\b | source\b | \.\s ) | = | ; | \( | \{ | \|\| | && /x
Branch: 1 branch
BranchTrueFalseTotal
$url =~ / ^\s*(?: exec\b | source\b | \.\s ) | = | ; | \( | \{ | \|\| | && /ux ? : 0 21 21
652 ? $url : "exec $url";
653 }
654  
655 37 my %config = broker_config $prefix;
656 Getopt::Long::GetOptionsFromArray (\@ARGV,
657 0 ▶ "eager" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
658 0 ▶ defined $command or die "--eager requires a stdio broker ('$prefix')\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $command or die "--eager requires a stdio broker ('${prefix}')\n" 0 0 0
659 0 $eager{$prefix} = 1;
660 },
661 2 ▶ "hidden" => sub { $hidden{$prefix} = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
662 0 ▶ "required" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
663 0 ▶ defined $command or die "--required requires a stdio broker ('$prefix')\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $command or die "--required requires a stdio broker ('${prefix}')\n" 0 0 0
664 0 $eager{$prefix} = $required{$prefix} = 1;
665 },
666 13 ▶ "bearer=s" => sub {
Subroutine __ANON__: called CC=4 SCAR=16.1
667 13 ▶ !defined $command && $url =~ m{\Ahttps?://} or die "Authentication options require an HTTP broker\n";
Branch: 1 branch
BranchTrueFalseTotal
not defined $command and $url =~ m[\Ahttps?://]u or die "Authentication options require an HTTP broker\n" 13 0 13
Condition33%1/3
not defined $command and $url =~ m[\Ahttps?://]u
!ll&&!rl&&r
0013
MC/DC0%0/2
not defined $command and $url =~ m[\Ahttps?://]u
not defined $command $url =~ m[\Ahttps?://]u
Truth table33%1/3
not(A) and B
A= not defined $commandB= $url =~ m[\Ahttps?://]u
ABresult
0X0
100
111
668 13 ▶ length $_[1] or die "Missing bearer token for '$prefix'\n";
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] or die "Missing bearer token for '${prefix}'\n" 13 0 13
669 13 $config{bearer} = $_[1];
670 },
671 37 ▶ ) or die $usage;
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray(\@ARGV, "eager", sub { defined $command or die "--eager requires a stdio broker ('${prefix}')\n"; $eager{$prefix} = 1; } , "hidden", sub { $hidden{$prefix} = 1; } , "required", sub { defined $command or die "--required requires a stdio broker ('${prefix}')\n"; $eager{$prefix} = $required{$prefix} = 1; } , "bearer=s", sub { not defined $command and $url =~ m[\Ahttps?://]u or die "Authentication options require an HTTP broker\n"; length $_[1] or die "Missing bearer token for '${prefix}'\n"; $config{"bearer"} = $_[1]; } ) or die $usage 37 0 37
672  
673 37 ▶ if (!defined $command && $url =~ m{\Ahttps?://}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $command and $url =~ m[\Ahttps?://]u) { } 15 22 37
elsif (defined $command) { } 22 0 22
Condition66%2/3
not defined $command and $url =~ m[\Ahttps?://]u
!ll&&!rl&&r
22015
MC/DC50%1/2
not defined $command and $url =~ m[\Ahttps?://]u
not defined $command $url =~ m[\Ahttps?://]u
Truth table66%2/3
not(A) and B
A= not defined $commandB= $url =~ m[\Ahttps?://]u
ABresult
0X0
100
111
674 15 $broker{$prefix} = AnyEvent::MCP::Client::HTTP->new (%config, url => $url);
675 } elsif (defined $command) {
676 22 $broker{$prefix} = AnyEvent::MCP::Client::JSONL->new (
677 %config, command => $command,
678 env => {
679 PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "stdio",
680 PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS => $prefix,
681 PERL_ANYEVENT_MCP_PERMISSION_ASK => "mcp",
682 },
683 );
684 22 $stdio{$prefix} = 1;
685 } else {
686 0 die "Invalid broker URL '$url'\n";
687 }
688 }
689  
690 21 my $toolset = AnyEvent::MCP::Server::Toolset->new;
691  
692 62 ▶sub broker_alias {
Subroutine broker_alias: called CC=2 SCAR=6.9
693 62 my ($name) = @_;
694 62 ▶ return $name if exists $broker{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $broker{$name} 57 5 62
695 5 $name =~ tr/_/-/;
696 5 $name
697 }
698  
699 20 ▶sub all_tools {
Subroutine all_tools: called CC=5 SCAR=16.2
700 20 my ($req, $done, $name) = @_;
701  
702 20 ▶ my @tools = defined $name ? () : map @{$_->list}, @{$req->{server}{toolsets}};
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 5 15 20
703 20 ▶ my @prefix = defined $name ? ($name) : grep !$hidden{$_}, sort keys %broker;
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 5 15 20
704 20 my @results;
705 20 my %pending = map { $_ => 1 } @prefix;
706 20 my $remaining = @prefix;
707 20 my $finished;
708 my $timer;
709 20 ▶ my $finish = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
710 20 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 0 20 20
711 20 undef $timer;
712 20 ▶ push @tools, @{$_ || []} for @results;
Condition100%2/2
$_ || []
l!l
311
MC/DC100%1/1
$_ || []
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
713 20 ▶ $done->(\@tools) unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 20 0 20
714 20 };
715 20 ▶ return $finish->() unless $remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless $remaining 0 20 20
716  
717 0 ▶ $timer = AnyEvent->timer (after => 15, cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
718 0 AE::log warn => "Tool discovery timed out: " . join ", ", sort keys %pending;
719 0 $finish->();
720 20 });
721 20 for my $i (0 .. $#prefix) {
722 32 my $prefix = $prefix[$i];
723 32 my $client = $broker{$prefix};
724 32 ▶ $client->tools (sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
725 32 ▶ return if $finished;
Branch: 1 branch
BranchTrueFalseTotal
if $finished 0 32 32
726 32 my ($success, $result) = @_;
727 32 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 31 1 32
728 31 $results[$i] = [map { +{ %$_, name => "$prefix.$_->{name}" } } @$result];
729 } else {
730 1 AE::log warn => "$prefix: $result->{message}";
731 }
732 32 delete $pending{$prefix};
733 32 ▶ $finish->() unless --$remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless --$remaining 20 12 32
734 32 });
735 }
736 }
737  
738 88 ▶sub call_tool {
Subroutine call_tool: called CC=21 SCAR=31.3
739 88 my ($req, $name, $args) = @_;
740  
741 88 ▶ return $req->error ("Invalid tool call") unless defined $name && !ref $name && ref $args eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and ref $args eq "HASH" 0 88 88
Condition33%2/6
defined $name and not ref $name and ref $args eq "HASH"
!ll&&!rl&&r
0088
defined $name and not ref $name
!ll&&!rl&&r
0088
MC/DC0%0/3
defined $name and not ref $name and ref $args eq "HASH"
defined $name not ref $name ref $args eq "HASH"
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= ref $args eq "HASH"
ABCresult
0XX0
10X0
1100
1111
742 88 ▶ if (my ($local) = grep exists $_->{tool}{$name}, @{$req->{server}{toolsets}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($local) = grep(exists $_->{"tool"}{$name}, @{$$req{"server"}{"toolsets"};})) 32 56 88
743 32 local $req->{tool} = $name;
744 32 local $req->{args} = $args;
745 32 return $local->call ($req)
746 }
747  
748 56 my ($prefix, $tool) = $name =~ /\A([^.]+)\.(.+)\z/;
749 56 ▶ $prefix = broker_alias $prefix if defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 56 0 56
750 56 my $client = $broker{$prefix};
751 56 ▶ return $req->error ("Unknown tool '$name'") unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 1 55 56
752  
753 55 AE::log trace => "multibroker $name";
754 55 my %params = (%{$req->{params}}, name => $tool, arguments => $args);
755 55 ▶ $params{_meta} = { %{$params{_meta} || {}} };
Condition100%2/2
$params{'_meta'} || {}
l!l
532
MC/DC100%1/1
$params{'_meta'} || {}
$params{'_meta'}
Truth table100%2/2
A
A= $params{'_meta'}
Aresult
11
00
756 55 $params{_meta}{"anyevent-mcp/user"} = $req->{user};
757 55 my $caps = $params{_meta}{"io.modelcontextprotocol/clientCapabilities"};
758 55 ▶ $caps = ref $caps eq "HASH" ? { %$caps } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $caps eq 'HASH' ? : 53 2 55
759 my $native_forms = $req->{protocol_version} ge "2026"
760 && ref $caps->{elicitation} eq "HASH"
761 55 ▶ && (!keys %{$caps->{elicitation}} || ref $caps->{elicitation}{form} eq "HASH");
Condition77%7/9
$req->{'protocol_version'} ge '2026' && ref $caps->{'elicitation'} eq 'HASH' && (!keys(%{$$caps{'elicitation'};}) || ref $caps->{'elicitation'}{'form'} eq 'HASH')
!ll&&!rl&&r
39016
$req->{"protocol_version"} ge 2026 and ref $caps->{"elicitation"} eq "HASH"
!ll&&!rl&&r
23716
not keys %{$$caps{"elicitation"};} or ref $caps->{"elicitation"}{"form"} eq "HASH"
l!l&&r!l&&!r
6100
MC/DC50%2/4
$req->{'protocol_version'} ge '2026' && ref $caps->{'elicitation'} eq 'HASH' && (!keys(%{$$caps{'elicitation'};}) || ref $caps->{'elicitation'}{'form'} eq 'HASH')
$req->{"protocol_version"} ge 2026 ref $caps->{"elicitation"} eq "HASH" not keys %{$$caps{"elicitation"};} ref $caps->{"elicitation"}{"form"} eq "HASH"
Truth table80%4/5
A and B && not(C) or D
A= $req->{"protocol_version"} ge 2026B= ref $caps->{"elicitation"} eq "HASH"C= not keys %{$$caps{"elicitation"};}D= ref $caps->{"elicitation"}{"form"} eq "HASH"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
762 55 ▶ my $local_forms = $ask_mode =~ /\A(?:helper|tty|yad|none)\z/ || $ask_mode eq "auto" && !$native_forms;
Condition83%5/6
$ask_mode =~ /\A(?:helper|tty|yad|none)\z/u || $ask_mode eq 'auto' && !$native_forms
l!l&&r!l&&!r
15238
$ask_mode eq "auto" and not $native_forms
!ll&&!rl&&r
3802
MC/DC66%2/3
$ask_mode =~ /\A(?:helper|tty|yad|none)\z/u || $ask_mode eq 'auto' && !$native_forms
$ask_mode =~ /\A(?:helper|tty|yad|none)\z/u $ask_mode eq "auto" not $native_forms
Truth table75%3/4
A || B and not(C)
A= $ask_mode =~ /\A(?:helper|tty|yad|none)\z/uB= $ask_mode eq "auto"C= not $native_forms
ABCresult
1XX1
0111
00X0
0100
763 55 ▶ if ($local_forms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($local_forms) { } 17 38 55
elsif ($req->{'protocol_version'} lt '2026') { } 2 36 38
764 17 $caps->{elicitation} = { form => {} };
765 } elsif ($req->{protocol_version} lt "2026") {
766 2 delete $caps->{elicitation};
767 }
768 $caps->{extensions} = {
769 55 ▶ %{ref $caps->{extensions} eq "HASH" ? $caps->{extensions} : {}},
Branch: 1 branch
BranchTrueFalseTotal
ref $caps->{'extensions'} eq 'HASH' ? : 0 55 55
770 "anyevent-mcp/multibroker" => { version => 1 },
771 };
772 55 $params{_meta}{"io.modelcontextprotocol/clientCapabilities"} = $caps;
773 55 my $token = $params{_meta}{progressToken};
774 55 ▶ $progress{$prefix}{$token} = $req if defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and not ref $token 4 51 55
Condition66%2/3
defined $token and not ref $token
!ll&&!rl&&r
5104
MC/DC50%1/2
defined $token and not ref $token
defined $token not ref $token
Truth table66%2/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
775 55 my $id;
776 55 my $old_cancel = $req->{on_cancel};
777 1 ▶ $req->{on_cancel} = sub {
Subroutine __ANON__: called CC=5 SCAR=19.2
778 1 ▶ delete $progress{$prefix}{$token} if defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and not ref $token 0 1 1
Condition33%1/3
defined $token and not ref $token
!ll&&!rl&&r
100
MC/DC0%0/2
defined $token and not ref $token
defined $token not ref $token
Truth table33%1/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
779 1 ▶ $client->cancel ($id, $_[1]) if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 1 0 1
780 1 ▶ $old_cancel->(@_) if $old_cancel;
Branch: 1 branch
BranchTrueFalseTotal
if $old_cancel 0 1 1
781 55 };
782 55 ▶ my $done = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
783 55 my ($success, $result) = @_;
784 55 ▶ delete $progress{$prefix}{$token} if defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and not ref $token 4 51 55
Condition66%2/3
defined $token and not ref $token
!ll&&!rl&&r
5104
MC/DC50%1/2
defined $token and not ref $token
defined $token not ref $token
Truth table66%2/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
785  
786 55 ▶ return if $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"finished"} 1 54 55
787 54 ▶ return $req->error ($result->{message}) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 54 54
788 54 $req->reply ($result);
789 55 };
790 55 ▶ if ($local_forms || $req->{protocol_version} lt "2026") {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_forms or $req->{'protocol_version'} lt '2026') { } 19 36 55
Condition100%3/3
$local_forms or $req->{"protocol_version"} lt 2026
l!l&&r!l&&!r
17236
MC/DC100%2/2
$local_forms or $req->{"protocol_version"} lt 2026
$local_forms $req->{"protocol_version"} lt 2026
Truth table100%3/3
A or B
A= $local_formsB= $req->{"protocol_version"} lt 2026
ABresult
1X1
011
000
791 19 $id = $client->call ($tool, $args, $done, %params);
792 } else {
793 36 $id = $client->rpc ("tools/call", \%params, $done);
794 }
795 }
796  
797 $toolset->tool (discover_brokers =>
798 "Get instructions from all brokers by name: string; null if reachable but none; {error: MESSAGE} if unreachable.",
799 { type => "object", properties => {} },
800 8 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
801 8 my ($req) = @_;
802  
803 8 my @prefix = sort keys %broker;
804 8 my %instructions;
805 8 my %pending = map { $_ => 1 } @prefix;
806 8 my $remaining = @prefix;
807 8 my $finished;
808 my $timer;
809 8 ▶ my $finish = sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
810 8 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 0 8 8
811 8 undef $timer;
812 8 $instructions{$_} = { error => "Discovery timed out" } for keys %pending;
813 8 ▶ $req->result (\%instructions) unless $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"finished"} 8 0 8
814 8 };
815 8 ▶ return $finish->() unless $remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless $remaining 0 8 8
816  
817 0 ▶ $timer = AnyEvent->timer (after => 15, cb => sub { $finish->() });
Subroutine __ANON__: not called CC=1 SCAR=1.2
818 8 for my $prefix (@prefix) {
819 18 my $client = $broker{$prefix};
820 18 ▶ $client->connect (sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
821 18 ▶ return if $finished;
Branch: 1 branch
BranchTrueFalseTotal
if $finished 0 18 18
822 18 my ($success, $result) = @_;
823 18 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 17 1 18
824 $instructions{$prefix} = $hidden{$prefix}
825 ? "Hidden tools; use list-tools broker=$prefix.\n" . ($client->{instructions} // "")
826 17 ▶ : $client->{instructions};
Branch: 1 branch
BranchTrueFalseTotal
$hidden{$prefix} ? : 2 15 17
Condition50%1/2
$client->{"instructions"} // ""
l!l
20
MC/DC0%0/1
$client->{"instructions"} // ""
$client->{"instructions"}
Truth table50%1/2
A
A= $client->{"instructions"}
Aresult
11
00
827 } else {
828 1 AE::log warn => "$prefix: $result->{message}";
829 1 $instructions{$prefix} = { error => $result->{message} };
830 }
831 18 delete $pending{$prefix};
832 18 ▶ $finish->() unless --$remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless --$remaining 8 10 18
833 18 });
834 }
835 },
836 21 );
837 $toolset->tool (list_tools =>
838 "Refresh tools; broker selects one downstream, including hidden ones. Omit for local + visible brokers.",
839 {
840 type => "object",
841 properties => {
842 broker => { type => "string", description => "Downstream broker, including hidden; underscores may replace hyphens. Omit for visible brokers + local tools." },
843 },
844 additionalProperties => JSON::XS::false,
845 },
846 14 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
847 14 my ($req, $args) = @_;
848  
849 14 for (keys %$args) {
850 8 ▶ $_ eq "broker" or return $req->error ("Unknown list-tools argument '$_'");
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "broker" or return $req->error("Unknown list-tools argument '${_}'") 7 1 8
851 }
852 13 my $name = $args->{broker};
853 13 ▶ $name = broker_alias $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 6 7 13
854 13 ▶ return $req->error ("Unknown broker '$name'") if defined $name && !exists $broker{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and not exists $broker{$name} 1 12 13
Condition100%3/3
defined $name and not exists $broker{$name}
!ll&&!rl&&r
751
MC/DC100%2/2
defined $name and not exists $broker{$name}
defined $name not exists $broker{$name}
Truth table100%3/3
A and not(B)
A= defined $nameB= not exists $broker{$name}
ABresult
0X0
100
111
855  
856 12 ▶ all_tools $req, sub { $req->result ({ tools => $_[0] }) }, $name;
Subroutine __ANON__: called CC=1 SCAR=0.0
857 },
858 21 );
859 $toolset->tool (call_tool =>
860 "Call tool by prefixed name from list-tools. Use when not directly exposed; call loaded tools directly.",
861 {
862 type => "object",
863 required => ["name"],
864 properties => {
865 name => { type => "string" },
866 arguments => { type => "object" },
867 },
868 },
869 8 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
870 8 my ($req, $args) = @_;
871  
872 8 ▶ return $req->error ("call-tool cannot call itself") if $args->{name} eq "call-tool";
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"name"} eq "call-tool" 1 7 8
873 7 ▶ call_tool $req, $args->{name}, $args->{arguments} || {};
Condition100%2/2
$args->{'arguments'} || {}
l!l
34
MC/DC100%1/1
$args->{'arguments'} || {}
$args->{'arguments'}
Truth table100%2/2
A
A= $args->{'arguments'}
Aresult
11
00
874 },
875 21 );
876  
877 112 ▶sub log_request {
Subroutine log_request: called CC=3 SCAR=11.0
878 112 my ($server, $req) = @_;
879  
880 # keep proxied calls at trace to avoid duplicating downstream activity logs.
881 112 ▶ return if $req->{method} eq "tools/call" && $req->{tool} =~ /\A[^.]+\./;
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"method"} eq "tools/call" and $req->{"tool"} =~ /\A[^.]+\./u 49 63 112
Condition100%3/3
$req->{"method"} eq "tools/call" and $req->{"tool"} =~ /\A[^.]+\./u
!ll&&!rl&&r
313249
MC/DC100%2/2
$req->{"method"} eq "tools/call" and $req->{"tool"} =~ /\A[^.]+\./u
$req->{"method"} eq "tools/call" $req->{"tool"} =~ /\A[^.]+\./u
Truth table100%3/3
A and B
A= $req->{"method"} eq "tools/call"B= $req->{"tool"} =~ /\A[^.]+\./u
ABresult
0X0
100
111
882  
883 63 mcp_log_request $server, $req;
884 }
885  
886 21 mcp_permissions undef; # downstream permission elicitation is handled by the broker
887  
888 mcp_config
889 server_name => "mcp-multibroker",
890 server_version => $AnyEvent::MCP::VERSION,
891 on_request => \&log_request,
892 0 ▶ on_error => sub { AE::log error => "MCP dispatch $_[1]{method}: $_[2]" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
893 21 short_instructions => "MCP tool router. discover-brokers lists servers including hidden ones; list-tools broker=NAME finds their tools.",
894 instructions => <<'EOF';
895 Proxy MCP servers as PREFIX.TOOL. discover-brokers lists servers, including hidden ones; list-tools broker=NAME retrieves a server's tools. list-tools without a broker returns visible tools; call-tool invokes by prefixed name, even when hidden. Tools may change.
896 EOF
897  
898 # start selected stdio children now, before the upstream event loop starts.
899 # their asynchronous initialization completes once that loop is running.
900 # optional children can still be reconnected by a later MCP request.
901 21 mcp_tool_help ($server_options->{tool_help}, $toolset);
902 20 delete $server_options->{tool_help};
903 20 for my $prefix (sort keys %eager) {
904 0 ▶ $broker{$prefix}->connect (sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
905 0 my ($success, $error) = @_;
906  
907 0 ▶ if (!$success && $required{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $success and $required{$prefix}) 0 0 0
Condition0%0/3
not $success and $required{$prefix}
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $required{$prefix}
not $success $required{$prefix}
Truth table0%0/3
not(A) and B
A= not $successB= $required{$prefix}
ABresult
0X0
100
111
908 0 required_failure $prefix, "initialization failed: $error->{message}";
909 }
910 0 ▶ AE::log warn => "$prefix: eager startup failed: $error->{message}"
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
911 unless $success;
912 0 });
913 }
914  
915 mcp_start
916 %$server_options,
917 ($listen ? (listen => 1) : ()),
918 toolsets => [$toolset],
919 on_extension => \&accept_extension,
920 8 ▶ on_tools_list => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
921 8 my ($server, $req) = @_;
922  
923 8 AE::log debug => "multibroker list-tools";
924 8 ▶ all_tools $req, sub { $req->reply ({ tools => $_[0] }) };
Subroutine __ANON__: called CC=1 SCAR=0.0
925 },
926 81 ▶ on_tools_call => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
927 81 my ($server, $req, $args) = @_;
928  
929 81 call_tool $req, $req->{tool}, $args;
930 20 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$listen ? : 0 20 20
931  
932 20 $shutting_down = 1;
933 20 $_->close for values %broker;
934  
« bin/mcp-http ↑ Summary bin/mcp-vim »