feat: [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134) - #151
Open
wryyyds7 wants to merge 4 commits into
Open
feat: [OSS26] 按签名模式搜代码 search_code_by_signature_pattern (#134)#151wryyyds7 wants to merge 4 commits into
wryyyds7 wants to merge 4 commits into
Conversation
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
requested review from
AntJiuFo,
Arielwyy and
alipaydeshui
as code owners
July 29, 2026 09:08
Author
… 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
This was referenced Aug 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
实现
search_code_by_signature_patternMCP 工具,用正则模式匹配代码元素(方法/类/字段)的签名,而非普通的行级关键词搜索。实现
逐行扫描架构
为避免正则回溯灾难(在 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)这些状态下的
{}不计入深度,确保字符串中的花括号不干扰深度计算。深度语义:
字段 vs 局部变量区分
利用 depth 数组确定每行是否在方法体内 (
method_body_lines),只提取类体级别的字段声明,排除方法体内局部变量。控制流关键字过滤
_CONTROL_FLOW_KEYWORDS黑名单:if/for/while/switch/catch/return/throw/new等。方法名和返回类型均检查此黑名单,双重保险防止控制流语句被误匹配为方法声明。类范围确定
_build_class_ranges_by_depth()利用 depth 数组快速确定每个类的行范围,用于确定方法所属类。测试
单元测试 (39 个)
集成测试 (10 个)
性能优化效果
花括号深度跟踪从 O(类数×文件长度) 优化为 O(文件长度):
全部 51 个测试通过。
Closes #134