| 1 | package MCP::Run; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | our $VERSION = '0.103'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Mojo::Base 'MCP::Server', -signatures; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: MCP server with a command execution tool | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | has allowed_commands => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | has validator => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | has working_directory => sub { undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | has timeout => 30; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | has tool_name => 'run'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | has tool_description => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 18 | my $base = 'Execute a command and return stdout, stderr, and exit code'; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 18 | ▶ | if ($self->compress) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $base .= '. Output is compressed for token efficiency - set compress:false in arguments if you need full detail'; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 18 | return $base; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | has compress => 0; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | ▶ | sub new ($class, %args) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 19 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 19 | $self->_register_run_tool; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 19 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | ▶ | sub _register_run_tool ($self) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_run_tool: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 19 | my $server = $self; | |||||||||||||||||||||||||||||||||||||||||
| 44 | ▶ | $self->tool( | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | name => $self->tool_name, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | description => $self->tool_description, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | input_schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | properties => { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | command => { type => 'string', description => 'The command to execute' }, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | working_directory => { type => 'string', description => 'Working directory for the command' }, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | timeout => { type => 'integer', description => 'Timeout in seconds' }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | compress => { type => 'boolean', description => 'Compress output for LLM efficiency' }, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | required => ['command'], | ||||||||||||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | code => sub ($tool, $args) { $server->_handle_run($tool, $args) }, | |||||||||||||||||||||||||||||||||||||||||
| 58 | 19 | ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | ▶ | sub _handle_run ($self, $tool, $args) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_run: called CC=13 SCAR=26.3
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | my $command = $args->{command}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | if (my $allowed = $self->allowed_commands) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my ($first_word) = $command =~ /^\s*(\S+)/; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return $tool->text_result("Command not allowed: $first_word", 1); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | if (my $validator = $self->validator) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | my $vresult = $validator->($command, $args->{working_directory}); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | return $tool->text_result("Command $reason", 1); | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | my $compress = $args->{compress} // $self->compress; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$$args{"compress"} // $self->compress
MC/DC0%0/2
$$args{"compress"} // $self->compress
$$args{"compress"}
$self->compress
Truth table33%1/3
A // B
A= $$args{"compress"}B= $self->compress
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my $result = $self->execute($command, $wd, $timeout); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | return $self->format_result($tool, $result, $compress, $command); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | ▶ | sub run_stdio ($class_or_self, %args) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_stdio: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $self = ref $class_or_self ? $class_or_self : $class_or_self->new(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->to_stdio; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | ▶ | sub execute ($self, $command, $working_directory, $timeout) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | die "execute() must be implemented by a subclass"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | ▶ | sub format_result ($self, $tool, $result, $compress = undef, $command = '') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_result: called CC=12 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 5 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | my $error = $result->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ($compress) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | (my $compressor) = $self->_get_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ($stdout, $stderr) = $compressor->compress($command, $stdout, $stderr); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | my $text = "Exit code: $exit_code\n"; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | $text .= "\n=== STDOUT ===\n$stdout\n" if length $stdout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | ▶ | $text .= "\n=== STDERR ===\n$stderr\n" if length $stderr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | $text .= "\n=== ERROR ===\n$error\n" if defined $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | my $is_error = $exit_code != 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | return $tool->text_result($text, $is_error); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my $_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | ▶ | sub _get_compressor ($self) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_compressor: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $_compressor //= MCP::Run::Compress->new; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_compressor //= "MCP::Run::Compress"->new
MC/DC0%0/2
$_compressor //= "MCP::Run::Compress"->new
$_compressor
"MCP::Run::Compress"->new
Truth table0%0/3
A //= B
A= $_compressorB= "MCP::Run::Compress"->new
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return $_compressor; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||||||||||||||||