.env Leaks Across AI Agent MCP Tool BoundariesWhen AI coding assistants call local Model Context Protocol (MCP) tools, they can accidentally read .env files, AWS AKIA... access keys, GitHub PATs, or Bearer JWTs and echo them straight into model context or telemetry logs.
audit: Records findings in JSONL and OTLP span attributes (mcp.guard.findings) without modifying payloads.redact: Walks JSON-RPC params and result trees recursively, replacing secrets with [REDACTED:<rule-id>] while preserving valid JSON-RPC framing.block: Rejects the tool call with a JSON-RPC error (-32001 / -32002) and marks the OpenTelemetry span as failed.Running regex directly on raw serialized JSON strings risks breaking escaping if a replacement contains special characters. Walking the parsed JSON object tree keeps the JSON-RPC envelope intact:
export function inspectAndRedact(value, rules) {
const findings = new Set();
function walk(node) {
if (typeof node === 'string') {
let out = node;
for (const rule of rules) {
const rx = new RegExp(rule.regex.source, 'g');
if (rx.test(out)) {
findings.add(rule.id);
out = out.replace(rx, `[REDACTED:${rule.id}]`);
}
}
return out;
}
if (Array.isArray(node)) return node.map(walk);
if (node && typeof node === 'object') {
return Object.fromEntries(Object.entries(node).map(([k, v]) => [k, walk(v)]));
}
return node;
}
return { findings: [...findings], redacted: walk(value) };
}
npx -y github:gyozomongo/mcp-otel-guard report --log ./mcp-audit.jsonl
Returns aggregated request counts, error totals, secret-hit counts, estimated token burn, and p50/p95 latency per tool. Full source on GitHub · Looking for 20+ enterprise credential/injection rules and a GitHub Actions CI gate? See the Pro Production Kit ($15).