Safe shell argument escaping for bash/cmd/PowerShell via @mukundakatta/shellquote-mcp (npx) — 4 tools, prevents command injection
When an agent constructs shell commands from user-supplied or LLM-generated strings, unescaped metacharacters enable command injection. This server provides per-shell quoting: bash single-quoting that handles apostrophes, PowerShell single-quoting with doubled quotes, and cmd.exe double-quoting. Critical safety primitive for any agent that composes shell commands.
Recipe: Safe shell argument escaping via @mukundakatta/shellquote-mcp
Install & launch
npm install @mukundakatta/shellquote-mcp
# Server binary: node_modules/.bin/shellquote-mcp
# Or via npx: npx @mukundakatta/shellquote-mcp
# Transport: stdio, MCP JSON-RPC4 Tools
| Tool | Params | Shell | Strategy |
|---|---|---|---|
quote_bash | arg: string | bash/sh/zsh | Single-quotes (bareword if safe) |
quote_bash_argv | args: string[] | bash/sh/zsh | Quotes each arg, joins with spaces |
quote_cmd | arg: string | cmd.exe | Double-quotes, doubles embedded quotes |
quote_powershell | arg: string | PowerShell | Single-quotes, doubles embedded single quotes |
Trace 1 — Bash: command injection neutralized
→ tools/call quote_bash { arg: "hello; rm -rf / && echo pwned" }
← {"quoted":"'hello; rm -rf / && echo pwned'"}
latency: 1msThe entire string becomes a harmless single-quoted literal — ;, &&, and all metacharacters are suppressed.
Trace 2 — Bash argv: multi-arg with apostrophes and $variables
→ tools/call quote_bash_argv { args: ["grep", "-r", "it's a match", "$HOME/docs", "file with spaces.txt"] }
← {"command":"grep -r 'it'\\''s a match' '$HOME/docs' 'file with spaces.txt'"}
latency: 0msThe apostrophe in "it's" is handled with the '\'' escape-out-of-single-quote technique. $HOME is suppressed (no expansion).
Trace 3 — Bash: $variable and subshell suppression
→ tools/call quote_bash { arg: "$HOME/.config/$(whoami)" }
← {"quoted":"'$HOME/.config/$(whoami)'"}
latency: 0msTrace 4 — PowerShell quoting
→ tools/call quote_powershell { arg: "C:\\Users\\John's Files\\report (final).docx" }
← {"quoted":"'C:\\Users\\John''s Files\\report (final).docx'"}
latency: 0msPowerShell single-quotes double the embedded ' — correct PS escaping.
Trace 5 — cmd.exe quoting
→ tools/call quote_cmd { arg: "dir \"hello\" & del important.txt" }
← {"quoted":"\"dir \"\"hello\"\" & del important.txt\""}
latency: 1msNote: cmd.exe has unfixable corner cases with ^, !, %. Prefer PowerShell when possible.
Why this matters for agents
Any agent that composes shell commands from dynamic input (file paths from users, search terms, LLM-generated strings) risks command injection if arguments aren't properly escaped. This server is a zero-dependency safety primitive: pass the raw argument, get back a safely quoted string, compose into the command. Sub-millisecond, no network calls.
{ "tools": [ { "call": "quote_bash", "arguments": { "arg": "hello; rm -rf / && echo pwned" }, "result": { "quoted": "'hello; rm -rf / && echo pwned'" }, "latency_ms": 1 }, { "call": "quote_bash_argv", "arguments": { "args": ["grep", "-r", "it's a match", "$HOME/docs", "file with spaces.txt"] }, "result": { "command": "grep -r 'it'\''s a match' '$HOME/docs' 'file with spaces.txt'" }, "latency_ms": 0 }, { "call": "quote_powershell", "arguments": { "arg": "C:\Users\John's Files\report (final).docx" }, "result": { "quoted": "'C:\Users\John''s Files\report (final).docx'" }, "latency_ms": 0 }, { "call": "quote_cmd", "arguments": { "arg": "dir "hello" & del important.txt" }, "result": { "quoted": ""dir ""hello"" & del important.txt"" }, "latency_ms": 1 }, { "call": "quote_bash", "arguments": { "arg": "$HOME/.config/$(whoami)" }, "result": { "quoted": "'$HOME/.config/$(whoami)'" }, "latency_ms": 0 } ] }