Skip to content

feat: [OSS26] 按关键词搜代码 search_code_by_keyword (#130) - #147

Open
wryyyds7 wants to merge 3 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-search-code-by-keyword-130
Open

feat: [OSS26] 按关键词搜代码 search_code_by_keyword (#130)#147
wryyyds7 wants to merge 3 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-search-code-by-keyword-130

Conversation

@wryyyds7

@wryyyds7 wryyyds7 commented Jul 29, 2026

Copy link
Copy Markdown

概述

实现 search_code_by_keyword MCP 工具,在指定代码仓库内按关键词检索代码,支持普通字符串与正则两种模式,返回所有命中的文件路径、行号与代码片段(含上下文行)。

实现

搜索逻辑 (_search_file)

  • 逐行读取文件,用 re.search(pattern, line_content) 匹配
  • 返回: file_path (相对路径)、line_number、column_start/column_end、line_content、context_before/context_after

模式支持

  • is_regex=False (默认): 普通字符串模式,自动用 re.escape() 转义特殊字符
  • is_regex=True: 正则模式,直接编译用户输入

大小写控制

  • case_sensitive=False (默认): re.IGNORECASE
  • case_sensitive=True: 不添加 IGNORECASE flag

范围控制

  • path_prefix: 限定搜索子目录 (如 "src/main/java/")
  • 排除目录: .git, node_modules, target, build, dist, __pycache__, .venv, venv, .idea, .vscode
  • 原地修改 dirnames 阻止 os.walk 进入排除目录

文件过滤

  • 二进制检测: 读取前 8192 字节,含 \x00 则判定为二进制并跳过 (_is_binary)
  • max_results 截断 (上限 1000),达到上限后立即停止

上下文行

  • 每个命中返回前后各 3 行上下文 (_CONTEXT_LINES = 3)
  • 边界处理: 文件开头/结尾时 context_before/context_after 可能为空

测试

单元测试

  • 基本搜索: 普通字符串匹配、单文件多次匹配、跨文件匹配、列位置验证
  • 正则模式: 正则匹配、非正则转义、单词边界
  • 大小写: 不区分大小写 (默认)、区分大小写
  • 空结果: 搜不到、空关键词、不存在仓库
  • max_results 截断
  • 排除目录: .git/node_modules/target
  • path_prefix 过滤
  • 二进制文件跳过
  • 上下文行: 前后上下文、文件开头边界
  • MCP 注册对齐: 自动发现、tools/list、描述、MCP 调用
  • 性能: 1k 文件 < 2s
  • 全部通过

Closes #130

wangrenyu.wry and others added 3 commits July 21, 2026 14:03
Implement the unified MCP Server framework as the runtime base for
all upcoming YASA MCP tools (Issue antgroup#129).

Framework:
- Dual transport: stdio (default) / streamable-http
- Tool registration via @mcp_tool decorator + auto-discovery
- Unified input validation (Pydantic), error handling, logging (stderr)
- Health check endpoint GET /healthz in HTTP mode
- Built-in demo tool ping returning server status

Modules:
- yasa_mcp/config.py: CLI args + env var parsing
- yasa_mcp/server.py: FastMCP lifecycle management
- yasa_mcp/registry.py: decorator-based tool auto-registration
- yasa_mcp/transport/http.py: streamable-http + /healthz
- yasa_mcp/tools/ping.py: demo health-check tool
- yasa_mcp/errors.py: unified error codes
- yasa_mcp/logging_config.py: stderr logging with level control

Tests (44 passing):
- test_registry.py: tool registration, auto-discovery, schema
- test_validation.py: param validation, error handling
- test_logging.py: stderr output, format, level filtering
- test_config.py: CLI parsing, env vars, error exit

Docs:
- docs/mcp/requirements.md: functional/non-functional requirements
- docs/mcp/system-design.md: architecture, module design, data flow
- docs/mcp/development-guide.md: step-by-step implementation guide
- yasa_mcp/README.md: usage + Claude Desktop/Cline config examples
- registry.py: prevent duplicate tool registration via __module__ check
- http.py: use app.add_route() instead of direct app.routes manipulation
- http.py: remove unused Route import
- http.py + server.py: pass log_level to uvicorn instead of hardcoding
- errors.py: fix empty string message fallback bug
- test_registry.py: use asyncio.run() instead of deprecated get_event_loop()
- test_validation.py: remove unused imports
- config.py: convert repo_root to absolute path
- logging_config.py: precise logger namespace check
- pyproject.toml: fix build-backend to setuptools.build_meta
- .gitignore: add .venv/ and Python project ignore rules
- Support plain string and regex search modes
- Support case_sensitive toggle
- Support path_prefix filtering
- Exclude .git/, node_modules/, target/, build/, dist/, __pycache__/
- Skip binary files (detect via null byte in first 8KB)
- Return file_path, line_number, column_start/end, line_content,
  context_before (3 lines), context_after (3 lines)
- max_results default 100, capped at 1000
- 28 unit tests covering all acceptance criteria
@wryyyds7

Copy link
Copy Markdown
Author

#130

@wryyyds7 wryyyds7 changed the title [OSS26] 按关键词搜代码 search_code_by_keyword (#130) feat: [OSS26] 按关键词搜代码 search_code_by_keyword (#130) Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OSS26] 关键词搜索代码 search_code_by_keyword

1 participant