blib/lib/MCP/Run.pm

filter: Statementstmt 87.6% 57 / 65 Branchbran 83.3% 20 / 24 Conditioncond 54.5% 18 / 33 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 84.6% 11 / 13 Podpod 100% 4 / 4 totaltotal 72.3% 115 / 159 CC 27 SCAR 22.6
CC
27
Cov
78%
_handle_run
13.8
format_result
13.2
run_stdio
6.0
CRAP 9.6
« lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run/Bash.pm »
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
BranchTrueFalseTotal
if ($self->compress) 0 18 18
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
BranchTrueFalseTotal
if (my $allowed = $self->allowed_commands) 2 4 6
65 2 my ($first_word) = $command =~ /^\s*(\S+)/;
66 2 ▶ unless ($first_word && grep { $_ eq $first_word } @$allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_word and grep {$_ eq $first_word;} @$allowed) 1 1 2
Condition66%2/3
$first_word and grep {$_ eq $first_word;} @$allowed
!ll&&!rl&&r
011
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (my $validator = $self->validator) 3 2 5
72 3 my $vresult = $validator->($command, $args->{working_directory});
73 3 ▶ unless (defined($vresult) && !ref($vresult) && $vresult eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vresult and not ref $vresult and $vresult eq 1) 1 1 2
Condition83%5/6
defined $vresult and not ref $vresult and $vresult eq 1
!ll&&!rl&&r
111
defined $vresult and not ref $vresult
!ll&&!rl&&r
102
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
ABCresult
0XX0
10X0
1100
1111
74 2 ▶ my $reason = defined($vresult) && length($vresult) ? $vresult : 'denied';
Branch: 1 branch
BranchTrueFalseTotal
defined $vresult && length $vresult ? : 1 1 2
Condition66%2/3
defined $vresult and length $vresult
!ll&&!rl&&r
101
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
ABresult
0X0
100
111
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
l!l&&r!l&&!r
012
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
ABresult
1X1
011
000
80 3 ▶ my $timeout = $args->{timeout} // $self->timeout;
Condition33%1/3
$$args{"timeout"} // $self->timeout
l!l&&r!l&&!r
030
MC/DC0%0/2
$$args{"timeout"} // $self->timeout
$$args{"timeout"} $self->timeout
Truth table33%1/3
A // B
A= $$args{"timeout"}B= $self->timeout
ABresult
1X1
011
000
81 3 ▶ my $compress = $args->{compress} // $self->compress;
Condition33%1/3
$$args{"compress"} // $self->compress
l!l&&r!l&&!r
003
MC/DC0%0/2
$$args{"compress"} // $self->compress
$$args{"compress"} $self->compress
Truth table33%1/3
A // B
A= $$args{"compress"}B= $self->compress
ABresult
1X1
011
000
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
BranchTrueFalseTotal
ref $class_or_self ? : 0 0 0
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
l!l
50
MC/DC0%0/1
$$result{"exit_code"} // -1
$$result{"exit_code"}
Truth table50%1/2
A
A= $$result{"exit_code"}
Aresult
11
00
101 5 ▶ my $stdout = $result->{stdout} // '';
Condition50%1/2
$$result{"stdout"} // ""
l!l
50
MC/DC0%0/1
$$result{"stdout"} // ""
$$result{"stdout"}
Truth table50%1/2
A
A= $$result{"stdout"}
Aresult
11
00
102 5 ▶ my $stderr = $result->{stderr} // '';
Condition50%1/2
$$result{"stderr"} // ""
l!l
50
MC/DC0%0/1
$$result{"stderr"} // ""
$$result{"stderr"}
Truth table50%1/2
A
A= $$result{"stderr"}
Aresult
11
00
103 5 my $error = $result->{error};
104  
105 5 ▶ $compress //= $self->compress;
Condition66%2/3
$compress //= $self->compress
l!l&&r!l&&!r
320
MC/DC0%0/2
$compress //= $self->compress
$compress $self->compress
Truth table66%2/3
A //= B
A= $compressB= $self->compress
ABresult
1X1
011
000
106  
107 5 ▶ if ($compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress) 0 5 5
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
BranchTrueFalseTotal
if length $stdout 4 1 5
114 5 ▶ $text .= "\n=== STDERR ===\n$stderr\n" if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 1 4 5
115 5 ▶ $text .= "\n=== ERROR ===\n$error\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 4 5
116  
117 5 ▶ my $is_error = $exit_code != 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exit_code != 0 ? : 1 4 5
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
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
125 0 return $_compressor;
126 }
127  
128  
129  
130 4 1;
131  
132 __END__
« lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run/Bash.pm »