-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for Repository Discussions #459
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xcorail
wants to merge
12
commits into
github:main
Choose a base branch
from
xcorail:main
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.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e110366
feat: initial support for discussions - Minimal
xcorail 6de3210
Add documentation
xcorail 5a48389
Fix linting issues and improve naming consistency
xcorail 647d0b3
Update README.md
xcorail 2f90a60
Update README.md
xcorail e10d76a
Add missing pagination parameters to ListDiscussions
xcorail bc5abbd
Add answered parameter to ListDiscussions
xcorail ae5f1b2
Implement pagination for ListDiscussionCategories
xcorail 58cd74d
Make ListDiscussions more user-friendly by using category name filter…
xcorail a77b2f4
Fix linting errors
xcorail 0f608eb
Merge branch 'main' into main
xcorail 4d26e31
Bump go-github to v72
xcorail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,333 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/go-viper/mapstructure/v2" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_discussions", | ||
mcp.WithDescription(t("TOOL_LIST_DISCUSSIONS_DESCRIPTION", "List discussions for a repository")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_LIST_DISCUSSIONS_USER_TITLE", "List discussions"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithString("owner", | ||
mcp.Required(), | ||
mcp.Description("Repository owner"), | ||
), | ||
mcp.WithString("repo", | ||
mcp.Required(), | ||
mcp.Description("Repository name"), | ||
), | ||
mcp.WithString("categoryId", | ||
mcp.Description("Category ID filter"), | ||
), | ||
mcp.WithString("since", | ||
mcp.Description("Filter by date (ISO 8601 timestamp)"), | ||
), | ||
mcp.WithString("sort", | ||
mcp.Description("Sort field"), | ||
mcp.DefaultString("CREATED_AT"), | ||
mcp.Enum("CREATED_AT", "UPDATED_AT"), | ||
), | ||
mcp.WithString("direction", | ||
mcp.Description("Sort direction"), | ||
mcp.DefaultString("DESC"), | ||
mcp.Enum("ASC", "DESC"), | ||
), | ||
mcp.WithNumber("first", | ||
mcp.Description("Number of discussions to return per page (min 1, max 100)"), | ||
mcp.Min(1), | ||
mcp.Max(100), | ||
), | ||
mcp.WithString("after", | ||
mcp.Description("Cursor for pagination, use the 'after' field from the previous response"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
// Decode params | ||
var params struct { | ||
Owner string | ||
Repo string | ||
CategoryID string | ||
Since string | ||
Sort string | ||
Direction string | ||
First int32 | ||
After string | ||
} | ||
if err := mapstructure.Decode(request.Params.Arguments, ¶ms); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
// Get GraphQL client | ||
client, err := getGQLClient(ctx) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil | ||
} | ||
// Prepare GraphQL query | ||
var q struct { | ||
Repository struct { | ||
Discussions struct { | ||
Nodes []struct { | ||
Number githubv4.Int | ||
Title githubv4.String | ||
CreatedAt githubv4.DateTime | ||
Category struct { | ||
Name githubv4.String | ||
} `graphql:"category"` | ||
URL githubv4.String `graphql:"url"` | ||
} | ||
} `graphql:"discussions(categoryId: $categoryId, orderBy: {field: $sort, direction: $direction}, first: $first, after: $after)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
// Build query variables | ||
vars := map[string]interface{}{ | ||
"owner": githubv4.String(params.Owner), | ||
"repo": githubv4.String(params.Repo), | ||
"categoryId": githubv4.ID(params.CategoryID), | ||
"sort": githubv4.DiscussionOrderField(params.Sort), | ||
"direction": githubv4.OrderDirection(params.Direction), | ||
"first": githubv4.Int(params.First), | ||
"after": githubv4.String(params.After), | ||
} | ||
// Execute query | ||
if err := client.Query(ctx, &q, vars); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
// Map nodes to GitHub Issue objects - there is no discussion type in the GitHub API, so we use Issue to benefit from existing code | ||
var discussions []*github.Issue | ||
for _, n := range q.Repository.Discussions.Nodes { | ||
di := &github.Issue{ | ||
Number: github.Ptr(int(n.Number)), | ||
Title: github.Ptr(string(n.Title)), | ||
HTMLURL: github.Ptr(string(n.URL)), | ||
CreatedAt: &github.Timestamp{Time: n.CreatedAt.Time}, | ||
} | ||
discussions = append(discussions, di) | ||
} | ||
|
||
// Post filtering discussions based on 'since' parameter | ||
if params.Since != "" { | ||
sinceTime, err := time.Parse(time.RFC3339, params.Since) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("invalid 'since' timestamp: %v", err)), nil | ||
} | ||
var filteredDiscussions []*github.Issue | ||
for _, d := range discussions { | ||
if d.CreatedAt.Time.After(sinceTime) { | ||
filteredDiscussions = append(filteredDiscussions, d) | ||
} | ||
} | ||
discussions = filteredDiscussions | ||
} | ||
|
||
// Marshal and return | ||
out, err := json.Marshal(discussions) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal discussions: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(out)), nil | ||
} | ||
} | ||
|
||
func GetDiscussion(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("get_discussion", | ||
mcp.WithDescription(t("TOOL_GET_DISCUSSION_DESCRIPTION", "Get a specific discussion by ID")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_GET_DISCUSSION_USER_TITLE", "Get discussion"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithString("owner", | ||
mcp.Required(), | ||
mcp.Description("Repository owner"), | ||
), | ||
mcp.WithString("repo", | ||
mcp.Required(), | ||
mcp.Description("Repository name"), | ||
), | ||
mcp.WithNumber("discussionNumber", | ||
mcp.Required(), | ||
mcp.Description("Discussion Number"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
// Decode params | ||
var params struct { | ||
Owner string | ||
Repo string | ||
DiscussionNumber int32 | ||
} | ||
if err := mapstructure.Decode(request.Params.Arguments, ¶ms); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
client, err := getGQLClient(ctx) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil | ||
} | ||
|
||
var q struct { | ||
Repository struct { | ||
Discussion struct { | ||
Number githubv4.Int | ||
Body githubv4.String | ||
State githubv4.String | ||
CreatedAt githubv4.DateTime | ||
URL githubv4.String `graphql:"url"` | ||
} `graphql:"discussion(number: $discussionNumber)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
vars := map[string]interface{}{ | ||
"owner": githubv4.String(params.Owner), | ||
"repo": githubv4.String(params.Repo), | ||
"discussionNumber": githubv4.Int(params.DiscussionNumber), | ||
} | ||
if err := client.Query(ctx, &q, vars); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
d := q.Repository.Discussion | ||
discussion := &github.Issue{ | ||
Number: github.Ptr(int(d.Number)), | ||
Body: github.Ptr(string(d.Body)), | ||
State: github.Ptr(string(d.State)), | ||
HTMLURL: github.Ptr(string(d.URL)), | ||
CreatedAt: &github.Timestamp{Time: d.CreatedAt.Time}, | ||
} | ||
out, err := json.Marshal(discussion) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal discussion: %w", err) | ||
} | ||
|
||
return mcp.NewToolResultText(string(out)), nil | ||
} | ||
} | ||
|
||
func GetDiscussionComments(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("get_discussion_comments", | ||
mcp.WithDescription(t("TOOL_GET_DISCUSSION_COMMENTS_DESCRIPTION", "Get comments from a discussion")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_GET_DISCUSSION_COMMENTS_USER_TITLE", "Get discussion comments"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithString("owner", mcp.Required(), mcp.Description("Repository owner")), | ||
mcp.WithString("repo", mcp.Required(), mcp.Description("Repository name")), | ||
mcp.WithNumber("discussionNumber", mcp.Required(), mcp.Description("Discussion Number")), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
// Decode params | ||
var params struct { | ||
Owner string | ||
Repo string | ||
DiscussionNumber int32 | ||
} | ||
if err := mapstructure.Decode(request.Params.Arguments, ¶ms); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
client, err := getGQLClient(ctx) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil | ||
} | ||
|
||
var q struct { | ||
Repository struct { | ||
Discussion struct { | ||
Comments struct { | ||
Nodes []struct { | ||
Body githubv4.String | ||
} | ||
} `graphql:"comments(first:100)"` | ||
} `graphql:"discussion(number: $discussionNumber)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
vars := map[string]interface{}{ | ||
"owner": githubv4.String(params.Owner), | ||
"repo": githubv4.String(params.Repo), | ||
"discussionNumber": githubv4.Int(params.DiscussionNumber), | ||
} | ||
if err := client.Query(ctx, &q, vars); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
var comments []*github.IssueComment | ||
for _, c := range q.Repository.Discussion.Comments.Nodes { | ||
comments = append(comments, &github.IssueComment{Body: github.Ptr(string(c.Body))}) | ||
} | ||
|
||
out, err := json.Marshal(comments) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal comments: %w", err) | ||
} | ||
|
||
return mcp.NewToolResultText(string(out)), nil | ||
} | ||
} | ||
|
||
func ListDiscussionCategories(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_discussion_categories", | ||
mcp.WithDescription(t("TOOL_LIST_DISCUSSION_CATEGORIES_DESCRIPTION", "List discussion categories with their id and name, for a repository")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
Title: t("TOOL_LIST_DISCUSSION_CATEGORIES_USER_TITLE", "List discussion categories"), | ||
ReadOnlyHint: toBoolPtr(true), | ||
}), | ||
mcp.WithString("owner", | ||
mcp.Required(), | ||
mcp.Description("Repository owner"), | ||
), | ||
mcp.WithString("repo", | ||
mcp.Required(), | ||
mcp.Description("Repository name"), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
owner, err := requiredParam[string](request, "owner") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
repo, err := requiredParam[string](request, "repo") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
client, err := getGQLClient(ctx) | ||
if err != nil { | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil | ||
} | ||
var q struct { | ||
Repository struct { | ||
DiscussionCategories struct { | ||
Nodes []struct { | ||
ID githubv4.ID | ||
Name githubv4.String | ||
} | ||
} `graphql:"discussionCategories(first: 30)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
vars := map[string]interface{}{ | ||
"owner": githubv4.String(owner), | ||
"repo": githubv4.String(repo), | ||
} | ||
if err := client.Query(ctx, &q, vars); err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
var categories []map[string]string | ||
for _, c := range q.Repository.DiscussionCategories.Nodes { | ||
categories = append(categories, map[string]string{ | ||
"id": fmt.Sprint(c.ID), | ||
"name": string(c.Name), | ||
}) | ||
} | ||
out, err := json.Marshal(categories) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal discussion categories: %w", err) | ||
} | ||
return mcp.NewToolResultText(string(out)), nil | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.