-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathsearch_utils.go
More file actions
112 lines (95 loc) · 2.84 KB
/
search_utils.go
File metadata and controls
112 lines (95 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package github
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"github.com/google/go-github/v74/github"
"github.com/mark3labs/mcp-go/mcp"
)
func hasFilter(query, filterType string) bool {
pattern := fmt.Sprintf(`(^|\s)%s:\S+`, regexp.QuoteMeta(filterType))
matched, _ := regexp.MatchString(pattern, query)
return matched
}
func hasSpecificFilter(query, filterType, filterValue string) bool {
pattern := fmt.Sprintf(`(^|\s)%s:%s($|\s)`, regexp.QuoteMeta(filterType), regexp.QuoteMeta(filterValue))
matched, _ := regexp.MatchString(pattern, query)
return matched
}
func hasRepoFilter(query string) bool {
return hasFilter(query, "repo")
}
func hasTypeFilter(query string) bool {
return hasFilter(query, "type")
}
func searchHandler(
ctx context.Context,
getClient GetClientFn,
request mcp.CallToolRequest,
searchType string,
errorPrefix string,
) (*mcp.CallToolResult, error) {
query, err := RequiredParam[string](request, "query")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if !hasSpecificFilter(query, "is", searchType) {
query = fmt.Sprintf("is:%s %s", searchType, query)
}
owner, err := OptionalParam[string](request, "owner")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
repo, err := OptionalParam[string](request, "repo")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if owner != "" && repo != "" && !hasRepoFilter(query) {
query = fmt.Sprintf("repo:%s/%s %s", owner, repo, query)
}
sort, err := OptionalParam[string](request, "sort")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
order, err := OptionalParam[string](request, "order")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
pagination, err := OptionalPaginationParams(request)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
opts := &github.SearchOptions{
// Default to "created" if no sort is provided, as it's a common use case.
Sort: sort,
Order: order,
ListOptions: github.ListOptions{
Page: pagination.Page,
PerPage: pagination.PerPage,
},
}
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("%s: failed to get GitHub client: %w", errorPrefix, err)
}
result, resp, err := client.Search.Issues(ctx, query, opts)
if err != nil {
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s: failed to read response body: %w", errorPrefix, err)
}
return mcp.NewToolResultError(fmt.Sprintf("%s: %s", errorPrefix, string(body))), nil
}
r, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("%s: failed to marshal response: %w", errorPrefix, err)
}
return mcp.NewToolResultText(string(r)), nil
}