Quick recap: anticipated vs protocol errors
We previously implemented a flaky tool that can fail in two distinct ways:- Anticipated tool failures (
ToolError): meaningful to the model — returned as an error result (isError: true) so the model can react (retry, fallback, etc.). - Protocol-level failures (
MCPError/ JSON-RPC errors): indicate the request itself failed and the model receives no normal result object.
- ToolError: propagated back to the model as an error result (
isError: true) and is useful to the model for decision making (e.g., retry). - MCPError: a protocol-level error (JSON-RPC-level) meaning the request failed and the model does not get a normal result.
This lesson: unexpected runtime errors and sensitive data
In real deployments you may encounter unanticipated runtime failures — for example, a database connection error that contains an IP address, username, or other sensitive information. The MCP Python SDK must avoid returning that sensitive text to the model or client, while still letting operators access the full details from server logs. Example tool that raises an unexpected runtime error (contains sensitive details):- The tool raises an unexpected exception (RuntimeError) that includes sensitive details.
- The MCP SDK does not forward that raw exception text to the model/client.
What the client/model sees
When an unexpected runtime exception occurs, the MCP Python SDK intentionally replaces the detailed, sensitive exception text with a short, non-sensitive message safe to send to the model. The model receives a result object withisError: true and a single non-sensitive text content item such as “Error executing tool flaky_lookup”.
JSON-RPC response returned to the client/model (example):
Where the full error goes
The MCP Python SDK writes the full exception and traceback to the server logs / console so developers can debug the issue. Consult your server console or log files to see the complete traceback and exception details. Example server console / traceback excerpt:The MCP SDK intentionally masks sensitive runtime details from the model/client and logs them to the server console instead. This design prevents leaking IPs, credentials, or stack traces to the model while ensuring developers can still access full diagnostic information in the server logs.
Error types at a glance
Summary
- Use
ToolErrorfor anticipated, meaningful tool failures you want the model to act on. - Use
MCPErrorfor protocol-level failures where no normal result should be returned. - Unexpected runtime exceptions are masked by the MCP SDK: the model receives a minimal, non-sensitive error string while the server logs hold the full exception and traceback for debugging.
Links and references
- Model Context Protocol (MCP) — Server SDK patterns
- JSON-RPC 2.0 specification
- Python logging best practices