Files
Matt Holloway 7ccc6b6493 Add pagination support to GraphQL-based tools (#683)
* initial pagination for `ListDiscussions`

* redo category id var cast

* add GraphQL pagination support for discussion comments and categories

* remove pageinfo returns

* fix out ref for linter

* update docs

* move to unified pagination for consensus on params

* update docs

* refactor pagination handling

* update docs

* linter fix

* conv rest to gql params for safe lint

* add nolint

* add error handling for perPage value in ToGraphQLParams

* refactor pagination error handling

* unified params for rest andn graphql and rennamed to be uniform for golang

* add 'after' for pagination

* update docs

* Update pkg/github/discussions.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update pkg/github/discussions.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update pkg/github/discussions_test.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* update default page size const

* reduce default pagination size from 100 to 30 in discussion tests

* update pagination for reverse and total

* update pagination to remove from discussions

* updated README

* improve the `ToGraphQLParams` function

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-21 16:31:29 +01:00

89 lines
2.2 KiB
Go

package github
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/go-github/v73/github"
"github.com/mark3labs/mcp-go/mcp"
)
func searchHandler(
ctx context.Context,
getClient GetClientFn,
request mcp.CallToolRequest,
searchType string,
errorPrefix string,
) (*mcp.CallToolResult, error) {
query, err := RequiredParam[string](request, "query")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
query = fmt.Sprintf("is:%s %s", searchType, query)
owner, err := OptionalParam[string](request, "owner")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
repo, err := OptionalParam[string](request, "repo")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
if owner != "" && repo != "" {
query = fmt.Sprintf("repo:%s/%s %s", owner, repo, query)
}
sort, err := OptionalParam[string](request, "sort")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
order, err := OptionalParam[string](request, "order")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
pagination, err := OptionalPaginationParams(request)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
opts := &github.SearchOptions{
// Default to "created" if no sort is provided, as it's a common use case.
Sort: sort,
Order: order,
ListOptions: github.ListOptions{
Page: pagination.Page,
PerPage: pagination.PerPage,
},
}
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("%s: failed to get GitHub client: %w", errorPrefix, err)
}
result, resp, err := client.Search.Issues(ctx, query, opts)
if err != nil {
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s: failed to read response body: %w", errorPrefix, err)
}
return mcp.NewToolResultError(fmt.Sprintf("%s: %s", errorPrefix, string(body))), nil
}
r, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("%s: failed to marshal response: %w", errorPrefix, err)
}
return mcp.NewToolResultText(string(r)), nil
}