sansan0/TrendRadar · warning · InvalidParameterError

keyword 长度不能超过100个字符

Error message

keyword 长度不能超过100个字符

What it means

Raised by the keyword validator in the MCP server when a search keyword exceeds 100 characters after stripping whitespace. The limit enforces concise search terms so downstream news-search providers do not truncate or reject the query. It is an InvalidParameterError carrying a suggestion ('请使用更简洁的关键词'), so it is a client-input validation error, not a server fault.

Source

Thrown at mcp_server/utils/validators.py:507

    Returns:
        处理后的关键词

    Raises:
        InvalidParameterError: 关键词无效
    """
    if not keyword:
        raise InvalidParameterError("keyword 不能为空")

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

    keyword = keyword.strip()

    if not keyword:
        raise InvalidParameterError("keyword 不能为空白字符")

    if len(keyword) > 100:
        raise InvalidParameterError(
            "keyword 长度不能超过100个字符",
            suggestion="请使用更简洁的关键词"
        )

    return keyword


def validate_top_n(top_n: Optional[Union[int, str]], default: int = 10) -> int:
    """
    验证TOP N参数

    Args:
        top_n: TOP N数量(整数或字符串)
        default: 默认值

    Returns:
        验证后的值

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Shorten the keyword to a concise term (<= 100 characters, excluding surrounding whitespace).
  2. If searching for multiple concepts, split into several tool calls, one keyword each.
  3. If the keyword is a long URL or quote, extract the distinctive phrase instead of passing the whole string.
  4. Catch InvalidParameterError client-side and surface the embedded suggestion text to the user.

Example fix

// before
tool_call(keyword="人工智能在医疗健康领域的最新研究进展、行业落地案例以及监管政策变化趋势的完整分析报告")

// after
tool_call(keyword="人工智能 医疗监管")
Defensive patterns

Strategy: validation

Validate before calling

def safe_keyword(kw: str) -> str:
    kw = (kw or "").strip()
    if not kw:
        raise ValueError("keyword is empty")
    if len(kw) > 100:
        raise ValueError(f"keyword too long: {len(kw)} > 100")
    return kw

tool_call(keyword=safe_keyword(user_input))

Type guard

def is_valid_keyword(v) -> bool:
    return isinstance(v, str) and 0 < len(v.strip()) <= 100

Try / catch

try:
    result = tool_call(keyword=kw)
except InvalidParameterError as e:
    # e.message contains the Chinese reason; e.suggestion has guidance
    show_user(e.message, getattr(e, "suggestion", None))

Prevention

When it happens

Trigger: Calling an MCP tool that accepts a `keyword` parameter (e.g. news search) with a string longer than 100 chars after `.strip()`. Leading/trailing whitespace does not count toward the limit because the check runs on the stripped value.

Common situations: A user pastes a full sentence, URL, or log excerpt into a keyword field; an LLM-driven MCP client concatenates multiple search terms into one keyword string instead of issuing separate calls.

Related errors


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