Libra CodeHub

CedarCopilot/cedar-mail

Branch: staging

fix(external-mcp): translate Mastra's output-validation envelope into isError

merged#2705CedarCopilot

CedarCopilot wants to merge 1 commit into staging from fix/config-read-mastra-output-envelope

Live on prod, no production signal yetTimeline and evidence
  1. Opened
    Sep 6, 2026, 5:50 PM
  2. Merged
    Sep 6, 2026, 6:01 PM
  3. Live on staging
    Sep 6, 2026, 6:01 PM
  4. Live on prod
    Sep 6, 2026, 6:01 PM
  5. Observed 0 hours
    Sep 6, 2026, 6:01 PM
  6. Unobserved

    Live on prod, no production signal yet

    Sep 6, 2026, 6:01 PM
  7. Pipelines steady after this deploy
    Sep 6, 2026, 6:01 PM
  8. Pipelines steady after this deploy
    Sep 6, 2026, 6:01 PM
  9. Sep 6, 2026, 6:03 PM

Behaviors Libra is checking

Config field options without a stored color are accepted so legacy data can be returned by the config-read list-fields action without output validation failure.Degradedlow confidence

Strict CloudWatch fallback found 1 prod failure log lines for mirror.upsertEmailThread failed (Failed query: \n UPDATE crm_thread_labels tl\n SET latest_message_at = t.latest_message_at\n FROM crm_email_threads t\n WHERE t.thread_id = tl.thread_id\n AND tl.thread_id = $1\n AND.

staging, checked Sep 9, 2026, 5:24 PM
External MCP family-tool calls that receive Mastra's output-validation envelope are returned as MCP errors with a descriptive message instead of being forwarded as invalid structured content.Inconclusivelow confidence

Verification reached its 120s wall-clock budget before a tied verdict. Libra is keeping this intent verifying until the next check.

staging, checked Sep 9, 2026, 5:26 PM
The config-read list-fields action accepts legacy field options that have no stored color value while continuing to validate other field-option properties.Not checked
prod
External MCP family-tool calls that return Mastra's output-validation envelope are returned to MCP clients as an error with a diagnostic message instead of being forwarded as invalid structured content.Not checked
prod

Failures attributed to this change

No prod customers are affected while this is only in staging. If promoted, prod impact is unknown because Libra still needs a concrete exception, route, and failed user action before assigning severity. 0 hits · no retained affected-user count · no retained trace sample.Introducedmedium confidence

internal_only

staging, first seen Sep 6, 2026, 7:26 PM
No prod customers are affected while this is only in staging. If promoted, prod impact is unknown because Libra still needs a concrete exception, route, and failed user action before assigning severity. 1 hit · no retained affected-user count · no retained trace sample.Introducedmedium confidence

internal_only

staging, first seen Sep 7, 2026, 5:30 AM
No prod customers are affected while this is only in staging. If promoted, prod impact is unknown because Libra still needs a concrete exception, route, and failed user action before assigning severity. 1 hit · no retained affected-user count · no retained trace sample.Introducedmedium confidence

internal_only

staging, first seen Sep 7, 2026, 7:45 AM

Libra has verdicts on 0 of 2 tracked behaviors on prod; 2 are still being checked. Libra checks hourly for 3 days after each deploy.

Summary

  • config-read's list-fields action was failing with MCP error -32602: Output validation error: ... path: ["success"], message: "Required" , an opaque error that pointed nowhere near the actual problem.
  • Root cause: Mastra's Tool.execute() validates a family tool's return value against its own outputSchema internally, and on a mismatch it doesn't throw , it silently returns an {error: true, message, validationErrors} envelope in place of the tool's real result. server.ts's toCallToolResult forwarded that envelope unchanged as structuredContent with no isError flag, so the MCP SDK then ran its own outputSchema check against the error object and failed on the missing success field.
  • The actual trigger: FieldOptionDataSchema.color was a required z.string(), but CustomFieldDefinition.options[].color is stored in a jsonb column with no runtime enforcement of the TS type , a legacy/hand-edited field option missing color failed Mastra's internal validation.

Changes

  • server.ts: toCallToolResult now detects Mastra's internal validation-failure envelope and converts it into a proper isError CallToolResult with a legible message, instead of letting the SDK reject it with a confusing schema error.
  • config-tool-shared.ts: FieldOptionDataSchema.color is now optional, matching the reality of stored field options.
  • server.test.ts: pins the envelope→isError translation, confirms a real {success, ...} result still passes through unchanged, and confirms a real result carrying its own domain-level error field alongside success isn't misclassified.

Went through a thermo-nuclear code review pass (dependency direction, false-positive risk against every other family tool's output schema, file size, type cleanliness) before opening.

Test plan

  • pnpm --filter @zero/server exec vitest run src/mastra/mcp/external/__tests__/server.test.ts src/mastra/tools/config/__tests__/configTool.test.ts , 119 passed
  • pnpm --filter @zero/server run types , clean
  • pnpm deps:check , no violations
  • Grepped all ~70 family-tool output schemas to confirm the new envelope-detection guard can't misclassify a legitimate result

🤖 Generated with Claude Code

https://claude.ai/code/session_01BJ92k9jHt3CmmqX1dji4Zc

Greptile Summary

This PR improves external MCP error handling and aligns config field-option validation with persisted legacy data.

  • Recognizes Mastra output-validation envelopes and translates them into legible MCP error results.
  • Allows legacy field options without a stored color to pass the config tool’s output schema.
  • Adds regression coverage for validation envelopes, normal structured output, and domain results containing an error property.

Confidence Score: 5/5

The PR appears safe to merge, with the validation-envelope translation and legacy color handling covered by focused regression tests.

No actionable failure remains: the guard distinguishes current legitimate family-tool results, the MCP error shape matches existing server conventions, and optional color reflects persisted legacy data without breaking repository consumers.

Important Files Changed

FilenameOverview
apps/server/src/mastra/mcp/external/server.tsDetects Mastra output-validation envelopes and converts them to conventional MCP error results without misclassifying normal family-tool output.
apps/server/src/mastra/mcp/external/tests/server.test.tsAdds focused regression tests for envelope translation and the principal false-positive cases.
apps/server/src/mastra/tools/config/config-tool-shared.tsMakes field-option color optional so the output schema reflects legacy JSONB records that legitimately omit it.

Flowchart

rendering diagram…
Show production surfaces and changed-file mapping

Production surfaces

Libra has not measured any production surfaces for this change yet.

Changed files → surfaces

  • apps/server/src/mastra/mcp/external/__tests__/server.test.tsno production surface mapped
  • apps/server/src/mastra/mcp/external/server.tsno production surface mapped
  • apps/server/src/mastra/tools/config/config-tool-shared.tsno production surface mapped