Preventing Credential and .env Leaks Across AI Agent MCP Tool Boundaries

When 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.

1. Audit vs. Redact vs. Block

2. Recursive Object-Tree Redaction

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) };
}

3. Local Summary Reporting

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).