Skip to content

fix: prepare_* methods crash on missing required fields instead of returning helpful errors #184

@sigvardt

Description

@sigvardt

Description

Several prepare_* methods crash with unhelpful errors when required fields are missing at runtime. Since MCP tool calls come from AI agents that may omit required parameters, the services must handle missing fields gracefully.

Example 1: prepareCreateFunnelAnalysis without steps

❌ Cannot read properties of undefined (reading 'length')

The method calls validateFunnelSteps(input.steps) where input.steps is undefined. The validator does steps.length < 2 which crashes on undefined.

Example 2: prepareCreateRetentionAnalysis without name

❌ Cannot read properties of undefined (reading 'trim')

Same pattern — calling .trim() on an undefined field.

Root Cause

The TypeScript interfaces mark these fields as required, but:

  1. MCP tool inputs are Record<string, unknown> — no TypeScript enforcement at runtime
  2. AI agents may omit fields they don't know about
  3. The validation functions assume the fields are present

Fix

Add null/undefined guards at the start of each prepare_* method:

if (!input.steps || !Array.isArray(input.steps)) {
  throw new Error('steps is required and must be an array of FunnelStep objects.');
}

This pattern should be applied to ALL prepare_* methods across all services.

Affected Services

Any service with required complex fields:

  • Funnels (steps required)
  • Segmentations (conditions required)
  • Data Manager (various required fields)
  • Tag Manager
  • Campaign Settings

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-review-readyPR created, awaiting reviewbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions