App-MCP-CLI

 view release on metacpan or  search on metacpan

lib/App/MCP/CLI/Config.pm  view on Meta::CPAN

C<App::MCP::CLI::Config> is the shared configuration layer for all
C<App::MCP::CLI> sub-commands. It provides two exported functions:

=over 4

=item * L</load_config> - reads and parses the JSON configuration file,
with automatic discovery of well-known paths when no explicit path is given.

=item * L</resolve_server_cmd> - maps a server name (or bare command string,
script path, or URL) to either a command array for stdio transport or a URL
for SSE transport.

=back

Centralising this logic here means that C<call>, C<list>, C<interactive>, and
any future sub-commands all share identical config-loading and server-resolution
behaviour.

=head1 CONFIGURATION FILE FORMAT

The JSON configuration file has the following top-level structure:

    {
      "confirmTools": true,
      "mcpServers": {
        "perl-math-server": {
          "command": "perl",
          "args": ["stdio_server.pl"],
          "env": { "DEBUG": "1" }
        },
        "remote-server": {
          "url": "http://mcp.example.com/sse"
        }
      }
    }

=head2 mcpServers

A JSON object whose keys are server alias names and whose values are server
descriptor objects. Each descriptor supports the following fields:

=over 4

=item C<command> (string)

The executable to launch when using stdio transport (e.g. C<"perl">,
C<"node">).  Required for stdio servers.

=item C<args> (array of strings)

Additional command-line arguments passed to C<command>
(e.g. C<["stdio_server.pl"]>). Defaults to an empty list.

=item C<env> (object)

A map of environment variable names to values. The entries are passed to the
server subprocess only; they are never set in the C<mcpcli> process itself.

=item C<url> (string)

The SSE endpoint URL for remote servers (e.g.
C<"http://mcp.example.com/sse">). When present, the server is contacted via
HTTP/SSE instead of a local subprocess.

=item C<transport> (string)

Informational (C<"stdio"> or C<"sse">). A server is treated as SSE when it has
a C<url>. C<transport: "sse"> without a C<url> is rejected with an error.

=back

=head2 confirmTools

Optional boolean (default C<true>). When set to C<false>, the
C<interactive>/C<chat> command executes tool calls autonomously without pausing
to ask the user for confirmation.

=head1 AUTO-DISCOVERY

When L</load_config> is called without an argument (or with C<undef>), it
searches for a configuration file in the following locations, in order:

=over 4

=item 1. C<./server_config.json> - in the current working directory.

=item 2. C<./mcp-config.json> - in the current working directory.

=item 3. C<$HOME/.config/mcp-cli/config.json> - XDG-style user config
(C<$USERPROFILE> is used as the home directory on Windows).

=back

If none of these paths exist, C<load_config> returns C<undef> and the
caller must fall back to other means of locating the server (e.g. a bare
C<--server> command string).

=head1 EXPORTS

Nothing is exported by default. The following functions may be imported
explicitly:

    use App::MCP::CLI::Config qw(load_config resolve_server_cmd);

=head1 FUNCTIONS

=head2 load_config

Signature: C<load_config( [$path] )>.

    my $config = load_config();
    my $config = load_config('/path/to/server_config.json');

Loads and parses the JSON configuration file at C<$path>. If C<$path> is
omitted or C<undef>, the auto-discovery sequence described in L</AUTO-DISCOVERY>
is used.

Returns a hashref on success, or C<undef> when no path was given and no
well-known file exists. When a path B<is> given explicitly, a missing or
unreadable file, or invalid JSON, is a fatal error (C<die>), so typos in
C<--config> are never silently ignored. A discovered file that cannot be
parsed only produces a warning on STDERR and C<undef>.

Informational messages are written to STDERR so that STDOUT stays clean for
piping.

=cut

sub load_config {
    my ($config_path) = @_;

    my $json     = JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1);
    my $explicit = defined $config_path && length $config_path;

    # Auto-discover if not explicitly given
    if (!$explicit) {
        my $home = $ENV{HOME} || $ENV{USERPROFILE} || '.';
        my $xdg  = File::Spec->catfile($home, '.config', 'mcp-cli', 'config.json');

        if    (-f 'server_config.json') { $config_path = 'server_config.json' }
        elsif (-f 'mcp-config.json')    { $config_path = 'mcp-config.json'    }
        elsif (-f $xdg)                 { $config_path = $xdg                 }
        else                            { return undef                        }
    }

    die "Config file not found: $config_path\n" if $explicit && !-f $config_path;

    print STDERR "[-] Reading configuration from: $config_path\n";
    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;
    };
    my $content = do { local $/; <$fh> };
    close $fh;

    my $data = eval { $json->decode($content) };
    if ($@) {
        (my $err = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
        die "Failed to parse config '$config_path': $err\n" if $explicit;
        warn "[-] Warning: Failed to parse config JSON ($config_path): $err\n";
        return undef;
    }
    return $data;
}

