Skip to content

Commit 37bcf93

Browse files
committed
extend to search_repositories, search_code, search_users and search_orgs
1 parent 13cfccb commit 37bcf93

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

pkg/github/search.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package github
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"io"
87

@@ -14,7 +13,7 @@ import (
1413
)
1514

1615
// SearchRepositories creates a tool to search for GitHub repositories.
17-
func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
16+
func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (tool mcp.Tool, handler server.ToolHandlerFunc) {
1817
return mcp.NewTool("search_repositories",
1918
mcp.WithDescription(t("TOOL_SEARCH_REPOSITORIES_DESCRIPTION", "Find GitHub repositories by name, description, readme, topics, or other metadata. Perfect for discovering projects, finding examples, or locating specific repositories across GitHub.")),
2019

@@ -93,7 +92,6 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
9392
}
9493

9594
// Return either minimal or full response based on parameter
96-
var r []byte
9795
if minimalOutput {
9896
minimalRepos := make([]MinimalRepository, 0, len(result.Repositories))
9997
for _, repo := range result.Repositories {
@@ -132,23 +130,15 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
132130
Items: minimalRepos,
133131
}
134132

135-
r, err = json.Marshal(minimalResult)
136-
if err != nil {
137-
return nil, fmt.Errorf("failed to marshal minimal response: %w", err)
138-
}
139-
} else {
140-
r, err = json.Marshal(result)
141-
if err != nil {
142-
return nil, fmt.Errorf("failed to marshal full response: %w", err)
143-
}
133+
return FormatResponse(minimalResult, flags)
144134
}
145135

146-
return mcp.NewToolResultText(string(r)), nil
136+
return FormatResponse(result, flags)
147137
}
148138
}
149139

