tree-sitter-analyzer 1.7.0__py3-none-any.whl → 1.7.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tree-sitter-analyzer might be problematic. Click here for more details.
- tree_sitter_analyzer/__init__.py +1 -1
- tree_sitter_analyzer/mcp/tools/find_and_grep_tool.py +184 -11
- tree_sitter_analyzer/mcp/tools/list_files_tool.py +112 -2
- tree_sitter_analyzer/mcp/tools/search_content_tool.py +210 -10
- {tree_sitter_analyzer-1.7.0.dist-info → tree_sitter_analyzer-1.7.2.dist-info}/METADATA +205 -238
- {tree_sitter_analyzer-1.7.0.dist-info → tree_sitter_analyzer-1.7.2.dist-info}/RECORD +8 -8
- {tree_sitter_analyzer-1.7.0.dist-info → tree_sitter_analyzer-1.7.2.dist-info}/entry_points.txt +1 -0
- {tree_sitter_analyzer-1.7.0.dist-info → tree_sitter_analyzer-1.7.2.dist-info}/WHEEL +0 -0
tree_sitter_analyzer/__init__.py
CHANGED
|
@@ -13,6 +13,7 @@ import time
|
|
|
13
13
|
from typing import Any
|
|
14
14
|
|
|
15
15
|
from ..utils.error_handler import handle_mcp_errors
|
|
16
|
+
from ..utils.file_output_manager import FileOutputManager
|
|
16
17
|
from ..utils.gitignore_detector import get_default_detector
|
|
17
18
|
from . import fd_rg_utils
|
|
18
19
|
from .base_tool import BaseMCPTool
|
|
@@ -23,6 +24,11 @@ logger = logging.getLogger(__name__)
|
|
|
23
24
|
class FindAndGrepTool(BaseMCPTool):
|
|
24
25
|
"""MCP tool that composes fd and ripgrep with safety limits and metadata."""
|
|
25
26
|
|
|
27
|
+
def __init__(self, project_root: str | None = None) -> None:
|
|
28
|
+
"""Initialize the find and grep tool."""
|
|
29
|
+
super().__init__(project_root)
|
|
30
|
+
self.file_output_manager = FileOutputManager(project_root)
|
|
31
|
+
|
|
26
32
|
def get_tool_definition(self) -> dict[str, Any]:
|
|
27
33
|
return {
|
|
28
34
|
"name": "find_and_grep",
|
|
@@ -191,6 +197,15 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
191
197
|
"default": False,
|
|
192
198
|
"description": "Return only the total match count as a number. Most token-efficient option for count queries. Takes priority over all other formats",
|
|
193
199
|
},
|
|
200
|
+
"output_file": {
|
|
201
|
+
"type": "string",
|
|
202
|
+
"description": "Optional filename to save output to file (extension auto-detected based on content)",
|
|
203
|
+
},
|
|
204
|
+
"suppress_output": {
|
|
205
|
+
"type": "boolean",
|
|
206
|
+
"description": "When true and output_file is specified, suppress detailed output in response to save tokens",
|
|
207
|
+
"default": False,
|
|
208
|
+
},
|
|
194
209
|
},
|
|
195
210
|
"required": ["roots", "query"],
|
|
196
211
|
"additionalProperties": False,
|
|
@@ -373,11 +388,7 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
373
388
|
context_before=arguments.get("context_before"),
|
|
374
389
|
context_after=arguments.get("context_after"),
|
|
375
390
|
encoding=arguments.get("encoding"),
|
|
376
|
-
max_count=
|
|
377
|
-
arguments.get("max_count"),
|
|
378
|
-
fd_rg_utils.DEFAULT_RESULTS_LIMIT,
|
|
379
|
-
fd_rg_utils.MAX_RESULTS_HARD_CAP,
|
|
380
|
-
),
|
|
391
|
+
max_count=arguments.get("max_count"),
|
|
381
392
|
timeout_ms=arguments.get("timeout_ms"),
|
|
382
393
|
roots=rg_roots,
|
|
383
394
|
files_from=None,
|
|
@@ -427,9 +438,18 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
427
438
|
else:
|
|
428
439
|
# Parse full match details
|
|
429
440
|
matches = fd_rg_utils.parse_rg_json_lines_to_matches(rg_out)
|
|
430
|
-
|
|
431
|
-
if
|
|
432
|
-
|
|
441
|
+
|
|
442
|
+
# Apply user-specified max_count limit if provided
|
|
443
|
+
# Note: ripgrep's -m option limits matches per file, not total matches
|
|
444
|
+
# So we need to apply the total limit here in post-processing
|
|
445
|
+
user_max_count = arguments.get("max_count")
|
|
446
|
+
if user_max_count is not None and len(matches) > user_max_count:
|
|
447
|
+
matches = matches[:user_max_count]
|
|
448
|
+
truncated_rg = True
|
|
449
|
+
else:
|
|
450
|
+
truncated_rg = len(matches) >= fd_rg_utils.MAX_RESULTS_HARD_CAP
|
|
451
|
+
if truncated_rg:
|
|
452
|
+
matches = matches[: fd_rg_utils.MAX_RESULTS_HARD_CAP]
|
|
433
453
|
|
|
434
454
|
# Apply path optimization if requested
|
|
435
455
|
optimize_paths = arguments.get("optimize_paths", False)
|
|
@@ -452,12 +472,55 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
452
472
|
"fd_elapsed_ms": fd_elapsed_ms,
|
|
453
473
|
"rg_elapsed_ms": rg_elapsed_ms,
|
|
454
474
|
}
|
|
475
|
+
|
|
476
|
+
# Handle output suppression and file output for grouped results
|
|
477
|
+
output_file = arguments.get("output_file")
|
|
478
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
479
|
+
|
|
480
|
+
# Handle file output if requested
|
|
481
|
+
if output_file:
|
|
482
|
+
try:
|
|
483
|
+
# Save full result to file
|
|
484
|
+
import json
|
|
485
|
+
json_content = json.dumps(grouped_result, indent=2, ensure_ascii=False)
|
|
486
|
+
file_path = self.file_output_manager.save_to_file(
|
|
487
|
+
content=json_content,
|
|
488
|
+
base_name=output_file
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
# If suppress_output is True, return minimal response
|
|
492
|
+
if suppress_output:
|
|
493
|
+
minimal_result = {
|
|
494
|
+
"success": grouped_result.get("success", True),
|
|
495
|
+
"count": grouped_result.get("count", 0),
|
|
496
|
+
"output_file": output_file,
|
|
497
|
+
"file_saved": f"Results saved to {file_path}"
|
|
498
|
+
}
|
|
499
|
+
return minimal_result
|
|
500
|
+
else:
|
|
501
|
+
# Include file info in full response
|
|
502
|
+
grouped_result["output_file"] = output_file
|
|
503
|
+
grouped_result["file_saved"] = f"Results saved to {file_path}"
|
|
504
|
+
except Exception as e:
|
|
505
|
+
logger.error(f"Failed to save output to file: {e}")
|
|
506
|
+
grouped_result["file_save_error"] = str(e)
|
|
507
|
+
grouped_result["file_saved"] = False
|
|
508
|
+
elif suppress_output:
|
|
509
|
+
# If suppress_output is True but no output_file, remove detailed results
|
|
510
|
+
minimal_result = {
|
|
511
|
+
"success": grouped_result.get("success", True),
|
|
512
|
+
"count": grouped_result.get("count", 0),
|
|
513
|
+
"summary": grouped_result.get("summary", {}),
|
|
514
|
+
"meta": grouped_result.get("meta", {})
|
|
515
|
+
}
|
|
516
|
+
return minimal_result
|
|
517
|
+
|
|
455
518
|
return grouped_result
|
|
456
519
|
|
|
457
520
|
# Check if summary_only mode is requested
|
|
458
521
|
if arguments.get("summary_only", False):
|
|
459
522
|
summary = fd_rg_utils.summarize_search_results(matches)
|
|
460
|
-
|
|
523
|
+
result = {
|
|
461
524
|
"success": True,
|
|
462
525
|
"summary_only": True,
|
|
463
526
|
"summary": summary,
|
|
@@ -468,10 +531,53 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
468
531
|
"rg_elapsed_ms": rg_elapsed_ms,
|
|
469
532
|
},
|
|
470
533
|
}
|
|
534
|
+
|
|
535
|
+
# Handle output suppression and file output for summary results
|
|
536
|
+
output_file = arguments.get("output_file")
|
|
537
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
538
|
+
|
|
539
|
+
# Handle file output if requested
|
|
540
|
+
if output_file:
|
|
541
|
+
try:
|
|
542
|
+
# Save full result to file
|
|
543
|
+
import json
|
|
544
|
+
json_content = json.dumps(result, indent=2, ensure_ascii=False)
|
|
545
|
+
file_path = self.file_output_manager.save_to_file(
|
|
546
|
+
content=json_content,
|
|
547
|
+
base_name=output_file
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
# If suppress_output is True, return minimal response
|
|
551
|
+
if suppress_output:
|
|
552
|
+
minimal_result = {
|
|
553
|
+
"success": result.get("success", True),
|
|
554
|
+
"count": len(matches),
|
|
555
|
+
"output_file": output_file,
|
|
556
|
+
"file_saved": f"Results saved to {file_path}"
|
|
557
|
+
}
|
|
558
|
+
return minimal_result
|
|
559
|
+
else:
|
|
560
|
+
# Include file info in full response
|
|
561
|
+
result["output_file"] = output_file
|
|
562
|
+
result["file_saved"] = f"Results saved to {file_path}"
|
|
563
|
+
except Exception as e:
|
|
564
|
+
logger.error(f"Failed to save output to file: {e}")
|
|
565
|
+
result["file_save_error"] = str(e)
|
|
566
|
+
result["file_saved"] = False
|
|
567
|
+
elif suppress_output:
|
|
568
|
+
# If suppress_output is True but no output_file, remove detailed results
|
|
569
|
+
minimal_result = {
|
|
570
|
+
"success": result.get("success", True),
|
|
571
|
+
"count": len(matches),
|
|
572
|
+
"summary": result.get("summary", {}),
|
|
573
|
+
"meta": result.get("meta", {})
|
|
574
|
+
}
|
|
575
|
+
return minimal_result
|
|
576
|
+
|
|
577
|
+
return result
|
|
471
578
|
else:
|
|
472
|
-
|
|
579
|
+
result = {
|
|
473
580
|
"success": True,
|
|
474
|
-
"results": matches,
|
|
475
581
|
"count": len(matches),
|
|
476
582
|
"meta": {
|
|
477
583
|
"searched_file_count": searched_file_count,
|
|
@@ -480,3 +586,70 @@ class FindAndGrepTool(BaseMCPTool):
|
|
|
480
586
|
"rg_elapsed_ms": rg_elapsed_ms,
|
|
481
587
|
},
|
|
482
588
|
}
|
|
589
|
+
|
|
590
|
+
# Handle output suppression and file output
|
|
591
|
+
output_file = arguments.get("output_file")
|
|
592
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
593
|
+
|
|
594
|
+
# Add results to response unless suppressed
|
|
595
|
+
if not suppress_output or not output_file:
|
|
596
|
+
result["results"] = matches
|
|
597
|
+
|
|
598
|
+
# Handle file output if requested
|
|
599
|
+
if output_file:
|
|
600
|
+
try:
|
|
601
|
+
# Create detailed output for file
|
|
602
|
+
file_content = {
|
|
603
|
+
"success": True,
|
|
604
|
+
"results": matches,
|
|
605
|
+
"count": len(matches),
|
|
606
|
+
"files": fd_rg_utils.group_matches_by_file(matches)["files"] if matches else [],
|
|
607
|
+
"summary": fd_rg_utils.summarize_search_results(matches),
|
|
608
|
+
"meta": result["meta"]
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
# Convert to JSON for file output
|
|
612
|
+
# Save full result to file using FileOutputManager
|
|
613
|
+
import json
|
|
614
|
+
json_content = json.dumps(file_content, indent=2, ensure_ascii=False)
|
|
615
|
+
file_path = self.file_output_manager.save_to_file(
|
|
616
|
+
content=json_content,
|
|
617
|
+
base_name=output_file
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
# Check if suppress_output is enabled
|
|
621
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
622
|
+
if suppress_output:
|
|
623
|
+
# Return minimal response to save tokens
|
|
624
|
+
minimal_result = {
|
|
625
|
+
"success": result.get("success", True),
|
|
626
|
+
"count": result.get("count", 0),
|
|
627
|
+
"output_file": output_file,
|
|
628
|
+
"file_saved": f"Results saved to {file_path}"
|
|
629
|
+
}
|
|
630
|
+
return minimal_result
|
|
631
|
+
else:
|
|
632
|
+
# Include file info in full response
|
|
633
|
+
result["output_file"] = output_file
|
|
634
|
+
result["file_saved"] = f"Results saved to {file_path}"
|
|
635
|
+
|
|
636
|
+
logger.info(f"Search results saved to: {file_path}")
|
|
637
|
+
|
|
638
|
+
except Exception as e:
|
|
639
|
+
logger.error(f"Failed to save output to file: {e}")
|
|
640
|
+
result["file_save_error"] = str(e)
|
|
641
|
+
result["file_saved"] = False
|
|
642
|
+
else:
|
|
643
|
+
# Handle suppress_output without file output
|
|
644
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
645
|
+
if suppress_output:
|
|
646
|
+
# Return minimal response without detailed match results
|
|
647
|
+
minimal_result = {
|
|
648
|
+
"success": result.get("success", True),
|
|
649
|
+
"count": result.get("count", 0),
|
|
650
|
+
"summary": result.get("summary", {}),
|
|
651
|
+
"meta": result.get("meta", {})
|
|
652
|
+
}
|
|
653
|
+
return minimal_result
|
|
654
|
+
|
|
655
|
+
return result
|
|
@@ -13,6 +13,7 @@ from pathlib import Path
|
|
|
13
13
|
from typing import Any
|
|
14
14
|
|
|
15
15
|
from ..utils.error_handler import handle_mcp_errors
|
|
16
|
+
from ..utils.file_output_manager import FileOutputManager
|
|
16
17
|
from ..utils.gitignore_detector import get_default_detector
|
|
17
18
|
from . import fd_rg_utils
|
|
18
19
|
from .base_tool import BaseMCPTool
|
|
@@ -110,6 +111,15 @@ class ListFilesTool(BaseMCPTool):
|
|
|
110
111
|
"default": False,
|
|
111
112
|
"description": "Return only the total count of matching files instead of file details. Useful for quick statistics",
|
|
112
113
|
},
|
|
114
|
+
"output_file": {
|
|
115
|
+
"type": "string",
|
|
116
|
+
"description": "Optional filename to save output to file (extension auto-detected based on content)"
|
|
117
|
+
},
|
|
118
|
+
"suppress_output": {
|
|
119
|
+
"type": "boolean",
|
|
120
|
+
"description": "When true and output_file is specified, suppress detailed output in response to save tokens",
|
|
121
|
+
"default": False
|
|
122
|
+
},
|
|
113
123
|
},
|
|
114
124
|
"required": ["roots"],
|
|
115
125
|
"additionalProperties": False,
|
|
@@ -243,7 +253,7 @@ class ListFilesTool(BaseMCPTool):
|
|
|
243
253
|
else:
|
|
244
254
|
truncated = False
|
|
245
255
|
|
|
246
|
-
|
|
256
|
+
result = {
|
|
247
257
|
"success": True,
|
|
248
258
|
"count_only": True,
|
|
249
259
|
"total_count": total_count,
|
|
@@ -251,6 +261,52 @@ class ListFilesTool(BaseMCPTool):
|
|
|
251
261
|
"elapsed_ms": elapsed_ms,
|
|
252
262
|
}
|
|
253
263
|
|
|
264
|
+
# Handle file output for count_only mode
|
|
265
|
+
output_file = arguments.get("output_file")
|
|
266
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
267
|
+
|
|
268
|
+
if output_file:
|
|
269
|
+
file_manager = FileOutputManager(self.project_root)
|
|
270
|
+
file_content = {
|
|
271
|
+
"count_only": True,
|
|
272
|
+
"total_count": total_count,
|
|
273
|
+
"truncated": truncated,
|
|
274
|
+
"elapsed_ms": elapsed_ms,
|
|
275
|
+
"query_info": {
|
|
276
|
+
"roots": arguments.get("roots", []),
|
|
277
|
+
"pattern": arguments.get("pattern"),
|
|
278
|
+
"glob": arguments.get("glob", False),
|
|
279
|
+
"types": arguments.get("types"),
|
|
280
|
+
"extensions": arguments.get("extensions"),
|
|
281
|
+
"exclude": arguments.get("exclude"),
|
|
282
|
+
"limit": limit,
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
try:
|
|
287
|
+
import json
|
|
288
|
+
json_content = json.dumps(file_content, indent=2, ensure_ascii=False)
|
|
289
|
+
saved_path = file_manager.save_to_file(
|
|
290
|
+
content=json_content,
|
|
291
|
+
base_name=output_file
|
|
292
|
+
)
|
|
293
|
+
result["output_file"] = saved_path
|
|
294
|
+
|
|
295
|
+
if suppress_output:
|
|
296
|
+
# Return minimal response to save tokens
|
|
297
|
+
return {
|
|
298
|
+
"success": True,
|
|
299
|
+
"count_only": True,
|
|
300
|
+
"total_count": total_count,
|
|
301
|
+
"output_file": saved_path,
|
|
302
|
+
"message": f"Count results saved to {saved_path}"
|
|
303
|
+
}
|
|
304
|
+
except Exception as e:
|
|
305
|
+
logger.warning(f"Failed to save output file: {e}")
|
|
306
|
+
result["output_file_error"] = str(e)
|
|
307
|
+
|
|
308
|
+
return result
|
|
309
|
+
|
|
254
310
|
# Truncate defensively even if fd didn't
|
|
255
311
|
truncated = False
|
|
256
312
|
if len(lines) > fd_rg_utils.MAX_RESULTS_HARD_CAP:
|
|
@@ -283,10 +339,64 @@ class ListFilesTool(BaseMCPTool):
|
|
|
283
339
|
except (OSError, ValueError): # nosec B112
|
|
284
340
|
continue
|
|
285
341
|
|
|
286
|
-
|
|
342
|
+
result = {
|
|
287
343
|
"success": True,
|
|
288
344
|
"count": len(results),
|
|
289
345
|
"truncated": truncated,
|
|
290
346
|
"elapsed_ms": elapsed_ms,
|
|
291
347
|
"results": results,
|
|
292
348
|
}
|
|
349
|
+
|
|
350
|
+
# Handle file output for detailed results
|
|
351
|
+
output_file = arguments.get("output_file")
|
|
352
|
+
suppress_output = arguments.get("suppress_output", False)
|
|
353
|
+
|
|
354
|
+
if output_file:
|
|
355
|
+
file_manager = FileOutputManager(self.project_root)
|
|
356
|
+
file_content = {
|
|
357
|
+
"count": len(results),
|
|
358
|
+
"truncated": truncated,
|
|
359
|
+
"elapsed_ms": elapsed_ms,
|
|
360
|
+
"results": results,
|
|
361
|
+
"query_info": {
|
|
362
|
+
"roots": arguments.get("roots", []),
|
|
363
|
+
"pattern": arguments.get("pattern"),
|
|
364
|
+
"glob": arguments.get("glob", False),
|
|
365
|
+
"types": arguments.get("types"),
|
|
366
|
+
"extensions": arguments.get("extensions"),
|
|
367
|
+
"exclude": arguments.get("exclude"),
|
|
368
|
+
"depth": arguments.get("depth"),
|
|
369
|
+
"follow_symlinks": arguments.get("follow_symlinks", False),
|
|
370
|
+
"hidden": arguments.get("hidden", False),
|
|
371
|
+
"no_ignore": no_ignore,
|
|
372
|
+
"size": arguments.get("size"),
|
|
373
|
+
"changed_within": arguments.get("changed_within"),
|
|
374
|
+
"changed_before": arguments.get("changed_before"),
|
|
375
|
+
"full_path_match": arguments.get("full_path_match", False),
|
|
376
|
+
"absolute": arguments.get("absolute", True),
|
|
377
|
+
"limit": limit,
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
try:
|
|
382
|
+
import json
|
|
383
|
+
json_content = json.dumps(file_content, indent=2, ensure_ascii=False)
|
|
384
|
+
saved_path = file_manager.save_to_file(
|
|
385
|
+
content=json_content,
|
|
386
|
+
base_name=output_file
|
|
387
|
+
)
|
|
388
|
+
result["output_file"] = saved_path
|
|
389
|
+
|
|
390
|
+
if suppress_output:
|
|
391
|
+
# Return minimal response to save tokens
|
|
392
|
+
return {
|
|
393
|
+
"success": True,
|
|
394
|
+
"count": len(results),
|
|
395
|
+
"output_file": saved_path,
|
|
396
|
+
"message": f"File list results saved to {saved_path}"
|
|
397
|
+
}
|
|
398
|
+
except Exception as e:
|
|
399
|
+
logger.warning(f"Failed to save output file: {e}")
|
|
400
|
+
result["output_file_error"] = str(e)
|
|
401
|
+
|
|
402
|
+
return result
|