Skip to content

feat: [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134) - #151

Open
wryyyds7 wants to merge 4 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-search-code-by-signature-pattern-134
Open

feat: [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134)#151
wryyyds7 wants to merge 4 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-search-code-by-signature-pattern-134

Conversation

@wryyyds7

Copy link
Copy Markdown

概述

实现 search_code_by_signature_pattern MCP 工具,用正则模式匹配代码元素(方法/类/字段)的签名,而非普通的行级关键词搜索。

实现

逐行扫描架构

为避免正则回溯灾难(在 9000+ 文件的大仓库上会卡死),采用逐行扫描而非跨行正则匹配:

  • _CLASS_PATTERN: 逐行匹配 class/interface ClassName
  • _METHOD_LINE_PATTERN: 逐行匹配 methodName(params) {;
  • _FIELD_LINE_PATTERN: 逐行匹配 Type name [= value];

注释过滤

_strip_comments() 在匹配前移除行注释 (//) 和块注释 (/* */),替换为等长空白保持行号不变。避免注释中的类/方法定义被误匹配。

花括号深度状态机

_compute_line_depths() 一次遍历 O(n) 计算每行的花括号嵌套深度,状态机跟踪:

  • 双引号字符串 (in_string)
  • 单引号字符 (in_char)
  • 行注释 (in_line_comment)
  • 块注释 (in_block_comment)
    这些状态下的 { } 不计入深度,确保字符串中的花括号不干扰深度计算。

深度语义:

  • depth=0: 文件顶级 (package, import)
  • depth=1: 类体级别 (字段、方法定义)
  • depth≥2: 方法体内 (局部变量)

字段 vs 局部变量区分

利用 depth 数组确定每行是否在方法体内 (method_body_lines),只提取类体级别的字段声明,排除方法体内局部变量。

控制流关键字过滤

_CONTROL_FLOW_KEYWORDS 黑名单: if/for/while/switch/catch/return/throw/new 等。方法名和返回类型均检查此黑名单,双重保险防止控制流语句被误匹配为方法声明。

类范围确定

_build_class_ranges_by_depth() 利用 depth 数组快速确定每个类的行范围,用于确定方法所属类。

测试

单元测试 (39 个)

  • 方法签名: HttpServletRequest 参数匹配、方法名搜索、ResponseEntity 返回类型、protected 方法、if/throw 排除、注释内不误命中、query/select/load 前缀
  • 类签名: extends Abstract*Filter、interface、abstract class、注释内不误命中、containing_class
  • 字段签名: 按类型匹配、String 字段、字段 vs 局部变量、containing_class、protected 字段、return 排除
  • any 模式: 多种元素类型混合
  • 边界情况: 空结果、空 pattern、不存在仓库、无效 element_kind、无效正则、无 Java 文件
  • max_results 截断
  • 注释过滤: 行注释、块注释、多行块注释
  • 花括号匹配: 简单、嵌套、同行
  • MCP 注册对齐

集成测试 (10 个)

  • java-design-patterns (~1900 Java 文件,中型仓库):
    • 抽象类搜索、implements 类搜索、static final 字段、private final 字段、Logger any 搜索
    • 性能 < 3s
  • spring-framework (~9200 Java 文件,大型仓库):
    • extends Abstract 类、Service 接口、get/find 方法
    • 性能 < 5s

性能优化效果

花括号深度跟踪从 O(类数×文件长度) 优化为 O(文件长度):

  • SF private final 字段搜索: 17.76s → 1.25s (14x 加速)

全部 51 个测试通过。

Closes #134

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
…ture pattern

Implement MCP tool that matches regex patterns against code element
signatures (methods, classes, fields) in Java repositories.

Key features:
- Supports element_kind: method, class, field, any
- Comment filtering (line + block comments stripped before matching)
- Field vs local variable disambiguation via brace-depth tracking
- Control flow keyword filtering (if/throw/return not matched as methods)
- Performance: O(n) single-pass depth computation with string/char/comment
  state machine

Test data: java-design-patterns (~1900 files) + spring-framework (~9200 files)
Performance: medium repo < 3s, large repo < 5s

All 51 tests passed.
@wryyyds7

Copy link
Copy Markdown
Author

#134

@wryyyds7 wryyyds7 changed the title [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134) feat: [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134) Jul 30, 2026
… handle multiline class declarations

Two bug fixes in search_code_by_signature_pattern.py:
1. _strip_comments: preserve newlines when replacing block comments
   (was collapsing multi-line comments, causing line number misalignment)
2. _build_class_ranges_by_depth: search for { when class declaration
   spans multiple lines (e.g. 'class X extends Y\n implements Z {')
   was returning wrong class body range, causing 0 methods found
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_signature_pattern

1 participant