Skip to content

Add support for org-level discussions in list_discussions tool #775

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

Merged
merged 5 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ The following sets of tools are available (all are on by default):
- `orderBy`: Order discussions by field. If provided, the 'direction' also needs to be provided. (string, optional)
- `owner`: Repository owner (string, required)
- `perPage`: Results per page for pagination (min 1, max 100) (number, optional)
- `repo`: Repository name (string, required)
- `repo`: Repository name. If not provided, discussions will be queried at the organisation level. (string, optional)

</details>

Expand Down
12 changes: 8 additions & 4 deletions pkg/github/discussions.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func getQueryType(useOrdering bool, categoryID *githubv4.ID) any {

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.WithDescription(t("TOOL_LIST_DISCUSSIONS_DESCRIPTION", "List discussions for a repository or organisation.")),
mcp.WithToolAnnotation(mcp.ToolAnnotation{
Title: t("TOOL_LIST_DISCUSSIONS_USER_TITLE", "List discussions"),
ReadOnlyHint: ToBoolPtr(true),
Expand All @@ -129,8 +129,7 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
mcp.Description("Repository owner"),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
mcp.Description("Repository name. If not provided, discussions will be queried at the organisation level."),
),
mcp.WithString("category",
mcp.Description("Optional filter by discussion category ID. If provided, only discussions with this category are listed."),
Expand All @@ -150,10 +149,15 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
repo, err := RequiredParam[string](request, "repo")
repo, err := OptionalParam[string](request, "repo")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
// when not provided, default to the .github repository
// this will query discussions at the organisation level
if repo == "" {
repo = ".github"
}

category, err := OptionalParam[string](request, "category")
if err != nil {
Expand Down
77 changes: 76 additions & 1 deletion pkg/github/discussions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@ var (
},
}

discussionsOrgLevel = []map[string]any{
{
"number": 1,
"title": "Org Discussion 1 - Community Guidelines",
"createdAt": "2023-01-15T00:00:00Z",
"updatedAt": "2023-01-15T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/1",
"category": map[string]any{"name": "Announcements"},
},
{
"number": 2,
"title": "Org Discussion 2 - Roadmap 2023",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/2",
"category": map[string]any{"name": "General"},
},
{
"number": 3,
"title": "Org Discussion 3 - Roadmap 2024",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/3",
"category": map[string]any{"name": "General"},
},
{
"number": 4,
"title": "Org Discussion 4 - Roadmap 2025",
"createdAt": "2023-02-20T00:00:00Z",
"updatedAt": "2023-02-20T00:00:00Z",
"author": map[string]any{"login": "org-admin"},
"url": "https://github.com/owner/.github/discussions/4",
"category": map[string]any{"name": "General"},
},

}

// Ordered mock responses
discussionsOrderedCreatedAsc = []map[string]any{
discussionsAll[0], // Discussion 1 (created 2023-01-01)
Expand Down Expand Up @@ -139,6 +179,22 @@ var (
},
},
})

mockResponseOrgLevel = githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"discussions": map[string]any{
"nodes": discussionsOrgLevel,
"pageInfo": map[string]any{
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "",
"endCursor": "",
},
"totalCount": 4,
},
},
})

mockErrorRepoNotFound = githubv4mock.ErrorResponse("repository not found")
)

Expand All @@ -151,7 +207,7 @@ func Test_ListDiscussions(t *testing.T) {
assert.Contains(t, toolDef.InputSchema.Properties, "repo")
assert.Contains(t, toolDef.InputSchema.Properties, "orderBy")
assert.Contains(t, toolDef.InputSchema.Properties, "direction")
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner", "repo"})
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner"})

// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
varsListAll := map[string]interface{}{
Expand Down Expand Up @@ -204,6 +260,13 @@ func Test_ListDiscussions(t *testing.T) {
"after": (*string)(nil),
}

varsOrgLevel := map[string]interface{}{
"owner": "owner",
"repo": ".github", // This is what gets set when repo is not provided
"first": float64(30),
"after": (*string)(nil),
}

tests := []struct {
name string
reqParams map[string]interface{}
Expand Down Expand Up @@ -314,6 +377,15 @@ func Test_ListDiscussions(t *testing.T) {
expectError: true,
errContains: "repository not found",
},
{
name: "list org-level discussions (no repo provided)",
reqParams: map[string]interface{}{
"owner": "owner",
// repo is not provided, it will default to ".github"
},
expectError: false,
expectedCount: 4,
},
}

// Define the actual query strings that match the implementation
Expand Down Expand Up @@ -351,6 +423,9 @@ func Test_ListDiscussions(t *testing.T) {
case "repository not found error":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsRepoNotFound, mockErrorRepoNotFound)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
case "list org-level discussions (no repo provided)":
matcher := githubv4mock.NewQueryMatcher(qBasicNoOrder, varsOrgLevel, mockResponseOrgLevel)
httpClient = githubv4mock.NewMockedHTTPClient(matcher)
}

gqlClient := githubv4.NewClient(httpClient)
Expand Down
Loading