feat: [OSS26] 按方法体模式搜代码 search_code_by_body_pattern (#135) - #158
Open
lsr-05 wants to merge 4 commits into
Open
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.
Implement search_code_by_body_pattern with Java-aware method ranges, nested-code attribution, context snippets, path filtering, and integration coverage for issue antgroup#135.
lsr-05
requested review from
AntJiuFo,
Arielwyy and
alipaydeshui
as code owners
August 3, 2026 09:40
Author
2 similar comments
Author
Author
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_body_patternMCP 工具,在 Java 方法体内按正则模式搜索代码片段,并返回命中位置所属方法的全限定名。与签名级搜索(#134)的核心区别是:本工具能够确定代码命中点属于哪个方法,同时排除 import、字段初始化和注释等非方法体内容。
实现
方法体边界识别
_compute_line_depths()的 O(n) 花括号深度状态机。_compute_brace_metadata()一次计算花括号配对、打开深度和声明起始位置。方法范围确定
_extract_method_ranges()仅提取类体级别的方法。com.example.Outer.Inner.run的全限定方法名。嵌套方法处理
run、call等方法名。方法体内搜索
multi_line=True时启用re.DOTALL,支持跨行匹配。multi_line=False时.不匹配换行符。match_line。match_snippet和命中行前后各 2 行的snippet_with_context。max_results截断和path_prefix范围过滤。path_prefix。language=java。排除非方法体内容
//、/*不会被误判为注释。复用 #134 的核心组件
_compute_line_depths()_compute_brace_metadata()_build_class_ranges_by_depth()_CLASS_PATTERN_strip_comments()及共享 Java 词法遮罩_CONTROL_FLOW_KEYWORDS_MODIFIERS_EXCLUDED_DIRS_is_binary()_MAX_RESULTS_LIMIT方法声明部分增加了多行 header 解析,以覆盖单行
_METHOD_LINE_PATTERN无法识别的长参数列表。测试
单元测试
覆盖基本搜索、全限定方法名、方法起止行号、上下文片段、多次命中、非方法体内容排除、Lambda、匿名类、具名内部类、单行方法、多行签名、跨行正则、异常参数、结果截断、路径过滤和 MCP 注册。
集成测试
<5s。131 passed in 61.40s。Closes #135