Skip to content
Draft
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.24.0

require (
github.com/google/go-github/v77 v77.0.0
github.com/google/jsonschema-go v0.3.0
github.com/josephburnett/jd v1.9.2
github.com/mark3labs/mcp-go v0.36.0
github.com/microcosm-cc/bluemonday v1.0.27
Expand All @@ -20,7 +21,6 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/google/go-github/v71 v71.0.0 // indirect
github.com/google/jsonschema-go v0.3.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/invopop/jsonschema v0.13.0 // indirect
Expand All @@ -37,8 +37,8 @@ require (
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0
github.com/google/go-querystring v1.1.0
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/modelcontextprotocol/go-sdk v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/oauth2 v0.29.0 h1:WdYw2tdTK1S8olAzWHdgeqfy+Mtm9XNhv/xJsY65d98=
golang.org/x/oauth2 v0.29.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
Expand All @@ -124,6 +122,8 @@ golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
339 changes: 180 additions & 159 deletions pkg/github/dependabot.go
Original file line number Diff line number Diff line change
@@ -1,161 +1,182 @@
package github

// import (
// "context"
// "encoding/json"
// "fmt"
// "io"
// "net/http"

// ghErrors "github.com/github/github-mcp-server/pkg/errors"
// "github.com/github/github-mcp-server/pkg/translations"
// "github.com/google/go-github/v77/github"
// "github.com/mark3labs/mcp-go/mcp"
// "github.com/mark3labs/mcp-go/server"
// )

// func GetDependabotAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// return mcp.NewTool(
// "get_dependabot_alert",
// mcp.WithDescription(t("TOOL_GET_DEPENDABOT_ALERT_DESCRIPTION", "Get details of a specific dependabot alert in a GitHub repository.")),
// mcp.WithToolAnnotation(mcp.ToolAnnotation{
// Title: t("TOOL_GET_DEPENDABOT_ALERT_USER_TITLE", "Get dependabot alert"),
// ReadOnlyHint: ToBoolPtr(true),
// }),
// mcp.WithString("owner",
// mcp.Required(),
// mcp.Description("The owner of the repository."),
// ),
// mcp.WithString("repo",
// mcp.Required(),
// mcp.Description("The name of the repository."),
// ),
// mcp.WithNumber("alertNumber",
// mcp.Required(),
// mcp.Description("The number of the alert."),
// ),
// ),
// 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
// }
// alertNumber, err := RequiredInt(request, "alertNumber")
// if err != nil {
// return mcp.NewToolResultError(err.Error()), nil
// }

// client, err := getClient(ctx)
// if err != nil {
// return nil, fmt.Errorf("failed to get GitHub client: %w", err)
// }

// alert, resp, err := client.Dependabot.GetRepoAlert(ctx, owner, repo, alertNumber)
// if err != nil {
// return ghErrors.NewGitHubAPIErrorResponse(ctx,
// fmt.Sprintf("failed to get alert with number '%d'", alertNumber),
// resp,
// err,
// ), nil
// }
// 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 get alert: %s", string(body))), nil
// }

// r, err := json.Marshal(alert)
// if err != nil {
// return nil, fmt.Errorf("failed to marshal alert: %w", err)
// }

// return mcp.NewToolResultText(string(r)), nil
// }
// }

// func ListDependabotAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// return mcp.NewTool(
// "list_dependabot_alerts",
// mcp.WithDescription(t("TOOL_LIST_DEPENDABOT_ALERTS_DESCRIPTION", "List dependabot alerts in a GitHub repository.")),
// mcp.WithToolAnnotation(mcp.ToolAnnotation{
// Title: t("TOOL_LIST_DEPENDABOT_ALERTS_USER_TITLE", "List dependabot alerts"),
// ReadOnlyHint: ToBoolPtr(true),
// }),
// mcp.WithString("owner",
// mcp.Required(),
// mcp.Description("The owner of the repository."),
// ),
// mcp.WithString("repo",
// mcp.Required(),
// mcp.Description("The name of the repository."),
// ),
// mcp.WithString("state",
// mcp.Description("Filter dependabot alerts by state. Defaults to open"),
// mcp.DefaultString("open"),
// mcp.Enum("open", "fixed", "dismissed", "auto_dismissed"),
// ),
// mcp.WithString("severity",
// mcp.Description("Filter dependabot alerts by severity"),
// mcp.Enum("low", "medium", "high", "critical"),
// ),
// ),
// 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
// }
// state, err := OptionalParam[string](request, "state")
// if err != nil {
// return mcp.NewToolResultError(err.Error()), nil
// }
// severity, err := OptionalParam[string](request, "severity")
// if err != nil {
// return mcp.NewToolResultError(err.Error()), nil
// }

// client, err := getClient(ctx)
// if err != nil {
// return nil, fmt.Errorf("failed to get GitHub client: %w", err)
// }

// alerts, resp, err := client.Dependabot.ListRepoAlerts(ctx, owner, repo, &github.ListAlertsOptions{
// State: ToStringPtr(state),
// Severity: ToStringPtr(severity),
// })
// if err != nil {
// return ghErrors.NewGitHubAPIErrorResponse(ctx,
// fmt.Sprintf("failed to list alerts for repository '%s/%s'", owner, repo),
// resp,
// err,
// ), nil
// }
// 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 alerts: %s", string(body))), nil
// }

// r, err := json.Marshal(alerts)
// if err != nil {
// return nil, fmt.Errorf("failed to marshal alerts: %w", err)
// }

// return mcp.NewToolResultText(string(r)), nil
// }
// }
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"

ghErrors "github.com/github/github-mcp-server/pkg/errors"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/google/go-github/v77/github"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
)

func GetDependabotAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
tool := mcp.Tool{
Name: "get_dependabot_alert",
Description: t("TOOL_GET_DEPENDABOT_ALERT_DESCRIPTION", "Get details of a specific dependabot alert in a GitHub repository."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_GET_DEPENDABOT_ALERT_USER_TITLE", "Get dependabot alert"),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "The owner of the repository.",
},
"repo": {
Type: "string",
Description: "The name of the repository.",
},
"alertNumber": {
Type: "number",
Description: "The number of the alert.",
},
},
Required: []string{"owner", "repo", "alertNumber"},
},
}

