- Notifications
You must be signed in to change notification settings - Fork937
Feat: Add initial Gist tools#340
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
xodene wants to merge3 commits intomainChoose a base branch fromxodene/feat-gists-tools-start
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
// ListGists creates a tool to list gists for a user | ||
func ListGists(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_gists", | ||
mcp.WithDescription(t("TOOL_LIST_GISTS_DESCRIPTION", "List gists for a user")), | ||
mcp.WithString("username", | ||
mcp.Description("GitHub username (omit for authenticated user's gists)"), | ||
), | ||
mcp.WithString("since", | ||
mcp.Description("Only gists updated after this time (ISO 8601 timestamp)"), | ||
), | ||
WithPagination(), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
username, err := OptionalParam[string](request, "username") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
since, err := OptionalParam[string](request, "since") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
pagination, err := OptionalPaginationParams(request) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
opts := &github.GistListOptions{ | ||
ListOptions: github.ListOptions{ | ||
Page: pagination.page, | ||
PerPage: pagination.perPage, | ||
}, | ||
} | ||
// Parse since timestamp if provided | ||
if since != "" { | ||
sinceTime, err := parseISOTimestamp(since) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("invalid since timestamp: %v", err)), nil | ||
} | ||
opts.Since = sinceTime | ||
} | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
gists, resp, err := client.Gists.List(ctx, username, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list gists: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != http.StatusOK { | ||
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 gists: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(gists) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} | ||
// CreateGist creates a tool to create a new gist | ||
func CreateGist(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("create_gist", | ||
mcp.WithDescription(t("TOOL_CREATE_GIST_DESCRIPTION", "Create a new gist")), | ||
mcp.WithString("description", | ||
mcp.Description("Description of the gist"), | ||
), | ||
mcp.WithString("filename", | ||
mcp.Required(), | ||
mcp.Description("Filename for simple single-file gist creation"), | ||
), | ||
mcp.WithString("content", | ||
mcp.Required(), | ||
mcp.Description("Content for simple single-file gist creation"), | ||
), | ||
mcp.WithBoolean("public", | ||
mcp.Description("Whether the gist is public"), | ||
mcp.DefaultBool(false), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
description, err := OptionalParam[string](request, "description") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
filename, err := requiredParam[string](request, "filename") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
content, err := requiredParam[string](request, "content") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
public, err := OptionalParam[bool](request, "public") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
files := make(map[github.GistFilename]github.GistFile) | ||
files[github.GistFilename(filename)] = github.GistFile{ | ||
Filename: github.Ptr(filename), | ||
Content: github.Ptr(content), | ||
} | ||
gist := &github.Gist{ | ||
Files: files, | ||
Public: github.Ptr(public), | ||
Description: github.Ptr(description), | ||
} | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
createdGist, resp, err := client.Gists.Create(ctx, gist) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create gist: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != http.StatusCreated { | ||
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 create gist: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(createdGist) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} | ||
// UpdateGist creates a tool to edit an existing gist | ||
func UpdateGist(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("update_gist", | ||
mcp.WithDescription(t("TOOL_UPDATE_GIST_DESCRIPTION", "Update an existing gist")), | ||
mcp.WithString("gist_id", | ||
mcp.Required(), | ||
mcp.Description("ID of the gist to update"), | ||
), | ||
mcp.WithString("description", | ||
mcp.Description("Updated description of the gist"), | ||
), | ||
mcp.WithString("filename", | ||
mcp.Required(), | ||
mcp.Description("Filename to update or create"), | ||
), | ||
mcp.WithString("content", | ||
mcp.Required(), | ||
mcp.Description("Content for the file"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
gistID, err := requiredParam[string](request, "gist_id") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
description, err := OptionalParam[string](request, "description") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
filename, err := requiredParam[string](request, "filename") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
content, err := requiredParam[string](request, "content") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
files := make(map[github.GistFilename]github.GistFile) | ||
files[github.GistFilename(filename)] = github.GistFile{ | ||
Filename: github.Ptr(filename), | ||
Content: github.Ptr(content), | ||
} | ||
gist := &github.Gist{ | ||
Files: files, | ||
Description: github.Ptr(description), | ||
} | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
} | ||
updatedGist, resp, err := client.Gists.Edit(ctx, gistID, gist) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to update gist: %w", err) | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
if resp.StatusCode != http.StatusOK { | ||
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 update gist: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(updatedGist) | ||
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.