62159
Sy
symb / files
symb / internal / mcptools / grep.go
4.0 KB
1// Package mcptools provides local MCP tool implementations. 2package mcptools 3 4import ( 5 "context" 6 "encoding/json" 7 "fmt" 8 "os" 9 "strings" 10 11 "github.com/xonecas/symb/internal/filesearch" 12 "github.com/xonecas/symb/internal/mcp" 13) 14 15// GrepArgs represents arguments for the grep tool. 16type GrepArgs struct { 17 Pattern string `json:"pattern"` // Pattern to search for (regex) 18 ContentSearch bool `json:"content_search,omitempty"` // Search file contents (default: false, searches filenames) 19 MaxResults int `json:"max_results,omitempty"` // Max results to return (default: 100) 20 CaseSensitive bool `json:"case_sensitive,omitempty"` // Case-sensitive matching (default: false) 21} 22 23// NewGrepTool creates the grep tool definition. 24func NewGrepTool() mcp.Tool { 25 return mcp.Tool{ 26 Name: "Grep", 27 Description: "Search for files by name (fuzzy) or search file contents (grep). Respects .gitignore. Use content_search=false for finding files, content_search=true for searching content.", 28 InputSchema: json.RawMessage(`{ 29 "type": "object", 30 "properties": { 31 "pattern": {"type": "string", "description": "Pattern to search for (regex). For filenames: matches against basename or path. For content: matches line contents."}, 32 "content_search": {"type": "boolean", "description": "If true, search file contents (grep); if false, search filenames (find). Default: false"}, 33 "max_results": {"type": "integer", "description": "Maximum number of results to return. Default: 100"}, 34 "case_sensitive": {"type": "boolean", "description": "Enable case-sensitive matching. Default: false (case-insensitive)"} 35 }, 36 "required": ["pattern"] 37 }`), 38 } 39} 40 41// MakeGrepHandler creates a handler for the grep tool. 42func MakeGrepHandler() mcp.ToolHandler { 43 return func(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) { 44 var args GrepArgs 45 if err := json.Unmarshal(arguments, &args); err != nil { 46 return &mcp.ToolResult{ 47 Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid arguments: %v", err)}}, 48 IsError: true, 49 }, nil 50 } 51 52 if args.Pattern == "" { 53 return &mcp.ToolResult{ 54 Content: []mcp.ContentBlock{{Type: "text", Text: "Pattern cannot be empty"}}, 55 IsError: true, 56 }, nil 57 } 58 59 // Default max results 60 if args.MaxResults <= 0 { 61 args.MaxResults = 100 62 } 63 64 // Get current working directory 65 cwd, err := os.Getwd() 66 if err != nil { 67 return &mcp.ToolResult{ 68 Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to get working directory: %v", err)}}, 69 IsError: true, 70 }, nil 71 } 72 73 // Create searcher 74 searcher, err := filesearch.NewSearcher(cwd) 75 if err != nil { 76 return &mcp.ToolResult{ 77 Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to create searcher: %v", err)}}, 78 IsError: true, 79 }, nil 80 } 81 82 // Execute search 83 results, err := searcher.Search(ctx, filesearch.Options{ 84 Pattern: args.Pattern, 85 ContentSearch: args.ContentSearch, 86 MaxResults: args.MaxResults, 87 CaseSensitive: args.CaseSensitive, 88 RootDir: cwd, 89 }) 90 91 if err != nil { 92 return &mcp.ToolResult{ 93 Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Search failed: %v", err)}}, 94 IsError: true, 95 }, nil 96 } 97 98 // Format results 99 var output strings.Builder 100 if len(results) == 0 { 101 output.WriteString("No matches found") 102 } else { 103 if args.ContentSearch { 104 output.WriteString(fmt.Sprintf("Found %d match(es):\n\n", len(results))) 105 for _, r := range results { 106 output.WriteString(fmt.Sprintf("%s:%d:%s\n", r.Path, r.Line, r.Content)) 107 } 108 } else { 109 output.WriteString(fmt.Sprintf("Found %d file(s):\n\n", len(results))) 110 for _, r := range results { 111 output.WriteString(fmt.Sprintf("%s\n", r.Path)) 112 } 113 } 114 115 if len(results) >= args.MaxResults { 116 output.WriteString(fmt.Sprintf("\n(Limited to %d results. Use max_results parameter to see more)", args.MaxResults)) 117 } 118 } 119 120 return &mcp.ToolResult{ 121 Content: []mcp.ContentBlock{{Type: "text", Text: output.String()}}, 122 IsError: false, 123 }, nil 124 } 125}