Skip to content

Commit 661daff

Browse files
committed
rewrite to allow providing user
1 parent 8d97b5e commit 661daff

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

pkg/github/__toolsnaps__/get_my_teams.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
},
66
"description": "Get details of the teams the authenticated user is a member of.",
77
"inputSchema": {
8-
"properties": {},
8+
"properties": {
9+
"user": {
10+
"description": "Username to get teams for. If not provided, uses the authenticated user.",
11+
"type": "string"
12+
}
13+
},
914
"type": "object"
1015
},
1116
"name": "get_my_teams"

pkg/github/context_tools.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,37 @@ func GetMe(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Too
9696
func GetMyTeams(getClient GetClientFn, getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
9797
tool := mcp.NewTool("get_my_teams",
9898
mcp.WithDescription(t("TOOL_GET_MY_TEAMS_DESCRIPTION", "Get details of the teams the authenticated user is a member of.")),
99+
mcp.WithString("user",
100+
mcp.Description(t("TOOL_GET_MY_TEAMS_USER_DESCRIPTION", "Username to get teams for. If not provided, uses the authenticated user.")),
101+
),
99102
mcp.WithToolAnnotation(mcp.ToolAnnotation{
100103
Title: t("TOOL_GET_MY_TEAMS_TITLE", "Get my teams"),
101104
ReadOnlyHint: ToBoolPtr(true),
102105
}),
103106
)
104107

105-
type args struct{}
106-
handler := mcp.NewTypedToolHandler(func(ctx context.Context, _ mcp.CallToolRequest, _ args) (*mcp.CallToolResult, error) {
107-
client, err := getClient(ctx)
108-
if err != nil {
109-
return mcp.NewToolResultErrorFromErr("failed to get GitHub client", err), nil
110-
}
111-
112-
user, res, err := client.Users.Get(ctx, "")
113-
if err != nil {
114-
return ghErrors.NewGitHubAPIErrorResponse(ctx,
115-
"failed to get user",
116-
res,
117-
err,
118-
), nil
108+
type args struct {
109+
User *string `json:"user,omitempty"`
110+
}
111+
handler := mcp.NewTypedToolHandler(func(ctx context.Context, _ mcp.CallToolRequest, a args) (*mcp.CallToolResult, error) {
112+
var username string
113+
if a.User != nil && *a.User != "" {
114+
username = *a.User
115+
} else {
116+
client, err := getClient(ctx)
117+
if err != nil {
118+
return mcp.NewToolResultErrorFromErr("failed to get GitHub client", err), nil
119+
}
120+
121+
user, res, err := client.Users.Get(ctx, "")
122+
if err != nil {
123+
return ghErrors.NewGitHubAPIErrorResponse(ctx,
124+
"failed to get user",
125+
res,
126+
err,
127+
), nil
128+
}
129+
username = user.GetLogin()
119130
}
120131

121132
gqlClient, err := getGQLClient(ctx)
@@ -140,7 +151,7 @@ func GetMyTeams(getClient GetClientFn, getGQLClient GetGQLClientFn, t translatio
140151
} `graphql:"user(login: $login)"`
141152
}
142153
vars := map[string]interface{}{
143-
"login": githubv4.String(user.GetLogin()),
154+
"login": githubv4.String(username),
144155
}
145156
if err := gqlClient.Query(ctx, &q, vars); err != nil {
146157
return mcp.NewToolResultError(err.Error()), nil

pkg/github/context_tools_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ func Test_GetMyTeams(t *testing.T) {
248248
expectToolError: false,
249249
expectedTeamsCount: 2,
250250
},
251+
{
252+
name: "successful get teams for specific user",
253+
stubbedGetClientFn: nil, // No REST client needed when user is provided
254+
stubbedGetGQLClientFn: func(_ context.Context) (*githubv4.Client, error) {
255+
queryStr := "query($login:String!){user(login: $login){organizations(first: 100){nodes{login,teams(first: 100, userLogins: [$login]){nodes{name,slug,description}}}}}}"
256+
vars := map[string]interface{}{
257+
"login": "specificuser",
258+
}
259+
matcher := githubv4mock.NewQueryMatcher(queryStr, vars, mockTeamsResponse)
260+
httpClient := githubv4mock.NewMockedHTTPClient(matcher)
261+
return githubv4.NewClient(httpClient), nil
262+
},
263+
requestArgs: map[string]any{
264+
"user": "specificuser",
265+
},
266+
expectToolError: false,
267+
expectedTeamsCount: 2,
268+
},
251269
{
252270
name: "no teams found",
253271
stubbedGetClientFn: stubGetClientFromHTTPFn(

0 commit comments

Comments
 (0)