blib/lib/MCP/Run.pm

filter: Statementstmt 100% 68 / 68 Branchbran 95.8% 23 / 24 Conditioncond 60.6% 20 / 33 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 82.8% 135 / 163 CC 27 SCAR 21.5
CC
27
Cov
89%
_handle_run
13.6
format_result
12.2
_get_compressor
2.3
CRAP 8.6
« bin/mcp-run-compress ↑ Summary blib/lib/MCP/Run/Bash.pm »
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
BranchTrueFalseTotal
if ($self->compress) 3 24 27
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
BranchTrueFalseTotal
if (my $allowed = $self->allowed_commands) 2 9 11
67 2 my ($first_word) = $command =~ /^\s*(\S+)/;
68 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
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
BranchTrueFalseTotal
if (my $validator = $self->validator) 3 7 10
74 3 my $vresult = $validator->($command, $args->{working_directory});
75 3 ▶ unless (defined($vresult) && !ref($vresult) && $vresult eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vresult and not ref $vresult and $vresult eq 1) 2 1 3
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
76 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
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
l!l&&r!l&&!r
017
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
82 8 ▶ my $timeout = $args->{timeout} // $self->timeout;
Condition33%1/3
$$args{"timeout"} // $self->timeout
l!l&&r!l&&!r
080
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
83 8 ▶ my $compress = $args->{compress} // $self->compress;
Condition66%2/3
$$args{"compress"} // $self->compress
l!l&&r!l&&!r
035
MC/DC50%1/2
$$args{"compress"} // $self->compress
$$args{"compress"} $self->compress
Truth table66%2/3
A // B
A= $$args{"compress"}B= $self->compress
ABresult
1X1
011
000
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
BranchTrueFalseTotal
ref $class_or_self ? : 0 4 4
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
l!l
100
MC/DC0%0/1
$$result{"exit_code"} // -1
$$result{"exit_code"}
Truth table50%1/2
A
A= $$result{"exit_code"}
Aresult
11
00
103 10 ▶ my $stdout = $result->{stdout} // '';
Condition50%1/2
$$result{"stdout"} // ""
l!l
100
MC/DC0%0/1
$$result{"stdout"} // ""
$$result{"stdout"}
Truth table50%1/2
A
A= $$result{"stdout"}
Aresult
11
00
104 10 ▶ my $stderr = $result->{stderr} // '';
Condition50%1/2
$$result{"stderr"} // ""
l!l
100
MC/DC0%0/1
$$result{"stderr"} // ""
$$result{"stderr"}
Truth table50%1/2
A
A= $$result{"stderr"}
Aresult
11
00
105 10 my $error = $result->{error};
106  
107 10 ▶ $compress //= $self->compress;
Condition66%2/3
$compress //= $self->compress
l!l&&r!l&&!r
820
MC/DC0%0/2
$compress //= $self->compress
$compress $self->compress
Truth table66%2/3
A //= B
A= $compressB= $self->compress
ABresult
1X1
011
000
108  
109 10 ▶ if ($compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress) 3 7 10
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
BranchTrueFalseTotal
if length $stdout 8 2 10
116 10 ▶ $text .= "\n=== STDERR ===\n$stderr\n" if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 1 9 10
117 10 ▶ $text .= "\n=== ERROR ===\n$error\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 9 10
118  
119 10 ▶ my $is_error = $exit_code != 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exit_code != 0 ? : 1 9 10
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
l!l&&r!l&&!r
030
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
ABresult
1X1
011
000
132 3 return $_compressor;
133 }
134  
135  
136  
137 22 1;
138  
139 __END__
« bin/mcp-run-compress ↑ Summary blib/lib/MCP/Run/Bash.pm »