| 1 | package App::MCP::CLI::Command::call; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 30 | our $VERSION = qv('v0.0.1'); | |||||||||||||||||||||||||||
| 8 | 30 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 30 | ▶ | use App::MCP::CLI -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 30 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 30 | ▶ | use App::MCP::CLI::Client qw(content_text); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 30 | ▶ | use App::MCP::CLI::Config qw(load_config resolve_server_cmd); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | App::MCP::CLI::Command::call - Invoke a specific tool on an MCP server | ||||||||||||||||||||||||||||
| 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 | # Invoke a tool with inline key=value arguments | ||||||||||||||||||||||||||||
| 29 | mcpcli call add_numbers --server perl-math-server a=10 b=5 | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Invoke a tool and be prompted for each argument | ||||||||||||||||||||||||||||
| 32 | mcpcli call add_numbers --server perl-math-server | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Against a script, a raw command, or an SSE URL | ||||||||||||||||||||||||||||
| 35 | mcpcli call add_numbers --server ./stdio_server.pl a=3 b=4 | ||||||||||||||||||||||||||||
| 36 | mcpcli call get_mock_weather --server http://localhost:8080/sse location=London | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | C<App::MCP::CLI::Command::call> implements the C<call> sub-command of the | ||||||||||||||||||||||||||||
| 41 | C<mcpcli> application. It connects to an MCP server over stdio or HTTP/SSE | ||||||||||||||||||||||||||||
| 42 | (through L<App::MCP::CLI::Client>), performs the handshake, and dispatches a | ||||||||||||||||||||||||||||
| 43 | C<tools/call> request. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 Argument handling | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Arguments are supplied on the command line as C<key=value> pairs after the | ||||||||||||||||||||||||||||
| 48 | tool name. Anything that is not a C<key=value> pair is a usage error. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | mcpcli call mytool param1=hello param2=42 | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | The command asks the server for the tool's input schema and uses it to give | ||||||||||||||||||||||||||||
| 53 | each value the right JSON type: | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =over 4 | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item * C<string> properties are always sent as strings, so C<007> stays | ||||||||||||||||||||||||||||
| 58 | C<"007"> and C<1.0> stays C<"1.0">. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item * C<integer> and C<number> properties are sent as numbers when the | ||||||||||||||||||||||||||||
| 61 | value looks numeric. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * C<boolean> properties accept C<true/false>, C<yes/no> and C<1/0>. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * C<array> and C<object> properties are parsed as JSON. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item * Properties without a declared type (or tools whose schema is not | ||||||||||||||||||||||||||||
| 68 | available) fall back to a conservative rule: plain decimal numbers without | ||||||||||||||||||||||||||||
| 69 | leading zeros become JSON numbers, everything else stays a string. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =back | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Required properties that were not supplied cause an error before anything is | ||||||||||||||||||||||||||||
| 74 | sent to the server. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | When no arguments are given at all, the command enters an B<interactive | ||||||||||||||||||||||||||||
| 77 | prompt mode>: it presents each property of the tool's schema in the terminal | ||||||||||||||||||||||||||||
| 78 | (prompts go to STDERR), labelling required fields in red when STDERR is a | ||||||||||||||||||||||||||||
| 79 | terminal. Entering an empty value for a required field aborts with an error. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 Output | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Only the tool's result is written to STDOUT, so it can be piped. Text blocks | ||||||||||||||||||||||||||||
| 84 | are printed as-is. Other block types are not dropped: images and audio become | ||||||||||||||||||||||||||||
| 85 | a one-line placeholder with their MIME type and size, resources are printed | ||||||||||||||||||||||||||||
| 86 | (or described), and C<structuredContent> is pretty-printed when there is no | ||||||||||||||||||||||||||||
| 87 | other content. Progress messages and errors go to STDERR. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 Exit status | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =over 4 | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item C<0> - the tool ran and reported success. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item C<1> - connection or configuration failure, timeout, unknown tool, | ||||||||||||||||||||||||||||
| 96 | missing argument, a JSON-RPC error from the server, or a tool result with | ||||||||||||||||||||||||||||
| 97 | C<isError: true> (its text is printed to STDERR). | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =back | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 USAGE | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | mcpcli call <tool_name> [key=value ...] [--server <name|path|url|command>] | ||||||||||||||||||||||||||||
| 104 | [--config <file>] [--timeout <seconds>] | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =over 4 | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =item C<--server|-s> I<name> | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Server name from the config file, a C<.pl> script, a URL, or a command. May be | ||||||||||||||||||||||||||||
| 113 | omitted when the config file defines exactly one server. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item C<--config|-c> I<path> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Path to the JSON configuration file. See L<App::MCP::CLI::Config>. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item C<--timeout|-t> I<seconds> | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | How long to wait for each server reply (default 30; C<0> waits forever). | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =back | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 opt_spec | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Defines the command-line option specification passed to L<Getopt::Long::Descriptive> | ||||||||||||||||||||||||||||
| 130 | via L<App::Cmd>. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 15 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | return ( | ||||||||||||||||||||||||||||
| 136 | 15 | [ 'server|s=s', 'server name, script path, URL or command' ], | |||||||||||||||||||||||||||
| 137 | [ 'config|c=s', 'path to config JSON file' ], | ||||||||||||||||||||||||||||
| 138 | [ 'timeout|t=i', 'seconds to wait for the server (0 = forever)', | ||||||||||||||||||||||||||||
| 139 | { default => 30 } ], | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 description | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Returns a one-line human-readable description of the command, used in the | ||||||||||||||||||||||||||||
| 146 | top-level C<mcpcli help> listing. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub description { 'Call/execute a specific tool on the server' } | ||||||||||||||||||||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 usage_desc | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Returns the usage string shown by C<mcpcli help call>. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 15 | ▶ | sub usage_desc { '%c call <tool_name> [key=value key2=value2]' } | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 validate_args | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Validates that a tool name was supplied and that every further argument is a | ||||||||||||||||||||||||||||
| 163 | C<key=value> pair; calls C<usage_error> otherwise. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 15 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 15 | my ($self, $opt, $args) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 15 | ▶ | $self->usage_error("You must specify a tool name to call.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | if !@$args; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 15 | my (undef, @pairs) = @$args; | |||||||||||||||||||||||||||
| 174 | 15 | foreach my $arg (@pairs) { | |||||||||||||||||||||||||||
| 175 | 20 | ▶ | $self->usage_error("Invalid argument '$arg': expected key=value.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | if $arg !~ /^[^=]+=/; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 execute( $opt, $args ) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Main entry point invoked by L<App::Cmd>. Connects to the configured MCP | ||||||||||||||||||||||||||||
| 183 | server, performs the protocol handshake, collects tool arguments (inline or | ||||||||||||||||||||||||||||
| 184 | interactively), submits the C<tools/call> request, and prints the response. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 14 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 14 | my ($self, $opt, $args) = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 14 | my ($tool_name, @pairs) = @$args; | |||||||||||||||||||||||||||
| 192 | 14 | ▶ | my %raw = map { /^([^=]+)=(.*)$/s ? ($1 => $2) : () } @pairs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 14 | my $status = eval { $self->_run($opt, $tool_name, \%raw, scalar @pairs) }; | |||||||||||||||||||||||||||
| 195 | 14 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | $err .= "\n" unless $err =~ /\n\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 4 | print STDERR "Error: $err"; | |||||||||||||||||||||||||||
| 198 | 4 | exit 1; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 10 | ▶ | exit $status if $status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3 | ▶ | sub _label { | ||||||||||||||||||||||||||
|
Subroutine
_label: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 205 | 3 | my ($text, $code) = @_; | |||||||||||||||||||||||||||
| 206 | 3 | ▶ | return -t STDERR ? "\e[${code}m$text\e[0m" : $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 14 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 210 | 14 | my ($self, $opt, $tool_name, $raw, $npairs) = @_; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 14 | my $config_data = load_config($opt->config); | |||||||||||||||||||||||||||
| 213 | 13 | my ($cmd, $url, $env) = resolve_server_cmd($config_data, $opt->server); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 12 | my $client = App::MCP::CLI::Client->new( | |||||||||||||||||||||||||||
| 216 | cmd => $cmd, url => $url, env => $env, timeout => $opt->timeout, | ||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||
| 218 | 12 | $client->connect; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 11 | my $status = eval { $self->_call($client, $tool_name, $raw, $npairs) }; | |||||||||||||||||||||||||||
| 221 | 11 | my $err = $@; | |||||||||||||||||||||||||||
| 222 | 11 | $client->close; | |||||||||||||||||||||||||||
| 223 | 11 | ▶ | die $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 10 | return $status; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 11 | ▶ | sub _call { | ||||||||||||||||||||||||||
|
Subroutine
_call: called CC=30 SCAR=39.1
| |||||||||||||||||||||||||||||
| 229 | 11 | my ($self, $client, $tool_name, $raw, $npairs) = @_; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Servers that do not implement tools/list can still be called directly. | ||||||||||||||||||||||||||||
| 232 | 11 | ▶ | my $tools = eval { $client->list_tools } || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$client->list_tools
}
} || []
MC/DC0%0/1
eval {
do {
$client->list_tools
}
} || []
eval {
do {
$client->list_tools
}
}
Truth table50%1/2
A
A= eval {
do {
$client->list_tools
}
}
| |||||||||||||||||||||||||||||
| 233 | 11 | ▶ | my ($tool) = grep { ($_->{name} // '') eq $tool_name } @$tools; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"name"} // ""
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table50%1/2
A
A= $_->{"name"}
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 11 | ▶ | if (@$tools && !$tool) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$tools and not $tool
MC/DC50%1/2
@$tools and not $tool
@$tools
not $tool
Truth table66%2/3
A and not(B)
A= @$toolsB= not $tool
| |||||||||||||||||||||||||||||
| 236 | print STDERR "Error: Tool '$tool_name' not found on server. Available: " | ||||||||||||||||||||||||||||
| 237 | 1 | ▶ | . join(', ', map { $_->{name} // () } @$tools) . "\n"; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_->{"name"} // ()
MC/DC0%0/2
$_->{"name"} // ()
$_->{"name"}
()
Truth table33%1/3
A // B
A= $_->{"name"}B= ()
| |||||||||||||||||||||||||||||
| 238 | 1 | return 1; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 10 | ▶ | my $schema = $tool ? ($tool->{inputSchema} || {}) : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$tool->{"inputSchema"} or {}
MC/DC0%0/1
$tool->{"inputSchema"} or {}
$tool->{"inputSchema"}
Truth table50%1/2
A
A= $tool->{"inputSchema"}
| |||||||||||||||||||||||||||||
| 242 | 10 | ▶ | my $props = ref $schema->{properties} eq 'HASH' ? $schema->{properties} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 10 | my %required = map { $_ => 1 } | |||||||||||||||||||||||||||
| 244 | 10 | ▶ | @{ ref $schema->{required} eq 'ARRAY' ? $schema->{required} : [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 10 | my %tool_args; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 10 | ▶ | if ($npairs) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 249 | 9 | $tool_args{$_} = _coerce_value($raw->{$_}, $props->{$_}) for keys %$raw; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 9 | my @missing = grep { !exists $tool_args{$_} } sort keys %required; | |||||||||||||||||||||||||||
| 252 | 9 | ▶ | if (@missing) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 1 | print STDERR "Error: Missing required argument(s): " . join(', ', @missing) . "\n"; | |||||||||||||||||||||||||||
| 254 | 1 | return 1; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | elsif (keys %$props) { | ||||||||||||||||||||||||||||
| 258 | 1 | print STDERR "\nEntering interactive prompt for '$tool_name':\n"; | |||||||||||||||||||||||||||
| 259 | 1 | foreach my $key (sort keys %$props) { | |||||||||||||||||||||||||||
| 260 | 2 | my $prop = $props->{$key}; | |||||||||||||||||||||||||||
| 261 | 2 | ▶ | my $desc = $prop->{description} // 'No description'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$prop->{"description"} // "No description"
MC/DC0%0/1
$prop->{"description"} // "No description"
$prop->{"description"}
Truth table50%1/2
A
A= $prop->{"description"}
| |||||||||||||||||||||||||||||
| 262 | 2 | ▶ | my $type = $prop->{type} // 'string'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$prop->{"type"} // "string"
MC/DC0%0/1
$prop->{"type"} // "string"
$prop->{"type"}
Truth table50%1/2
A
A= $prop->{"type"}
| |||||||||||||||||||||||||||||
| 263 | 2 | ▶ | $type = join('|', @$type) if ref $type eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 2 | ▶ | my $req = $required{$key} ? _label('(required)', '1;31') : '(optional)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 2 | print STDERR "=> $key $req [$type - $desc]: "; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 2 | my $input = <STDIN>; | |||||||||||||||||||||||||||
| 269 | 2 | ▶ | defined($input) ? chomp($input) : ($input = ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 2 | ▶ | if ($input eq '' && $required{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$input eq "" and $required{$key}
MC/DC0%0/2
$input eq "" and $required{$key}
$input eq ""
$required{$key}
Truth table33%1/3
A and B
A= $input eq ""B= $required{$key}
| |||||||||||||||||||||||||||||
| 272 | 0 | print STDERR _label("Error: $key is required!", '1;31') . "\n"; | |||||||||||||||||||||||||||
| 273 | 0 | return 1; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 2 | ▶ | $tool_args{$key} = _coerce_value($input, $prop) if $input ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 1 | print STDERR "\n"; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 9 | print STDERR "[-] Calling tool '$tool_name'...\n"; | |||||||||||||||||||||||||||
| 282 | 9 | my $res = $client->call_tool($tool_name, \%tool_args); | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 8 | ▶ | if (my $e = $res->{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | my $code = defined $e->{code} ? " (code $e->{code})" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | print STDERR _label('Error:', '1;31') . " " . ($e->{message} // 'unknown error') . "$code\n"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$e->{"message"} // "unknown error"
MC/DC0%0/1
$e->{"message"} // "unknown error"
$e->{"message"}
Truth table0%0/2
A
A= $e->{"message"}
| |||||||||||||||||||||||||||||
| 287 | 0 | return 1; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 8 | ▶ | my $result = $res->{result} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$res->{'result'} || {}
MC/DC0%0/1
$res->{'result'} || {}
$res->{'result'}
Truth table50%1/2
A
A= $res->{'result'}
| |||||||||||||||||||||||||||||
| 291 | 8 | my $text = content_text($result); | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 8 | ▶ | if ($result->{isError}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 1 | ▶ | print STDERR _label('Error:', '1;31') . " " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | . (length $text ? $text : 'the tool reported an error') . "\n"; | ||||||||||||||||||||||||||||
| 296 | 1 | return 1; | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 7 | ▶ | print "$text\n" if length $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 7 | return 0; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head2 _coerce_value( $value, $property_schema ) | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Plain function (not a method). Converts the command-line string C<$value> to | ||||||||||||||||||||||||||||
| 306 | the JSON type declared by C<$property_schema> (the property's entry in the | ||||||||||||||||||||||||||||
| 307 | tool's C<inputSchema>, may be C<undef>). See L</Argument handling> for the | ||||||||||||||||||||||||||||
| 308 | rules. Values that do not fit the declared type are passed through unchanged | ||||||||||||||||||||||||||||
| 309 | so that the server can report a precise validation error. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =cut | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 27 | ▶ | sub _coerce_value { | ||||||||||||||||||||||||||
|
Subroutine
_coerce_value: called CC=20 SCAR=30.3
| |||||||||||||||||||||||||||||
| 314 | 27 | my ($val, $prop) = @_; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 27 | ▶ | my $type = ref $prop eq 'HASH' ? $prop->{type} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 27 | ▶ | $type = (grep { defined && $_ ne 'null' } @$type)[0] if ref $type eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ && $_ ne 'null'
MC/DC50%1/2
defined $_ && $_ ne 'null'
defined $_
$_ ne 'null'
Truth table66%2/3
A && B
A= defined $_B= $_ ne 'null'
| |||||||||||||||||||||||||||||
| 318 | 27 | ▶ | $type //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$type //= ""
MC/DC100%1/1
$type //= ""
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # Numbers with more than 15 digits would lose precision as a Perl NV. | ||||||||||||||||||||||||||||
| 321 | 27 | ▶ | my $safe = sub { length($_[0] =~ s/\D//gr) <= 15 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 27 | ▶ | if ($type eq 'string') { return $val } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 24 | ▶ | if ($type eq 'integer') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 2 | ▶ | return ($val =~ /^-?\d+$/ && $safe->($val)) ? 0 + $val : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val =~ /^-?\d+$/ and &$safe($val)
MC/DC0%0/2
$val =~ /^-?\d+$/ and &$safe($val)
$val =~ /^-?\d+$/
&$safe($val)
Truth table33%1/3
A and B
A= $val =~ /^-?\d+$/B= &$safe($val)
| |||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 22 | ▶ | if ($type eq 'number') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 11 | ▶ | return ($val =~ /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?$/ && $safe->($val)) ? 0 + $val : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$val =~ /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?$/ and &$safe($val)
MC/DC50%1/2
$val =~ /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?$/ and &$safe($val)
$val =~ /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?$/
&$safe($val)
Truth table66%2/3
A and B
A= $val =~ /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?$/B= &$safe($val)
| |||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 11 | ▶ | if ($type eq 'boolean') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 3 | ▶ | return JSON::MaybeXS::true() if $val =~ /^(?:true|yes|1)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 1 | ▶ | return JSON::MaybeXS::false() if $val =~ /^(?:false|no|0)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | return $val; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 8 | ▶ | if ($type eq 'array' || $type eq 'object') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "array" or $type eq "object"
MC/DC100%2/2
$type eq "array" or $type eq "object"
$type eq "array"
$type eq "object"
Truth table100%3/3
A or B
A= $type eq "array"B= $type eq "object"
| |||||||||||||||||||||||||||||
| 340 | 2 | my $data = eval { JSON::MaybeXS->new(utf8 => 0, allow_nonref => 1)->decode($val) }; | |||||||||||||||||||||||||||
| 341 | 2 | ▶ | return ref $data ? $data : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # Unknown type: only plain decimals without leading zeros are numbers. | ||||||||||||||||||||||||||||
| 345 | 6 | ▶ | return 0 + $val if $val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/ && $safe->($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/ and &$safe($val)
MC/DC100%2/2
$val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/ and &$safe($val)
$val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/
&$safe($val)
Truth table100%3/3
A and B
A= $val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/B= &$safe($val)
| |||||||||||||||||||||||||||||
| 346 | 2 | return $val; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =over 4 | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =item * L<App::MCP::CLI> | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =item * L<App::MCP::CLI::Command::list> | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =item * L<App::MCP::CLI::Command::interactive> | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =back | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | L<https://github.com/manwar/App-MCP-CLI> | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head1 BUGS | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/App-MCP-CLI/issues>. | ||||||||||||||||||||||||||||
| 372 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 373 | bug as I make changes. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | perldoc App::MCP::CLI | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | You can also look for information at: | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =over 4 | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =item * BUG Report | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | L<https://github.com/manwar/App-MCP-CLI/issues> | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | L<http://metacpan.org/dist/App-MCP-CLI> | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =back | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | This program is free software; you can redistribute it and / or modify it under | ||||||||||||||||||||||||||||
| 400 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||||||||||
| 401 | license at: | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||||||||||||||||||||
| 406 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||||||||||||||||||||
| 407 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||||||||||||||||||||
| 408 | not accept this license. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||||||||||||||||||||
| 411 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||||||||||||||||||||
| 412 | complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | This license does not grant you the right to use any trademark, service mark, | ||||||||||||||||||||||||||||
| 415 | tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||||||||||||||||||||
| 418 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||||||||||||||||||||
| 419 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||||||||||||||||||||
| 420 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||||||||||||||||||||
| 421 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||||||||||||||||||||
| 422 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||||||||||||||||||||
| 423 | License to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||||||||||||||||||||
| 426 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||||||||||||||||||||
| 427 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||||||||||||||||||||
| 428 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||||||||||||||||||||
| 429 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||||||||||||||||||||
| 430 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||||||||||||||||||||
| 431 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =cut | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 30 | 1; # End of App::MCP::CLI::Command::call | |||||||||||||||||||||||||||