This repository was archived by the owner on Aug 20, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgithub_search.go
More file actions
128 lines (112 loc) · 3.39 KB
/
Copy pathgithub_search.go
File metadata and controls
128 lines (112 loc) · 3.39 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mcptools
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-github/v60/github"
"github.com/shaharia-lab/goai"
)
// GetSearchTool returns a tool for GitHub search operations
func (g *GitHub) GetSearchTool() goai.Tool {
return goai.Tool{
Name: GitHubSearchToolName,
Description: "Performs GitHub search operations across repositories, code, issues, and users",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["repositories", "code", "issues", "users"],
"description": "Search type to perform"
},
"query": {
"type": "string",
"description": "Search query"
},
"language": {
"type": "string",
"description": "Programming language filter for code search"
},
"sort": {
"type": "string",
"enum": ["stars", "forks", "updated", "best-match"],
"description": "Sort order for results"
},
"order": {
"type": "string",
"enum": ["asc", "desc"],
"description": "Sort direction"
}
},
"required": ["operation", "query"]
}`),
Handler: g.handleSearchOperation,
}
}
func (g *GitHub) handleSearchOperation(ctx context.Context, params goai.CallToolParams) (goai.CallToolResult, error) {
ctx, span := goai.StartSpan(ctx, fmt.Sprintf("%s.Handler", params.Name))
defer span.End()
var input struct {
Operation string `json:"operation"`
Query string `json:"query"`
Language string `json:"language"`
Sort string `json:"sort"`
Order string `json:"order"`
}
g.logger.WithFields(map[string]interface{}{
"tool": params.Name,
"tool_argument": string(params.Arguments),
}).Info("Received input")
if err := json.Unmarshal(params.Arguments, &input); err != nil {
return goai.CallToolResult{}, fmt.Errorf("failed to unmarshal input: %w", err)
}
var result interface{}
var err error
searchOpts := &github.SearchOptions{
Sort: input.Sort,
Order: input.Order,
}
g.logger.WithFields(map[string]interface{}{
"tool": params.Name,
"operation": input.Operation,
"query": input.Query,
"language": input.Language,
"sort": input.Sort,
"order": input.Order,
}).Info("Handling search operation")
// Append language to query if specified
if input.Language != "" && input.Operation == "code" {
input.Query = input.Query + " language:" + input.Language
}
switch input.Operation {
case "repositories":
result, _, err = g.client.Search.Repositories(ctx, input.Query, searchOpts)
case "code":
result, _, err = g.client.Search.Code(ctx, input.Query, searchOpts)
case "issues":
result, _, err = g.client.Search.Issues(ctx, input.Query, searchOpts)
case "users":
result, _, err = g.client.Search.Users(ctx, input.Query, searchOpts)
default:
return returnErrorOutput(fmt.Errorf("unsupported operation: %s", input.Operation)), nil
}
if err != nil {
g.logger.WithFields(map[string]interface{}{
"operation": input.Operation,
"error": err,
}).Error("GitHub search operation failed")
return returnErrorOutput(err), nil
}
m := mustMarshal(result)
g.logger.WithFields(map[string]interface{}{
"tool": GitHubSearchToolName,
"operation": input.Operation,
"result_length": len(m),
}).Info("GitHub search operation completed successfully")
return goai.CallToolResult{
Content: []goai.ToolResultContent{{
Type: "json",
Text: m,
}},
}, nil
}