Error Mapper¶
Feature spec for code-forge implementation planning. Source: extracted from apcore-mcp/docs/tech-design-apcore-mcp.md Created: 2026-04-06
Purpose¶
The Error Mapper is responsible for translating apcore's rich error hierarchy (including schema validation errors, ACL denials, and timeouts) into structured, protocol-compliant error responses for MCP clients and OpenAI's API. It ensures that AI agents receive actionable, machine-readable feedback without leaking sensitive system information.
Scope¶
Included:
- Mapping all ModuleError subclasses to CallToolResult error responses.
- Formatting field-level schema validation details for display in the client.
- Redaction of sensitive details (e.g., specific caller_id or trace_id in ACL denials) for protocol-facing output.
- Attachment of AI guidance metadata (e.g., retryable, aiGuidance) to error responses.
- Sanitation of unexpected non-module exceptions into generic internal error messages.
Excluded:
- Implementation of the Executor (where errors originate).
- Protocol-level response wrapping (handled by the ExecutionRouter).
Core Responsibilities¶
- Category Mapping — Maps specific apcore exceptions (e.g.,
ModuleNotFoundError,ModuleTimeoutError) to corresponding protocol-level error codes and formatted messages. - Detail Extraction — Extracts structured information (e.g., specific fields that failed validation) and converts them into a human-readable, bulleted string.
- AI Guidance Injection — Attaches metadata to the error response to help agents understand whether to retry, what the user should fix, or what the model should adjust.
- Information Security — Replaces internal exceptions and stack traces with sanitized, safe messages (e.g., "Internal error occurred") for external consumption.
Interfaces¶
Inputs¶
- Exception Instance (Execution Router) — The error raised during the execution pipeline.
Outputs¶
- CallToolResult (MCP SDK) — A protocol result object with
isError=trueand formatted text content.
Dependencies¶
- apcore SDK (language-equivalent: apcore-python / apcore-js / apcore Rust crate) — Provides the
ModuleErrorbase class and its specific subclasses. - language MCP SDK (Python:
mcp; TypeScript:@modelcontextprotocol/sdk; Rust:mcp-sdk) — Provides theCallToolResultandTextContenttypes.
Note: Python and Rust additionally expose
internal_error_response()andto_mcp_error_any(error)for generic-error fallback (EM-6, CHANGELOG 0.14.0). Python exports bothMcpErrorFormatter(canonical) andMCPErrorFormatter(alias) per EM-1.
Data Flow¶
graph LR
A[apcore Exception] --> B[Identify Error Type]
B --> C[Extract Field Details]
C --> D[Apply AI Guidance]
D --> E[Sanitize Message]
E --> F[CallToolResult Output]
Key Behaviors¶
Structured Validation Feedback¶
For SchemaValidationError, the mapper generates an indented summary of each error's location
and human-readable message. apcore 0.32.0 uses {path, keyword, message}: path is an RFC 6901
JSON Pointer and takes precedence over the legacy field key. An empty pointer identifies the
root object and is displayed as <root>; nested pointers and ~0 / ~1 escaping are retained
verbatim. If no string path is present, the mapper falls back to field, then unknown.
The original details.errors entries, including keyword, are passed through unchanged.
AI Guidance Metadata¶
If an error object carries guidance attributes (retryable, ai_guidance, user_fixable, suggestion), the mapper extracts these and attaches them to the response, often as a JSON appendix in the error text content to ensure visibility for LLMs.
Security Sanitization¶
Only errors derived from ModuleError (which are designed to be user-facing) are allowed to pass their messages to the client. All other exceptions result in a generic "Internal error occurred" message, with the original traceback only visible in server-side logs.
Constraints¶
- Consistency: The same error code from apcore must always result in the same message format and wire key convention.
- Protocol Requirements: Every mapped error result must explicitly set
isError=True. - Wire Case: Use camelCase for keys in the JSON guidance appendix to align with MCP/OpenAI conventions.
Error Handling¶
- Unrecognized Error: If the mapper receives a
ModuleErrorit doesn't specifically know, it returns a generic "Module error: {code}" response using the original code. - Extreme Failure: If the mapping logic itself fails, it reverts to a hardcoded "Internal error occurred" safety response.
Notes¶
- This component is critical for the "auto-retry" and "intelligent fix" behaviors in AI agents.
- It leverages apcore's built-in error code registry to stay synchronized with the core framework.
Contract: ErrorMapper.to_mcp_error¶
Inputs¶
- error: per-language input type, required:
- Python:
Exception— any exception; duck-typed for ModuleError-like (must havecode,message,detailsattributes for the apcore path) - TypeScript:
unknown— any value; duck-typed for ModuleError-like; non-matching values fall through to the INTERNAL_ERROR envelope - Rust:
&apcore::ModuleErroronly — strongly typed; for arbitrary&dyn std::error::Erroruseto_mcp_error_any(see EM-6 below)
Errors¶
- Never raises; all exceptions produce a valid error dict
Returns¶
- On success: dict with camelCase top-level keys —
isError: true,errorType: str,message: str,details: dict|None - All SDKs return camelCase keys directly — Python uses isError/errorType with no intermediate snake_case conversion
- Additional optional fields:
retryable: bool,aiGuidance: str,userFixable: bool,suggestion: str— populated from error attributes when present userFixable— all three now read it off the error object rather than hardcoding it, so a code that carriesuser_fixablegets the key and one that does not, does not. The attribute name differs by upstream: Python reads snake_caseerror.user_fixableand maps it to camelCase (errors.py:135); TypeScript reads the already-camelCaseerror.userFixable(errors.ts:397, driven by_AI_GUIDANCE_FIELDS); Rust copieserror.user_fixablewhen the response has not already set one (errors.rs:314). An earlier revision of this spec saiduserFixable: truewas "TypeScript only, hardcoded at errors.ts:241,273" — that has not been true since the_AI_GUIDANCE_FIELDSrefactor. The practical consequence is unchanged: a code whose upstream error class does not setuser_fixable(e.g.DependencyNotFoundErrorin apcore-python) yields a response with nouserFixablekey at all- Non-ModuleError exceptions →
errorType: "GENERAL_INTERNAL_ERROR",message: "Internal error occurred",details: null(matchesinternal_error_response(); see EM-6 below) - ExecutionCancelledError →
errorType: "EXECUTION_CANCELLED",retryable: true - CircuitBreakerOpenError →
errorType: "CIRCUIT_BREAKER_OPEN",retryable: true,aiGuidance: "Upstream circuit is open; retry after backoff."(added in 0.15.0; dispatched via theCIRCUIT_BREAKER_OPENcode constant) - On failure: returns hardcoded GENERAL_INTERNAL_ERROR dict (never raises)
Properties¶
- async: false
- thread_safe: true
- pure: false
- idempotent: true
Contract: ErrorMapper._handle_apcore_error¶
Inputs¶
- error: Exception, required, validates[duck-typed: has
code,message,detailsattributes], reject_with=INTERNAL_ERROR dict
Errors¶
- Never raises; internal errors are converted to sanitized responses
Returns¶
- On success: dict with keys
isError: True,errorType: str(from error.code),message: str,details: dict|None - CALL_DEPTH_EXCEEDED, CIRCULAR_CALL, CALL_FREQUENCY_EXCEEDED → sanitized "Internal error occurred" message
- ACL_DENIED → sanitized "Access denied", details: null
- SCHEMA_VALIDATION_ERROR → multi-line bulleted message with field-level errors; details preserved
- APPROVAL_PENDING → details narrowed to
{"approvalId": ...}(camelCase) - APPROVAL_TIMEOUT →
retryable: true - AI guidance fields (retryable, aiGuidance, userFixable, suggestion) attached from error attributes when present and not already set
- On failure: returns safe fallback dict (never raises)
Properties¶
- async: false
- thread_safe: true
- pure: false
- idempotent: true
Contract: ErrorMapper.internal_error_response¶
EM-6 generic-error fallback. Python and Rust expose this as a free helper alongside the class; TypeScript inlines the same envelope inside toMcpError(error) for any non-ModuleError input. All three SDKs MUST emit byte-identical envelopes to keep MCP clients' errorType === "GENERAL_INTERNAL_ERROR" branch portable.
Inputs¶
- (none)
Errors¶
- Never raises.
Returns¶
- On success (Python):
dictwith keysisError: True,errorType: "GENERAL_INTERNAL_ERROR",message: "Internal error occurred",details: None - On success (Rust):
serde_json::Value(object) with the same keys/values - On success (TS): equivalent object inlined inside
toMcpError(error)for non-ModuleErrorinputs - camelCase wire keys;
errorTypeis the exact string"GENERAL_INTERNAL_ERROR";detailsis JSON null
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Contract: ErrorMapper.to_mcp_error_any¶
EM-6 generic-error fallback for arbitrary error inputs. Python signature to_mcp_error_any(error: Exception) and Rust signature ErrorMapper::to_mcp_error_any<E: std::error::Error>(error: E) both accept any error-like value and return the internal_error_response() envelope unchanged — they NEVER inspect the error's contents. TypeScript covers the same surface inside toMcpError(error: unknown) when the input fails the ModuleError shape check.
Inputs¶
- error: any error-like value (Python
Exception, Rust anystd::error::Error, TSunknown) — required, no validation; the function deliberately ignores the contents to avoid leaking server-side detail
Errors¶
- Never raises.
Returns¶
- On success: identical envelope to
internal_error_response()—isError: true,errorType: "GENERAL_INTERNAL_ERROR",message: "Internal error occurred",details: null - The original error's class name, message, traceback, or details are NEVER surfaced on the wire (security: avoid leaking internal state to MCP clients)
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Contract: CIRCUIT_BREAKER_OPEN error-code constant¶
Public error-code constant introduced in 0.15.0. Exposed alongside the existing apcore error codes so SDK callers can branch on
error.errorType === "CIRCUIT_BREAKER_OPEN"portably across all three languages. Names per language: PythonERROR_CODES["CIRCUIT_BREAKER_OPEN"], TypeScriptErrorCodes.CIRCUIT_BREAKER_OPEN, RustApcoreErrorCode::CircuitBreakerOpen(serializes to the literal string"CIRCUIT_BREAKER_OPEN"for wire output).
Inputs¶
- (none — constant)
Errors¶
- (none)
Returns¶
- Python:
strliteral"CIRCUIT_BREAKER_OPEN" - TypeScript:
constliteral"CIRCUIT_BREAKER_OPEN"(typed asErrorCodes.CIRCUIT_BREAKER_OPEN) - Rust: enum variant
ApcoreErrorCode::CircuitBreakerOpen;Display/ serde implementation MUST emit the exact wire string"CIRCUIT_BREAKER_OPEN"
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Wire format¶
The errorType field of an to_mcp_error envelope dispatched from a CircuitBreakerOpenError MUST equal this literal string. MCP clients branch on response.errorType === "CIRCUIT_BREAKER_OPEN" to apply backoff-and-retry logic before falling through to generic retryable handling.