sansan0/TrendRadar · error · InvalidParameterError

limit 参数必须是整数类型

Error message

limit 参数必须是整数类型

What it means

Raised by validate_limit when, after the string-to-int coercion step, the value is still not an int. Because strings are already converted by _parse_string_to_int (which raises its own, more specific error for bad strings), this branch catches non-string, non-int types: floats like 3.7 (isinstance(3.7, int) is False), bools pass but floats don't, dicts, lists, None is handled earlier.

Source

Thrown at mcp_server/utils/validators.py:280

        limit: 限制数量(整数或字符串)
        default: 默认值
        max_limit: 最大限制

    Returns:
        验证后的限制值

    Raises:
        InvalidParameterError: 参数无效
    """
    if limit is None:
        return default

    # 支持字符串形式的整数(某些 MCP 客户端会将数字序列化为字符串)
    if isinstance(limit, str):
        limit = _parse_string_to_int(limit, "limit")

    if not isinstance(limit, int):
        raise InvalidParameterError("limit 参数必须是整数类型")

    if limit <= 0:
        raise InvalidParameterError("limit 必须大于0")

    if limit > max_limit:
        raise InvalidParameterError(
            f"limit 不能超过 {max_limit}",
            suggestion=f"请使用分页或降低limit值"
        )

    return limit


def validate_date(date_str: str) -> datetime:
    """
    验证日期格式

    Args:

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Send an integer literal: 20, not 20.0
  2. In JS, round before sending: Math.trunc(limit) or limit | 0
  3. If the value is computed, coerce client-side with parseInt/Number.isInteger checks
  4. Catch InvalidParameterError and retry with a sane default (e.g. 20)

Example fix

// before
{"limit": 20.5}
// after
{"limit": 20}
Defensive patterns

Strategy: type-guard

Validate before calling

const limit = Number.isInteger(rawLimit) ? rawLimit : Math.trunc(Number(rawLimit));

Type guard

const isInt = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v);

Try / catch

try { call({ limit }); }
catch (e) {
  if (/limit 参数必须是整数类型/.test(e.message)) call({ limit: Math.trunc(limit) });
  else throw e;
}

Prevention

When it happens

Trigger: Passing limit as a JSON float {"limit": 20.0} (some clients emit 20.0 instead of 20), a dict, a list, or a bool. Note isinstance(True, int) is True in Python, so true is accepted as 1 and won't hit this.

Common situations: JavaScript/TypeScript clients where a computed limit becomes a float (e.g. avg, division); JSON encoders emitting .0 for whole numbers; accidentally nesting the parameter {"limit": {"value": 20}}.

Related errors


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