Building AI Agents in Production: What MCP Rejections, .env Leaks, and 70‑Line Loops Taught Me
Описание и применение
Внедрение обработчиков отказов MCP, ограничение переменных окружения, динамическая подача учётных данных и детекция бесконечных циклов обеспечили стабильную работу AI‑агентов в продакшене.
Задача
AI agents often fail in production due to naive engineering: unhandled MCP rejections, environment variable leaks, and infinite reasoning loops.
Результат
The agent now handles MCP rejections gracefully, no longer leaks credentials, and avoids infinite loops, resulting in stable production runs and reduced on‑call incidents.
Рабочий процесс
- 1. Treat MCP rejections as first‑class events and implement a rejection handler that maps codes to recovery actions.
- 2. Scope environment variables per agent deployment and avoid shared containers.
- 3. Fetch credentials dynamically from secure endpoints instead of loading at startup.
- 4. Add an output sanitization layer to redact credential patterns.
- 5. Enforce maximum iteration bounds and progress detection to prevent infinite loops.
- 6. Maintain a hash of recent tool calls to detect and reject duplicates.
- 7. Log and alert on MCP rejections, leaks, and loop detections for rapid incident response.
Доказательства
MCP rejections should be treated as first‑class events, not exceptions
The .env leak cost us $47,000
- Дата записи: 2026-09-15T08:46:55.954Z