sansan0/TrendRadar · error · InvalidParameterError

{param_name} 必须是整数,无法解析: {value}

Error message

{param_name} 必须是整数,无法解析: {value}

What it means

Raised by _parse_string_to_int in validators.py when a string parameter cannot be coerced to int. The helper first tries int(value), then int(float(value)) so '3.5' becomes 3; only if both fail does it raise. param_name is interpolated so the message names the offending parameter (commonly 'limit' or 'top_n').

Source

Thrown at mcp_server/utils/validators.py:99

    Returns:
        解析后的整数

    Raises:
        InvalidParameterError: 解析失败
    """
    value = value.strip()

    try:
        # 尝试直接转换
        return int(value)
    except ValueError:
        pass

    # 尝试解析浮点数后取整
    try:
        return int(float(value))
    except ValueError:
        raise InvalidParameterError(
            f"{param_name} 必须是整数,无法解析: {value}",
            suggestion=f"请提供有效的整数值,如: 10, 50, 100"
        )


def _parse_string_to_float(value: str, param_name: str = "参数") -> float:
    """
    将字符串解析为浮点数

    Args:
        value: 字符串值
        param_name: 参数名(用于错误消息)

    Returns:
        解析后的浮点数

    Raises:
        InvalidParameterError: 解析失败

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Provide a clean numeric string or an actual integer: 10, "10", or "10.0"
  2. Strip whitespace and thousands separators before sending: value.replace(',', '') if the comma is a grouping separator
  3. If the value can be empty, send null/omit the parameter instead of an empty string so the validator applies its default
  4. Catch InvalidParameterError client-side and re-prompt with the suggestion (valid integers like 10, 50, 100)

Example fix

// before
{"limit": "10 items"}
// after
{"limit": 10}
Defensive patterns

Strategy: validation

Validate before calling

function toInt(v: unknown, name = 'limit'): number {
  const n = Number(String(v).trim());
  if (!Number.isFinite(n)) throw new Error(`${name} must be numeric`);
  return Math.trunc(n);
}
const limit = toInt(rawLimit); // call tool with this

Type guard

const isIntLike = (v: unknown): v is number => {
  if (typeof v === 'number') return Number.isInteger(v);
  return /^-?\d+(\.\d+)?$/.test(String(v).trim());
};

Try / catch

try { call({ limit }); }
catch (e) {
  if (e.name === 'InvalidParameterError' && /整数/.test(e.message)) {
    call({ limit: 20 }); // retry with default
  } else throw e;
}

Prevention

When it happens

Trigger: Passing limit/top_n as a string like "abc", "10x", "1e", "", or "null". Typically happens when an MCP client serializes numbers as strings and the user supplied a non-numeric value, e.g. tools/call with {"limit": "many"}.

Common situations: HTTP-based MCP clients that stringify all arguments; LLMs hallucinating word-based numbers ('twenty'); empty-string defaults from form inputs; locale decimal separators like '1,5' or '1.000,5'.

Related errors


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