sansan0/TrendRadar · error · InvalidParameterError

date_range 必须包含 start 和 end 字段

Error message

date_range 必须包含 start 和 end 字段

What it means

Raised when date_range is a dict but either 'start' or 'end' is missing or falsy (empty string/None). Both keys are mandatory — a single-sided range like {"start": "2025-10-01"} is rejected rather than defaulted. Extra keys are ignored; only presence of the two matters.

Source

Thrown at mcp_server/utils/validators.py:438

            except InvalidParameterError:
                raise
            except Exception:
                raise InvalidParameterError(
                    f"日期解析失败: {stripped}",
                    suggestion="支持格式: YYYY-MM-DD, {\"start\": \"...\", \"end\": \"...\"}, 或自然语言(今天、本周、最近7天等)"
                )

    if not isinstance(date_range, dict):
        raise InvalidParameterError(
            "date_range 必须是字典类型、日期字符串或有效的JSON字符串",
            suggestion='例如: {"start": "2025-10-01", "end": "2025-10-11"} 或 "2025-10-01"'
        )

    start_str = date_range.get("start")
    end_str = date_range.get("end")

    if not start_str or not end_str:
        raise InvalidParameterError(
            "date_range 必须包含 start 和 end 字段",
            suggestion='例如: {"start": "2025-10-01", "end": "2025-10-11"}'
        )

    start_date = validate_date(start_str)
    end_date = validate_date(end_str)

    if start_date > end_date:
        raise InvalidParameterError(
            "开始日期不能晚于结束日期",
            suggestion=f"start: {start_str}, end: {end_str}"
        )

    # 检查日期是否在未来
    today = datetime.now().date()
    if start_date.date() > today or end_date.date() > today:
        # 获取可用日期范围提示
        try:

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Always include both keys: {"start": "2025-10-01", "end": "2025-10-11"}
  2. Use exactly 'start' and 'end' (lowercase) — not from/to, startDate/endDate
  3. For an open-ended need, bound it explicitly (e.g. end = today)
  4. Build the dict programmatically and assert both keys before sending

Example fix

// before
{"date_range": {"from": "2025-10-01", "to": "2025-10-11"}}
// after
{"date_range": {"start": "2025-10-01", "end": "2025-10-11"}}
Defensive patterns

Strategy: validation

Validate before calling

if (!dr?.start || !dr?.end) {
  dr = { start: dr?.start || isoDaysAgo(7), end: dr?.end || isoToday() };
}

Type guard

const hasStartAndEnd = (v: unknown): v is { start: string; end: string } =>
  typeof v === 'object' && v !== null &&
  typeof (v as any).start === 'string' && typeof (v as any).end === 'string' &&
  (v as any).start !== '' && (v as any).end !== '';

Try / catch

try { call({ date_range: dr }); }
catch (e) {
  if (/必须包含 start 和 end/.test(e.message)) call({ date_range: { start: dr.start || isoDaysAgo(7), end: dr.end || isoToday() } });
  else throw e;
}

Prevention

When it happens

Trigger: Sending {"start": "2025-10-01"} only, {"end": "2025-10-11"} only, {"from": ..., "to": ...} (wrong key names), or {"start": "", "end": ""}. Also LLMs using key names like startDate/endDate.

Common situations: Clients porting from APIs that accept open-ended ranges; key-name mismatches (from/to vs start/end); forms where one bound is optional; typos in keys ('Start', 'starts').

Related errors


AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15). Data as JSON: /api/errors/6470484cb3a89e77. Report an issue: GitHub.