Keep secrets out of tool descriptions and logs
In this article (5 sections)
Tool descriptions may be sent to models and cached in traces. Logs can reach broad observability systems. Neither should contain credentials.
Execute a redaction fixture
The MCP contract lab sanitizes an invented API key and bearer token.
from mcp_cases import secret_logging_case
result = secret_logging_case()
assert result["secret_fragments_remaining"] is False
assert result["raw_log_persisted"] is False
assert "[REDACTED]" in result["description"]
assert result["log"]["authorization"] == "[REDACTED]"The first implementation overmatched a JSON quote and broke parsing; the corrected regex stops at string boundaries. This still covers only fixture patterns.
Keep secrets out by design
Tool descriptions explain purpose and schema, never credential values or locations that expose them. Load secrets from approved secret storage inside the adapter. Use structured logging with an allowlist, argument digests and opaque request IDs.
Apply redaction before serialization/export and test nested objects, headers, URLs and exceptions. Detection is a backup; data minimization is primary. Restrict and expire access to traces.
Test leakage paths
Inject secrets into descriptions, arguments, results, errors and stack traces. Verify they never enter model context or logs. Rotate any real credential exposed during testing rather than trusting redaction alone.
The Generative & Agentic AI course includes secret-safe tool and observability contracts.
Exercise
Create synthetic tokens in ten nested locations. Apply allowlisted logging and redaction, then scan every generated artifact for original fragments.
Continue learning
This article is part of the MCP and integration contracts sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Expose business errors as structured tool results.
- Continue with Test tenant isolation in a retrieval tool.
Reference: OWASP secrets management guidance.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI