blib/lib/MCP/Run.pm

filter: Statementstmt 100% 70 / 70 Branchbran 96.1% 25 / 26 Conditioncond 63.8% 23 / 36 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 83.1% 143 / 172 CC 29 SCAR 22.6
CC
29
Cov
89%
_handle_run
15.5
format_result
12.2
_get_compressor
2.3
CRAP 9.5
« bin/mcp-run-compress ↑ Summary bin/mcp-run-bash »
1 package MCP::Run;
2 78 our $VERSION = '0.107';
3 78 ▶use Mojo::Base 'MCP::Server', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 78 ▶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 133 ▶has allowed_commands => sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12  
13 85 ▶has validator => sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15  
16 84 ▶has working_directory => sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18  
19 78 has timeout => 30;
20  
21  
22 78 has tool_name => 'run';
23  
24  
25 678 ▶has tool_description => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 678 my $self = shift;
27 678 my $base = 'Execute a command and return stdout, stderr, and exit code';
28 678 ▶ if ($self->compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compress) 8 670 678
29 8 $base .= '. Output is compressed for token efficiency - set compress:false in arguments if you need full detail';
30 }
31 678 return $base;
32 78 };
33  
34  
35 78 has compress => 0;
36  
37  
38 710 ▶sub new ($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 710 my $self = $class->SUPER::new(%args);
40 710 $self->_register_run_tool;
41 710 return $self;
42 }
43  
44 710 ▶sub _register_run_tool ($self) {
Subroutine _register_run_tool: called CC=1 SCAR=0.0
45 710 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 # No `minimum` here on purpose: MCP validates the schema for real, so a
55 # `minimum => 1` would answer `timeout: 0` with a hard -32602 "Invalid
56 # arguments" and run nothing at all, while the same 0 arriving via
57 # MCP_RUN_TIMEOUT has no schema in front of it and would still fall
58 # back. One value, two answers. The fallback handles it instead.
59 timeout => {
60 type => 'integer',
61 description => 'Timeout in seconds; must be a positive number. Any other value falls back to the server default - 0 does not disable the timeout',
62 },
63 compress => { type => 'boolean', description => 'Compress output for LLM efficiency' },
64 },
65 required => ['command'],
66 },
67 198 ▶ code => sub ($tool, $args) { $server->_handle_run($tool, $args) },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 710 );
69 }
70  
71 198 ▶sub _handle_run ($self, $tool, $args) {
Subroutine _handle_run: called CC=15 SCAR=27.4
72 198 my $command = $args->{command};
73  
74 # 'command' is required by the input schema, but nothing enforces the schema,
75 # so a request without one used to fall through both gates into execute() and
76 # come back as a cheerful 'Exit code: 0' — indistinguishable from a real run,
77 # and a process started for nothing. Whitespace-only counts as empty: the
78 # allowlist has always said so (/^\s*(\S+)/ finds no first word there), and
79 # the two paths must not disagree about what an empty command is.
80 198 ▶ unless (defined $command && $command =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command and $command =~ /\S/u) 11 187 198
Condition100%3/3
defined $command and $command =~ /\S/u
!ll&&!rl&&r
47187
MC/DC100%2/2
defined $command and $command =~ /\S/u
defined $command $command =~ /\S/u
Truth table100%3/3
A and B
A= defined $commandB= $command =~ /\S/u
ABresult
0X0
100
111
81 11 return $tool->text_result('No command given: the command parameter must be a non-empty string', 1);
82 }
83  
84 187 ▶ if (my $allowed = $self->allowed_commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $self->allowed_commands) 55 132 187
85 # $command holds at least one non-whitespace character by now, so
86 # $first_word is always defined. The guard is `defined` and not truth so
87 # that a command literally named "0" stays matchable.
88 55 my ($first_word) = $command =~ /^\s*(\S+)/;
89 55 ▶ unless (defined $first_word && grep { $_ eq $first_word } @$allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first_word and grep {$_ eq $first_word;} @$allowed) 26 29 55
Condition66%2/3
defined $first_word and grep {$_ eq $first_word;} @$allowed
!ll&&!rl&&r
02629
MC/DC50%1/2
defined $first_word and grep {$_ eq $first_word;} @$allowed
defined $first_word grep {$_ eq $first_word;} @$allowed
Truth table66%2/3
A and B
A= defined $first_wordB= grep {$_ eq $first_word;} @$allowed
ABresult
0X0
100
111
90 26 return $tool->text_result("Command not allowed: $first_word", 1);
91 }
92 }
93  
94 161 ▶ if (my $validator = $self->validator) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $validator = $self->validator) 76 85 161
95 76 my $vresult = $validator->($command, $args->{working_directory});
96 76 ▶ unless (defined($vresult) && !ref($vresult) && $vresult eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vresult and not ref $vresult and $vresult eq 1) 50 26 76
Condition83%5/6
defined $vresult and not ref $vresult and $vresult eq 1
!ll&&!rl&&r
252526
defined $vresult and not ref $vresult
!ll&&!rl&&r
25051
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
97 50 ▶ my $reason = defined($vresult) && length($vresult) ? $vresult : 'denied';
Branch: 1 branch
BranchTrueFalseTotal
defined $vresult && length $vresult ? : 25 25 50
Condition66%2/3
defined $vresult and length $vresult
!ll&&!rl&&r
25025
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
98 50 return $tool->text_result("Command $reason", 1);
99 }
100 }
101  
102 111 ▶ my $wd = $args->{working_directory} // $self->working_directory;
Condition66%2/3
$$args{"working_directory"} // $self->working_directory
l!l&&r!l&&!r
02883
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
103 111 ▶ my $timeout = $args->{timeout} // $self->timeout;
Condition66%2/3
$$args{"timeout"} // $self->timeout
l!l&&r!l&&!r
15960
MC/DC0%0/2
$$args{"timeout"} // $self->timeout
$$args{"timeout"} $self->timeout
Truth table66%2/3
A // B
A= $$args{"timeout"}B= $self->timeout
ABresult
1X1
011
000
104 111 ▶ my $compress = $args->{compress} // $self->compress;
Condition33%1/3
$$args{"compress"} // $self->compress
l!l&&r!l&&!r
01110
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
105  
106 111 my $result = $self->execute($command, $wd, $timeout);
107 101 return $self->format_result($tool, $result, $compress, $command);
108 }
109  
110 10 ▶sub run_stdio ($class_or_self, %args) {
Subroutine run_stdio: called CC=2 SCAR=7.1
Pod documented
111 10 ▶ my $self = ref $class_or_self ? $class_or_self : $class_or_self->new(%args);
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 0 10 10
112 10 $self->to_stdio;
113 5 return $self;
114 }
115  
116  
117 1 ▶sub execute ($self, $command, $working_directory, $timeout) {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
118 1 die "execute() must be implemented by a subclass";
119 }
120  
121  
122 172 ▶sub format_result ($self, $tool, $result, $compress = undef, $command = '') {
Subroutine format_result: called CC=12 SCAR=25.0
Pod documented
123 172 ▶ my $exit_code = $result->{exit_code} // -1;
Condition50%1/2
$$result{"exit_code"} // -1
l!l
1720
MC/DC0%0/1
$$result{"exit_code"} // -1
$$result{"exit_code"}
Truth table50%1/2
A
A= $$result{"exit_code"}
Aresult
11
00
124 172 ▶ my $stdout = $result->{stdout} // '';
Condition50%1/2
$$result{"stdout"} // ""
l!l
1720
MC/DC0%0/1
$$result{"stdout"} // ""
$$result{"stdout"}
Truth table50%1/2
A
A= $$result{"stdout"}
Aresult
11
00
125 172 ▶ my $stderr = $result->{stderr} // '';
Condition50%1/2
$$result{"stderr"} // ""
l!l
1720
MC/DC0%0/1
$$result{"stderr"} // ""
$$result{"stderr"}
Truth table50%1/2
A
A= $$result{"stderr"}
Aresult
11
00
126 172 my $error = $result->{error};
127  
128 172 ▶ $compress //= $self->compress;
Condition66%2/3
$compress //= $self->compress
l!l&&r!l&&!r
101710
MC/DC0%0/2
$compress //= $self->compress
$compress $self->compress
Truth table66%2/3
A //= B
A= $compressB= $self->compress
ABresult
1X1
011
000
129  
130 172 ▶ if ($compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress) 4 168 172
131 4 (my $compressor) = $self->_get_compressor;
132 4 ($stdout, $stderr) = $compressor->compress($command, $stdout, $stderr);
133 }
134  
135 172 my $text = "Exit code: $exit_code\n";
136 172 ▶ $text .= "\n=== STDOUT ===\n$stdout\n" if length $stdout;
Branch: 1 branch
BranchTrueFalseTotal
if length $stdout 108 64 172
137 172 ▶ $text .= "\n=== STDERR ===\n$stderr\n" if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 27 145 172
138 172 ▶ $text .= "\n=== ERROR ===\n$error\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 41 131 172
139  
140 172 ▶ my $is_error = $exit_code != 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exit_code != 0 ? : 61 111 172
141 172 return $tool->text_result($text, $is_error);
142 }
143  
144 # Lazily constructed, and shared: building the default filter table is the
145 # expensive part, and it is identical for every server instance. The class
146 # itself is loaded at compile time (see the use above) so a broken or missing
147 # MCP::Run::Compress fails at server startup rather than mid-session on the
148 # first tools/call.
149 78 my $_compressor;
150  
151 4 ▶sub _get_compressor ($self) {
Subroutine _get_compressor: called CC=2 SCAR=8.1
152 4 ▶ $_compressor //= MCP::Run::Compress->new;
Condition33%1/3
$_compressor //= "MCP::Run::Compress"->new
l!l&&r!l&&!r
040
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
153 4 return $_compressor;
154 }
155  
156  
157  
158 78 1;
159  
160 __END__
« bin/mcp-run-compress ↑ Summary bin/mcp-run-bash »