blib/lib/App/MCP/CLI/Config.pm

filter: Statementstmt 83.1% 59 / 71 Branchbran 73.8% 31 / 42 Conditioncond 53.5% 15 / 28 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 72.6% 122 / 168 CC 32 SCAR 32.3
CC
32
Cov
74%
load_config
43.9
resolve_server_cmd
19.2
BEGIN
1.0
CRAP 25.3
« blib/lib/App/MCP/CLI/Command/call.pm ↑ Summary blib/lib/App/MCP/CLI/Client.pm »
1 package App::MCP::CLI::Config;
2  
3 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 our $VERSION = qv('v0.0.1');
8 31 our $AUTHORITY = 'cpan:MANWAR';
9  
10 31 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 31 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 our @EXPORT_OK = qw(load_config resolve_server_cmd);
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 App::MCP::CLI::Config - Configuration loader and server resolver for App::MCP::CLI
21  
22 =head1 VERSION
23  
24 This module is distributed as part of L<App::MCP::CLI> v0.0.1.
25  
26 =head1 SYNOPSIS
27  
28 use App::MCP::CLI::Config qw(load_config resolve_server_cmd);
29  
30 # Load config from an explicit path, or auto-discover
31 my $config = load_config('/path/to/server_config.json');
32 my $config = load_config(); # auto-discovers
33  
34 # Resolve a named server to a command array (stdio) or URL (SSE)
35 my ($cmd, $url, $env) = resolve_server_cmd($config, 'perl-math-server');
36  
37 if ($url) {
38 # SSE transport
39 }
40 else {
41 # stdio transport - see App::MCP::CLI::Client, which spawns @$cmd
42 # with %$env applied to the child only
43 }
44  
45 =head1 DESCRIPTION
46  
47 C<App::MCP::CLI::Config> is the shared configuration layer for all
48 C<App::MCP::CLI> sub-commands. It provides two exported functions:
49  
50 =over 4
51  
52 =item * L</load_config> - reads and parses the JSON configuration file,
53 with automatic discovery of well-known paths when no explicit path is given.
54  
55 =item * L</resolve_server_cmd> - maps a server name (or bare command string,
56 script path, or URL) to either a command array for stdio transport or a URL
57 for SSE transport.
58  
59 =back
60  
61 Centralising this logic here means that C<call>, C<list>, C<interactive>, and
62 any future sub-commands all share identical config-loading and server-resolution
63 behaviour.
64  
65 =head1 CONFIGURATION FILE FORMAT
66  
67 The JSON configuration file has the following top-level structure:
68  
69 {
70 "confirmTools": true,
71 "mcpServers": {
72 "perl-math-server": {
73 "command": "perl",
74 "args": ["stdio_server.pl"],
75 "env": { "DEBUG": "1" }
76 },
77 "remote-server": {
78 "url": "http://mcp.example.com/sse"
79 }
80 }
81 }
82  
83 =head2 mcpServers
84  
85 A JSON object whose keys are server alias names and whose values are server
86 descriptor objects. Each descriptor supports the following fields:
87  
88 =over 4
89  
90 =item C<command> (string)
91  
92 The executable to launch when using stdio transport (e.g. C<"perl">,
93 C<"node">). Required for stdio servers.
94  
95 =item C<args> (array of strings)
96  
97 Additional command-line arguments passed to C<command>
98 (e.g. C<["stdio_server.pl"]>). Defaults to an empty list.
99  
100 =item C<env> (object)
101  
102 A map of environment variable names to values. The entries are passed to the
103 server subprocess only; they are never set in the C<mcpcli> process itself.
104  
105 =item C<url> (string)
106  
107 The SSE endpoint URL for remote servers (e.g.
108 C<"http://mcp.example.com/sse">). When present, the server is contacted via
109 HTTP/SSE instead of a local subprocess.
110  
111 =item C<transport> (string)
112  
113 Informational (C<"stdio"> or C<"sse">). A server is treated as SSE when it has
114 a C<url>. C<transport: "sse"> without a C<url> is rejected with an error.
115  
116 =back
117  
118 =head2 confirmTools
119  
120 Optional boolean (default C<true>). When set to C<false>, the
121 C<interactive>/C<chat> command executes tool calls autonomously without pausing
122 to ask the user for confirmation.
123  
124 =head1 AUTO-DISCOVERY
125  
126 When L</load_config> is called without an argument (or with C<undef>), it
127 searches for a configuration file in the following locations, in order:
128  
129 =over 4
130  
131 =item 1. C<./server_config.json> - in the current working directory.
132  
133 =item 2. C<./mcp-config.json> - in the current working directory.
134  
135 =item 3. C<$HOME/.config/mcp-cli/config.json> - XDG-style user config
136 (C<$USERPROFILE> is used as the home directory on Windows).
137  
138 =back
139  
140 If none of these paths exist, C<load_config> returns C<undef> and the
141 caller must fall back to other means of locating the server (e.g. a bare
142 C<--server> command string).
143  
144 =head1 EXPORTS
145  
146 Nothing is exported by default. The following functions may be imported
147 explicitly:
148  
149 use App::MCP::CLI::Config qw(load_config resolve_server_cmd);
150  
151 =head1 FUNCTIONS
152  
153 =head2 load_config
154  
155 Signature: C<load_config( [$path] )>.
156  
157 my $config = load_config();
158 my $config = load_config('/path/to/server_config.json');
159  
160 Loads and parses the JSON configuration file at C<$path>. If C<$path> is
161 omitted or C<undef>, the auto-discovery sequence described in L</AUTO-DISCOVERY>
162 is used.
163  
164 Returns a hashref on success, or C<undef> when no path was given and no
165 well-known file exists. When a path B<is> given explicitly, a missing or
166 unreadable file, or invalid JSON, is a fatal error (C<die>), so typos in
167 C<--config> are never silently ignored. A discovered file that cannot be
168 parsed only produces a warning on STDERR and C<undef>.
169  
170 Informational messages are written to STDERR so that STDOUT stays clean for
171 piping.
172  
173 =cut
174  
175 29 ▶sub load_config {
Subroutine load_config: called CC=14 SCAR=37.8
Pod documented
176 29 my ($config_path) = @_;
177  
178 29 my $json = JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1);
179 29 ▶ my $explicit = defined $config_path && length $config_path;
Condition33%1/3
defined $config_path && length $config_path
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $config_path && length $config_path
defined $config_path length $config_path
Truth table33%1/3
A && B
A= defined $config_pathB= length $config_path
ABresult
0X0
100
111
180  
181 # Auto-discover if not explicitly given
182 29 ▶ if (!$explicit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($explicit) 0 29 29
183 0 ▶ my $home = $ENV{HOME} || $ENV{USERPROFILE} || '.';
Condition0%0/5
$ENV{'HOME'} || $ENV{'USERPROFILE'} || '.'
l!l
00
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || $ENV{'USERPROFILE'} || '.'
$ENV{"HOME"} $ENV{"USERPROFILE"}
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}
ABresult
1X1
011
000
184 0 my $xdg = File::Spec->catfile($home, '.config', 'mcp-cli', 'config.json');
185  
186 0 ▶ if (-f 'server_config.json') { $config_path = 'server_config.json' }
Branch: 3 branches
BranchTrueFalseTotal
if (-f 'server_config.json') { } 0 0 0
elsif (-f 'mcp-config.json') { } 0 0 0
elsif (-f $xdg) { } 0 0 0
187 0 elsif (-f 'mcp-config.json') { $config_path = 'mcp-config.json' }
188 0 elsif (-f $xdg) { $config_path = $xdg }
189 0 else { return undef }
190 }
191  
192 29 ▶ die "Config file not found: $config_path\n" if $explicit && !-f $config_path;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit and not -f $config_path 2 27 29
Condition66%2/3
$explicit and not -f $config_path
!ll&&!rl&&r
0272
MC/DC50%1/2
$explicit and not -f $config_path
$explicit not -f $config_path
Truth table66%2/3
A and not(B)
A= $explicitB= not -f $config_path
ABresult
0X0
100
111
193  
194 27 print STDERR "[-] Reading configuration from: $config_path\n";
195 27 ▶ open my $fh, '<', $config_path or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $config_path or do { die "Could not open config file '${config_path}': $!\n" if $explicit; warn "[-] Warning: Could not open config file: $!\n"; return undef } 27 0 27
196 0 ▶ die "Could not open config file '$config_path': $!\n" if $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit 0 0 0
197 0 warn "[-] Warning: Could not open config file: $!\n";
198 0 return undef;
199 };
200 27 my $content = do { local $/; <$fh> };
201 27 close $fh;
202  
203 27 my $data = eval { $json->decode($content) };
204 27 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 26 27
205 1 (my $err = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
206 1 ▶ die "Failed to parse config '$config_path': $err\n" if $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit 1 0 1
207 0 warn "[-] Warning: Failed to parse config JSON ($config_path): $err\n";
208 0 return undef;
209 }
210 26 return $data;
211 }
212  
213 =head2 resolve_server_cmd
214  
215 Signature: C<resolve_server_cmd( $config, $server_input )>.
216  
217 my ($cmd, $url, $env) = resolve_server_cmd($config, 'perl-math-server');
218 my ($cmd, $url, $env) = resolve_server_cmd($config, 'http://localhost/sse');
219 my ($cmd, $url, $env) = resolve_server_cmd($config, 'perl stdio_server.pl');
220 my ($cmd, $url, $env) = resolve_server_cmd($config); # sole configured server
221  
222 Resolves C<$server_input> to a transport endpoint. C<$config> is the hashref
223 returned by L</load_config> (may be C<undef>).
224  
225 Resolution order:
226  
227 =over 4
228  
229 =item 1. If C<$server_input> is empty or C<undef>: when the configuration
230 defines exactly one server, that server is used; otherwise this is a fatal
231 error that lists the available names.
232  
233 =item 2. If C<$config> contains a matching C<mcpServers> entry, that
234 descriptor is used. A descriptor with a C<url> is an SSE server. One whose
235 C<transport> is C<sse> but which has no C<url>, or one with neither C<url>
236 nor C<command>, is a fatal error with an explanatory message.
237  
238 =item 3. If C<$server_input> looks like an HTTP(S) URL, SSE mode is selected.
239  
240 =item 4. If C<$server_input> ends in C<.pl> and the file exists, it is run
241 with the current Perl interpreter (C<$^X>).
242  
243 =item 5. Otherwise, C<$server_input> is split on whitespace and used as a
244 raw command list.
245  
246 =back
247  
248 Return value is a three-element list C<($cmd, $url, $env)>:
249  
250 =over 4
251  
252 =item * For B<stdio> servers: C<$cmd> is an arrayref of command + arguments
253 suitable for passing to C<IPC::Open2::open2>; C<$url> is C<undef>.
254  
255 =item * For B<SSE> servers: C<$url> is the endpoint URL string; C<$cmd> is
256 C<undef>.
257  
258 =item * C<$env> is a hashref of extra environment variables from the
259 descriptor's C<env> block (empty when there is none). It is B<not> applied to
260 the current process; L<App::MCP::CLI::Client> sets it for the spawned server
261 only, so variables never leak from one server to another.
262  
263 =back
264  
265 =cut
266  
267 51 ▶sub resolve_server_cmd {
Subroutine resolve_server_cmd: called CC=19 SCAR=29.5
Pod documented
268 51 my ($config_data, $server_input) = @_;
269  
270 my $servers = ($config_data && ref $config_data->{mcpServers} eq 'HASH')
271 51 ▶ ? $config_data->{mcpServers} : {};
Branch: 1 branch
BranchTrueFalseTotal
$config_data && ref $config_data->{'mcpServers'} eq 'HASH' ? : 47 4 51
Condition66%2/3
$config_data and ref $config_data->{"mcpServers"} eq "HASH"
!ll&&!rl&&r
4047
MC/DC50%1/2
$config_data and ref $config_data->{"mcpServers"} eq "HASH"
$config_data ref $config_data->{"mcpServers"} eq "HASH"
Truth table66%2/3
A and B
A= $config_dataB= ref $config_data->{"mcpServers"} eq "HASH"
ABresult
0X0
100
111
272  
273 51 ▶ if (!defined $server_input || $server_input eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $server_input or $server_input eq "") 18 33 51
Condition66%2/3
not defined $server_input or $server_input eq ""
l!l&&r!l&&!r
18033
MC/DC50%1/2
not defined $server_input or $server_input eq ""
not defined $server_input $server_input eq ""
Truth table66%2/3
not(A) or B
A= not defined $server_inputB= $server_input eq ""
ABresult
1X1
011
000
274 18 my @names = sort keys %$servers;
275 18 ▶ return resolve_server_cmd($config_data, $names[0]) if @names == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names == 1 15 3 18
276  
277 3 ▶ die "No server specified. Use --server <name|url|command>"
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 2 1 3
278 . (@names ? ". Configured servers: " . join(', ', @names) : '')
279 . "\n";
280 }
281  
282 33 ▶ if (ref $servers->{$server_input} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $servers->{$server_input} eq "HASH") 30 3 33
283 30 my $srv = $servers->{$server_input};
284 30 print STDERR "[-] Resolved server settings for '$server_input' via config file.\n";
285  
286 30 ▶ my $env = ref $srv->{env} eq 'HASH' ? { %{ $srv->{env} } } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $srv->{'env'} eq 'HASH' ? : 1 29 30
287  
288 30 ▶ return (undef, $srv->{url}, $env) if defined $srv->{url} && length $srv->{url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $srv->{"url"} and length $srv->{"url"} 3 27 30
Condition66%2/3
defined $srv->{"url"} and length $srv->{"url"}
!ll&&!rl&&r
2703
MC/DC50%1/2
defined $srv->{"url"} and length $srv->{"url"}
defined $srv->{"url"} length $srv->{"url"}
Truth table66%2/3
A and B
A= defined $srv->{"url"}B= length $srv->{"url"}
ABresult
0X0
100
111
289  
290 die "Server '$server_input' has transport 'sse' but no 'url'.\n"
291 27 ▶ if lc($srv->{transport} // '') eq 'sse';
Branch: 1 branch
BranchTrueFalseTotal
if lc($srv->{"transport"} // "") eq "sse" 2 25 27
Condition100%2/2
$srv->{"transport"} // ""
l!l
225
MC/DC100%1/1
$srv->{"transport"} // ""
$srv->{"transport"}
Truth table100%2/2
A
A= $srv->{"transport"}
Aresult
11
00
292  
293 die "Server '$server_input' has neither a 'command' nor a 'url'.\n"
294 25 ▶ unless defined $srv->{command} && length $srv->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $srv->{"command"} and length $srv->{"command"} 1 24 25
Condition66%2/3
defined $srv->{"command"} and length $srv->{"command"}
!ll&&!rl&&r
1024
MC/DC50%1/2
defined $srv->{"command"} and length $srv->{"command"}
defined $srv->{"command"} length $srv->{"command"}
Truth table66%2/3
A and B
A= defined $srv->{"command"}B= length $srv->{"command"}
ABresult
0X0
100
111
295  
296 24 ▶ my $args = ref $srv->{args} eq 'ARRAY' ? $srv->{args} : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $srv->{'args'} eq 'ARRAY' ? : 20 4 24
297 24 return ([$srv->{command}, @$args], undef, $env);
298 }
299  
300 # Fallback
301 3 ▶ if ($server_input =~ /^https?:\/\//i) { return (undef, $server_input, {}) }
Branch: 2 branches
BranchTrueFalseTotal
if ($server_input =~ m[^https?://]i) { } 1 2 3
elsif ($server_input =~ /\.pl$/ and -f $server_input) { } 1 1 2
302 1 ▶ elsif ($server_input =~ /\.pl$/ && -f $server_input) { return ([$^X, $server_input], undef, {}) }
Condition66%2/3
$server_input =~ /\.pl$/ and -f $server_input
!ll&&!rl&&r
101
MC/DC50%1/2
$server_input =~ /\.pl$/ and -f $server_input
$server_input =~ /\.pl$/ -f $server_input
Truth table66%2/3
A and B
A= $server_input =~ /\.pl$/B= -f $server_input
ABresult
0X0
100
111
303 1 else { return ([split ' ', $server_input], undef, {}) }
304 }
305  
306 =head1 SEE ALSO
307  
308 =over 4
309  
310 =item * L<App::MCP::CLI>
311  
312 =item * L<App::MCP::CLI::Command::call>
313  
314 =item * L<App::MCP::CLI::Command::list>
315  
316 =item * L<App::MCP::CLI::Command::interactive>
317  
318 =item * L<App::MCP::CLI::Command::servers>
319  
320 =item * L<App::MCP::CLI::Client>
321  
322 =item * L<JSON::MaybeXS> - JSON parser used internally.
323  
324 =item * L<File::Spec> - used for portable config path construction.
325  
326 =item * MCP specification: L<https://spec.modelcontextprotocol.io/>
327  
328 =back
329  
330 =head1 AUTHOR
331  
332 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
333  
334 =head1 REPOSITORY
335  
336 L<https://github.com/manwar/App-MCP-CLI>
337  
338 =head1 BUGS
339  
340 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/App-MCP-CLI/issues>.
341 I will be notified and then you'll automatically be notified of progress on your
342 bug as I make changes.
343  
344 =head1 SUPPORT
345  
346 You can find documentation for this module with the perldoc command.
347  
348 perldoc App::MCP::CLI::Config
349  
350 You can also look for information at:
351  
352 =over 4
353  
354 =item * BUG Report
355  
356 L<https://github.com/manwar/App-MCP-CLI/issues>
357  
358 =item * Search MetaCPAN
359  
360 L<http://metacpan.org/dist/App-MCP-CLI>
361  
362 =back
363  
364 =head1 LICENSE AND COPYRIGHT
365  
366 Copyright (C) 2026 Mohammad Sajid Anwar.
367  
368 This program is free software; you can redistribute it and / or modify it under
369 the terms of the the Artistic License (2.0). You may obtain a copy of the full
370 license at:
371  
372 L<http://www.perlfoundation.org/artistic_license_2_0>
373  
374 Any use, modification, and distribution of the Standard or Modified Versions is
375 governed by this Artistic License.By using, modifying or distributing the Package,
376 you accept this license. Do not use, modify, or distribute the Package, if you do
377 not accept this license.
378  
379 If your Modified Version has been derived from a Modified Version made by someone
380 other than you,you are nevertheless required to ensure that your Modified Version
381 complies with the requirements of this license.
382  
383 This license does not grant you the right to use any trademark, service mark,
384 tradename, or logo of the Copyright Holder.
385  
386 This license includes the non-exclusive, worldwide, free-of-charge patent license
387 to make, have made, use, offer to sell, sell, import and otherwise transfer the
388 Package with respect to any patent claims licensable by the Copyright Holder that
389 are necessarily infringed by the Package. If you institute patent litigation
390 (including a cross-claim or counterclaim) against any party alleging that the
391 Package constitutes direct or contributory patent infringement,then this Artistic
392 License to you shall terminate on the date that such litigation is filed.
393  
394 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
395 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
396 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
397 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
398 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
399 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
400 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
401  
402 =cut
403  
404 31 1; # End of App::MCP::CLI::Config
« blib/lib/App/MCP/CLI/Command/call.pm ↑ Summary blib/lib/App/MCP/CLI/Client.pm »