sansan0/TrendRadar · error · InvalidParameterError

mode 必须是字符串类型

Error message

mode 必须是字符串类型

What it means

Raised by validate_mode when the `mode` argument is not None and not a str instance. The MCP server validates mode-like parameters against an allow-list; a non-string (int, dict, list, bool) fails type validation before the membership check. Only None is permitted (it maps to the default).

Source

Thrown at mcp_server/utils/validators.py:551

    """
    验证模式参数

    Args:
        mode: 模式字符串
        valid_modes: 有效模式列表
        default: 默认模式

    Returns:
        验证后的模式

    Raises:
        InvalidParameterError: 模式无效
    """
    if mode is None:
        return default

    if not isinstance(mode, str):
        raise InvalidParameterError("mode 必须是字符串类型")

    if mode not in valid_modes:
        raise InvalidParameterError(
            f"无效的模式: {mode}",
            suggestion=f"支持的模式: {', '.join(valid_modes)}"
        )

    return mode


def validate_config_section(section: Optional[str]) -> str:
    """
    验证配置节参数

    Args:
        section: 配置节名称

    Returns:

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Pass the mode as a plain string, e.g. mode="fast", matching one of valid_modes.
  2. Omit the parameter entirely (or pass null) to accept the documented default.
  3. Check the tool schema in the MCP client and map enum values to strings before the call.

Example fix

// before
tool_call(mode=1)

// after
tool_call(mode="fast")
Defensive patterns

Strategy: type-guard

Validate before calling

if mode is not None and not isinstance(mode, str):
    mode = str(mode)  # or reject explicitly
tool_call(mode=mode)

Type guard

def is_valid_mode(v) -> bool:
    return v is None or isinstance(v, str)

Try / catch

try:
    tool_call(mode=mode)
except InvalidParameterError as e:
    if "必须是字符串类型" in str(e):
        mode = str(mode)  # coerce and retry once

Prevention

When it happens

Trigger: Calling an MCP tool with `mode=1`, `mode={"name":"fast"}`, `mode=["fast"]`, or `mode=True` instead of `mode="fast"`. Any JSON type other than string or null triggers it.

Common situations: MCP clients serializing enum-like params as numbers or nested objects; LLM tool-calling producing structured JSON where a bare string was expected; passing a Python bool because a mode was named like a flag.

Related errors


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