A filesystem MCP server will read your .env file if an agent asks it to. This repo gates tools/call arguments at agentgateway and shows the policy that actually has to run outside the gateway's built-in rule engine to do it.
📖 Read the write-up: https://webofmike.com/filesystem-mcp-env-guard/
=== scenario 1: baseline, no guardrails ===
--- direct .env read through the unguarded gateway ---
{
"content": [{"type": "text", "text": "DATABASE_URL=postgres://demo:demopass@localhost:5432/demodb\nAPI_KEY=sk-demo-FAKE1234567890abcdef\n"}]
}
=== scenario 2: guarded by the ExtMcp policy server ===
--- .env is rejected before the filesystem server ever opens it ---
{"error":{"code":"error","message":"path argument matches denied pattern '.env'; ..."}}
--- allowed path, secret-shaped content: response-mask backstop ---
{"content": [{"type": "text", "text": "... API_KEY=[REDACTED] ... password=[REDACTED] ..."}]}
=== scenario 3: fail-closed when the policy server is unreachable ===
{"error":{"code":"error","message":"mcpGuardrails checkRequest failed: upstream call failed: Connect: Connection refused"}}
Full output: scripts/demo.sh prints 7/7 PASS, captured in captures/.
The filesystem MCP server's root scoping stops an agent leaving the sandboxed directory. It does nothing about which file inside that directory gets read, and .env, .ssh/, and .git/ all live inside the directory an agent is scoped to by design. A r/mcp incident post from 2026-10-05 ("Claude almost ingested my production .env via filesystem MCP") is exactly this: root scoping held, the model still asked for the one file it shouldn't have, and nothing in the path stopped it. Trend Micro's MCP server survey separately puts ~48% of reviewed servers at recommending insecure credential storage in the first place.
The first thing I tried was agentgateway's built-in mcpAuthorization policy: CEL rules evaluated against mcp.tool.name, mcp.tool.target, and JWT claims. It cannot do this. mcp.tool.arguments is not populated when mcpAuthorization rules run; it's a post-request, access-log-only field. You can allow or deny read_text_file as a tool, but you cannot write a rule that looks at the path argument and denies .env specifically. I checked this against the agentgateway documentation and confirmed it empirically before writing a line of this repo's config: the same CEL rule style used for tool-name allowlists in the agentgateway MCP authorization docs has no path field to match against.
Argument inspection, and mutating or redacting a response, both require ExtMCP: an external gRPC policy server that agentgateway calls before (CheckRequest) and after (CheckResponse) the upstream MCP call, defined in ext_mcp.proto. That's the control this repo builds: a ~150-line Python gRPC server that does exactly two things, and nothing clever.
guard/guard_server.py implements the ExtMcp service:
CheckRequest(pre-execution): fortools/callon a path-bearing tool (read_text_file,read_file,read_media_file,get_file_info,read_multiple_files), it checks every path argument (a singlepathstring, or every entry in apathsarray) against a denylist of substrings:.env,.ssh,.git/,credentials,id_rsa,id_ed25519,.aws/,.npmrc,.netrc. A match returns anAuthorizationError, and the filesystem server never opens the file.CheckResponse(post-execution, backstop): scans returned text content for secret-shaped substrings (API keys,password=, Postgres DSNs, PEM private key blocks, AWS access key IDs) and redacts matches in place. This catches a file whose path looked innocent but whose contents didn't, which a path denylist alone can't.
failureMode: failClosed in config.yaml means the gateway denies the call if the guard server itself is unreachable, rather than falling open. Scenario 3 in the demo proves this: a perfectly safe file read is denied when the policy server is down, because the gateway has no way to know the server would have said yes.
- The denylist is substring matching on the path string, not a general-purpose path-policy language. A sufficiently indirect path (a symlink created before the guard is in place, a Unicode homoglyph in a directory name) is not handled here. The honest framing is "first line of defense for an obvious case," not "the whole control."
- The response-mask patterns are a fixed regex set for the demo's credential shapes. A real deployment's secret surface is broader;
CheckResponseis the right hook, not a finished DLP engine. - This was tested against a real local filesystem MCP server and a real agentgateway v1.5.0 binary, both on macOS/arm64. It was not tested in Kubernetes, though the same
mcpGuardrailspolicy block applies there viaAgentgatewayPolicyper the Kubernetes ExtMCP docs.
Requires Node.js/npx (for @modelcontextprotocol/server-filesystem and the MCP inspector CLI), Python 3.13 (grpcio wheels lag behind the newest CPython; 3.13 has them, 3.14 did not at the time of writing), and an agentgateway binary. Validated against agentgateway v1.5.0, arm64 macOS.
git clone https://github.com/themsquared/filesystem-mcp-env-guard
cd filesystem-mcp-env-guard
AGENTGATEWAY_BIN=/path/to/agentgateway bash scripts/demo.shExpected: 7/7 PASS lines at the end, with captured JSON for every call in captures/.
The script installs grpcio/grpcio-tools into guard/.venv and generates the gRPC bindings from guard/ext_mcp.proto on first run (bindings are regenerated only if guard/ext_mcp_pb2.py is missing).
# terminal 1: the policy server cd guard && source .venv/bin/activate && python3 guard_server.py # terminal 2: agentgateway, guarded agentgateway -f config.yaml # terminal 3: an MCP call npx -y @modelcontextprotocol/inspector --cli http://localhost:13300/mcp \ --method tools/call --tool-name read_text_file --tool-arg path="$(pwd)/sandbox/.env"
config.yaml/config-unguarded.yaml: agentgateway standalone configs, guarded and baseline. Both front the same filesystem MCP server as a stdio target rooted atsandbox/.guard/ext_mcp.proto: the upstream ExtMCP service definition, copied verbatim fromagentgateway/agentgateway.guard/guard_server.py: the policy server (path denylist + response redaction).sandbox/: a.env, an.ssh/id_rsa, and a log file, all containing fake demo values, plus one genuinely harmless file.scripts/demo.sh: runs all three scenarios and checks the results.
mcp-discovery-prompt-injection covers the equivalent controls (digest pinning, instruction caps) on the tools/list-time discovery surface. This repo covers tools/call-time filesystem arguments instead.
Apache-2.0