@@ -29,9 +29,36 @@ def dump_syntax_tree(
2929 """
3030 return run_ast_grep_dump (code , language , format .value )
3131
32+
33+ @mcp .tool ()
34+ def test_match_code_rule (
35+ code : str = Field (description = "The code to test against the rule" ),
36+ yaml : str = Field (description = "The ast-grep YAML rule to search. It must have id, language, rule fields." ),
37+ ) -> List [dict [str , Any ]]:
38+ """
39+ Test a code against an ast-grep YAML rule.
40+ This is useful to test a rule before using it in a project.
41+ """
42+ args = ["ast-grep" , "scan" ,"--inline-rules" , yaml , "--json" , "--stdin" ]
43+ try :
44+ # Run command and capture output
45+ result = subprocess .run (
46+ args ,
47+ capture_output = True ,
48+ input = code ,
49+ text = True ,
50+ check = True # Raises CalledProcessError if return code is non-zero
51+ )
52+ matches = json .loads (result .stdout .strip ())
53+ if not matches :
54+ raise ValueError ("No matches found for the given code and rule. Try adding `stopBy: end` to your inside/has rule." )
55+ return matches
56+ except subprocess .CalledProcessError as e :
57+ return e .stderr
58+
3259@mcp .tool ()
3360def find_code (
34- project_folder : str = Field (description = "The path to the project folder" ),
61+ project_folder : str = Field (description = "The absolute path to the project folder. It must be absolute path. " ),
3562 pattern : str = Field (description = "The ast-grep pattern to search for. Note the pattern must has valid AST structure." ),
3663 language : str = Field (description = "The language of the query" , default = "" ),
3764) -> List [dict [str , Any ]]:
0 commit comments