=head2 resolve_server_cmd

Signature: C<resolve_server_cmd( $config, $server_input )>.

    my ($cmd, $url, $env) = resolve_server_cmd($config, 'perl-math-server');
    my ($cmd, $url, $env) = resolve_server_cmd($config, 'http://localhost/sse');
    my ($cmd, $url, $env) = resolve_server_cmd($config, 'perl stdio_server.pl');
    my ($cmd, $url, $env) = resolve_server_cmd($config);   # sole configured server

Resolves C<$server_input> to a transport endpoint. C<$config> is the hashref
returned by L</load_config> (may be C<undef>).

Resolution order:

=over 4

=item 1. If C<$server_input> is empty or C<undef>: when the configuration
defines exactly one server, that server is used; otherwise this is a fatal
error that lists the available names.

=item 2. If C<$config> contains a matching C<mcpServers> entry, that
descriptor is used. A descriptor with a C<url> is an SSE server. One whose
C<transport> is C<sse> but which has no C<url>, or one with neither C<url>
nor C<command>, is a fatal error with an explanatory message.

=item 3. If C<$server_input> looks like an HTTP(S) URL, SSE mode is selected.

=item 4. If C<$server_input> ends in C<.pl> and the file exists, it is run
with the current Perl interpreter (C<$^X>).

=item 5. Otherwise, C<$server_input> is split on whitespace and used as a
raw command list.

=back

Return value is a three-element list C<($cmd, $url, $env)>:

=over 4

=item * For B<stdio> servers: C<$cmd> is an arrayref of command + arguments
suitable for passing to C<IPC::Open2::open2>; C<$url> is C<undef>.

=item * For B<SSE> servers: C<$url> is the endpoint URL string; C<$cmd> is
C<undef>.

=item * C<$env> is a hashref of extra environment variables from the
descriptor's C<env> block (empty when there is none). It is B<not> applied to
the current process; L<App::MCP::CLI::Client> sets it for the spawned server
only, so variables never leak from one server to another.

=back

=cut

sub resolve_server_cmd {
    my ($config_data, $server_input) = @_;

    my $servers = ($config_data && ref $config_data->{mcpServers} eq 'HASH')
                ? $config_data->{mcpServers} : {};

    if (!defined $server_input || $server_input eq '') {
        my @names = sort keys %$servers;
        return resolve_server_cmd($config_data, $names[0]) if @names == 1;

        die "No server specified. Use --server <name|url|command>"
          . (@names ? ". Configured servers: " . join(', ', @names) : '')
          . "\n";
    }

    if (ref $servers->{$server_input} eq 'HASH') {
        my $srv = $servers->{$server_input};
        print STDERR "[-] Resolved server settings for '$server_input' via config file.\n";

        my $env = ref $srv->{env} eq 'HASH' ? { %{ $srv->{env} } } : {};

        return (undef, $srv->{url}, $env) if defined $srv->{url} && length $srv->{url};

        die "Server '$server_input' has transport 'sse' but no 'url'.\n"
            if lc($srv->{transport} // '') eq 'sse';

        die "Server '$server_input' has neither a 'command' nor a 'url'.\n"
            unless defined $srv->{command} && length $srv->{command};

        my $args = ref $srv->{args} eq 'ARRAY' ? $srv->{args} : [];
        return ([$srv->{command}, @$args], undef, $env);
    }

    # Fallback
    if    ($server_input =~ /^https?:\/\//i)             { return (undef, $server_input, {}) }
    elsif ($server_input =~ /\.pl$/ && -f $server_input) { return ([$^X, $server_input], undef, {}) }
    else  { return ([split ' ', $server_input], undef, {}) }
}

=head1 SEE ALSO

=over 4

=item * L<App::MCP::CLI>

=item * L<App::MCP::CLI::Command::call>

=item * L<App::MCP::CLI::Command::list>



( run in 1.119 second using v1.01-cache-2.11-cpan-eb134014566 )