- Notifications
You must be signed in to change notification settings - Fork1k
Add support for managing users SSH key related operations#474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
glendsoza wants to merge1 commit intogithub:mainChoose a base branch fromglendsoza:feat/473
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/google/go-github/v72/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
// ListUsersPublicSSHKeys creates a tool to list public ssh keys for user | ||
func ListUsersPublicSSHKeys(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_users_public_ssh_keys", | ||
mcp.WithDescription(t("TOOL_LIST_USERS_PUBLIC_SSH_KEYS", "Lists the public SSH keys for the authenticated user's GitHub account")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_LIST_USERS_PUBLIC_SSH_KEYS_USER_TITLE", "List users public ssh keys"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
WithPagination(), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
pagination, err := OptionalPaginationParams(request) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
opts := &github.ListOptions{ | ||
Page: pagination.page, | ||
PerPage: pagination.perPage, | ||
} | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
result, resp, err := client.Users.ListKeys(ctx, "", opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list users ssh keys: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != 200 { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to list users ssh keys: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} | ||
// GetUsersPublicSSHKey creates a tool to get public ssh key for user | ||
func GetUsersPublicSSHKey(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("get_users_public_ssh_key", | ||
mcp.WithDescription(t("TOOL_GET_USERS_PUBLIC_SSH_KEY", "View extended details for a single public SSH key")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_GET_USERS_PUBLIC_SSH_KEY_USER_TITLE", "Get public ssh key details"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithNumber("key_id", | ||
mcp.Required(), | ||
mcp.Description("The unique identifier of the key"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
keyId, err := RequiredInt(request, "key_id") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
result, resp, err := client.Users.GetKey(ctx, int64(keyId)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get ssh key details: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != 200 { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to get ssh key details: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} | ||
// AddPublicSSHKey Adds a public SSH key to the authenticated user's GitHub account | ||
func AddUsersPublicSSHKey(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("add_users_public_ssh_key", | ||
mcp.WithDescription(t("TOOL_ADD_USERS_PUBLIC_SSH_KEY", "Adds a public SSH key to the authenticated user's GitHub account")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_ADD_USERS_PUBLIC_SSH_KEY_USER_TITLE", "Add users public ssh key"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithString("title", | ||
mcp.Description("A descriptive name for the new key"), | ||
), | ||
mcp.WithString("key", | ||
mcp.Required(), | ||
mcp.Description("The public SSH key to add to your GitHub account"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
title, err := OptionalParam[string](request, "title") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
key, err := requiredParam[string](request, "key") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
githubKey := &github.Key{ | ||
Title: &title, | ||
Key: &key, | ||
} | ||
result, resp, err := client.Users.CreateKey(ctx, githubKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to add ssh key: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != 201 { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to add ssh key: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.