150140
// SearchCode creates a tool to search for code across GitHub repositories.
151-
func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
141+
func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (tool mcp.Tool, handler server.ToolHandlerFunc) {
152142
return mcp.NewTool("search_code",
153143
mcp.WithDescription(t("TOOL_SEARCH_CODE_DESCRIPTION", "Fast and precise code search across ALL GitHub repositories using GitHub's native search engine. Best for finding exact symbols, functions, classes, or specific code patterns.")),
154144
mcp.WithToolAnnotation(mcp.ToolAnnotation{
@@ -218,16 +208,11 @@ func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (to
218208
return mcp.NewToolResultError(fmt.Sprintf("failed to search code: %s", string(body))), nil
219209
}
220210

221-
r, err := json.Marshal(result)
222-
if err != nil {
223-
return nil, fmt.Errorf("failed to marshal response: %w", err)
224-
}
225-
226-
return mcp.NewToolResultText(string(r)), nil
211+
return FormatResponse(result, flags)
227212
}
228213
}
229214

230-
func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHandlerFunc {
215+
func userOrOrgHandler(accountType string, getClient GetClientFn, flags FeatureFlags) server.ToolHandlerFunc {
231216
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
232217
query, err := RequiredParam[string](request, "query")
233218
if err != nil {
@@ -307,16 +292,12 @@ func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHand
307292
minimalResp.IncompleteResults = *result.IncompleteResults
308293
}
309294

310-
r, err := json.Marshal(minimalResp)
311-
if err != nil {
312-
return nil, fmt.Errorf("failed to marshal response: %w", err)
313-
}
314-
return mcp.NewToolResultText(string(r)), nil
295+
return FormatResponse(minimalResp, flags)
315296
}
316297
}
317298

318299
// SearchUsers creates a tool to search for GitHub users.
319-
func SearchUsers(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
300+
func SearchUsers(getClient GetClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (tool mcp.Tool, handler server.ToolHandlerFunc) {
320301
return mcp.NewTool("search_users",
321302
mcp.WithDescription(t("TOOL_SEARCH_USERS_DESCRIPTION", "Find GitHub users by username, real name, or other profile information. Useful for locating developers, contributors, or team members.")),
322303
mcp.WithToolAnnotation(mcp.ToolAnnotation{
@@ -336,11 +317,11 @@ func SearchUsers(getClient GetClientFn, t translations.TranslationHelperFunc) (t
336317
mcp.Enum("asc", "desc"),
337318
),
338319
WithPagination(),
339-
), userOrOrgHandler("user", getClient)
320+
), userOrOrgHandler("user", getClient, flags)
340321
}
341322

342323
// SearchOrgs creates a tool to search for GitHub organizations.
343-
func SearchOrgs(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
324+
func SearchOrgs(getClient GetClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (tool mcp.Tool, handler server.ToolHandlerFunc) {
344325
return mcp.NewTool("search_orgs",
345326
mcp.WithDescription(t("TOOL_SEARCH_ORGS_DESCRIPTION", "Find GitHub organizations by name, location, or other organization metadata. Ideal for discovering companies, open source foundations, or teams.")),
346327

@@ -361,5 +342,5 @@ func SearchOrgs(getClient GetClientFn, t translations.TranslationHelperFunc) (to
361342
mcp.Enum("asc", "desc"),
362343
),
363344
WithPagination(),
364-
), userOrOrgHandler("org", getClient)
345+
), userOrOrgHandler("org", getClient, flags)
365346
}

pkg/github/search_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func Test_SearchRepositories(t *testing.T) {
1818
// Verify tool definition once
1919
mockClient := github.NewClient(nil)
20-
tool, _ := SearchRepositories(stubGetClientFn(mockClient), translations.NullTranslationHelper)
20+
tool, _ := SearchRepositories(stubGetClientFn(mockClient), translations.NullTranslationHelper, stubFeatureFlags(nil))
2121
require.NoError(t, toolsnaps.Test(tool.Name, tool))
2222

2323
assert.Equal(t, "search_repositories", tool.Name)
@@ -130,7 +130,7 @@ func Test_SearchRepositories(t *testing.T) {
130130
t.Run(tc.name, func(t *testing.T) {
131131
// Setup client with mock
132132
client := github.NewClient(tc.mockedClient)
133-
_, handler := SearchRepositories(stubGetClientFn(client), translations.NullTranslationHelper)
133+
_, handler := SearchRepositories(stubGetClientFn(client), translations.NullTranslationHelper, stubFeatureFlags(nil))
134134

135135
// Create call request
136136
request := createMCPRequest(tc.requestArgs)
@@ -201,7 +201,7 @@ func Test_SearchRepositories_FullOutput(t *testing.T) {
201201
)
202202

203203
client := github.NewClient(mockedClient)
204-
_, handlerTest := SearchRepositories(stubGetClientFn(client), translations.NullTranslationHelper)
204+
_, handlerTest := SearchRepositories(stubGetClientFn(client), translations.NullTranslationHelper, stubFeatureFlags(nil))
205205

206206
request := createMCPRequest(map[string]interface{}{
207207
"query": "golang test",
@@ -231,7 +231,7 @@ func Test_SearchRepositories_FullOutput(t *testing.T) {
231231
func Test_SearchCode(t *testing.T) {
232232
// Verify tool definition once
233233
mockClient := github.NewClient(nil)
234-
tool, _ := SearchCode(stubGetClientFn(mockClient), translations.NullTranslationHelper)
234+
tool, _ := SearchCode(stubGetClientFn(mockClient), translations.NullTranslationHelper, stubFeatureFlags(nil))
235235
require.NoError(t, toolsnaps.Test(tool.Name, tool))
236236

237237
assert.Equal(t, "search_code", tool.Name)
@@ -342,7 +342,7 @@ func Test_SearchCode(t *testing.T) {
342342
t.Run(tc.name, func(t *testing.T) {
343343
// Setup client with mock
344344
client := github.NewClient(tc.mockedClient)
345-
_, handler := SearchCode(stubGetClientFn(client), translations.NullTranslationHelper)
345+
_, handler := SearchCode(stubGetClientFn(client), translations.NullTranslationHelper, stubFeatureFlags(nil))
346346

347347
// Create call request
348348
request := createMCPRequest(tc.requestArgs)
@@ -386,7 +386,7 @@ func Test_SearchCode(t *testing.T) {
386386
func Test_SearchUsers(t *testing.T) {
387387
// Verify tool definition once
388388
mockClient := github.NewClient(nil)
389-
tool, _ := SearchUsers(stubGetClientFn(mockClient), translations.NullTranslationHelper)
389+
tool, _ := SearchUsers(stubGetClientFn(mockClient), translations.NullTranslationHelper, stubFeatureFlags(nil))
390390
require.NoError(t, toolsnaps.Test(tool.Name, tool))
391391

392392
assert.Equal(t, "search_users", tool.Name)
@@ -536,7 +536,7 @@ func Test_SearchUsers(t *testing.T) {
536536
t.Run(tc.name, func(t *testing.T) {
537537
// Setup client with mock
538538
client := github.NewClient(tc.mockedClient)
539-
_, handler := SearchUsers(stubGetClientFn(client), translations.NullTranslationHelper)
539+
_, handler := SearchUsers(stubGetClientFn(client), translations.NullTranslationHelper, stubFeatureFlags(nil))
540540

541541
// Create call request
542542
request := createMCPRequest(tc.requestArgs)
@@ -581,7 +581,7 @@ func Test_SearchUsers(t *testing.T) {
581581
func Test_SearchOrgs(t *testing.T) {
582582
// Verify tool definition once
583583
mockClient := github.NewClient(nil)
584-
tool, _ := SearchOrgs(stubGetClientFn(mockClient), translations.NullTranslationHelper)
584+
tool, _ := SearchOrgs(stubGetClientFn(mockClient), translations.NullTranslationHelper, stubFeatureFlags(nil))
585585

586586
assert.Equal(t, "search_orgs", tool.Name)
587587
assert.NotEmpty(t, tool.Description)
@@ -703,7 +703,7 @@ func Test_SearchOrgs(t *testing.T) {
703703
t.Run(tc.name, func(t *testing.T) {
704704
// Setup client with mock
705705
client := github.NewClient(tc.mockedClient)
706-
_, handler := SearchOrgs(stubGetClientFn(client), translations.NullTranslationHelper)
706+
_, handler := SearchOrgs(stubGetClientFn(client), translations.NullTranslationHelper, stubFeatureFlags(nil))
707707

708708
// Create call request
709709
request := createMCPRequest(tc.requestArgs)

pkg/github/tools.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG
166166
// Create toolsets
167167
repos := toolsets.NewToolset(ToolsetMetadataRepos.ID, ToolsetMetadataRepos.Description).
168168
AddReadTools(
169-
toolsets.NewServerTool(SearchRepositories(getClient, t)),
169+
toolsets.NewServerTool(SearchRepositories(getClient, t, flags)),
170170
toolsets.NewServerTool(GetFileContents(getClient, getRawClient, t)),
171171
toolsets.NewServerTool(ListCommits(getClient, t, flags)),
172-
toolsets.NewServerTool(SearchCode(getClient, t)),
172+
toolsets.NewServerTool(SearchCode(getClient, t, flags)),
173173
toolsets.NewServerTool(GetCommit(getClient, t, flags)),
174174
toolsets.NewServerTool(ListBranches(getClient, t, flags)),
175175
toolsets.NewServerTool(ListTags(getClient, t, flags)),
@@ -216,11 +216,11 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG
216216
)
217217
users := toolsets.NewToolset(ToolsetMetadataUsers.ID, ToolsetMetadataUsers.Description).
218218
AddReadTools(
219-
toolsets.NewServerTool(SearchUsers(getClient, t)),
219+
toolsets.NewServerTool(SearchUsers(getClient, t, flags)),
220220
)
221221
orgs := toolsets.NewToolset(ToolsetMetadataOrgs.ID, ToolsetMetadataOrgs.Description).
222222
AddReadTools(
223-
toolsets.NewServerTool(SearchOrgs(getClient, t)),
223+
toolsets.NewServerTool(SearchOrgs(getClient, t, flags)),
224224
)
225225
pullRequests := toolsets.NewToolset(ToolsetMetadataPullRequests.ID, ToolsetMetadataPullRequests.Description).
226226
AddReadTools(

0 commit comments

Comments
 (0)