[yasamcp] get_file_by_keyword / keywords 在 use_regex=True 且传入多关键词(或列表)时必现 TypeError 崩溃

Open Beginner friendly
#170 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli, search

Research direction

Read yasamcp/src/yasa_mcp/tools/ripgrep/ripgrep_tool.py around search_keywords_with_line, especially how it builds the pattern for regex and literal searches. Add tests for regex searches with one and multiple keywords, then run those tests. Done means the searches return matches without a TypeError and preserve the intended behavior for each keyword.

Written by the indexing model from the issue text.

Description

问题描述

使用 yasamcp 的关键字搜索功能(MCP 工具 get_file_by_keyword 或 CLI 命令 yasamcp keywords),在开启正则模式(use_regex=True / --use-regex)且进入批量搜索分支 search_keywords_with_line 时,100% 抛出 TypeError 崩溃:

  • MCP 路径:keyword 传入 list[str](即使只包含 1 个元素,例如 ["foo"])且 use_regex=True 时必现崩溃;
  • CLI 路径:yasamcp keywords --use-regex 传入 ≥ 2 个关键词时必现崩溃;
  • 同一组关键词在关闭正则(use_regex=False)或以单个 str 传入时均工作正常。
环境信息
  • yasamcp 版本:YASA-MCP 1.2.0 (build 20260924, commit f5965e6)(上游 main 分支当前最新相关提交 59d22349 同样存在该问题)
  • 操作系统:Ubuntu 24.04.4 LTS (x86_64, WSL2)
  • ripgrep:14.1.1(随 yasamcp tools 分发)
最小复现步骤
mkdir -p /tmp/yasamcp-repro
printf 'def hello():\n    return "foo"\n' > /tmp/yasamcp-repro/demo.py
yasamcp init /tmp/yasamcp-repro

# 1. 单关键词 + --use-regex:正常命中(返回 demo.py 第 2 行)
yasamcp keywords -p /tmp/yasamcp-repro --use-regex -j foo

# 2. 多关键词 + 不开正则:正常命中(返回 demo.py 第 1、2 行)
yasamcp keywords -p /tmp/yasamcp-repro -j foo hello

# 3. 多关键词 + --use-regex:100% 必现崩溃
yasamcp keywords -p /tmp/yasamcp-repro --use-regex -j foo hello

实际报错输出:

Traceback (most recent call last):
  ...
  File "yasa_mcp/service/code_search_service.py", line 820, in get_file_by_keyword
  File "yasa_mcp/util/timer_util.py", line 166, in wrapper
  File "yasa_mcp/tools/ripgrep/ripgrep_tool.py", line 183, in search_keywords_with_line
TypeError: sequence item 0: expected str instance, tuple found

MCP 侧复现:通过 MCP stdio 调用 get_file_by_keyword(project_path="/tmp/yasamcp-repro", keyword=["foo"], use_regex=True) 同样在 search_keywords_with_line:183 抛出完全相同的 TypeError。

根因定位

问题位于 yasamcp/src/yasa_mcp/tools/ripgrep/ripgrep_tool.py 第 180–183 行:

values = tuple(value for value in keywords if value)
if not values:
    return []
pattern = "(?:" + "|".join(values if use_regex else re.escape(value) for value in values) + ")"

第 183 行生成器表达式中将循环变量 value 误写成了外层元组 values。当 use_regex=True 时,生成器产出的是 tuple[str, ...] 而非 str,导致 "|".join(...) 直接抛出 TypeError: sequence item 0: expected str instance, tuple found。

上游调用路径(yasamcp/src/yasa_mcp/service/code_search_service.py 第 809–822 行)在 isinstance(keyword, str) 为 False 时统一进入 search_keywords_with_line,因此 MCP 传单元素列表(如 ["foo"])与 CLI 传多关键词都会命中此崩溃。

修复建议
  1. 修正变量名,并对正则分支逐个包裹非捕获分组 (?:...)(避免子模式中的 | 优先级串扰,同时限制前导内联标志如 (?i)foo 仅作用于当前分支,而不泄漏到后续分支):
    pattern = "|".join(
        f"(?:{value})" if use_regex else re.escape(value)
        for value in values
    )
    
    (或改为向 rg 传入多个 -e <pattern> 参数并以 -- 终止选项解析。)
  2. 建议为 RipgrepTool.search_keywords_with_line 补充 use_regex=True 的单元测试(覆盖 1 元素列表、多元素列表及含内联标志的场景)。
Dominant language
TypeScript
Stars
339
Forks
40
Avg merge
2h 1m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from antgroup/YASA-Engine

All issues in antgroup/YASA-Engine

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.