Skip to content

fix(api-client): Relax 'context' field schema in BaseEventSchema - #1301

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/relax-event-context-schema
Open

sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/relax-event-context-schema

Conversation

@sentry

@sentry sentry Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

The BaseEventSchema in packages/mcp-core/src/api-client/schema.ts defined the context field (the legacy 'extra' field) as z.record(z.string(), z.unknown()).optional().

This schema was too strict, as Sentry's API can return non-object shapes (such as arrays or null) for this field on certain events. This led to Zod validation failures and Event failed schema validation errors being logged.

This change relaxes the schema for the context field to z.unknown().optional() to correctly handle the arbitrary nature of this legacy field, preventing validation errors when fetching events from the Sentry API.

Fixes MCP-SERVER-G93

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.

// "context" (singular) is the legacy "extra" field for arbitrary user-defined data
// This is different from "contexts" (plural) which are structured contexts
context: z.record(z.string(), z.unknown()).optional(),
context: z.unknown().optional(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relaxing context to unknown crashes formatting and fails typecheck

Widening context to z.unknown() lets non-object values through validation, but formatContext() still calls Object.keys/Object.entries on it, so arrays/primitives throw at runtime and the strict tsc job rejects unknown there.

Evidence
  • Hunk changes BaseEventSchema.context from z.record(z.string(), z.unknown()).optional() to z.unknown().optional(), so inferred Event["context"] is now unknown | undefined.
  • formatEventOutput() always calls formatContext(eventToRender.context) (formatting.ts).
  • formatContext() does Object.keys(context) and Object.entries(context) with no plain-object guard; non-null non-objects (arrays, numbers, strings, true) throw, and TypeScript under strict: true rejects those calls on unknown.
  • PR motivation is that Sentry can return non-object context shapes; CI runs pnpm -w run tsc in .github/workflows/test.yml.

Identified by Warden · code-review · EKN-GGF

@sentry
sentry Bot marked this pull request as ready for review September 9, 2026 22:45
@sentry
sentry Bot requested a review from dcramer September 9, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants