Skip to main content
Welcome — in this demo we continue exploring error handling in the MCP Python SDK and how the SDK treats errors raised by tools. This lesson focuses on unexpected runtime failures that may contain sensitive details (IP addresses, database users, stack traces) and how the SDK prevents those details from leaking to the model or client while still providing developers with full diagnostics in server logs.

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.
Example (anticipated vs protocol modes)
Key points:
  • 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):
Behavior to expect:
  • 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 with isError: 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):
Note how the model only sees the minimal, non-sensitive message “Error executing tool flaky_lookup”. Sensitive details (IP addresses, DB users, stack traces) are deliberately omitted from the payload sent to the model.

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 ToolError for anticipated, meaningful tool failures you want the model to act on.
  • Use MCPError for 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.
That covers how the MCP Python SDK handles different error classes and why it masks unexpected runtime details before sending anything to the model.

Watch Video