bin/mcp-run-compress

filter: Statementstmt 90.5% 115 / 127 Branchbran 62.9% 34 / 54 Conditioncond 65.1% 28 / 43 MC/DCmcdc 36.3% 8 / 22 Subroutinesub 94.4% 17 / 18 Podpod n/a totaltotal 76.5% 202 / 264 CC 43 SCAR 24.6
CC
43
Cov
79%
install_claude
21.3
run_filter_files
12.7
run_command
12.6
CRAP 11.7
« blib/lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run.pm »
1 #!/usr/bin/env perl
2 # PODNAME: mcp-run-compress
3 # ABSTRACT: Wrap a shell command through MCP::Run::Compress for LLM-friendly output
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Getopt::Long qw( GetOptions );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use MIME::Base64 qw( encode_base64 decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use MCP::Run::Bash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use MCP::Run::Compress;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 my ( $hook, $b64, $install, $help, $filter_files, $cmd_b64 );
16 GetOptions(
17 'hook' => \$hook,
18 'b64=s' => \$b64,
19 'install-claude' => \$install,
20 'filter-files' => \$filter_files,
21 'cmd-b64=s' => \$cmd_b64,
22 'help|h' => \$help,
23 14 ▶) or do { print_help(); exit 2 };
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("hook", \$hook, "b64=s", \$b64, "install-claude", \$install, "filter-files", \$filter_files, "cmd-b64=s", \$cmd_b64, "help|h", \$help) or do { print_help(); exit 2 } 14 0 14
24  
25 14 ▶if ($hook) { exit run_hook() }
Branch: 1 branch
BranchTrueFalseTotal
if ($hook) 6 8 14
26 8 ▶if (defined $b64) { exit run_command($b64) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b64) 3 5 8
27 5 ▶if ($filter_files) { exit run_filter_files( $cmd_b64, @ARGV ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_files) 1 4 5
28 4 ▶if ($install) { exit install_claude() }
Branch: 1 branch
BranchTrueFalseTotal
if ($install) 4 0 4
29  
30 0 print_help();
31 0 exit 0;
32  
33 # 'docker' means: install_claude writes a hook that spawns a docker
34 # container, and --hook emits a pipe-through-docker rewrite instead of
35 # a --b64 self-wrap. Baked into the Docker image via ENV; unset on a
36 # native Perl install. Override manually by exporting the env var.
37 5 ▶sub install_mode {
Subroutine install_mode: called CC=2 SCAR=7.6
38 5 ▶ return $ENV{MCP_RUN_COMPRESS_INSTALL_MODE} // 'native';
Condition50%1/2
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"} // "native"
l!l
50
MC/DC0%0/1
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"} // "native"
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"}
Truth table50%1/2
A
A= $ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"}
Aresult
11
00
39 }
40  
41 2 ▶sub image_ref {
Subroutine image_ref: called CC=2 SCAR=7.6
42 2 ▶ return $ENV{MCP_RUN_COMPRESS_IMAGE} // 'raudssus/mcp-run-compress:latest';
Condition50%1/2
$ENV{"MCP_RUN_COMPRESS_IMAGE"} // "raudssus/mcp-run-compress:latest"
l!l
02
MC/DC0%0/1
$ENV{"MCP_RUN_COMPRESS_IMAGE"} // "raudssus/mcp-run-compress:latest"
$ENV{"MCP_RUN_COMPRESS_IMAGE"}
Truth table50%1/2
A
A= $ENV{"MCP_RUN_COMPRESS_IMAGE"}
Aresult
11
00
43 }
44  
45 6 ▶sub _json {
Subroutine _json: called CC=1 SCAR=0.0
46 6 return JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 );
47 }
48  
49 6 ▶sub run_hook {
Subroutine run_hook: called CC=9 SCAR=22.0
50 6 my ( $self ) = @_;
51 6 my $json = _json();
52 6 my $raw = do { local $/; <STDIN> };
53 6 ▶ my $in = eval { $json->decode($raw) } || {};
Condition100%2/2
eval { do { $json->decode($raw) } } || {}
l!l
51
MC/DC100%1/1
eval { do { $json->decode($raw) } } || {}
eval { do { $json->decode($raw) } }
Truth table100%2/2
A
A= eval { do { $json->decode($raw) } }
Aresult
11
00
54 6 my $cmd = $in->{tool_input}{command};
55  
56 my $bypass =
57 !defined $cmd
58 || !length $cmd
59 || $in->{tool_input}{run_in_background}
60 6 ▶ || $cmd =~ /\bmcp-run-compress\b/;
Condition88%8/9
!(defined $cmd && length $cmd) || $$in{'tool_input'}{'run_in_background'} || $cmd =~ /\bmcp-run-compress\b/
l!l&&r!l&&!r
213
not defined $cmd && length $cmd or $$in{"tool_input"}{"run_in_background"}
l!l&&r!l&&!r
114
defined $cmd && length $cmd
!ll&&!rl&&r
105
MC/DC75%3/4
!(defined $cmd && length $cmd) || $$in{'tool_input'}{'run_in_background'} || $cmd =~ /\bmcp-run-compress\b/
defined $cmd length $cmd $$in{"tool_input"}{"run_in_background"} $cmd =~ /\bmcp-run-compress\b/
Truth table80%4/5
not(A && B) or C || D
A= defined $cmdB= length $cmdC= $$in{"tool_input"}{"run_in_background"}D= $cmd =~ /\bmcp-run-compress\b/
ABCDresult
0XXX1
10XX1
111X1
11011
11000
61  
62 6 my $out = {
63 hookSpecificOutput => {
64 hookEventName => 'PreToolUse',
65 },
66 };
67  
68 6 ▶ if ( !$bypass && $cmd =~ /\A\s*no-compress\s+(.+)\z/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $bypass and $cmd =~ /\A\s*no-compress\s+(.+)\z/s) 1 5 6
Condition100%3/3
not $bypass and $cmd =~ /\A\s*no-compress\s+(.+)\z/s
!ll&&!rl&&r
321
MC/DC100%2/2
not $bypass and $cmd =~ /\A\s*no-compress\s+(.+)\z/s
not $bypass $cmd =~ /\A\s*no-compress\s+(.+)\z/s
Truth table100%3/3
not(A) and B
A= not $bypassB= $cmd =~ /\A\s*no-compress\s+(.+)\z/s
ABresult
0X0
100
111
69 1 $out->{hookSpecificOutput}{updatedInput} = { command => $1 };
70 1 $bypass = 1;
71 }
72  
73 6 ▶ unless ($bypass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bypass) 2 4 6
74 2 $cmd = MCP::Run::Compress->new->transform_command($cmd);
75 2 my $encoded = encode_base64( encode_utf8($cmd), '' );
76 2 ▶ my $rewrite = install_mode() eq 'docker'
Branch: 1 branch
BranchTrueFalseTotal
install_mode() eq 'docker' ? : 1 1 2
77 ? docker_pipe_rewrite($encoded)
78 : "mcp-run-compress --b64 $encoded";
79 2 $out->{hookSpecificOutput}{updatedInput} = { command => $rewrite };
80 }
81  
82 6 print $json->encode($out);
83 6 return 0;
84 }
85  
86 # Emitted when the hook is running inside the Docker image. The rewritten
87 # command runs the user's original bash on the HOST (host cwd, host env,
88 # host binaries — `git`, `dzil`, etc. all work), then streams stdout and
89 # stderr into the docker image for compression. The host only needs bash,
90 # coreutils (mktemp, base64) and docker — no Perl.
91 1 ▶sub docker_pipe_rewrite {
Subroutine docker_pipe_rewrite: called CC=1 SCAR=0.0
92 1 my ($encoded) = @_;
93 1 my $image = image_ref();
94 # base64 alphabet is [A-Za-z0-9+/=] so single-quote wrapping is safe.
95 1 return join( ' ',
96 q({),
97 q(__o=$(mktemp) && __e=$(mktemp) || exit 1;),
98 q(trap 'rm -f "$__o" "$__e"' EXIT;),
99 qq(bash -c "\$(printf %s '$encoded' | base64 -d)" >"\$__o" 2>"\$__e";),
100 q(__ec=$?;),
101 qq(docker run --rm -v "\$__o:/in/stdout:ro" -v "\$__e:/in/stderr:ro" '$image' --filter-files --cmd-b64 '$encoded' /in/stdout /in/stderr;),
102 q(exit $__ec;),
103 q(}),
104 );
105 }
106  
107 1 ▶sub run_filter_files {
Subroutine run_filter_files: called CC=8 SCAR=25.4
108 1 my ( $encoded, $stdout_path, $stderr_path ) = @_;
109 1 ▶ unless ( defined $stdout_path && defined $stderr_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stdout_path and defined $stderr_path) 0 1 1
Condition33%1/3
defined $stdout_path and defined $stderr_path
!ll&&!rl&&r
001
MC/DC0%0/2
defined $stdout_path and defined $stderr_path
defined $stdout_path defined $stderr_path
Truth table33%1/3
A and B
A= defined $stdout_pathB= defined $stderr_path
ABresult
0X0
100
111
110 0 print STDERR "mcp-run-compress: --filter-files needs STDOUT_PATH STDERR_PATH\n";
111 0 return 2;
112 }
113 1 ▶ my $command = defined $encoded ? decode_base64($encoded) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $encoded ? : 1 0 1
114 1 my $stdout = path($stdout_path)->slurp_raw;
115 1 my $stderr = path($stderr_path)->slurp_raw;
116  
117 1 my $compressor = MCP::Run::Compress->new;
118 1 my ( $o, $e ) = $compressor->compress( $command, $stdout, $stderr );
119  
120 1 ▶ if ( length $o ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $o) 1 0 1
121 1 print STDOUT $o;
122 1 ▶ print STDOUT "\n" unless $o =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $o =~ /\n\z/ 1 0 1
123 }
124 1 ▶ if ( length $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $e) 0 1 1
125 0 print STDERR $e;
126 0 ▶ print STDERR "\n" unless $e =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /\n\z/ 0 0 0
127 }
128 1 return 0;
129 }
130  
131 3 ▶sub run_command {
Subroutine run_command: called CC=9 SCAR=25.4
132 3 my ( $encoded ) = @_;
133 3 my $command = decode_base64($encoded);
134  
135 3 my $server = MCP::Run::Bash->new;
136 3 my $result = $server->execute( $command, undef, 1800 );
137  
138 3 my $compressor = MCP::Run::Compress->new;
139 my ( $stdout, $stderr ) = $compressor->compress(
140 $command,
141 $result->{stdout} // '',
142 3 ▶ $result->{stderr} // '',
Condition50%2/4
$$result{"stdout"} // ""
l!l
30
$$result{"stderr"} // ""
l!l
30
MC/DC0%0/1
$$result{"stdout"} // ""
$$result{"stdout"}
MC/DC0%0/1
$$result{"stderr"} // ""
$$result{"stderr"}
Truth table50%1/2
A
A= $$result{"stdout"}
Aresult
11
00
Truth table50%1/2
A
A= $$result{"stderr"}
Aresult
11
00
143 );
144  
145 3 ▶ if ( length $stdout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stdout) 2 1 3
146 2 print STDOUT $stdout;
147 2 ▶ print STDOUT "\n" unless $stdout =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stdout =~ /\n\z/ 2 0 2
148 }
149 3 ▶ if ( length $stderr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stderr) 0 3 3
150 0 print STDERR $stderr;
151 0 ▶ print STDERR "\n" unless $stderr =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stderr =~ /\n\z/ 0 0 0
152 }
153 3 ▶ if ( defined $result->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$result{"error"}) 0 3 3
154 0 print STDERR "mcp-run-compress: $result->{error}\n";
155 }
156  
157 3 ▶ return $result->{exit_code} // 1;
Condition50%1/2
$$result{"exit_code"} // 1
l!l
30
MC/DC0%0/1
$$result{"exit_code"} // 1
$$result{"exit_code"}
Truth table50%1/2
A
A= $$result{"exit_code"}
Aresult
11
00
158 }
159  
160 4 ▶sub install_claude {
Subroutine install_claude: called CC=18 SCAR=30.6
161 4 ▶ my $file = path( $ENV{HOME} // glob('~'), '.claude', 'settings.json' );
Condition33%1/3
$ENV{"HOME"} // glob("~")
l!l&&r!l&&!r
400
MC/DC0%0/2
$ENV{"HOME"} // glob("~")
$ENV{"HOME"} glob("~")
Truth table33%1/3
A // B
A= $ENV{"HOME"}B= glob("~")
ABresult
1X1
011
000
162 4 my $json = JSON::MaybeXS->new(
163 utf8 => 1,
164 pretty => 1,
165 canonical => 1,
166 convert_blessed => 1,
167 );
168  
169 4 my $cfg = {};
170 4 ▶ if ( $file->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 1 3 4
171 1 my $raw = $file->slurp_utf8;
172 1 ▶ $cfg = $json->decode($raw) if length $raw;
Branch: 1 branch
BranchTrueFalseTotal
if length $raw 1 0 1
173 }
174  
175 4 ▶ $cfg->{hooks} //= {};
Condition100%2/2
$$cfg{"hooks"} //= {}
l!l
13
MC/DC100%1/1
$$cfg{"hooks"} //= {}
$$cfg{"hooks"}
Truth table100%2/2
A
A= $$cfg{"hooks"}
Aresult
11
00
176 4 ▶ $cfg->{hooks}{PreToolUse} //= [];
Condition100%2/2
$$cfg{"hooks"}{"PreToolUse"} //= []
l!l
13
MC/DC100%1/1
$$cfg{"hooks"}{"PreToolUse"} //= []
$$cfg{"hooks"}{"PreToolUse"}
Truth table100%2/2
A
A= $$cfg{"hooks"}{"PreToolUse"}
Aresult
11
00
177  
178 4 for my $group ( @{ $cfg->{hooks}{PreToolUse} } ) {
179 1 ▶ next unless ref $group eq 'HASH' && ( $group->{matcher} // '' ) eq 'Bash';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $group eq "HASH" and ($$group{"matcher"} // "") eq "Bash" 0 1 1
Condition40%2/5
ref $group eq "HASH" and ($$group{"matcher"} // "") eq "Bash"
!ll&&!rl&&r
001
$$group{"matcher"} // ""
l!l
10
MC/DC0%0/2
ref $group eq "HASH" and ($$group{"matcher"} // "") eq "Bash"
ref $group eq "HASH" $$group{"matcher"}
Truth table33%1/3
A and B
A= ref $group eq "HASH"B= $$group{"matcher"}
ABresult
0X0
100
111
180 1 ▶ for my $h ( @{ $group->{hooks} // [] } ) {
Condition50%1/2
$$group{"hooks"} // []
l!l
10
MC/DC0%0/1
$$group{"hooks"} // []
$$group{"hooks"}
Truth table50%1/2
A
A= $$group{"hooks"}
Aresult
11
00
181 1 ▶ next unless ref $h eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 0 1 1
182 1 ▶ if ( ( $h->{command} // '' ) =~ /\bmcp-run-compress\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($$h{"command"} // "") =~ /\bmcp-run-compress\b/) 1 0 1
Condition50%1/2
$$h{"command"} // ""
l!l
10
MC/DC0%0/1
$$h{"command"} // ""
$$h{"command"}
Truth table50%1/2
A
A= $$h{"command"}
Aresult
11
00
183 1 print "Already installed in $file\n";
184 1 return 0;
185 }
186 }
187 }
188  
189 3 ▶ my $hook_cmd = install_mode() eq 'docker'
Branch: 1 branch
BranchTrueFalseTotal
install_mode() eq 'docker' ? : 1 2 3
190 ? 'docker run --rm -i ' . image_ref() . ' --hook'
191 : 'mcp-run-compress --hook';
192  
193 3 push @{ $cfg->{hooks}{PreToolUse} }, {
194 matcher => 'Bash',
195 hooks => [ { type => 'command', command => $hook_cmd } ],
196 };
197  
198 3 ▶ $file->parent->mkpath unless $file->parent->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->parent->exists 3 0 3
199 3 $file->spew_utf8( $json->encode($cfg) );
200 3 print "Installed PreToolUse hook in $file\n";
201  
202 3 my $skill = $file->parent->child(
203 'skills', 'bash-output-is-compressed-prefix-no-compress-to-bypass', 'SKILL.md',
204 );
205 3 ▶ $skill->parent->mkpath unless $skill->parent->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $skill->parent->exists 3 0 3
206 3 $skill->spew_utf8(<<'SKILL');
207 ---
208 name: bash-output-is-compressed-prefix-no-compress-to-bypass
209 description: READ THIS BEFORE RUNNING ANY Bash TOOL COMMAND. Every Bash tool call is routed through the mcp-run-compress hook, which strips columns, noise, and truncates long output so you see a compressed view — NOT the real shell output. If you need raw unfiltered output (full `ls -l` columns, verbose logs, exact byte counts, etc.), prefix the command with `no-compress ` for that single invocation.
210 ---
211  
212 Every Bash command you run is silently rewritten by a PreToolUse hook into
213 `mcp-run-compress --b64 <...>`, which filters and truncates the output.
214  
215 To get the real, unmodified output for one command, prefix it with `no-compress `:
216  
217 no-compress ls -l
218 no-compress cat /var/log/syslog
219 no-compress dmesg
220  
221 The hook strips the prefix and runs the rest raw. Use it whenever compressed
222 output would hide something you actually need to see.
223 SKILL
224 3 print "Installed skill at $skill\n";
225  
226 3 return 0;
227 }
228  
229 0 ▶sub print_help {
Subroutine print_help: not called CC=1 SCAR=6.9
230 0 print <<'HELP';
231 mcp-run-compress — wrap a shell command through MCP::Run::Compress filters
232 so the output is LLM-friendly (noise stripped, long output truncated).
233  
234 INSTALL MODES
235  
236 Two install modes, selected automatically by the environment:
237  
238 native User has Perl and installed MCP::Run (via cpanm / Dist::Zilla).
239 The hook is `mcp-run-compress --hook` and rewrites bash
240 commands to `mcp-run-compress --b64 <…>`, all in-process.
241  
242 docker User has only Docker (no Perl). The hook is
243 `docker run --rm -i raudssus/mcp-run-compress --hook` and
244 rewrites bash commands to a small shell snippet that runs
245 the original command on the HOST and pipes stdout/stderr
246 through a Docker container for compression.
247  
248 The mode is selected by the MCP_RUN_COMPRESS_INSTALL_MODE env var.
249 The Docker image sets this to `docker` in its ENV; native installs
250 leave it unset (default `native`). Override manually if you need to.
251  
252 USAGE
253  
254 mcp-run-compress --b64 <BASE64>
255 Decode the base64-encoded command, execute it, and emit the compressed
256 stdout/stderr with the original exit code. Used by the native-mode
257 hook rewrite.
258  
259 mcp-run-compress --filter-files --cmd-b64 <BASE64> STDOUT_PATH STDERR_PATH
260 Read captured stdout and stderr from two files, compress them using
261 the decoded command as context, emit compressed stdout to fd 1 and
262 compressed stderr to fd 2. Used by the docker-mode hook rewrite
263 (the host runs the original command and mounts the temp files into
264 the container).
265  
266 mcp-run-compress --hook
267 Claude Code PreToolUse hook mode. Reads the hook invocation JSON on
268 stdin, emits a response JSON on stdout that rewrites the Bash tool's
269 `command`. The exact rewrite depends on the install mode (see
270 above). Background jobs (`run_in_background: true`) and already-
271 wrapped commands are passed through untouched.
272  
273 mcp-run-compress --install-claude
274 Patch ~/.claude/settings.json to register the hook as a PreToolUse
275 hook on the Bash tool. The hook command written into settings.json
276 matches the current install mode. Idempotent.
277  
278 mcp-run-compress [--help]
279 Print this help.
280  
281 ENV VARS
282  
283 MCP_RUN_COMPRESS_INSTALL_MODE `native` (default) or `docker`.
284 MCP_RUN_COMPRESS_IMAGE Docker image ref for the hook command
285 and for the pipe-through container.
286 Defaults to `raudssus/mcp-run-compress:latest`
287 but the shipped Docker image pins this
288 to the exact version that built it.
289  
290 MANUAL INSTALL
291  
292 Add this block to ~/.claude/settings.json (merge with existing hooks):
293  
294 {
295 "hooks": {
296 "PreToolUse": [
297 {
298 "matcher": "Bash",
299 "hooks": [
300 { "type": "command", "command": "mcp-run-compress --hook" }
301 ]
302 }
303 ]
304 }
305 }
306  
307 From then on every Bash tool call is rewritten to:
308  
309 mcp-run-compress --b64 <base64 of your command>
310  
311 which executes via IPC::Open3, routes stdout/stderr through the
312 MCP::Run::Compress filter pipeline, and emits the compressed streams
313 with the original exit code — transparent to Claude Code.
314  
315 NOTES
316  
317 * The hook only rewrites the Bash `command`; it does not set a
318 permission decision, so the usual allow/deny prompts keep firing.
319 * Background commands (`run_in_background: true`) and commands that
320 already reference `mcp-run-compress` are not rewritten.
321 * Prefix a command with `no-compress ` to bypass compression for that
322 single invocation — the hook strips the prefix and runs the rest raw.
323 `--install-claude` also drops a minimal skill at
324 ~/.claude/skills/bash-output-is-compressed-prefix-no-compress-to-bypass/
325 that tells Claude about it.
326  
327 SEE ALSO
328 MCP::Run::Compress, MCP::Run::Bash, mcp-run-bash
329 HELP
330 }
331  
332 __END__
« blib/lib/MCP/Run/Compress.pm ↑ Summary blib/lib/MCP/Run.pm »