| 1 | package App::MCP::CLI::Command::interactive; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 29 | our $VERSION = qv('v0.0.1'); | |||||||||||||||||||||||||||||||||||||
| 8 | 29 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 29 | ▶ | use App::MCP::CLI -command; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 29 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 29 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 29 | ▶ | use App::MCP::CLI::Client qw(content_text); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 29 | ▶ | use App::MCP::CLI::Config qw(load_config resolve_server_cmd); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | App::MCP::CLI::Command::interactive - Agent REPL that routes natural-language queries to MCP tools via a local LLM | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | This module is distributed as part of L<App::MCP::CLI> v0.0.1. | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | # Start an interactive session against a named server in your config file | ||||||||||||||||||||||||||||||||||||||
| 30 | mcpcli interactive --server myserver | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | # Alias: the 'chat' sub-command is identical | ||||||||||||||||||||||||||||||||||||||
| 33 | mcpcli chat --server myserver | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | # Use a local Perl script as the server with a custom model | ||||||||||||||||||||||||||||||||||||||
| 36 | mcpcli interactive --server ./server.pl --model llama3 | ||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | # Point at an SSE server by URL | ||||||||||||||||||||||||||||||||||||||
| 39 | mcpcli interactive --server http://localhost:8080/sse | ||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | # Override every option explicitly | ||||||||||||||||||||||||||||||||||||||
| 42 | mcpcli interactive \ | ||||||||||||||||||||||||||||||||||||||
| 43 | --server myserver \ | ||||||||||||||||||||||||||||||||||||||
| 44 | --provider ollama \ | ||||||||||||||||||||||||||||||||||||||
| 45 | --model qwen2.5:1.5b \ | ||||||||||||||||||||||||||||||||||||||
| 46 | --config /etc/mcp/config.json | ||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | C<App::MCP::CLI::Command::interactive> implements the C<interactive> (alias | ||||||||||||||||||||||||||||||||||||||
| 51 | C<chat>) sub-command of the C<mcpcli> application. It starts a Read-Eval-Print | ||||||||||||||||||||||||||||||||||||||
| 52 | Loop (REPL) that: | ||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | =item 1. | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | Connects to the MCP server specified by C<--server> (either as a stdio | ||||||||||||||||||||||||||||||||||||||
| 59 | subprocess or via HTTP/SSE). | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | =item 2. | ||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | Retrieves the full tool catalogue from the server with C<tools/list>. | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | =item 3. | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | Enters a prompt loop where each user message is forwarded to a local LLM | ||||||||||||||||||||||||||||||||||||||
| 68 | (currently only B<Ollama> is supported) together with the tool catalogue | ||||||||||||||||||||||||||||||||||||||
| 69 | and a structured JSON dispatch schema. | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | =item 4. | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | If the LLM decides to invoke a tool (C<action: "call_tool">), the command | ||||||||||||||||||||||||||||||||||||||
| 74 | optionally asks the user for confirmation before executing the C<tools/call> | ||||||||||||||||||||||||||||||||||||||
| 75 | request and summarising the raw result via a second LLM call. | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | =item 5. | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | If no tool is needed (C<action: "reply">), the LLM's plain-text answer is | ||||||||||||||||||||||||||||||||||||||
| 80 | printed directly. | ||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | Type C<exit> or C<quit> (case-insensitive) at the prompt to end the session. | ||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | =head1 TRANSPORT MODES | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | =head2 stdio (default) | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | The server is launched as a child process via C<IPC::Open2>. Messages are | ||||||||||||||||||||||||||||||||||||||
| 91 | exchanged as newline-delimited JSON on its stdin/stdout. This is the simplest | ||||||||||||||||||||||||||||||||||||||
| 92 | mode and works with any compliant MCP server script. | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | =head2 HTTP/SSE | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | When the resolved server endpoint begins with C<http://> or C<https://>, or | ||||||||||||||||||||||||||||||||||||||
| 97 | when the config entry contains a C<url> key, the client connects in SSE mode. | ||||||||||||||||||||||||||||||||||||||
| 98 | It opens the event stream with a C<GET> request, reads the C<endpoint> event | ||||||||||||||||||||||||||||||||||||||
| 99 | to learn the C<POST> URL, then sends all JSON-RPC messages as C<POST> requests | ||||||||||||||||||||||||||||||||||||||
| 100 | to that URL. Responses are accepted either in the C<POST> reply body or as | ||||||||||||||||||||||||||||||||||||||
| 101 | C<message> events on the stream. See L<App::MCP::CLI::Client>. | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =head1 CONFIGURATION FILE | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | The command accepts a JSON configuration file (C<--config>) with the following | ||||||||||||||||||||||||||||||||||||||
| 106 | optional top-level keys: | ||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||
| 108 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | =item C<mcpServers> (object) | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | A map of server alias names to server descriptor objects. Each descriptor may | ||||||||||||||||||||||||||||||||||||||
| 113 | contain: | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | =item C<command> (string) - executable to launch (stdio mode). | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | =item C<args> (array of strings) - additional arguments for the executable. | ||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | =item C<env> (object) - environment variables for the launched server only. | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | =item C<url> (string) - SSE endpoint URL (SSE mode). | ||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | =item C<transport> (string) - informational (C<"stdio"> or C<"sse">); a server | ||||||||||||||||||||||||||||||||||||||
| 126 | is SSE when it has a C<url>. C<"sse"> without a C<url> is an error. | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | =item C<confirmTools> (boolean, default true) | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | When true, the agent pauses before executing any tool call and asks the user | ||||||||||||||||||||||||||||||||||||||
| 133 | for explicit confirmation. Set to C<false> to allow autonomous execution. | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||
| 137 | The configuration file is searched in the following order when C<--config> is | ||||||||||||||||||||||||||||||||||||||
| 138 | not given: | ||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | =item 1. C<./server_config.json> in the current directory. | ||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||
| 144 | =item 2. C<./mcp-config.json> in the current directory. | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | =item 3. C<$HOME/.config/mcp-cli/config.json>. | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | Example configuration: | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||||||||||||
| 153 | "confirmTools": true, | ||||||||||||||||||||||||||||||||||||||
| 154 | "mcpServers": { | ||||||||||||||||||||||||||||||||||||||
| 155 | "myserver": { | ||||||||||||||||||||||||||||||||||||||
| 156 | "command": "node", | ||||||||||||||||||||||||||||||||||||||
| 157 | "args": ["/opt/mcp/server.js"], | ||||||||||||||||||||||||||||||||||||||
| 158 | "env": { "DEBUG": "1" } | ||||||||||||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||||||||||||
| 160 | "remote": { | ||||||||||||||||||||||||||||||||||||||
| 161 | "url": "http://mcp.example.com/sse" | ||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||
| 166 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||
| 170 | =item C<--server|-s> I<name_or_path> | ||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||
| 172 | The MCP server to connect to (optional when the config file defines exactly | ||||||||||||||||||||||||||||||||||||||
| 173 | one server). Can be: | ||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||
| 177 | =item * A key in the C<mcpServers> section of the config file. | ||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | =item * A C<.pl> file path (launched with the current Perl interpreter). | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | =item * An C<http://> or C<https://> URL (SSE mode). | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | =item * A bare shell command string (split on whitespace). | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | =back | ||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||
| 187 | =item C<--provider|-p> I<name> (default: C<ollama>) | ||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||
| 189 | The LLM provider to use for query routing and result summarisation. Only | ||||||||||||||||||||||||||||||||||||||
| 190 | C<ollama> is currently supported. | ||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||
| 192 | =item C<--model|-m> I<name> (default: C<qwen2.5:1.5b>) | ||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||
| 194 | The Ollama model tag to query. Any model available on the Ollama instance | ||||||||||||||||||||||||||||||||||||||
| 195 | (L<http://localhost:11434> unless C<OLLAMA_HOST> is set) can be specified. | ||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | =item C<--config|-c> I<path> | ||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||
| 199 | Path to a JSON configuration file. See L</CONFIGURATION FILE> above. | ||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||
| 201 | =item C<--timeout|-t> I<seconds> | ||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | How long to wait for each MCP server reply (default 30; C<0> waits forever). | ||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||
| 207 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub description { 'Start an interactive agent shell with an MCP server' } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub abstract { 'Start an interactive agent shell with an MCP server' } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
abstract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||
| 214 | =head2 command_names | ||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||
| 216 | Returns C<qw(interactive chat)> so that the sub-command can be invoked by | ||||||||||||||||||||||||||||||||||||||
| 217 | either name. | ||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||
| 221 | 36 | ▶ | sub command_names { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
command_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 222 | 36 | return qw(interactive chat); | |||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | =head2 opt_spec | ||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||
| 227 | Defines the command-line option specification passed to L<Getopt::Long::Descriptive> | ||||||||||||||||||||||||||||||||||||||
| 228 | via L<App::Cmd>. | ||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||
| 232 | 8 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 233 | return ( | ||||||||||||||||||||||||||||||||||||||
| 234 | 8 | [ 'server|s=s', | |||||||||||||||||||||||||||||||||||||
| 235 | 'MCP server command, script path, or server name from config' | ||||||||||||||||||||||||||||||||||||||
| 236 | ], | ||||||||||||||||||||||||||||||||||||||
| 237 | [ 'provider|p=s', | ||||||||||||||||||||||||||||||||||||||
| 238 | 'LLM provider name (e.g., ollama)', | ||||||||||||||||||||||||||||||||||||||
| 239 | { default => 'ollama' } | ||||||||||||||||||||||||||||||||||||||
| 240 | ], | ||||||||||||||||||||||||||||||||||||||
| 241 | [ 'model|m=s', | ||||||||||||||||||||||||||||||||||||||
| 242 | 'LLM model to query (e.g., qwen2.5:1.5b)', | ||||||||||||||||||||||||||||||||||||||
| 243 | { default => 'qwen2.5:1.5b' } | ||||||||||||||||||||||||||||||||||||||
| 244 | ], | ||||||||||||||||||||||||||||||||||||||
| 245 | [ 'config|c=s', | ||||||||||||||||||||||||||||||||||||||
| 246 | 'Path to MCP configuration JSON file' | ||||||||||||||||||||||||||||||||||||||
| 247 | ], | ||||||||||||||||||||||||||||||||||||||
| 248 | [ 'timeout|t=i', | ||||||||||||||||||||||||||||||||||||||
| 249 | 'seconds to wait for the MCP server (0 = forever)', | ||||||||||||||||||||||||||||||||||||||
| 250 | { default => 30 } | ||||||||||||||||||||||||||||||||||||||
| 251 | ], | ||||||||||||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||
| 255 | =head2 execute( $opt, $args ) | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | Main entry point. Resolves the server, connects to it, retrieves tools, | ||||||||||||||||||||||||||||||||||||||
| 258 | and runs the agent REPL until the user types C<exit>/C<quit> or sends EOF. | ||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||
| 260 | The model's decision is validated before anything is executed: it must be a | ||||||||||||||||||||||||||||||||||||||
| 261 | JSON object, and a requested tool must exist on the server (otherwise the | ||||||||||||||||||||||||||||||||||||||
| 262 | request is ignored with a message). Tool failures (transport errors, JSON-RPC | ||||||||||||||||||||||||||||||||||||||
| 263 | errors, or results flagged C<isError>) are reported and the session continues. | ||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||
| 267 | 8 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=34 SCAR=41.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 268 | 8 | my ($self, $opt, $args) = @_; | |||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 8 | my $json = JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1); | |||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | 8 | my ($client, $tools); | |||||||||||||||||||||||||||||||||||||
| 273 | 8 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||
| 274 | 8 | my $config_data = load_config($opt->config); | |||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | 8 | $self->{confirm_tools} = 1; | |||||||||||||||||||||||||||||||||||||
| 277 | 8 | ▶ | if ($config_data && exists $config_data->{confirmTools}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config_data and exists $config_data->{"confirmTools"}
MC/DC50%1/2
$config_data and exists $config_data->{"confirmTools"}
$config_data
exists $config_data->{"confirmTools"}
Truth table66%2/3
A and B
A= $config_dataB= exists $config_data->{"confirmTools"}
| |||||||||||||||||||||||||||||||||||||||
| 278 | 7 | ▶ | $self->{confirm_tools} = $config_data->{confirmTools} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | 8 | ▶ | my ($cmd, $url, $env) = | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opt->server // $args->[0]
MC/DC0%0/2
$opt->server // $args->[0]
$opt->server
$args->[0]
Truth table33%1/3
A // B
A= $opt->serverB= $args->[0]
| |||||||||||||||||||||||||||||||||||||||
| 282 | resolve_server_cmd($config_data, $opt->server // $args->[0]); | ||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||
| 284 | 8 | ▶ | if (defined $url) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 285 | 0 | print "[-] Connecting to MCP Server via HTTP/SSE: $url\n"; | |||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||
| 287 | else { | ||||||||||||||||||||||||||||||||||||||
| 288 | 8 | print "[-] Launching Local Pipe MCP Server: @$cmd\n"; | |||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||
| 291 | 8 | $client = App::MCP::CLI::Client->new( | |||||||||||||||||||||||||||||||||||||
| 292 | cmd => $cmd, url => $url, env => $env, | ||||||||||||||||||||||||||||||||||||||
| 293 | timeout => $opt->timeout, client_name => 'mcp-interactive-client', | ||||||||||||||||||||||||||||||||||||||
| 294 | ); | ||||||||||||||||||||||||||||||||||||||
| 295 | 8 | $client->connect; | |||||||||||||||||||||||||||||||||||||
| 296 | 7 | ▶ | print "[-] SSE Channel Established. Target Post Route: $client->{post_url}\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 297 | if $client->is_sse; | ||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||
| 299 | 7 | $tools = $client->list_tools; | |||||||||||||||||||||||||||||||||||||
| 300 | 7 | 1; | |||||||||||||||||||||||||||||||||||||
| 301 | }; | ||||||||||||||||||||||||||||||||||||||
| 302 | 8 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | my $err = $@ || "unknown error\n"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || "unknown error\n"
MC/DC0%0/1
$@ || "unknown error\n"
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | $err .= "\n" unless $err =~ /\n\z/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 305 | 1 | print STDERR "Error: $err"; | |||||||||||||||||||||||||||||||||||||
| 306 | 1 | exit 1; | |||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||
| 309 | 7 | print "[+] Connected to MCP Server. Found " . scalar(@$tools) . " tool(s).\n"; | |||||||||||||||||||||||||||||||||||||
| 310 | 7 | print "[-] Connected to LLM provider '" . $opt->provider . "' using model '" . $opt->model . "'.\n"; | |||||||||||||||||||||||||||||||||||||
| 311 | 7 | print "[-] Starting agent loop. Type 'exit' or 'quit' to end session.\n\n"; | |||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||
| 313 | 7 | $self->{provider} = $opt->provider; | |||||||||||||||||||||||||||||||||||||
| 314 | 7 | $self->{model} = $opt->model; | |||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||
| 316 | 2 | ▶ | my $warn = sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||
| 317 | 2 | my ($label, $msg) = @_; | |||||||||||||||||||||||||||||||||||||
| 318 | 2 | ▶ | $msg = "unknown error" unless defined $msg && length $msg; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $msg and length $msg
MC/DC0%0/2
defined $msg and length $msg
defined $msg
length $msg
Truth table33%1/3
A and B
A= defined $msgB= length $msg
| |||||||||||||||||||||||||||||||||||||||
| 319 | 2 | $msg =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||
| 320 | 2 | print "\e[1;31m$label\e[0m $msg\n\n"; | |||||||||||||||||||||||||||||||||||||
| 321 | 7 | }; | |||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||
| 323 | 7 | while (1) { | |||||||||||||||||||||||||||||||||||||
| 324 | 15 | print "\e[1;36mYou>\e[0m "; | |||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | 15 | my $user_query = <STDIN>; | |||||||||||||||||||||||||||||||||||||
| 327 | 15 | ▶ | last if !defined $user_query; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 328 | 15 | chomp($user_query); | |||||||||||||||||||||||||||||||||||||
| 329 | 15 | ▶ | last if $user_query =~ /^(exit|quit)$/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 330 | 8 | ▶ | next if $user_query eq ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||
| 332 | 8 | print "Thinking...\n"; | |||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||
| 334 | 8 | my $decision = $self->_ask_llm($user_query, $tools); | |||||||||||||||||||||||||||||||||||||
| 335 | my $action = ref $decision eq 'HASH' && !ref $decision->{action} | ||||||||||||||||||||||||||||||||||||||
| 336 | 8 | ▶ | ? ($decision->{action} // '') : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
ref $decision eq "HASH" and not ref $decision->{"action"}
$decision->{"action"} // ""
MC/DC0%0/2
ref $decision eq "HASH" and not ref $decision->{"action"}
ref $decision eq "HASH"
not ref $decision->{"action"}
MC/DC0%0/1
$decision->{"action"} // ""
$decision->{"action"}
Truth table33%1/3
A and not(B)
A= ref $decision eq "HASH"B= not ref $decision->{"action"}
Truth table50%1/2
A
A= $decision->{"action"}
| |||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||
| 338 | 8 | ▶ | if ($action eq 'call_tool') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 339 | 5 | ▶ | my $tool_name = ref $decision->{tool} ? '' : ($decision->{tool} // ''); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$decision->{"tool"} // ""
MC/DC0%0/1
$decision->{"tool"} // ""
$decision->{"tool"}
Truth table50%1/2
A
A= $decision->{"tool"}
| |||||||||||||||||||||||||||||||||||||||
| 340 | 5 | ▶ | my $tool_args = ref $decision->{arguments} eq 'HASH' ? $decision->{arguments} : {}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||
| 342 | 5 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||
| 343 | 5 | ▶ | if (!$tool) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 344 | 1 | $warn->('Response:', "The model asked for a tool that does not exist ('$tool_name'); ignoring it."); | |||||||||||||||||||||||||||||||||||||
| 345 | 1 | next; | |||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||
| 348 | 4 | print " -> \e[1;33m[Agent decided to call tool '$tool_name' with args: " . | |||||||||||||||||||||||||||||||||||||
| 349 | $json->encode($tool_args) . | ||||||||||||||||||||||||||||||||||||||
| 350 | "]\e[0m\n"; | ||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||
| 352 | 4 | my $proceed = 1; | |||||||||||||||||||||||||||||||||||||
| 353 | 4 | ▶ | if ($self->{confirm_tools}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 354 | 0 | print " -> \e[1;35m[Security] Allow execution of '$tool_name'? (y/N):\e[0m "; | |||||||||||||||||||||||||||||||||||||
| 355 | 0 | my $ans = <STDIN>; | |||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | chomp($ans) if defined $ans; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if (lc($ans // '') ne 'y') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ans // ""
MC/DC0%0/1
$ans // ""
$ans
Truth table0%0/2
A
A= $ans
| |||||||||||||||||||||||||||||||||||||||
| 358 | 0 | print "[-] Execution aborted by user request.\n\n"; | |||||||||||||||||||||||||||||||||||||
| 359 | 0 | $proceed = 0; | |||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | 4 | ▶ | if ($proceed) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 364 | 4 | my $tool_res = eval { $client->call_tool($tool_name, $tool_args) }; | |||||||||||||||||||||||||||||||||||||
| 365 | 4 | ▶ | if (!$tool_res) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 366 | 0 | $warn->('Error:', $@); | |||||||||||||||||||||||||||||||||||||
| 367 | 0 | next; | |||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||
| 369 | 4 | ▶ | if (my $e = $tool_res->{error}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $warn->('Error:', $e->{message}); | |||||||||||||||||||||||||||||||||||||
| 371 | 0 | next; | |||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||
| 374 | 4 | ▶ | my $result = $tool_res->{result} || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tool_res->{'result'} || {}
MC/DC0%0/1
$tool_res->{'result'} || {}
$tool_res->{'result'}
Truth table50%1/2
A
A= $tool_res->{'result'}
| |||||||||||||||||||||||||||||||||||||||
| 375 | 4 | my $raw_result = content_text($result); | |||||||||||||||||||||||||||||||||||||
| 376 | 4 | ▶ | if ($result->{isError}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 377 | 1 | $warn->('Tool error:', $raw_result); | |||||||||||||||||||||||||||||||||||||
| 378 | 1 | next; | |||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||
| 381 | 3 | my $final_reply = $self->_summarize_with_llm($user_query, $raw_result); | |||||||||||||||||||||||||||||||||||||
| 382 | 3 | print "\e[1;32mResponse:\e[0m $final_reply\n\n"; | |||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||
| 385 | elsif ($action eq 'reply') { | ||||||||||||||||||||||||||||||||||||||
| 386 | 3 | my $answer = $decision->{answer}; | |||||||||||||||||||||||||||||||||||||
| 387 | 3 | ▶ | $answer = $json->encode($answer) if ref $answer; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 388 | 3 | ▶ | print "\e[1;32mResponse:\e[0m " . ($answer // '') . "\n\n"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$answer // ""
MC/DC0%0/1
$answer // ""
$answer
Truth table50%1/2
A
A= $answer
| |||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||
| 390 | else { | ||||||||||||||||||||||||||||||||||||||
| 391 | 0 | print "\e[1;31mResponse:\e[0m Failed to parse structural response from LLM.\n\n"; | |||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||
| 395 | 7 | $client->close; | |||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||
| 397 | 7 | print "[-] Session Closed.\n"; | |||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||
| 400 | =head2 _ollama_url | ||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||
| 402 | Returns the Ollama C</api/generate> URL. The base address defaults to | ||||||||||||||||||||||||||||||||||||||
| 403 | C<http://localhost:11434> and can be changed with the C<OLLAMA_HOST> | ||||||||||||||||||||||||||||||||||||||
| 404 | environment variable (with or without the C<http://> prefix). | ||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||
| 408 | 11 | ▶ | sub _ollama_url { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_ollama_url: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||
| 409 | 11 | ▶ | my $base = $ENV{OLLAMA_HOST} || 'http://localhost:11434'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'OLLAMA_HOST'} || 'http://localhost:11434'
MC/DC0%0/1
$ENV{'OLLAMA_HOST'} || 'http://localhost:11434'
$ENV{'OLLAMA_HOST'}
Truth table50%1/2
A
A= $ENV{'OLLAMA_HOST'}
| |||||||||||||||||||||||||||||||||||||||
| 410 | 11 | ▶ | $base = "http://$base" unless $base =~ m{^https?://}i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 411 | 11 | $base =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||
| 412 | 11 | return "$base/api/generate"; | |||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||
| 415 | =head2 _ask_llm( $query, $tools ) | ||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||
| 417 | Sends C<$query> together with the serialised C<$tools> array to the Ollama | ||||||||||||||||||||||||||||||||||||||
| 418 | C</api/generate> endpoint. The system prompt instructs the model to respond | ||||||||||||||||||||||||||||||||||||||
| 419 | with a bare JSON object in one of two forms: | ||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | { "action": "call_tool", "tool": "<name>", "arguments": { ... } } | ||||||||||||||||||||||||||||||||||||||
| 422 | { "action": "reply", "answer": "<text>" } | ||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||
| 424 | Returns the decoded hashref. If the model's output is not a JSON object, or | ||||||||||||||||||||||||||||||||||||||
| 425 | the request fails, a synthetic C<reply> hashref describing the problem (or | ||||||||||||||||||||||||||||||||||||||
| 426 | containing the raw text) is returned instead. | ||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||
| 430 | 8 | ▶ | sub _ask_llm { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_ask_llm: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||||||||||||
| 431 | 8 | my ($self, $query, $tools) = @_; | |||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||
| 433 | 8 | ▶ | if (lc($self->{provider}) ne 'ollama') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 434 | return { | ||||||||||||||||||||||||||||||||||||||
| 435 | action => 'reply', | ||||||||||||||||||||||||||||||||||||||
| 436 | 0 | answer => "Error: Provider '" . $self->{provider} . "' is not supported. Please configure '--provider ollama'." | |||||||||||||||||||||||||||||||||||||
| 437 | }; | ||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||
| 440 | 8 | my $ua = HTTP::Tiny->new(timeout => 30); | |||||||||||||||||||||||||||||||||||||
| 441 | 8 | my $json = JSON::MaybeXS->new(utf8 => 1); | |||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||
| 443 | 8 | my $system_prompt = "You are an AI assistant with access to these local tools:\n" | |||||||||||||||||||||||||||||||||||||
| 444 | . $json->encode($tools) . "\n" | ||||||||||||||||||||||||||||||||||||||
| 445 | . "If the user request requires a tool, respond ONLY with a JSON object like: {\"action\":\"call_tool\",\"tool\":\"tool_name\",\"arguments\":{...}}\n" | ||||||||||||||||||||||||||||||||||||||
| 446 | . "If no tool is needed, respond with: {\"action\":\"reply\",\"answer\":\"your text answer here\"}\n" | ||||||||||||||||||||||||||||||||||||||
| 447 | . "Respond with valid raw JSON only, no markdown blocks."; | ||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||
| 449 | my $res = $ua->post( | ||||||||||||||||||||||||||||||||||||||
| 450 | _ollama_url(), | ||||||||||||||||||||||||||||||||||||||
| 451 | { | ||||||||||||||||||||||||||||||||||||||
| 452 | headers => { 'content-type' => 'application/json' }, | ||||||||||||||||||||||||||||||||||||||
| 453 | content => $json->encode({ | ||||||||||||||||||||||||||||||||||||||
| 454 | model => $self->{model}, | ||||||||||||||||||||||||||||||||||||||
| 455 | 8 | prompt => "$system_prompt\n\nUser Question: $query", | |||||||||||||||||||||||||||||||||||||
| 456 | stream => JSON::MaybeXS::false() | ||||||||||||||||||||||||||||||||||||||
| 457 | }) | ||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||
| 459 | ); | ||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||
| 461 | 8 | ▶ | if ($res->{success}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 462 | # Raw text body resides inside the 'content' key | ||||||||||||||||||||||||||||||||||||||
| 463 | 8 | my $body = eval { $json->decode($res->{content}) }; | |||||||||||||||||||||||||||||||||||||
| 464 | 8 | ▶ | my $raw_response = ref $body eq 'HASH' ? $body->{response} : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||
| 466 | 8 | ▶ | return { action => 'reply', answer => 'The LLM returned an empty or unreadable response.' } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $raw_response and not ref $raw_response
MC/DC50%1/2
defined $raw_response and not ref $raw_response
defined $raw_response
not ref $raw_response
Truth table66%2/3
A and not(B)
A= defined $raw_responseB= not ref $raw_response
| |||||||||||||||||||||||||||||||||||||||
| 467 | unless defined $raw_response && !ref $raw_response; | ||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||
| 469 | 7 | $raw_response =~ s/\`{3}(?:json)?//g; | |||||||||||||||||||||||||||||||||||||
| 470 | 7 | $raw_response =~ s/\`{3}//g; | |||||||||||||||||||||||||||||||||||||
| 471 | 7 | $raw_response =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||
| 473 | 7 | my $parsed = eval { $json->decode($raw_response) }; | |||||||||||||||||||||||||||||||||||||
| 474 | 7 | ▶ | if ($@ || ref $parsed ne 'HASH') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or ref $parsed ne "HASH"
MC/DC50%1/2
$@ or ref $parsed ne "HASH"
$@
ref $parsed ne "HASH"
Truth table66%2/3
A or B
A= $@B= ref $parsed ne "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 475 | 1 | return { action => 'reply', answer => $raw_response }; | |||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||
| 478 | 6 | return $parsed; | |||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||
| 481 | return { | ||||||||||||||||||||||||||||||||||||||
| 482 | 0 | action => 'reply', | |||||||||||||||||||||||||||||||||||||
| 483 | answer => "HTTP Error ($res->{status}): $res->{reason}" | ||||||||||||||||||||||||||||||||||||||
| 484 | }; | ||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||
| 487 | =head2 _summarize_with_llm( $query, $result ) | ||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||
| 489 | Sends the original C<$query> and the raw tool C<$result> text to Ollama and | ||||||||||||||||||||||||||||||||||||||
| 490 | asks for a concise natural-language summary. Returns the summary string, or | ||||||||||||||||||||||||||||||||||||||
| 491 | the raw C<$result> on failure. | ||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||
| 493 | =cut | ||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||
| 495 | 3 | ▶ | sub _summarize_with_llm { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_summarize_with_llm: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||
| 496 | 3 | my ($self, $query, $result) = @_; | |||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||
| 498 | 3 | ▶ | if (lc($self->{provider}) ne 'ollama') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 499 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||
| 502 | 3 | my $ua = HTTP::Tiny->new(timeout => 30); | |||||||||||||||||||||||||||||||||||||
| 503 | 3 | my $json = JSON::MaybeXS->new(utf8 => 1); | |||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||
| 505 | 3 | my $prompt = "The user asked: '$query'. The tool returned this raw result data:\n$result\n" | |||||||||||||||||||||||||||||||||||||
| 506 | . "Provide a brief, natural language response answering the user's question based on this data."; | ||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||
| 508 | my $res = $ua->post( | ||||||||||||||||||||||||||||||||||||||
| 509 | _ollama_url(), | ||||||||||||||||||||||||||||||||||||||
| 510 | { | ||||||||||||||||||||||||||||||||||||||
| 511 | headers => { 'content-type' => 'application/json' }, | ||||||||||||||||||||||||||||||||||||||
| 512 | content => $json->encode({ | ||||||||||||||||||||||||||||||||||||||
| 513 | model => $self->{model}, | ||||||||||||||||||||||||||||||||||||||
| 514 | 3 | prompt => $prompt, | |||||||||||||||||||||||||||||||||||||
| 515 | stream => JSON::MaybeXS::false() | ||||||||||||||||||||||||||||||||||||||
| 516 | }) | ||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||
| 518 | ); | ||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||
| 520 | 3 | ▶ | if ($res->{success}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 521 | 3 | my $body = eval { $json->decode($res->{content}) }; | |||||||||||||||||||||||||||||||||||||
| 522 | 3 | ▶ | return (ref $body eq 'HASH' && defined $body->{response}) ? $body->{response} : $result; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $body eq "HASH" and defined $body->{"response"}
MC/DC0%0/2
ref $body eq "HASH" and defined $body->{"response"}
ref $body eq "HASH"
defined $body->{"response"}
Truth table33%1/3
A and B
A= ref $body eq "HASH"B= defined $body->{"response"}
| |||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||
| 525 | 0 | return "HTTP Error ($res->{status}): $res->{reason}"; | |||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||
| 528 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||
| 530 | In addition to the core L<App::MCP::CLI> dependencies, this command requires: | ||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||
| 532 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||
| 534 | =item * L<HTTP::Tiny> - for Ollama API calls and SSE transport. | ||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||
| 536 | =back | ||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||
| 538 | A running L<Ollama|https://ollama.com> instance on C<http://localhost:11434> | ||||||||||||||||||||||||||||||||||||||
| 539 | is required for the LLM routing functionality. | ||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||
| 541 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||
| 543 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||
| 545 | =item * L<App::MCP::CLI> | ||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||
| 547 | =item * L<App::MCP::CLI::Command::list> | ||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||
| 549 | =item * L<App::MCP::CLI::Command::call> | ||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||
| 551 | =back | ||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||
| 553 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||
| 555 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||
| 557 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||
| 559 | L<https://github.com/manwar/App-MCP-CLI> | ||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||
| 561 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||
| 563 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/App-MCP-CLI/issues>. | ||||||||||||||||||||||||||||||||||||||
| 564 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||||||||||||
| 565 | bug as I make changes. | ||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||
| 567 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||
| 569 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||
| 571 | perldoc App::MCP::CLI | ||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||
| 573 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||
| 575 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||
| 577 | =item * BUG Report | ||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||
| 579 | L<https://github.com/manwar/App-MCP-CLI/issues> | ||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||
| 581 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||
| 583 | L<http://metacpan.org/dist/App-MCP-CLI> | ||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||
| 585 | =back | ||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||
| 587 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||
| 589 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||
| 591 | This program is free software; you can redistribute it and / or modify it under | ||||||||||||||||||||||||||||||||||||||
| 592 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||||||||||||||||||||
| 593 | license at: | ||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||
| 595 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||
| 597 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||||||||||||||||||||||||||||||
| 598 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||||||||||||||||||||||||||||||
| 599 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||||||||||||||||||||||||||||||
| 600 | not accept this license. | ||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||
| 602 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||||||||||||||||||||||||||||||
| 603 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||||||||||||||||||||||||||||||
| 604 | complies with the requirements of this license. | ||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||
| 606 | This license does not grant you the right to use any trademark, service mark, | ||||||||||||||||||||||||||||||||||||||
| 607 | tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||
| 609 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||||||||||||||||||||||||||||||
| 610 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||||||||||||||||||||||||||||||
| 611 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||||||||||||||||||||||||||||||
| 612 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||||||||||||||||||||||||||||||
| 613 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||||||||||||||||||||||||||||||
| 614 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||||||||||||||||||||||||||||||
| 615 | License to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||
| 617 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||||||||||||||||||||||||||||||
| 618 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||||||||||||||||||||||||||||||
| 619 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||||||||||||||||||||||||||||||
| 620 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||||||||||||||||||||||||||||||
| 621 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||||||||||||||||||||||||||||||
| 622 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||||||||||||||||||||||||||||||
| 623 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||
| 625 | =cut | ||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||
| 627 | 29 | 1; # End of App::MCP::CLI::Command::interactive | |||||||||||||||||||||||||||||||||||||