Error surface overview
There are three primary error surfaces:- Policy errors (explicit denies) returned by middleware.
- Tool errors returned by upstream servers.
- Unexpected failures (exceptions) thrown in hooks or middleware.
Deny a tool call
deny for policy violations. It returns a structured error to the MCP client without throwing.
Inject a safe error message
Catch middleware errors
Handle upstream errors safely
If you wrap tool calls or integrate custom transports, make sure to normalize errors and avoid leaking stack traces to clients.Common error patterns
- Return
context.res.deny(...)for policy violations. - Throw errors for unexpected failures; Fentaris returns a JSON-RPC error.
- Use
context.res.on("error", ...)for telemetry hooks.
Best practices
- Always sanitize error messages returned to clients.
- Log the full error internally with correlation ids.
- Prefer explicit
denyresponses for known invalid states. - Keep denial reasons user-friendly; they often appear in UI.
- Normalize errors in custom transports before returning them.