| 1 | package MCP::Run; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 22 | our $VERSION = '0.106'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use Mojo::Base 'MCP::Server', -signatures; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use MCP::Run::Compress; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # ABSTRACT: MCP server with a command execution tool | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | has allowed_commands => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | has validator => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | ▶ | has working_directory => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 22 | has timeout => 30; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 22 | has tool_name => 'run'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 27 | ▶ | has tool_description => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 27 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 27 | my $base = 'Execute a command and return stdout, stderr, and exit code'; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 27 | ▶ | if ($self->compress) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | $base .= '. Output is compressed for token efficiency - set compress:false in arguments if you need full detail'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 27 | return $base; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 22 | }; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 22 | has compress => 0; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 28 | ▶ | sub new ($class, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 28 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 28 | $self->_register_run_tool; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 28 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 28 | ▶ | sub _register_run_tool ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_run_tool: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 28 | my $server = $self; | |||||||||||||||||||||||||||||||||||||||||
| 46 | $self->tool( | ||||||||||||||||||||||||||||||||||||||||||
| 47 | name => $self->tool_name, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | description => $self->tool_description, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | input_schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | properties => { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | command => { type => 'string', description => 'The command to execute' }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | working_directory => { type => 'string', description => 'Working directory for the command' }, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | timeout => { type => 'integer', description => 'Timeout in seconds' }, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | compress => { type => 'boolean', description => 'Compress output for LLM efficiency' }, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | required => ['command'], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | ▶ | code => sub ($tool, $args) { $server->_handle_run($tool, $args) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 28 | ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | sub _handle_run ($self, $tool, $args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_run: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 11 | my $command = $args->{command}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | if (my $allowed = $self->allowed_commands) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | my ($first_word) = $command =~ /^\s*(\S+)/; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | unless ($first_word && grep { $_ eq $first_word } @$allowed) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$first_word and grep {$_ eq $first_word;} @$allowed
MC/DC50%1/2
$first_word and grep {$_ eq $first_word;} @$allowed
$first_word
grep {$_ eq $first_word;} @$allowed
Truth table66%2/3
A and B
A= $first_wordB= grep {$_ eq $first_word;} @$allowed
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | return $tool->text_result("Command not allowed: $first_word", 1); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | ▶ | if (my $validator = $self->validator) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my $vresult = $validator->($command, $args->{working_directory}); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | unless (defined($vresult) && !ref($vresult) && $vresult eq '1') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $vresult and not ref $vresult and $vresult eq 1
defined $vresult and not ref $vresult
MC/DC66%2/3
defined $vresult and not ref $vresult and $vresult eq 1
defined $vresult
not ref $vresult
$vresult eq 1
Truth table75%3/4
A and not(B) and C
A= defined $vresultB= not ref $vresultC= $vresult eq 1
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my $reason = defined($vresult) && length($vresult) ? $vresult : 'denied'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $vresult and length $vresult
MC/DC50%1/2
defined $vresult and length $vresult
defined $vresult
length $vresult
Truth table66%2/3
A and B
A= defined $vresultB= length $vresult
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | return $tool->text_result("Command $reason", 1); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | ▶ | my $wd = $args->{working_directory} // $self->working_directory; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$args{"working_directory"} // $self->working_directory
MC/DC50%1/2
$$args{"working_directory"} // $self->working_directory
$$args{"working_directory"}
$self->working_directory
Truth table66%2/3
A // B
A= $$args{"working_directory"}B= $self->working_directory
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 8 | ▶ | my $timeout = $args->{timeout} // $self->timeout; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$$args{"timeout"} // $self->timeout
MC/DC0%0/2
$$args{"timeout"} // $self->timeout
$$args{"timeout"}
$self->timeout
Truth table33%1/3
A // B
A= $$args{"timeout"}B= $self->timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | ▶ | my $compress = $args->{compress} // $self->compress; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$args{"compress"} // $self->compress
MC/DC50%1/2
$$args{"compress"} // $self->compress
$$args{"compress"}
$self->compress
Truth table66%2/3
A // B
A= $$args{"compress"}B= $self->compress
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | my $result = $self->execute($command, $wd, $timeout); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | return $self->format_result($tool, $result, $compress, $command); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | sub run_stdio ($class_or_self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_stdio: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | my $self = ref $class_or_self ? $class_or_self : $class_or_self->new(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | $self->to_stdio; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub execute ($self, $command, $working_directory, $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | die "execute() must be implemented by a subclass"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 10 | ▶ | sub format_result ($self, $tool, $result, $compress = undef, $command = '') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_result: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | my $exit_code = $result->{exit_code} // -1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$result{"exit_code"} // -1
MC/DC0%0/1
$$result{"exit_code"} // -1
$$result{"exit_code"}
Truth table50%1/2
A
A= $$result{"exit_code"}
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 10 | ▶ | my $stdout = $result->{stdout} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$result{"stdout"} // ""
MC/DC0%0/1
$$result{"stdout"} // ""
$$result{"stdout"}
Truth table50%1/2
A
A= $$result{"stdout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 10 | ▶ | my $stderr = $result->{stderr} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$result{"stderr"} // ""
MC/DC0%0/1
$$result{"stderr"} // ""
$$result{"stderr"}
Truth table50%1/2
A
A= $$result{"stderr"}
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 10 | my $error = $result->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 10 | ▶ | $compress //= $self->compress; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$compress //= $self->compress
MC/DC0%0/2
$compress //= $self->compress
$compress
$self->compress
Truth table66%2/3
A //= B
A= $compressB= $self->compress
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 10 | ▶ | if ($compress) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | (my $compressor) = $self->_get_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ($stdout, $stderr) = $compressor->compress($command, $stdout, $stderr); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | my $text = "Exit code: $exit_code\n"; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 10 | ▶ | $text .= "\n=== STDOUT ===\n$stdout\n" if length $stdout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | ▶ | $text .= "\n=== STDERR ===\n$stderr\n" if length $stderr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | $text .= "\n=== ERROR ===\n$error\n" if defined $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | ▶ | my $is_error = $exit_code != 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | return $tool->text_result($text, $is_error); | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | # Lazily constructed, and shared: building the default filter table is the | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # expensive part, and it is identical for every server instance. The class | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # itself is loaded at compile time (see the use above) so a broken or missing | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # MCP::Run::Compress fails at server startup rather than mid-session on the | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # first tools/call. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 22 | my $_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | sub _get_compressor ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_compressor: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | ▶ | $_compressor //= MCP::Run::Compress->new; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$_compressor //= "MCP::Run::Compress"->new
MC/DC0%0/2
$_compressor //= "MCP::Run::Compress"->new
$_compressor
"MCP::Run::Compress"->new
Truth table33%1/3
A //= B
A= $_compressorB= "MCP::Run::Compress"->new
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | return $_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 22 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||||||||||||||||