handler := mcp.ToolHandlerFor[map[string]any, any](func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
alertNumber, err := RequiredInt(args, "alertNumber")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}

client, err := getClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}

alert, resp, err := client.Dependabot.GetRepoAlert(ctx, owner, repo, alertNumber)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to get alert with number '%d'", alertNumber),
resp,
err,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
}
return utils.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil, nil
}

r, err := json.Marshal(alert)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal alert", err), nil, nil
}

return utils.NewToolResultText(string(r)), nil, nil
})

return tool, handler
}

func ListDependabotAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
tool := mcp.Tool{
Name: "list_dependabot_alerts",
Description: t("TOOL_LIST_DEPENDABOT_ALERTS_DESCRIPTION", "List dependabot alerts in a GitHub repository."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_LIST_DEPENDABOT_ALERTS_USER_TITLE", "List dependabot alerts"),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "The owner of the repository.",
},
"repo": {
Type: "string",
Description: "The name of the repository.",
},
"state": {
Type: "string",
Description: "Filter dependabot alerts by state. Defaults to open",
Enum: []any{"open", "fixed", "dismissed", "auto_dismissed"},
Default: json.RawMessage(`"open"`),
},
"severity": {
Type: "string",
Description: "Filter dependabot alerts by severity",
Enum: []any{"low", "medium", "high", "critical"},
},
},
Required: []string{"owner", "repo"},
},
}

handler := mcp.ToolHandlerFor[map[string]any, any](func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
state, err := OptionalParam[string](args, "state")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
severity, err := OptionalParam[string](args, "severity")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}

client, err := getClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}

alerts, resp, err := client.Dependabot.ListRepoAlerts(ctx, owner, repo, &github.ListAlertsOptions{
State: ToStringPtr(state),
Severity: ToStringPtr(severity),
})
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to list alerts for repository '%s/%s'", owner, repo),
resp,
err,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
}
return utils.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil, nil
}

r, err := json.Marshal(alerts)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal alerts", err), nil, nil
}

return utils.NewToolResultText(string(r)), nil, nil
})

return tool, handler
}
Loading